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);
|
||||
|
||||
auto luaEngine = LuaEngine::create();
|
||||
auto decorateLuaContext = [this](LuaContext& context, MemoryAssetSourcePtr mySource) {
|
||||
auto decorateLuaContext = [this](LuaContext& context, MemoryAssetSourcePtr newFiles) {
|
||||
context.setCallbacks("sb", LuaBindings::makeUtilityCallbacks());
|
||||
LuaCallbacks callbacks;
|
||||
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());
|
||||
});
|
||||
|
||||
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;
|
||||
if (auto str = engine.luaMaybeTo<String>(data))
|
||||
bytes = ByteArray(str->utf8Ptr(), str->utf8Size());
|
||||
@ -132,13 +132,13 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
||||
auto json = engine.luaTo<Json>(data).repr();
|
||||
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 (mySource->contains(patchPath)) {
|
||||
file->patchSources.append(make_pair(patchPath, mySource));
|
||||
if (newFiles->contains(patchPath)) {
|
||||
file->patchSources.append(make_pair(patchPath, newFiles));
|
||||
return true;
|
||||
} else {
|
||||
if (auto asset = m_files.ptr(patchPath)) {
|
||||
@ -150,7 +150,7 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
||||
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);
|
||||
if (erased)
|
||||
m_filesByExtension[AssetPath::extension(path).toLower()].erase(path);
|
||||
@ -160,14 +160,6 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
||||
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) {
|
||||
m_assetSourcePaths.add(sourcePath, source);
|
||||
|
||||
@ -195,34 +187,51 @@ Assets::Assets(Settings settings, StringList assetSources) {
|
||||
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();
|
||||
if (auto scripts = metadata.ptr("scripts")) {
|
||||
if (auto onLoad = scripts->optArray("onLoad")) {
|
||||
JsonObject memoryMetadata{
|
||||
{"name", strf("{}.onLoad", metadata.value("name", File::baseName(sourcePath)))}
|
||||
};
|
||||
auto memoryAssets = make_shared<MemoryAssetSource>(memoryMetadata);
|
||||
Logger::info("Running onLoad scripts {}", *onLoad);
|
||||
if (auto scriptGroup = scripts->optArray(groupName)) {
|
||||
auto memoryName = strf("{}::{}", metadata.value("name", File::baseName(sourcePath)), groupName);
|
||||
JsonObject memoryMetadata{ {"name", memoryName} };
|
||||
auto memoryAssets = make_shared<MemoryAssetSource>(memoryName, memoryMetadata);
|
||||
Logger::info("Running {} scripts {}", groupName, *scriptGroup);
|
||||
try {
|
||||
auto context = luaEngine->createContext();
|
||||
decorateLuaContext(context, memoryAssets);
|
||||
for (auto& jPath : *onLoad) {
|
||||
for (auto& jPath : *scriptGroup) {
|
||||
auto path = jPath.toString();
|
||||
auto script = source->read(path);
|
||||
context.load(script, path);
|
||||
}
|
||||
}
|
||||
catch (LuaException const& e) {
|
||||
Logger::error("Exception while running onLoad scripts from asset source '{}': {}", sourcePath, e.what());
|
||||
} catch (LuaException const& e) {
|
||||
Logger::error("Exception while running {} scripts from asset source '{}': {}", groupName, sourcePath, e.what());
|
||||
}
|
||||
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;
|
||||
|
||||
for (auto const& assetPath : m_files.keys().transformed([](String const& s) {
|
||||
|
@ -5,7 +5,11 @@
|
||||
|
||||
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 {
|
||||
return m_metadata;
|
||||
@ -71,8 +75,8 @@ bool MemoryAssetSource::erase(String const& path) {
|
||||
return m_files.erase(path) != 0;
|
||||
}
|
||||
|
||||
bool MemoryAssetSource::set(String const& path, ByteArray data) {
|
||||
return m_files.emplace(path, make_shared<ByteArray>(std::move(data))).second;
|
||||
void MemoryAssetSource::set(String const& path, ByteArray data) {
|
||||
m_files[path] = make_shared<ByteArray>(std::move(data));
|
||||
}
|
||||
|
||||
ByteArray MemoryAssetSource::read(String const& path) {
|
||||
|
@ -9,8 +9,9 @@ STAR_CLASS(MemoryAssetSource);
|
||||
|
||||
class MemoryAssetSource : public AssetSource {
|
||||
public:
|
||||
MemoryAssetSource(JsonObject metadata = JsonObject());
|
||||
MemoryAssetSource(String const& name, JsonObject metadata = JsonObject());
|
||||
|
||||
String name() const;
|
||||
JsonObject metadata() const override;
|
||||
StringList assetPaths() const override;
|
||||
|
||||
@ -19,9 +20,10 @@ public:
|
||||
bool empty() const;
|
||||
bool contains(String const& path) const;
|
||||
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;
|
||||
private:
|
||||
String m_name;
|
||||
JsonObject m_metadata;
|
||||
StringMap<ByteArrayPtr> m_files;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user