diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 9ba7029..784d686 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -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("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(data)) bytes = ByteArray(str->utf8Ptr(), str->utf8Size()); @@ -132,13 +132,13 @@ Assets::Assets(Settings settings, StringList assetSources) { auto json = engine.luaTo(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,6 +160,61 @@ Assets::Assets(Settings settings, StringList assetSources) { context.setCallbacks("assets", callbacks); }; + auto addSource = [&](String const& sourcePath, AssetSourcePtr source) { + m_assetSourcePaths.add(sourcePath, source); + + for (auto const& filename : source->assetPaths()) { + if (filename.contains(AssetsPatchSuffix, String::CaseInsensitive)) { + if (filename.endsWith(AssetsPatchSuffix, String::CaseInsensitive)) { + auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix)); + if (auto p = m_files.ptr(targetPatchFile)) + p->patchSources.append({filename, source}); + } else { + for (int i = 0; i < 10; i++) { + if (filename.endsWith(AssetsPatchSuffix + toString(i), String::CaseInsensitive)) { + auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix) + 1); + if (auto p = m_files.ptr(targetPatchFile)) + p->patchSources.append({filename, source}); + break; + } + } + } + } + + auto& descriptor = m_files[filename]; + descriptor.sourceName = filename; + descriptor.source = source; + m_filesByExtension[AssetPath::extension(filename).toLower()].insert(filename); + } + }; + + auto runLoadScripts = [&](String const& groupName, String const& sourcePath, AssetSourcePtr source) { + auto metadata = source->metadata(); + if (auto scripts = metadata.ptr("scripts")) { + if (auto scriptGroup = scripts->optArray(groupName)) { + auto memoryName = strf("{}::{}", metadata.value("name", File::baseName(sourcePath)), groupName); + JsonObject memoryMetadata{ {"name", memoryName} }; + auto memoryAssets = make_shared(memoryName, memoryMetadata); + Logger::info("Running {} scripts {}", groupName, *scriptGroup); + try { + auto context = luaEngine->createContext(); + decorateLuaContext(context, memoryAssets); + 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 {} scripts from asset source '{}': {}", groupName, sourcePath, e.what()); + } + if (!memoryAssets->empty()) + addSource(strf("{}::{}", sourcePath, groupName), memoryAssets); + } + } + }; + + List> sources; + for (auto& sourcePath : m_assetSources) { Logger::info("Loading assets from: '{}'", sourcePath); AssetSourcePtr source; @@ -168,61 +223,15 @@ Assets::Assets(Settings settings, StringList assetSources) { else source = std::make_shared(sourcePath); - auto addSource = [&](String const& sourcePath, AssetSourcePtr source) { - m_assetSourcePaths.add(sourcePath, source); - - for (auto const& filename : source->assetPaths()) { - if (filename.contains(AssetsPatchSuffix, String::CaseInsensitive)) { - if (filename.endsWith(AssetsPatchSuffix, String::CaseInsensitive)) { - auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix)); - if (auto p = m_files.ptr(targetPatchFile)) - p->patchSources.append({filename, source}); - } else { - for (int i = 0; i < 10; i++) { - if (filename.endsWith(AssetsPatchSuffix + toString(i), String::CaseInsensitive)) { - auto targetPatchFile = filename.substr(0, filename.size() - strlen(AssetsPatchSuffix) + 1); - if (auto p = m_files.ptr(targetPatchFile)) - p->patchSources.append({filename, source}); - break; - } - } - } - } - - auto& descriptor = m_files[filename]; - descriptor.sourceName = filename; - descriptor.source = source; - m_filesByExtension[AssetPath::extension(filename).toLower()].insert(filename); - } - }; addSource(sourcePath, source); + sources.append(make_pair(sourcePath, 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(memoryMetadata); - Logger::info("Running onLoad scripts {}", *onLoad); - try { - auto context = luaEngine->createContext(); - decorateLuaContext(context, memoryAssets); - for (auto& jPath : *onLoad) { - 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()); - } - if (!memoryAssets->empty()) - addSource(memoryMetadata.get("name").toString(), memoryAssets); - } - } + 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) { diff --git a/source/base/StarMemoryAssetSource.cpp b/source/base/StarMemoryAssetSource.cpp index 9c777ed..1176f5a 100644 --- a/source/base/StarMemoryAssetSource.cpp +++ b/source/base/StarMemoryAssetSource.cpp @@ -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(std::move(data))).second; +void MemoryAssetSource::set(String const& path, ByteArray data) { + m_files[path] = make_shared(std::move(data)); } ByteArray MemoryAssetSource::read(String const& path) { diff --git a/source/base/StarMemoryAssetSource.hpp b/source/base/StarMemoryAssetSource.hpp index 353a132..e5ef740 100644 --- a/source/base/StarMemoryAssetSource.hpp +++ b/source/base/StarMemoryAssetSource.hpp @@ -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 m_files; };