| 1040 | } |
| 1041 | |
| 1042 | Json Assets::readJson(String const& path) const { |
| 1043 | ByteArray streamData = read(path); |
| 1044 | try { |
| 1045 | Json result = inputUtf8Json(streamData.begin(), streamData.end(), JsonParseType::Top); |
| 1046 | for (auto const& pair : m_files.get(path).patchSources) { |
| 1047 | auto patchAssetPath = AssetPath::split(pair.first); |
| 1048 | auto& patchBasePath = patchAssetPath.basePath; |
| 1049 | auto& patchSource = pair.second; |
| 1050 | auto patchStream = patchSource->read(patchBasePath); |
| 1051 | if (patchBasePath.endsWith(".lua")) { |
| 1052 | std::pair<AssetSource*, String> contextKey = make_pair(patchSource.get(), patchBasePath); |
| 1053 | RecursiveMutexLocker luaLocker(m_luaMutex); |
| 1054 | // Kae: i don't like that lock. perhaps have a LuaEngine and patch context cache per worker thread later on? |
| 1055 | LuaContextPtr& context = m_patchContexts[contextKey]; |
| 1056 | if (!context) { |
| 1057 | context = make_shared<LuaContext>(as<LuaEngine>(m_luaEngine.get())->createContext()); |
| 1058 | context->load(patchStream, patchBasePath); |
| 1059 | } |
| 1060 | auto newResult = context->invokePath<Json>("patch", result, path); |
| 1061 | if (newResult) |
| 1062 | result = std::move(newResult); |
| 1063 | } else { |
| 1064 | try { |
| 1065 | auto patchJson = inputUtf8Json(patchStream.begin(), patchStream.end(), JsonParseType::Top); |
| 1066 | if (patchAssetPath.subPath) |
| 1067 | patchJson = patchJson.query(*patchAssetPath.subPath); |
| 1068 | if (patchJson.isType(Json::Type::Array)) { |
| 1069 | auto patchData = patchJson.toArray(); |
| 1070 | try { |
| 1071 | result = checkPatchArray(pair.first, patchSource, result, patchData, {}); |
| 1072 | } catch (JsonPatchTestFail const& e) { |
| 1073 | Logger::debug("Patch test failure from file {} in source: '{}' at '{}'. Caused by: {}", pair.first, patchSource->metadata().value("name", ""), m_assetSourcePaths.getLeft(patchSource), e.what()); |
| 1074 | } catch (JsonPatchException const& e) { |
| 1075 | Logger::error("Could not apply patch from file {} in source: '{}' at '{}'. Caused by: {}", pair.first, patchSource->metadata().value("name", ""), m_assetSourcePaths.getLeft(patchSource), e.what()); |
| 1076 | } |
| 1077 | } else if (patchJson.isType(Json::Type::Object)) { |
| 1078 | result = jsonMergeNulling(result, patchJson.toObject()); |
| 1079 | } |
| 1080 | } catch (std::exception const& e) { |
| 1081 | throw JsonParsingException(strf("Cannot parse json patch file: {} in source {}", patchBasePath, patchSource->metadata().value("name", "")), e); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | return result; |
| 1086 | } catch (std::exception const& e) { |
| 1087 | throw JsonParsingException(strf("Cannot parse json file: {}", path), e); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | bool Assets::doLoad(AssetId const& id) const { |
nothing calls this directly
no test coverage detected