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