| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/GraphicsModSystem/Config/GraphicsModAsset.h" |
| 5 | |
| 6 | #include "Common/Logging/Log.h" |
| 7 | #include "Common/StringUtil.h" |
| 8 | |
| 9 | void GraphicsModAssetConfig::SerializeToConfig(picojson::object& json_obj) const |
| 10 | { |
| 11 | json_obj.emplace("name", m_asset_id); |
| 12 | |
| 13 | picojson::object serialized_data; |
| 14 | for (const auto& [name, path] : m_map) |
| 15 | { |
| 16 | serialized_data.emplace(name, PathToString(path)); |
| 17 | } |
| 18 | json_obj.emplace("data", std::move(serialized_data)); |
| 19 | } |
| 20 | |
| 21 | bool GraphicsModAssetConfig::DeserializeFromConfig(const picojson::object& obj) |
| 22 | { |
| 23 | auto name_iter = obj.find("name"); |
| 24 | if (name_iter == obj.end()) |
| 25 | { |
| 26 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset has no name"); |
| 27 | return false; |
| 28 | } |
| 29 | if (!name_iter->second.is<std::string>()) |
| 30 | { |
| 31 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset has a name " |
| 32 | "that is not a string"); |
| 33 | return false; |
| 34 | } |
| 35 | m_asset_id = name_iter->second.to_str(); |
| 36 | |
| 37 | auto data_iter = obj.find("data"); |
| 38 | if (data_iter == obj.end()) |
| 39 | { |
| 40 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset '{}' has no data", |
| 41 | m_asset_id); |
| 42 | return false; |
| 43 | } |
| 44 | if (!data_iter->second.is<picojson::object>()) |
| 45 | { |
| 46 | ERROR_LOG_FMT(VIDEO, |
| 47 | "Failed to load mod configuration file, specified asset '{}' has data " |
| 48 | "that is not an object", |
| 49 | m_asset_id); |
| 50 | return false; |
| 51 | } |
| 52 | for (const auto& [key, value] : data_iter->second.get<picojson::object>()) |
| 53 | { |
| 54 | if (!value.is<std::string>()) |
| 55 | { |
| 56 | ERROR_LOG_FMT(VIDEO, |
| 57 | "Failed to load mod configuration file, specified asset '{}' has data " |
| 58 | "with a value for key '{}' that is not a string", |
| 59 | m_asset_id, key); |
| 60 | return false; |
| 61 | } |
| 62 | m_map[key] = value.to_str(); |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |