GameModDescriptor.cpp 7.3 KB

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