Debian Codegraph @

Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "InputCommon/DynamicInputTextures/DITConfiguration.h"
5
6 #include <optional>
7 #include <string>
8
9 #include <picojson.h>
10
11 #include "Common/IniFile.h"
12 #include "Common/JsonUtil.h"
13 #include "Common/Logging/Log.h"
14 #include "Common/StringUtil.h"
15 #include "Core/ConfigManager.h"
16 #include "InputCommon/DynamicInputTextures/DITSpecification.h"
17 #include "InputCommon/ImageOperations.h"
18
19 namespace InputCommon::DynamicInputTextures
20 {
21 Configuration::Configuration(const std::string& json_path)
22 {
23 picojson::value root;
24 std::string error;
25 if (!JsonFromFile(json_path, &root, &error))
26 {
27 ERROR_LOG_FMT(VIDEO, "Failed to load dynamic input json file '{}' due to parse error: {}",
28 json_path, error);
29 m_valid = false;
30 return;
31 }
32
33 SplitPath(json_path, &m_base_path, nullptr, nullptr);
34
35 const picojson::value& specification_json = root.get("specification");
36 u8 specification = 1;
37 if (specification_json.is<double>())
38 {
39 const double spec_from_json = specification_json.get<double>();
40 if (spec_from_json < static_cast<double>(std::numeric_limits<u8>::min()) ||
41 spec_from_json > static_cast<double>(std::numeric_limits<u8>::max()))
42 {
43 ERROR_LOG_FMT(
44 VIDEO,
45 "Failed to load dynamic input json file '{}', specification '{}' is not within bounds",
46 json_path, spec_from_json);
47 m_valid = false;
48 return;
49 }
50 specification = static_cast<u8>(spec_from_json);
51 }
52
53 if (specification != 1)
54 {
55 ERROR_LOG_FMT(VIDEO,
56 "Failed to load dynamic input json file '{}', specification '{}' is invalid",
57 json_path, specification);
58 m_valid = false;
59 return;
60 }
61
62 m_valid = ProcessSpecificationV1(root, m_dynamic_input_textures, m_base_path, json_path);
63 }
64
65 Configuration::~Configuration() = default;
66
67 void Configuration::GenerateTextures(const Common::IniFile& file,
68 const std::vector<std::string>& controller_names,
69 OutputDetails* output) const
70 {
71 for (const auto& texture_data : m_dynamic_input_textures)
72 {
73 GenerateTexture(file, controller_names, texture_data, output);
74 }
75 }
76
77 void Configuration::GenerateTexture(const Common::IniFile& file,
78 const std::vector<std::string>& controller_names,
79 const Data& texture_data, OutputDetails* output) const
80 {
81 // Two copies of the loaded texture
82 // The first one is used as a fallback if a key or device isn't mapped
83 // the second one is used as the final image to write to the textures directory
84 const auto original_image = LoadImage(m_base_path + texture_data.m_image_name);
85 if (!original_image)
86 {
87 ERROR_LOG_FMT(VIDEO, "Failed to load image '{}' needed for dynamic input texture generation",
88 texture_data.m_image_name);
89 return;
90 }
91 auto image_to_write = original_image;
92
93 bool dirty = false;
94
95 for (const auto& controller_name : controller_names)
96 {
97 auto* sec = file.GetSection(controller_name);
98 if (!sec)
99 {
100 continue;
101 }
102
103 std::string device_name;
104 if (!sec->Get("Device", &device_name))
105 {
106 continue;
107 }
108
109 auto emulated_controls_iter = texture_data.m_emulated_controllers.find(controller_name);
110 if (emulated_controls_iter == texture_data.m_emulated_controllers.end())
111 {
112 continue;
113 }
114
115 bool device_found = true;
116 auto host_devices_iter = texture_data.m_host_devices.find(device_name);
117 if (host_devices_iter == texture_data.m_host_devices.end())
118 {
119 // If we fail to find our exact device,
120 // it's possible the creator doesn't care (single player game)
121 // and has used a wildcard for any device
122 host_devices_iter = texture_data.m_host_devices.find("");
123
124 if (host_devices_iter == texture_data.m_host_devices.end())
125 {
126 device_found = false;
127 }
128 }
129
130 for (auto& [emulated_key, rects] : emulated_controls_iter->second)
131 {
132 if (!device_found)
133 {
134 // If we get here, that means the controller is set to a
135 // device not exposed to the pack
136 dirty = true;
137 continue;
138 }
139
140 std::string host_key;
141 sec->Get(emulated_key, &host_key);
142
143 const auto input_image_iter = host_devices_iter->second.find(host_key);
144 if (input_image_iter == host_devices_iter->second.end())
145 {
146 dirty = true;
147 }
148 else
149 {
150 const std::string full_image_path = m_base_path + input_image_iter->second;
151 const auto host_key_image = LoadImage(full_image_path);
152 if (!host_key_image)
153 {
154 ERROR_LOG_FMT(VIDEO,
155 "Failed to load image '{}' needed for dynamic input texture generation",
156 full_image_path);
157 continue;
158 }
159
160 for (const auto& rect : rects)
161 {
162 InputCommon::ImagePixelData pixel_data;
163 if (host_key_image->width == rect.GetWidth() &&
164 host_key_image->height == rect.GetHeight())
165 {
166 pixel_data = *host_key_image;
167 }
168 else if (texture_data.m_preserve_aspect_ratio)
169 {
170 pixel_data =
171 ResizeKeepAspectRatio(ResizeMode::Nearest, *host_key_image, rect.GetWidth(),
172 rect.GetHeight(), Pixel{0, 0, 0, 0});
173 }
174 else
175 {
176 pixel_data =
177 Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight());
178 }
179
180 CopyImageRegion(pixel_data, *image_to_write,
181 Rect{0, 0, rect.GetWidth(), rect.GetHeight()}, rect);
182 dirty = true;
183 }
184 }
185 }
186 }
187
188 if (dirty)
189 {
190 (*output)[texture_data.m_generated_folder_name][texture_data.m_hires_texture_name] =
191 std::move(*image_to_write);
192 }
193 }
194 } // namespace InputCommon::DynamicInputTextures