diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/base/StarAssets.cpp | 107 | ||||
-rw-r--r-- | source/base/StarMemoryAssetSource.cpp | 10 | ||||
-rw-r--r-- | source/base/StarMemoryAssetSource.hpp | 6 |
3 files changed, 69 insertions, 54 deletions
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<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,69 +160,78 @@ 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); - 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; - } + 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); + 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 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) { 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<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) { 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<ByteArrayPtr> m_files; }; |