| 1 | // Copyright 2022 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/GraphicsModSystem/Config/GraphicsTarget.h" |
| 5 | |
| 6 | #include "Common/Logging/Log.h" |
| 7 | #include "Common/StringUtil.h" |
| 8 | #include "Common/VariantUtil.h" |
| 9 | #include "VideoCommon/TextureCacheBase.h" |
| 10 | |
| 11 | namespace |
| 12 | { |
| 13 | template <typename T, std::enable_if_t<std::is_base_of_v<FBTarget, T>, int> = 0> |
| 14 | std::optional<T> DeserializeFBTargetFromConfig(const picojson::object& obj, std::string_view prefix) |
| 15 | { |
| 16 | T fb; |
| 17 | const auto texture_filename_iter = obj.find("texture_filename"); |
| 18 | if (texture_filename_iter == obj.end()) |
| 19 | { |
| 20 | ERROR_LOG_FMT(VIDEO, |
| 21 | "Failed to load mod configuration file, option 'texture_filename' not found"); |
| 22 | return std::nullopt; |
| 23 | } |
| 24 | if (!texture_filename_iter->second.is<std::string>()) |
| 25 | { |
| 26 | ERROR_LOG_FMT( |
| 27 | VIDEO, |
| 28 | "Failed to load mod configuration file, option 'texture_filename' is not a string type"); |
| 29 | return std::nullopt; |
| 30 | } |
| 31 | const auto texture_filename = texture_filename_iter->second.get<std::string>(); |
| 32 | const auto texture_filename_without_prefix = texture_filename.substr(prefix.size() + 1); |
| 33 | const auto split_str_values = SplitString(texture_filename_without_prefix, '_'); |
| 34 | if (split_str_values.size() == 1) |
| 35 | { |
| 36 | ERROR_LOG_FMT( |
| 37 | VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is not valid"); |
| 38 | return std::nullopt; |
| 39 | } |
| 40 | const auto split_width_height_values = SplitString(texture_filename_without_prefix, 'x'); |
| 41 | if (split_width_height_values.size() != 2) |
| 42 | { |
| 43 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is " |
| 44 | "not valid, width and height separator found more matches than expected"); |
| 45 | return std::nullopt; |
| 46 | } |
| 47 | |
| 48 | const std::size_t width_underscore_pos = split_width_height_values[0].find_last_of('_'); |
| 49 | std::string width_str; |
| 50 | if (width_underscore_pos == std::string::npos) |
| 51 | { |
| 52 | width_str = split_width_height_values[0]; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | width_str = split_width_height_values[0].substr(width_underscore_pos + 1); |
| 57 | } |
| 58 | if (!TryParse(width_str, &fb.m_width)) |
| 59 | { |
| 60 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is " |
| 61 | "not valid, width not a number"); |
| 62 | return std::nullopt; |
| 63 | } |
| 64 | |
| 65 | const std::size_t height_underscore_pos = split_width_height_values[1].find_first_of('_'); |
| 66 | if (height_underscore_pos == std::string::npos || |
| 67 | height_underscore_pos == split_width_height_values[1].size() - 1) |
| 68 | { |
| 69 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is " |
| 70 | "not valid, underscore after height is missing or incomplete"); |
| 71 | return std::nullopt; |
| 72 | } |
| 73 | const std::string height_str = split_width_height_values[1].substr(0, height_underscore_pos); |
| 74 | if (!TryParse(height_str, &fb.m_height)) |
| 75 | { |
| 76 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is " |
| 77 | "not valid, height not a number"); |
| 78 | return std::nullopt; |
| 79 | } |
| 80 | |
| 81 | const std::size_t format_underscore_pos = |
| 82 | split_width_height_values[1].find_first_of('_', height_underscore_pos + 1); |
| 83 | |
| 84 | std::string format_str; |
| 85 | if (format_underscore_pos == std::string::npos) |
| 86 | { |
| 87 | format_str = split_width_height_values[1].substr(height_underscore_pos + 1); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | format_str = split_width_height_values[1].substr( |
| 92 | height_underscore_pos + 1, (format_underscore_pos - height_underscore_pos) - 1); |
| 93 | } |
| 94 | u32 format; |
| 95 | if (!TryParse(format_str, &format)) |
| 96 | { |
| 97 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is " |
| 98 | "not valid, texture format is not a number"); |
| 99 | return std::nullopt; |
| 100 | } |
| 101 | if (!IsValidTextureFormat(static_cast<TextureFormat>(format))) |
| 102 | { |
| 103 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is " |
| 104 | "not valid, texture format is not valid"); |
| 105 | return std::nullopt; |
| 106 | } |
| 107 | fb.m_texture_format = static_cast<TextureFormat>(format); |
| 108 | |
| 109 | return fb; |
| 110 | } |
| 111 | std::optional<std::string> ExtractTextureFilenameForConfig(const picojson::object& obj) |
| 112 | { |
| 113 | const auto texture_filename_iter = obj.find("texture_filename"); |
| 114 | if (texture_filename_iter == obj.end()) |
| 115 | { |
| 116 | ERROR_LOG_FMT(VIDEO, |
| 117 | "Failed to load mod configuration file, option 'texture_filename' not found"); |
| 118 | return std::nullopt; |
| 119 | } |
| 120 | if (!texture_filename_iter->second.is<std::string>()) |
| 121 | { |
| 122 | ERROR_LOG_FMT( |
| 123 | VIDEO, |
| 124 | "Failed to load mod configuration file, option 'texture_filename' is not a string type"); |
| 125 | return std::nullopt; |
| 126 | } |
| 127 | std::string texture_info = texture_filename_iter->second.get<std::string>(); |
| 128 | |
| 129 | const auto handle_fb_texture = |
| 130 | [&texture_info](std::string_view type) -> std::optional<std::string> { |
| 131 | const auto letter_n_pos = texture_info.find("_n"); |
| 132 | if (letter_n_pos == std::string::npos) |
| 133 | { |
| 134 | ERROR_LOG_FMT(VIDEO, |
| 135 | "Failed to load mod configuration file, value in 'texture_filename' " |
| 136 | "is {} without a count", |
| 137 | type); |
| 138 | return std::nullopt; |
| 139 | } |
| 140 | |
| 141 | const auto post_underscore = texture_info.find_first_of('_', letter_n_pos + 2); |
| 142 | if (post_underscore == std::string::npos) |
| 143 | return texture_info.erase(letter_n_pos, texture_info.size() - letter_n_pos); |
| 144 | else |
| 145 | return texture_info.erase(letter_n_pos, post_underscore - letter_n_pos); |
| 146 | }; |
| 147 | |
| 148 | if (texture_info.starts_with(EFB_DUMP_PREFIX)) |
| 149 | return handle_fb_texture("an efb"); |
| 150 | else if (texture_info.starts_with(XFB_DUMP_PREFIX)) |
| 151 | return handle_fb_texture("a xfb"); |
| 152 | return texture_info; |
| 153 | } |
| 154 | } // namespace |
| 155 | |
| 156 | void SerializeTargetToConfig(picojson::object& json_obj, const GraphicsTargetConfig& target) |
| 157 | { |
| 158 | std::visit(overloaded{ |
| 159 | [&](const DrawStartedTextureTarget& the_target) { |
| 160 | json_obj.emplace("type", "draw_started"); |
| 161 | json_obj.emplace("texture_filename", the_target.m_texture_info_string); |
| 162 | }, |
| 163 | [&](const LoadTextureTarget& the_target) { |
| 164 | json_obj.emplace("type", "load_texture"); |
| 165 | json_obj.emplace("texture_filename", the_target.m_texture_info_string); |
| 166 | }, |
| 167 | [&](const CreateTextureTarget& the_target) { |
| 168 | json_obj.emplace("type", "create_texture"); |
| 169 | json_obj.emplace("texture_filename", the_target.m_texture_info_string); |
| 170 | }, |
| 171 | [&](const EFBTarget& the_target) { |
| 172 | json_obj.emplace("type", "efb"); |
| 173 | json_obj.emplace("texture_filename", |
| 174 | fmt::format("{}_{}x{}_{}", EFB_DUMP_PREFIX, the_target.m_width, |
| 175 | the_target.m_height, |
| 176 | static_cast<int>(the_target.m_texture_format))); |
| 177 | }, |
| 178 | [&](const XFBTarget& the_target) { |
| 179 | json_obj.emplace("type", "xfb"); |
| 180 | json_obj.emplace("texture_filename", |
| 181 | fmt::format("{}_{}x{}_{}", XFB_DUMP_PREFIX, the_target.m_width, |
| 182 | the_target.m_height, |
| 183 | static_cast<int>(the_target.m_texture_format))); |
| 184 | }, |
| 185 | [&](const ProjectionTarget& the_target) { |
| 186 | const char* type_name = "3d"; |
| 187 | if (the_target.m_projection_type == ProjectionType::Orthographic) |
| 188 | type_name = "2d"; |
| 189 | |
| 190 | json_obj.emplace("type", type_name); |
| 191 | |
| 192 | if (the_target.m_texture_info_string) |
| 193 | { |
| 194 | json_obj.emplace("texture_filename", *the_target.m_texture_info_string); |
| 195 | } |
| 196 | }, |
| 197 | }, |
| 198 | target); |
| 199 | } |
| 200 | |
| 201 | std::optional<GraphicsTargetConfig> DeserializeTargetFromConfig(const picojson::object& obj) |
| 202 | { |
| 203 | const auto type_iter = obj.find("type"); |
| 204 | if (type_iter == obj.end()) |
| 205 | { |
| 206 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, option 'type' not found"); |
| 207 | return std::nullopt; |
| 208 | } |
| 209 | if (!type_iter->second.is<std::string>()) |
| 210 | { |
| 211 | ERROR_LOG_FMT(VIDEO, |
| 212 | "Failed to load mod configuration file, option 'type' is not a string type"); |
| 213 | return std::nullopt; |
| 214 | } |
| 215 | const std::string& type = type_iter->second.get<std::string>(); |
| 216 | if (type == "draw_started") |
| 217 | { |
| 218 | std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj); |
| 219 | if (!texture_info.has_value()) |
| 220 | return std::nullopt; |
| 221 | |
| 222 | DrawStartedTextureTarget target; |
| 223 | target.m_texture_info_string = texture_info.value(); |
| 224 | return target; |
| 225 | } |
| 226 | else if (type == "load_texture") |
| 227 | { |
| 228 | std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj); |
| 229 | if (!texture_info.has_value()) |
| 230 | return std::nullopt; |
| 231 | |
| 232 | LoadTextureTarget target; |
| 233 | target.m_texture_info_string = texture_info.value(); |
| 234 | return target; |
| 235 | } |
| 236 | else if (type == "create_texture") |
| 237 | { |
| 238 | std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj); |
| 239 | if (!texture_info.has_value()) |
| 240 | return std::nullopt; |
| 241 | |
| 242 | CreateTextureTarget target; |
| 243 | target.m_texture_info_string = texture_info.value(); |
| 244 | return target; |
| 245 | } |
| 246 | else if (type == "efb") |
| 247 | { |
| 248 | return DeserializeFBTargetFromConfig<EFBTarget>(obj, EFB_DUMP_PREFIX); |
| 249 | } |
| 250 | else if (type == "xfb") |
| 251 | { |
| 252 | return DeserializeFBTargetFromConfig<XFBTarget>(obj, EFB_DUMP_PREFIX); |
| 253 | } |
| 254 | else if (type == "projection") |
| 255 | { |
| 256 | ProjectionTarget target; |
| 257 | const auto texture_iter = obj.find("texture_filename"); |
| 258 | if (texture_iter != obj.end()) |
| 259 | { |
| 260 | std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj); |
| 261 | if (!texture_info.has_value()) |
| 262 | return std::nullopt; |
| 263 | target.m_texture_info_string = texture_info; |
| 264 | } |
| 265 | const auto value_iter = obj.find("value"); |
| 266 | if (value_iter == obj.end()) |
| 267 | { |
| 268 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, option 'value' not found"); |
| 269 | return std::nullopt; |
| 270 | } |
| 271 | if (!value_iter->second.is<std::string>()) |
| 272 | { |
| 273 | ERROR_LOG_FMT(VIDEO, |
| 274 | "Failed to load mod configuration file, option 'value' is not a string type"); |
| 275 | return std::nullopt; |
| 276 | } |
| 277 | const auto& value_str = value_iter->second.get<std::string>(); |
| 278 | if (value_str == "2d") |
| 279 | { |
| 280 | target.m_projection_type = ProjectionType::Orthographic; |
| 281 | } |
| 282 | else if (value_str == "3d") |
| 283 | { |
| 284 | target.m_projection_type = ProjectionType::Perspective; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, option 'value' is not a valid " |
| 289 | "value, valid values are: 2d, 3d"); |
| 290 | return std::nullopt; |
| 291 | } |
| 292 | return target; |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | ERROR_LOG_FMT(VIDEO, |
| 297 | "Failed to load mod configuration file, option 'type' is not a valid value"); |
| 298 | } |
| 299 | return std::nullopt; |
| 300 | } |
| 301 | |
| 302 | void SerializeTargetToProfile(picojson::object*, const GraphicsTargetConfig&) |
| 303 | { |
| 304 | // Added for consistency, no functionality as of now |
| 305 | } |
| 306 | |
| 307 | void DeserializeTargetFromProfile(const picojson::object&, GraphicsTargetConfig*) |
| 308 | { |
| 309 | // Added for consistency, no functionality as of now |
| 310 | } |