postLoad scripts too
This commit is contained in:
parent
7eec15098e
commit
78a68b6a20
@ -109,7 +109,7 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
|||||||
m_assetSources = std::move(assetSources);
|
m_assetSources = std::move(assetSources);
|
||||||
|
|
||||||
auto luaEngine = LuaEngine::create();
|
auto luaEngine = LuaEngine::create();
|
||||||
auto decorateLuaContext = [this](LuaContext& context, MemoryAssetSourcePtr mySource) {
|
auto decorateLuaContext = [this](LuaContext& context, MemoryAssetSourcePtr newFiles) {
|
||||||
context.setCallbacks("sb", LuaBindings::makeUtilityCallbacks());
|
context.setCallbacks("sb", LuaBindings::makeUtilityCallbacks());
|
||||||
LuaCallbacks callbacks;
|
LuaCallbacks callbacks;
|
||||||
callbacks.registerCallbackWithSignature<StringSet, String>("byExtension", bind(&Assets::scanExtension, this, _1));
|
callbacks.registerCallbackWithSignature<StringSet, String>("byExtension", bind(&Assets::scanExtension, this, _1));
|
||||||
@ -124,7 +124,7 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
|||||||
return b ? scan(a.value(), *b) : scan(a.value());
|
return b ? scan(a.value(), *b) : scan(a.value());
|
||||||
});
|
});
|
||||||
|
|
||||||
callbacks.registerCallback("add", [this, &mySource](LuaEngine& engine, String const& path, LuaValue const& data) -> bool {
|
callbacks.registerCallback("add", [this, &newFiles](LuaEngine& engine, String const& path, LuaValue const& data) {
|
||||||
ByteArray bytes;
|
ByteArray bytes;
|
||||||
if (auto str = engine.luaMaybeTo<String>(data))
|
if (auto str = engine.luaMaybeTo<String>(data))
|
||||||
bytes = ByteArray(str->utf8Ptr(), str->utf8Size());
|
bytes = ByteArray(str->utf8Ptr(), str->utf8Size());
|
||||||
@ -132,13 +132,13 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
|||||||
auto json = engine.luaTo<Json>(data).repr();
|
auto json = engine.luaTo<Json>(data).repr();
|
||||||
bytes = ByteArray(json.utf8Ptr(), json.utf8Size());
|
bytes = ByteArray(json.utf8Ptr(), json.utf8Size());
|
||||||
}
|
}
|
||||||
return mySource->set(path, bytes);
|
newFiles->set(path, bytes);
|
||||||
});
|
});
|
||||||
|
|
||||||
callbacks.registerCallback("patch", [this, &mySource](String const& path, String const& patchPath) -> bool {
|
callbacks.registerCallback("patch", [this, &newFiles](String const& path, String const& patchPath) -> bool {
|
||||||
if (auto file = m_files.ptr(path)) {
|
if (auto file = m_files.ptr(path)) {
|
||||||
if (mySource->contains(patchPath)) {
|
if (newFiles->contains(patchPath)) {
|
||||||
file->patchSources.append(make_pair(patchPath, mySource));
|
file->patchSources.append(make_pair(patchPath, newFiles));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
if (auto asset = m_files.ptr(patchPath)) {
|
if (auto asset = m_files.ptr(patchPath)) {
|
||||||
@ -150,7 +150,7 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
callbacks.registerCallback("erase", [this, &mySource](String const& path) -> bool {
|
callbacks.registerCallback("erase", [this](String const& path) -> bool {
|
||||||
bool erased = m_files.erase(path);
|
bool erased = m_files.erase(path);
|
||||||
if (erased)
|
if (erased)
|
||||||
m_filesByExtension[AssetPath::extension(path).toLower()].erase(path);
|
m_filesByExtension[AssetPath::extension(path).toLower()].erase(path);
|
||||||
@ -160,14 +160,6 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
|||||||
context.setCallbacks("assets", callbacks);
|
context.setCallbacks("assets", callbacks);
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto& sourcePath : m_assetSources) {
|
|
||||||
Logger::info("Loading assets from: '{}'", sourcePath);
|
|
||||||
AssetSourcePtr source;
|
|
||||||
if (File::isDirectory(sourcePath))
|
|
||||||
source = std::make_shared<DirectoryAssetSource>(sourcePath, m_settings.pathIgnore);
|
|
||||||
else
|
|
||||||
source = std::make_shared<PackedAssetSource>(sourcePath);
|
|
||||||
|
|
||||||
auto addSource = [&](String const& sourcePath, AssetSourcePtr source) {
|
auto addSource = [&](String const& sourcePath, AssetSourcePtr source) {
|
||||||
m_assetSourcePaths.add(sourcePath, source);
|
m_assetSourcePaths.add(sourcePath, source);
|
||||||
|
|
||||||
@ -195,34 +187,51 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
|||||||
m_filesByExtension[AssetPath::extension(filename).toLower()].insert(filename);
|
m_filesByExtension[AssetPath::extension(filename).toLower()].insert(filename);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
addSource(sourcePath, source);
|
|
||||||
|
|
||||||
|
auto runLoadScripts = [&](String const& groupName, String const& sourcePath, AssetSourcePtr source) {
|
||||||
auto metadata = source->metadata();
|
auto metadata = source->metadata();
|
||||||
if (auto scripts = metadata.ptr("scripts")) {
|
if (auto scripts = metadata.ptr("scripts")) {
|
||||||
if (auto onLoad = scripts->optArray("onLoad")) {
|
if (auto scriptGroup = scripts->optArray(groupName)) {
|
||||||
JsonObject memoryMetadata{
|
auto memoryName = strf("{}::{}", metadata.value("name", File::baseName(sourcePath)), groupName);
|
||||||
{"name", strf("{}.onLoad", metadata.value("name", File::baseName(sourcePath)))}
|
JsonObject memoryMetadata{ {"name", memoryName} };
|
||||||
};
|
auto memoryAssets = make_shared<MemoryAssetSource>(memoryName, memoryMetadata);
|
||||||
auto memoryAssets = make_shared<MemoryAssetSource>(memoryMetadata);
|
Logger::info("Running {} scripts {}", groupName, *scriptGroup);
|
||||||
Logger::info("Running onLoad scripts {}", *onLoad);
|
|
||||||
try {
|
try {
|
||||||
auto context = luaEngine->createContext();
|
auto context = luaEngine->createContext();
|
||||||
decorateLuaContext(context, memoryAssets);
|
decorateLuaContext(context, memoryAssets);
|
||||||
for (auto& jPath : *onLoad) {
|
for (auto& jPath : *scriptGroup) {
|
||||||
auto path = jPath.toString();
|
auto path = jPath.toString();
|
||||||
auto script = source->read(path);
|
auto script = source->read(path);
|
||||||
context.load(script, path);
|
context.load(script, path);
|
||||||
}
|
}
|
||||||
}
|
} catch (LuaException const& e) {
|
||||||
catch (LuaException const& e) {
|
Logger::error("Exception while running {} scripts from asset source '{}': {}", groupName, sourcePath, e.what());
|
||||||
Logger::error("Exception while running onLoad scripts from asset source '{}': {}", sourcePath, e.what());
|
|
||||||
}
|
}
|
||||||
if (!memoryAssets->empty())
|
if (!memoryAssets->empty())
|
||||||
addSource(memoryMetadata.get("name").toString(), memoryAssets);
|
addSource(strf("{}::{}", sourcePath, groupName), memoryAssets);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<pair<String, AssetSourcePtr>> sources;
|
||||||
|
|
||||||
|
for (auto& sourcePath : m_assetSources) {
|
||||||
|
Logger::info("Loading assets from: '{}'", sourcePath);
|
||||||
|
AssetSourcePtr source;
|
||||||
|
if (File::isDirectory(sourcePath))
|
||||||
|
source = std::make_shared<DirectoryAssetSource>(sourcePath, m_settings.pathIgnore);
|
||||||
|
else
|
||||||
|
source = std::make_shared<PackedAssetSource>(sourcePath);
|
||||||
|
|
||||||
|
addSource(sourcePath, source);
|
||||||
|
sources.append(make_pair(sourcePath, source));
|
||||||
|
|
||||||
|
runLoadScripts("onLoad", sourcePath, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto& pair : sources)
|
||||||
|
runLoadScripts("postLoad", pair.first, pair.second);
|
||||||
|
|
||||||
Sha256Hasher digest;
|
Sha256Hasher digest;
|
||||||
|
|
||||||
for (auto const& assetPath : m_files.keys().transformed([](String const& s) {
|
for (auto const& assetPath : m_files.keys().transformed([](String const& s) {
|
||||||
|
@ -5,7 +5,11 @@
|
|||||||
|
|
||||||
namespace Star {
|
namespace Star {
|
||||||
|
|
||||||
MemoryAssetSource::MemoryAssetSource(JsonObject metadata) : m_metadata(metadata) {}
|
MemoryAssetSource::MemoryAssetSource(String const& name, JsonObject metadata) : m_name(name), m_metadata(metadata) {}
|
||||||
|
|
||||||
|
String MemoryAssetSource::name() const {
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
JsonObject MemoryAssetSource::metadata() const {
|
JsonObject MemoryAssetSource::metadata() const {
|
||||||
return m_metadata;
|
return m_metadata;
|
||||||
@ -71,8 +75,8 @@ bool MemoryAssetSource::erase(String const& path) {
|
|||||||
return m_files.erase(path) != 0;
|
return m_files.erase(path) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MemoryAssetSource::set(String const& path, ByteArray data) {
|
void MemoryAssetSource::set(String const& path, ByteArray data) {
|
||||||
return m_files.emplace(path, make_shared<ByteArray>(std::move(data))).second;
|
m_files[path] = make_shared<ByteArray>(std::move(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteArray MemoryAssetSource::read(String const& path) {
|
ByteArray MemoryAssetSource::read(String const& path) {
|
||||||
|
@ -9,8 +9,9 @@ STAR_CLASS(MemoryAssetSource);
|
|||||||
|
|
||||||
class MemoryAssetSource : public AssetSource {
|
class MemoryAssetSource : public AssetSource {
|
||||||
public:
|
public:
|
||||||
MemoryAssetSource(JsonObject metadata = JsonObject());
|
MemoryAssetSource(String const& name, JsonObject metadata = JsonObject());
|
||||||
|
|
||||||
|
String name() const;
|
||||||
JsonObject metadata() const override;
|
JsonObject metadata() const override;
|
||||||
StringList assetPaths() const override;
|
StringList assetPaths() const override;
|
||||||
|
|
||||||
@ -19,9 +20,10 @@ public:
|
|||||||
bool empty() const;
|
bool empty() const;
|
||||||
bool contains(String const& path) const;
|
bool contains(String const& path) const;
|
||||||
bool erase(String const& path);
|
bool erase(String const& path);
|
||||||
bool set(String const& path, ByteArray data);
|
void set(String const& path, ByteArray data);
|
||||||
ByteArray read(String const& path) override;
|
ByteArray read(String const& path) override;
|
||||||
private:
|
private:
|
||||||
|
String m_name;
|
||||||
JsonObject m_metadata;
|
JsonObject m_metadata;
|
||||||
StringMap<ByteArrayPtr> m_files;
|
StringMap<ByteArrayPtr> m_files;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user