| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/GameModDescriptor.h" |
| 5 | |
| 6 | #include <optional> |
| 7 | #include <string> |
| 8 | #include <string_view> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <picojson.h> |
| 12 | |
| 13 | #include "Common/IOFile.h" |
| 14 | #include "Common/MathUtil.h" |
| 15 | #include "Common/StringUtil.h" |
| 16 | |
| 17 | namespace DiscIO |
| 18 | { |
| 19 | static std::string MakeAbsolute(const std::string& directory, const std::string& path) |
| 20 | { |
| 21 | return PathToString(StringToPath(directory) / StringToPath(path)); |
| 22 | } |
| 23 | |
| 24 | std::optional<GameModDescriptor> ParseGameModDescriptorFile(const std::string& filename) |
| 25 | { |
| 26 | File::IOFile f(filename, "rb"); |
| 27 | if (!f) |
| 28 | return std::nullopt; |
| 29 | |
| 30 | std::vector<char> data; |
| 31 | data.resize(f.GetSize()); |
| 32 | if (!f.ReadBytes(data.data(), data.size())) |
| 33 | return std::nullopt; |
| 34 | |
| 35 | #ifdef _WIN32 |
| 36 | std::string path = ReplaceAll(filename, "\\", "/"); |
| 37 | #else |
| 38 | const std::string& path = filename; |
| 39 | #endif |
| 40 | return ParseGameModDescriptorString(std::string_view(data.data(), data.size()), path); |
| 41 | } |
| 42 | |
| 43 | static std::vector<GameModDescriptorRiivolutionPatchOption> |
| 44 | ParseRiivolutionOptions(const picojson::array& array) |
| 45 | { |
| 46 | std::vector<GameModDescriptorRiivolutionPatchOption> options; |
| 47 | for (const auto& option_object : array) |
| 48 | { |
| 49 | if (!option_object.is<picojson::object>()) |
| 50 | continue; |
| 51 | |
| 52 | auto& option = options.emplace_back(); |
| 53 | for (const auto& [key, value] : option_object.get<picojson::object>()) |
| 54 | { |
| 55 | if (key == "section-name" && value.is<std::string>()) |
| 56 | option.section_name = value.get<std::string>(); |
| 57 | else if (key == "option-id" && value.is<std::string>()) |
| 58 | option.option_id = value.get<std::string>(); |
| 59 | else if (key == "option-name" && value.is<std::string>()) |
| 60 | option.option_name = value.get<std::string>(); |
| 61 | else if (key == "choice" && value.is<double>()) |
| 62 | option.choice = MathUtil::SaturatingCast<u32>(value.get<double>()); |
| 63 | } |
| 64 | } |
| 65 | return options; |
| 66 | } |
| 67 | |
| 68 | static GameModDescriptorRiivolution ParseRiivolutionObject(const std::string& json_directory, |
| 69 | const picojson::object& object) |
| 70 | { |
| 71 | GameModDescriptorRiivolution r; |
| 72 | for (const auto& [element_key, element_value] : object) |
| 73 | { |
| 74 | if (element_key == "patches" && element_value.is<picojson::array>()) |
| 75 | { |
| 76 | for (const auto& patch_object : element_value.get<picojson::array>()) |
| 77 | { |
| 78 | if (!patch_object.is<picojson::object>()) |
| 79 | continue; |
| 80 | |
| 81 | auto& patch = r.patches.emplace_back(); |
| 82 | for (const auto& [key, value] : patch_object.get<picojson::object>()) |
| 83 | { |
| 84 | if (key == "xml" && value.is<std::string>()) |
| 85 | patch.xml = MakeAbsolute(json_directory, value.get<std::string>()); |
| 86 | else if (key == "root" && value.is<std::string>()) |
| 87 | patch.root = MakeAbsolute(json_directory, value.get<std::string>()); |
| 88 | else if (key == "options" && value.is<picojson::array>()) |
| 89 | patch.options = ParseRiivolutionOptions(value.get<picojson::array>()); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return r; |
| 95 | } |
| 96 | |
| 97 | std::optional<GameModDescriptor> ParseGameModDescriptorString(std::string_view json, |
| 98 | std::string_view json_path) |
| 99 | { |
| 100 | std::string json_directory; |
| 101 | SplitPath(json_path, &json_directory, nullptr, nullptr); |
| 102 | |
| 103 | picojson::value json_root; |
| 104 | std::string err; |
| 105 | picojson::parse(json_root, json.begin(), json.end(), &err); |
| 106 | if (!err.empty()) |
| 107 | return std::nullopt; |
| 108 | if (!json_root.is<picojson::object>()) |
| 109 | return std::nullopt; |
| 110 | |
| 111 | GameModDescriptor descriptor; |
| 112 | bool is_game_mod_descriptor = false; |
| 113 | bool is_valid_version = false; |
| 114 | for (const auto& [key, value] : json_root.get<picojson::object>()) |
| 115 | { |
| 116 | if (key == "type" && value.is<std::string>()) |
| 117 | { |
| 118 | is_game_mod_descriptor = value.get<std::string>() == "dolphin-game-mod-descriptor"; |
| 119 | } |
| 120 | else if (key == "version" && value.is<double>()) |
| 121 | { |
| 122 | is_valid_version = value.get<double>() == 1.0; |
| 123 | } |
| 124 | else if (key == "base-file" && value.is<std::string>()) |
| 125 | { |
| 126 | descriptor.base_file = MakeAbsolute(json_directory, value.get<std::string>()); |
| 127 | } |
| 128 | else if (key == "display-name" && value.is<std::string>()) |
| 129 | { |
| 130 | descriptor.display_name = value.get<std::string>(); |
| 131 | } |
| 132 | else if (key == "maker" && value.is<std::string>()) |
| 133 | { |
| 134 | descriptor.maker = value.get<std::string>(); |
| 135 | } |
| 136 | else if (key == "banner" && value.is<std::string>()) |
| 137 | { |
| 138 | descriptor.banner = MakeAbsolute(json_directory, value.get<std::string>()); |
| 139 | } |
| 140 | else if (key == "riivolution" && value.is<picojson::object>()) |
| 141 | { |
| 142 | descriptor.riivolution = |
| 143 | ParseRiivolutionObject(json_directory, value.get<picojson::object>()); |
| 144 | } |
| 145 | } |
| 146 | if (!is_game_mod_descriptor || !is_valid_version) |
| 147 | return std::nullopt; |
| 148 | return descriptor; |
| 149 | } |
| 150 | |
| 151 | static picojson::object |
| 152 | WriteGameModDescriptorRiivolution(const GameModDescriptorRiivolution& riivolution) |
| 153 | { |
| 154 | picojson::array json_patches; |
| 155 | for (const auto& patch : riivolution.patches) |
| 156 | { |
| 157 | picojson::object json_patch; |
| 158 | if (!patch.xml.empty()) |
| 159 | json_patch["xml"] = picojson::value(patch.xml); |
| 160 | if (!patch.root.empty()) |
| 161 | json_patch["root"] = picojson::value(patch.root); |
| 162 | if (!patch.options.empty()) |
| 163 | { |
| 164 | picojson::array json_options; |
| 165 | for (const auto& option : patch.options) |
| 166 | { |
| 167 | picojson::object json_option; |
| 168 | if (!option.section_name.empty()) |
| 169 | json_option["section-name"] = picojson::value(option.section_name); |
| 170 | if (!option.option_id.empty()) |
| 171 | json_option["option-id"] = picojson::value(option.option_id); |
| 172 | if (!option.option_name.empty()) |
| 173 | json_option["option-name"] = picojson::value(option.option_name); |
| 174 | json_option["choice"] = picojson::value(static_cast<double>(option.choice)); |
| 175 | json_options.emplace_back(std::move(json_option)); |
| 176 | } |
| 177 | json_patch["options"] = picojson::value(std::move(json_options)); |
| 178 | } |
| 179 | json_patches.emplace_back(std::move(json_patch)); |
| 180 | } |
| 181 | |
| 182 | picojson::object json_riivolution; |
| 183 | json_riivolution["patches"] = picojson::value(std::move(json_patches)); |
| 184 | return json_riivolution; |
| 185 | } |
| 186 | |
| 187 | std::string WriteGameModDescriptorString(const GameModDescriptor& descriptor, bool pretty) |
| 188 | { |
| 189 | picojson::object json_root; |
| 190 | json_root["type"] = picojson::value("dolphin-game-mod-descriptor"); |
| 191 | json_root["version"] = picojson::value(1.0); |
| 192 | if (!descriptor.base_file.empty()) |
| 193 | json_root["base-file"] = picojson::value(descriptor.base_file); |
| 194 | if (!descriptor.display_name.empty()) |
| 195 | json_root["display-name"] = picojson::value(descriptor.display_name); |
| 196 | if (!descriptor.maker.empty()) |
| 197 | json_root["maker"] = picojson::value(descriptor.maker); |
| 198 | if (!descriptor.banner.empty()) |
| 199 | json_root["banner"] = picojson::value(descriptor.banner); |
| 200 | if (descriptor.riivolution) |
| 201 | { |
| 202 | json_root["riivolution"] = |
| 203 | picojson::value(WriteGameModDescriptorRiivolution(*descriptor.riivolution)); |
| 204 | } |
| 205 | return picojson::value(json_root).serialize(pretty); |
| 206 | } |
| 207 | |
| 208 | bool WriteGameModDescriptorFile(const std::string& filename, const GameModDescriptor& descriptor, |
| 209 | bool pretty) |
| 210 | { |
| 211 | auto json = WriteGameModDescriptorString(descriptor, pretty); |
| 212 | if (json.empty()) |
| 213 | return false; |
| 214 | |
| 215 | File::IOFile f(filename, "wb"); |
| 216 | if (!f) |
| 217 | return false; |
| 218 | |
| 219 | if (!f.WriteString(json)) |
| 220 | return false; |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | } // namespace DiscIO |