DITSpecification.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2021 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "InputCommon/DynamicInputTextures/DITSpecification.h"
  4. #include <fmt/format.h>
  5. #include "Common/FileUtil.h"
  6. #include "Common/IOFile.h"
  7. #include "Common/Logging/Log.h"
  8. #include "Core/ConfigManager.h"
  9. namespace InputCommon::DynamicInputTextures
  10. {
  11. bool ProcessSpecificationV1(picojson::value& root, std::vector<Data>& input_textures,
  12. const std::string& base_path, const std::string& json_file)
  13. {
  14. const picojson::value& output_textures_json = root.get("output_textures");
  15. if (!output_textures_json.is<picojson::object>())
  16. {
  17. ERROR_LOG_FMT(
  18. VIDEO,
  19. "Failed to load dynamic input json file '{}' because 'output_textures' is missing or "
  20. "was not of type object",
  21. json_file);
  22. return false;
  23. }
  24. const picojson::value& preserve_aspect_ratio_json = root.get("preserve_aspect_ratio");
  25. bool preserve_aspect_ratio = true;
  26. if (preserve_aspect_ratio_json.is<bool>())
  27. {
  28. preserve_aspect_ratio = preserve_aspect_ratio_json.get<bool>();
  29. }
  30. const picojson::value& generated_folder_name_json = root.get("generated_folder_name");
  31. const std::string& game_id = SConfig::GetInstance().GetGameID();
  32. std::string generated_folder_name = fmt::format("{}_Generated", game_id);
  33. if (generated_folder_name_json.is<std::string>())
  34. {
  35. generated_folder_name = generated_folder_name_json.get<std::string>();
  36. }
  37. const picojson::value& default_host_controls_json = root.get("default_host_controls");
  38. picojson::object default_host_controls;
  39. if (default_host_controls_json.is<picojson::object>())
  40. {
  41. default_host_controls = default_host_controls_json.get<picojson::object>();
  42. }
  43. const auto output_textures = output_textures_json.get<picojson::object>();
  44. for (auto& [name, data] : output_textures)
  45. {
  46. Data texture_data;
  47. texture_data.m_hires_texture_name = name;
  48. // Required fields
  49. const picojson::value& image = data.get("image");
  50. const picojson::value& emulated_controls = data.get("emulated_controls");
  51. if (!image.is<std::string>() || !emulated_controls.is<picojson::object>())
  52. {
  53. ERROR_LOG_FMT(VIDEO,
  54. "Failed to load dynamic input json file '{}' because required fields "
  55. "'image', or 'emulated_controls' are either "
  56. "missing or the incorrect type",
  57. json_file);
  58. return false;
  59. }
  60. texture_data.m_image_name = image.to_str();
  61. texture_data.m_preserve_aspect_ratio = preserve_aspect_ratio;
  62. texture_data.m_generated_folder_name = generated_folder_name;
  63. const std::string image_full_path = base_path + texture_data.m_image_name;
  64. if (!File::Exists(image_full_path))
  65. {
  66. ERROR_LOG_FMT(VIDEO,
  67. "Failed to load dynamic input json file '{}' because the image '{}' "
  68. "could not be loaded",
  69. json_file, image_full_path);
  70. return false;
  71. }
  72. const auto& emulated_controls_json = emulated_controls.get<picojson::object>();
  73. for (auto& [emulated_controller_name, map] : emulated_controls_json)
  74. {
  75. if (!map.is<picojson::object>())
  76. {
  77. ERROR_LOG_FMT(VIDEO,
  78. "Failed to load dynamic input json file '{}' because 'emulated_controls' "
  79. "map key '{}' is incorrect type. Expected map ",
  80. json_file, emulated_controller_name);
  81. return false;
  82. }
  83. auto& key_to_regions = texture_data.m_emulated_controllers[emulated_controller_name];
  84. for (auto& [emulated_control, regions_array] : map.get<picojson::object>())
  85. {
  86. if (!regions_array.is<picojson::array>())
  87. {
  88. ERROR_LOG_FMT(
  89. VIDEO,
  90. "Failed to load dynamic input json file '{}' because emulated controller '{}' "
  91. "key '{}' has incorrect value type. Expected array ",
  92. json_file, emulated_controller_name, emulated_control);
  93. return false;
  94. }
  95. std::vector<Rect> region_rects;
  96. for (auto& region : regions_array.get<picojson::array>())
  97. {
  98. Rect r;
  99. if (!region.is<picojson::array>())
  100. {
  101. ERROR_LOG_FMT(
  102. VIDEO,
  103. "Failed to load dynamic input json file '{}' because emulated controller '{}' "
  104. "key '{}' has a region with the incorrect type. Expected array ",
  105. json_file, emulated_controller_name, emulated_control);
  106. return false;
  107. }
  108. auto region_offsets = region.get<picojson::array>();
  109. if (region_offsets.size() != 4)
  110. {
  111. ERROR_LOG_FMT(
  112. VIDEO,
  113. "Failed to load dynamic input json file '{}' because emulated controller '{}' "
  114. "key '{}' has a region that does not have 4 offsets (left, top, right, "
  115. "bottom).",
  116. json_file, emulated_controller_name, emulated_control);
  117. return false;
  118. }
  119. if (!std::ranges::all_of(region_offsets, &picojson::value::is<double>))
  120. {
  121. ERROR_LOG_FMT(
  122. VIDEO,
  123. "Failed to load dynamic input json file '{}' because emulated controller '{}' "
  124. "key '{}' has a region that has the incorrect offset type.",
  125. json_file, emulated_controller_name, emulated_control);
  126. return false;
  127. }
  128. r.left = static_cast<u32>(region_offsets[0].get<double>());
  129. r.top = static_cast<u32>(region_offsets[1].get<double>());
  130. r.right = static_cast<u32>(region_offsets[2].get<double>());
  131. r.bottom = static_cast<u32>(region_offsets[3].get<double>());
  132. region_rects.push_back(r);
  133. }
  134. key_to_regions.insert_or_assign(emulated_control, std::move(region_rects));
  135. }
  136. }
  137. // Default to the default controls but overwrite if the creator
  138. // has provided something specific
  139. picojson::object host_controls = default_host_controls;
  140. const picojson::value& host_controls_json = data.get("host_controls");
  141. if (host_controls_json.is<picojson::object>())
  142. {
  143. host_controls = host_controls_json.get<picojson::object>();
  144. }
  145. if (host_controls.empty())
  146. {
  147. ERROR_LOG_FMT(VIDEO,
  148. "Failed to load dynamic input json file '{}' because field "
  149. "'host_controls' is missing ",
  150. json_file);
  151. return false;
  152. }
  153. for (auto& [host_device, map] : host_controls)
  154. {
  155. if (!map.is<picojson::object>())
  156. {
  157. ERROR_LOG_FMT(VIDEO,
  158. "Failed to load dynamic input json file '{}' because 'host_controls' "
  159. "map key '{}' is incorrect type ",
  160. json_file, host_device);
  161. return false;
  162. }
  163. auto& host_control_to_imagename = texture_data.m_host_devices[host_device];
  164. for (auto& [host_control, image_name] : map.get<picojson::object>())
  165. {
  166. host_control_to_imagename.insert_or_assign(host_control, image_name.to_str());
  167. }
  168. }
  169. input_textures.emplace_back(std::move(texture_data));
  170. }
  171. return true;
  172. }
  173. } // namespace InputCommon::DynamicInputTextures