| 958 | } |
| 959 | |
| 960 | ImageConstPtr Assets::readImage(String const& path) const { |
| 961 | if (auto p = m_files.ptr(path)) { |
| 962 | ImageConstPtr image; |
| 963 | if (auto memorySource = as<MemoryAssetSource>(p->source)) |
| 964 | image = memorySource->image(p->sourceName); |
| 965 | if (!image) |
| 966 | image = make_shared<Image>(Image::readPng(p->source->open(p->sourceName))); |
| 967 | |
| 968 | if (!p->patchSources.empty()) { |
| 969 | RecursiveMutexLocker luaLocker(m_luaMutex); |
| 970 | LuaEngine* luaEngine = as<LuaEngine>(m_luaEngine.get()); |
| 971 | LuaValue result = luaEngine->createUserData(*image); |
| 972 | luaLocker.unlock(); |
| 973 | for (auto const& pair : p->patchSources) { |
| 974 | auto& patchPath = pair.first; |
| 975 | auto& patchSource = pair.second; |
| 976 | auto patchStream = patchSource->read(patchPath); |
| 977 | if (patchPath.endsWith(".lua")) { |
| 978 | std::pair<AssetSource*, String> contextKey = make_pair(patchSource.get(), patchPath); |
| 979 | luaLocker.lock(); |
| 980 | LuaContextPtr& context = m_patchContexts[contextKey]; |
| 981 | if (!context) { |
| 982 | context = make_shared<LuaContext>(luaEngine->createContext()); |
| 983 | context->load(patchStream, patchPath); |
| 984 | } |
| 985 | auto newResult = context->invokePath<LuaValue>("patch", result, path); |
| 986 | if (!newResult.is<LuaNilType>()) { |
| 987 | if (auto ud = newResult.ptr<LuaUserData>()) { |
| 988 | if (ud->is<Image>()) |
| 989 | result = std::move(newResult); |
| 990 | else |
| 991 | Logger::warn("Patch '{}' for image '{}' returned a non-Image userdata value, ignoring"); |
| 992 | } else { |
| 993 | Logger::warn("Patch '{}' for image '{}' returned a non-Image value, ignoring"); |
| 994 | } |
| 995 | } |
| 996 | luaLocker.unlock(); |
| 997 | } else { |
| 998 | Logger::warn("Patch '{}' for image '{}' isn't a Lua script, ignoring", patchPath, path); |
| 999 | } |
| 1000 | } |
| 1001 | image = make_shared<Image>(std::move(result.get<LuaUserData>().get<Image>())); |
| 1002 | } |
| 1003 | |
| 1004 | return image; |
| 1005 | } |
| 1006 | throw AssetException(strf("No such asset '{}'", path)); |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | Json Assets::checkPatchArray(String const& path, AssetSourcePtr const& source, Json const result, JsonArray const patchData, Maybe<Json> const external) const { |
nothing calls this directly
no test coverage detected