| 111 | } |
| 112 | |
| 113 | Assets::Assets(Settings settings, StringList assetSources) { |
| 114 | const char* AssetsPatchSuffix = ".patch"; |
| 115 | const char* AssetsPatchListSuffix = ".patchlist"; |
| 116 | const char* AssetsLuaPatchSuffix = ".patch.lua"; |
| 117 | |
| 118 | m_settings = std::move(settings); |
| 119 | m_stopThreads = false; |
| 120 | m_assetSources = std::move(assetSources); |
| 121 | |
| 122 | auto luaEngine = LuaEngine::create(); |
| 123 | m_luaEngine = luaEngine; |
| 124 | auto pushGlobalContext = [&luaEngine](String const& name, LuaCallbacks && callbacks) { |
| 125 | auto table = luaEngine->createTable(); |
| 126 | for (auto const& p : callbacks.callbacks()) |
| 127 | table.set(p.first, luaEngine->createWrappedFunction(p.second)); |
| 128 | luaEngine->setGlobal(name, table); |
| 129 | }; |
| 130 | |
| 131 | auto makeBaseAssetCallbacks = [this]() { |
| 132 | LuaCallbacks callbacks; |
| 133 | callbacks.registerCallbackWithSignature<StringSet, String>("byExtension", bind(&Assets::scanExtension, this, _1)); |
| 134 | callbacks.registerCallbackWithSignature<Json, String>("json", bind(&Assets::json, this, _1)); |
| 135 | callbacks.registerCallbackWithSignature<bool, String>("exists", bind(&Assets::assetExists, this, _1)); |
| 136 | |
| 137 | callbacks.registerCallback("sourcePaths", [this](LuaEngine& engine, Maybe<bool> withMetaData) -> LuaTable { |
| 138 | auto assetSources = this->assetSources(); |
| 139 | auto table = engine.createTable(assetSources.size(), 0); |
| 140 | if (withMetaData.value()) { |
| 141 | for (auto& assetSource : assetSources) |
| 142 | table.set(assetSource, this->assetSourceMetadata(assetSource)); |
| 143 | } |
| 144 | else { |
| 145 | size_t i = 0; |
| 146 | for (auto& assetSource : assetSources) |
| 147 | table.set(++i, assetSource); |
| 148 | } |
| 149 | return table; |
| 150 | }); |
| 151 | |
| 152 | callbacks.registerCallback("sourceMetadata", [this](String const& sourcePath) -> Maybe<JsonObject> { |
| 153 | if (auto assetSource = m_assetSourcePaths.rightPtr(sourcePath)) |
| 154 | return (*assetSource)->metadata(); |
| 155 | return {}; |
| 156 | }); |
| 157 | |
| 158 | callbacks.registerCallback("origin", [this](String const& path) -> Maybe<String> { |
| 159 | if (auto descriptor = this->assetDescriptor(path)) |
| 160 | return this->assetSourcePath(descriptor->source); |
| 161 | return {}; |
| 162 | }); |
| 163 | |
| 164 | callbacks.registerCallback("bytes", [this](String const& path) -> String { |
| 165 | auto assetBytes = bytes(path); |
| 166 | return String(assetBytes->ptr(), assetBytes->size()); |
| 167 | }); |
| 168 | |
| 169 | callbacks.registerCallback("image", [this](String const& path) -> Image { |
| 170 | auto assetImage = image(path); |
nothing calls this directly
no test coverage detected