| 1 | // Copyright 2022 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/GraphicsModSystem/Config/GraphicsMod.h" |
| 5 | |
| 6 | #include <fmt/format.h> |
| 7 | |
| 8 | #include "Common/FileUtil.h" |
| 9 | #include "Common/JsonUtil.h" |
| 10 | #include "Common/Logging/Log.h" |
| 11 | #include "Common/StringUtil.h" |
| 12 | |
| 13 | #include "VideoCommon/GraphicsModSystem/Constants.h" |
| 14 | |
| 15 | std::optional<GraphicsModConfig> GraphicsModConfig::Create(const std::string& file_path, |
| 16 | Source source) |
| 17 | { |
| 18 | picojson::value root; |
| 19 | std::string error; |
| 20 | if (!JsonFromFile(file_path, &root, &error)) |
| 21 | { |
| 22 | ERROR_LOG_FMT(VIDEO, "Failed to load graphics mod json file '{}' due to parse error: {}", |
| 23 | file_path, error); |
| 24 | return std::nullopt; |
| 25 | } |
| 26 | |
| 27 | GraphicsModConfig result; |
| 28 | if (!result.DeserializeFromConfig(root)) |
| 29 | { |
| 30 | return std::nullopt; |
| 31 | } |
| 32 | result.m_source = source; |
| 33 | if (source == Source::User) |
| 34 | { |
| 35 | const std::string base_path = File::GetUserPath(D_GRAPHICSMOD_IDX); |
| 36 | if (base_path.size() > file_path.size()) |
| 37 | { |
| 38 | ERROR_LOG_FMT( |
| 39 | VIDEO, |
| 40 | "Failed to load graphics mod json file '{}' due to it not matching the base path: {}", |
| 41 | file_path, base_path); |
| 42 | return std::nullopt; |
| 43 | } |
| 44 | result.m_relative_path = file_path.substr(base_path.size()); |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | const std::string base_path = File::GetSysDirectory() + DOLPHIN_SYSTEM_GRAPHICS_MOD_DIR; |
| 49 | if (base_path.size() > file_path.size()) |
| 50 | { |
| 51 | ERROR_LOG_FMT( |
| 52 | VIDEO, |
| 53 | "Failed to load graphics mod json file '{}' due to it not matching the base path: {}", |
| 54 | file_path, base_path); |
| 55 | return std::nullopt; |
| 56 | } |
| 57 | result.m_relative_path = file_path.substr(base_path.size()); |
| 58 | } |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | std::optional<GraphicsModConfig> GraphicsModConfig::Create(const picojson::object* obj) |
| 64 | { |
| 65 | if (!obj) |
| 66 | return std::nullopt; |
| 67 | |
| 68 | const auto source_it = obj->find("source"); |
| 69 | if (source_it == obj->end()) |
| 70 | { |
| 71 | return std::nullopt; |
| 72 | } |
| 73 | const std::string source_str = source_it->second.to_str(); |
| 74 | |
| 75 | const auto path_it = obj->find("path"); |
| 76 | if (path_it == obj->end()) |
| 77 | { |
| 78 | return std::nullopt; |
| 79 | } |
| 80 | const std::string relative_path = path_it->second.to_str(); |
| 81 | |
| 82 | if (source_str == "system") |
| 83 | { |
| 84 | return Create(fmt::format("{}{}{}", File::GetSysDirectory(), DOLPHIN_SYSTEM_GRAPHICS_MOD_DIR, |
| 85 | relative_path), |
| 86 | Source::System); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | return Create(File::GetUserPath(D_GRAPHICSMOD_IDX) + relative_path, Source::User); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | std::string GraphicsModConfig::GetAbsolutePath() const |
| 95 | { |
| 96 | if (m_source == Source::System) |
| 97 | { |
| 98 | return WithUnifiedPathSeparators(fmt::format("{}{}{}", File::GetSysDirectory(), |
| 99 | DOLPHIN_SYSTEM_GRAPHICS_MOD_DIR, m_relative_path)); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | return WithUnifiedPathSeparators(File::GetUserPath(D_GRAPHICSMOD_IDX) + m_relative_path); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void GraphicsModConfig::SerializeToConfig(picojson::object& json_obj) const |
| 108 | { |
| 109 | picojson::object serialized_metadata; |
| 110 | serialized_metadata.emplace("title", m_title); |
| 111 | serialized_metadata.emplace("author", m_author); |
| 112 | serialized_metadata.emplace("description", m_description); |
| 113 | json_obj.emplace("meta", std::move(serialized_metadata)); |
| 114 | |
| 115 | picojson::array serialized_groups; |
| 116 | for (const auto& group : m_groups) |
| 117 | { |
| 118 | picojson::object serialized_group; |
| 119 | group.SerializeToConfig(serialized_group); |
| 120 | serialized_groups.emplace_back(std::move(serialized_group)); |
| 121 | } |
| 122 | json_obj.emplace("groups", std::move(serialized_groups)); |
| 123 | |
| 124 | picojson::array serialized_features; |
| 125 | for (const auto& feature : m_features) |
| 126 | { |
| 127 | picojson::object serialized_feature; |
| 128 | feature.SerializeToConfig(serialized_feature); |
| 129 | serialized_features.emplace_back(std::move(serialized_feature)); |
| 130 | } |
| 131 | json_obj.emplace("features", std::move(serialized_features)); |
| 132 | |
| 133 | picojson::array serialized_assets; |
| 134 | for (const auto& asset : m_assets) |
| 135 | { |
| 136 | picojson::object serialized_asset; |
| 137 | asset.SerializeToConfig(serialized_asset); |
| 138 | serialized_assets.emplace_back(std::move(serialized_asset)); |
| 139 | } |
| 140 | json_obj.emplace("assets", std::move(serialized_assets)); |
| 141 | } |
| 142 | |
| 143 | bool GraphicsModConfig::DeserializeFromConfig(const picojson::value& value) |
| 144 | { |
| 145 | const auto& meta = value.get("meta"); |
| 146 | if (meta.is<picojson::object>()) |
| 147 | { |
| 148 | const auto& title = meta.get("title"); |
| 149 | if (title.is<std::string>()) |
| 150 | { |
| 151 | m_title = title.to_str(); |
| 152 | } |
| 153 | |
| 154 | const auto& author = meta.get("author"); |
| 155 | if (author.is<std::string>()) |
| 156 | { |
| 157 | m_author = author.to_str(); |
| 158 | } |
| 159 | |
| 160 | const auto& description = meta.get("description"); |
| 161 | if (description.is<std::string>()) |
| 162 | { |
| 163 | m_description = description.to_str(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | const auto& groups = value.get("groups"); |
| 168 | if (groups.is<picojson::array>()) |
| 169 | { |
| 170 | for (const auto& group_val : groups.get<picojson::array>()) |
| 171 | { |
| 172 | if (!group_val.is<picojson::object>()) |
| 173 | { |
| 174 | ERROR_LOG_FMT( |
| 175 | VIDEO, "Failed to load mod configuration file, specified group is not a json object"); |
| 176 | return false; |
| 177 | } |
| 178 | GraphicsTargetGroupConfig group; |
| 179 | if (!group.DeserializeFromConfig(group_val.get<picojson::object>())) |
| 180 | { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | m_groups.push_back(std::move(group)); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | const auto& features = value.get("features"); |
| 189 | if (features.is<picojson::array>()) |
| 190 | { |
| 191 | for (const auto& feature_val : features.get<picojson::array>()) |
| 192 | { |
| 193 | if (!feature_val.is<picojson::object>()) |
| 194 | { |
| 195 | ERROR_LOG_FMT( |
| 196 | VIDEO, "Failed to load mod configuration file, specified feature is not a json object"); |
| 197 | return false; |
| 198 | } |
| 199 | GraphicsModFeatureConfig feature; |
| 200 | if (!feature.DeserializeFromConfig(feature_val.get<picojson::object>())) |
| 201 | { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | m_features.push_back(std::move(feature)); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | const auto& assets = value.get("assets"); |
| 210 | if (assets.is<picojson::array>()) |
| 211 | { |
| 212 | for (const auto& asset_val : assets.get<picojson::array>()) |
| 213 | { |
| 214 | if (!asset_val.is<picojson::object>()) |
| 215 | { |
| 216 | ERROR_LOG_FMT( |
| 217 | VIDEO, "Failed to load mod configuration file, specified asset is not a json object"); |
| 218 | return false; |
| 219 | } |
| 220 | GraphicsModAssetConfig asset; |
| 221 | if (!asset.DeserializeFromConfig(asset_val.get<picojson::object>())) |
| 222 | { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | m_assets.push_back(std::move(asset)); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | void GraphicsModConfig::SerializeToProfile(picojson::object* obj) const |
| 234 | { |
| 235 | if (!obj) |
| 236 | return; |
| 237 | |
| 238 | auto& json_obj = *obj; |
| 239 | switch (m_source) |
| 240 | { |
| 241 | case Source::User: |
| 242 | json_obj.emplace("source", "user"); |
| 243 | break; |
| 244 | case Source::System: |
| 245 | json_obj.emplace("source", "system"); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | json_obj.emplace("path", m_relative_path); |
| 250 | |
| 251 | picojson::array serialized_groups; |
| 252 | for (const auto& group : m_groups) |
| 253 | { |
| 254 | picojson::object serialized_group; |
| 255 | group.SerializeToProfile(&serialized_group); |
| 256 | serialized_groups.emplace_back(std::move(serialized_group)); |
| 257 | } |
| 258 | json_obj.emplace("groups", std::move(serialized_groups)); |
| 259 | |
| 260 | picojson::array serialized_features; |
| 261 | for (const auto& feature : m_features) |
| 262 | { |
| 263 | picojson::object serialized_feature; |
| 264 | feature.SerializeToProfile(&serialized_feature); |
| 265 | serialized_features.emplace_back(std::move(serialized_feature)); |
| 266 | } |
| 267 | json_obj.emplace("features", std::move(serialized_features)); |
| 268 | |
| 269 | json_obj.emplace("enabled", m_enabled); |
| 270 | |
| 271 | json_obj.emplace("weight", static_cast<double>(m_weight)); |
| 272 | } |
| 273 | |
| 274 | void GraphicsModConfig::DeserializeFromProfile(const picojson::object& obj) |
| 275 | { |
| 276 | if (const auto it = obj.find("groups"); it != obj.end()) |
| 277 | { |
| 278 | if (it->second.is<picojson::array>()) |
| 279 | { |
| 280 | const auto& serialized_groups = it->second.get<picojson::array>(); |
| 281 | if (serialized_groups.size() != m_groups.size()) |
| 282 | return; |
| 283 | |
| 284 | for (std::size_t i = 0; i < serialized_groups.size(); i++) |
| 285 | { |
| 286 | const auto& serialized_group_val = serialized_groups[i]; |
| 287 | if (serialized_group_val.is<picojson::object>()) |
| 288 | { |
| 289 | const auto& serialized_group = serialized_group_val.get<picojson::object>(); |
| 290 | m_groups[i].DeserializeFromProfile(serialized_group); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (const auto it = obj.find("features"); it != obj.end()) |
| 297 | { |
| 298 | if (it->second.is<picojson::array>()) |
| 299 | { |
| 300 | const auto& serialized_features = it->second.get<picojson::array>(); |
| 301 | if (serialized_features.size() != m_features.size()) |
| 302 | return; |
| 303 | |
| 304 | for (std::size_t i = 0; i < serialized_features.size(); i++) |
| 305 | { |
| 306 | const auto& serialized_feature_val = serialized_features[i]; |
| 307 | if (serialized_feature_val.is<picojson::object>()) |
| 308 | { |
| 309 | const auto& serialized_feature = serialized_feature_val.get<picojson::object>(); |
| 310 | m_features[i].DeserializeFromProfile(serialized_feature); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (const auto it = obj.find("enabled"); it != obj.end()) |
| 317 | { |
| 318 | if (it->second.is<bool>()) |
| 319 | { |
| 320 | m_enabled = it->second.get<bool>(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (const auto it = obj.find("weight"); it != obj.end()) |
| 325 | { |
| 326 | if (it->second.is<double>()) |
| 327 | { |
| 328 | m_weight = static_cast<u16>(it->second.get<double>()); |
| 329 | } |
| 330 | } |
| 331 | } |