summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsTarget.cpp
blob: b9f5ef8f2907138215333afabd5bbd0d59a98d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2022 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "VideoCommon/GraphicsModSystem/Config/GraphicsTarget.h"

#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "Common/VariantUtil.h"
#include "VideoCommon/TextureCacheBase.h"

namespace
{
template <typename T, std::enable_if_t<std::is_base_of_v<FBTarget, T>, int> = 0>
std::optional<T> DeserializeFBTargetFromConfig(const picojson::object& obj, std::string_view prefix)
{
  T fb;
  const auto texture_filename_iter = obj.find("texture_filename");
  if (texture_filename_iter == obj.end())
  {
    ERROR_LOG_FMT(VIDEO,
                  "Failed to load mod configuration file, option 'texture_filename' not found");
    return std::nullopt;
  }
  if (!texture_filename_iter->second.is<std::string>())
  {
    ERROR_LOG_FMT(
        VIDEO,
        "Failed to load mod configuration file, option 'texture_filename' is not a string type");
    return std::nullopt;
  }
  const auto texture_filename = texture_filename_iter->second.get<std::string>();
  const auto texture_filename_without_prefix = texture_filename.substr(prefix.size() + 1);
  const auto split_str_values = SplitString(texture_filename_without_prefix, '_');
  if (split_str_values.size() == 1)
  {
    ERROR_LOG_FMT(
        VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is not valid");
    return std::nullopt;
  }
  const auto split_width_height_values = SplitString(texture_filename_without_prefix, 'x');
  if (split_width_height_values.size() != 2)
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is "
                         "not valid, width and height separator found more matches than expected");
    return std::nullopt;
  }

  const std::size_t width_underscore_pos = split_width_height_values[0].find_last_of('_');
  std::string width_str;
  if (width_underscore_pos == std::string::npos)
  {
    width_str = split_width_height_values[0];
  }
  else
  {
    width_str = split_width_height_values[0].substr(width_underscore_pos + 1);
  }
  if (!TryParse(width_str, &fb.m_width))
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is "
                         "not valid, width not a number");
    return std::nullopt;
  }

  const std::size_t height_underscore_pos = split_width_height_values[1].find_first_of('_');
  if (height_underscore_pos == std::string::npos ||
      height_underscore_pos == split_width_height_values[1].size() - 1)
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is "
                         "not valid, underscore after height is missing or incomplete");
    return std::nullopt;
  }
  const std::string height_str = split_width_height_values[1].substr(0, height_underscore_pos);
  if (!TryParse(height_str, &fb.m_height))
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is "
                         "not valid, height not a number");
    return std::nullopt;
  }

  const std::size_t format_underscore_pos =
      split_width_height_values[1].find_first_of('_', height_underscore_pos + 1);

  std::string format_str;
  if (format_underscore_pos == std::string::npos)
  {
    format_str = split_width_height_values[1].substr(height_underscore_pos + 1);
  }
  else
  {
    format_str = split_width_height_values[1].substr(
        height_underscore_pos + 1, (format_underscore_pos - height_underscore_pos) - 1);
  }
  u32 format;
  if (!TryParse(format_str, &format))
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is "
                         "not valid, texture format is not a number");
    return std::nullopt;
  }
  if (!IsValidTextureFormat(static_cast<TextureFormat>(format)))
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, value in 'texture_filename' is "
                         "not valid, texture format is not valid");
    return std::nullopt;
  }
  fb.m_texture_format = static_cast<TextureFormat>(format);

  return fb;
}
std::optional<std::string> ExtractTextureFilenameForConfig(const picojson::object& obj)
{
  const auto texture_filename_iter = obj.find("texture_filename");
  if (texture_filename_iter == obj.end())
  {
    ERROR_LOG_FMT(VIDEO,
                  "Failed to load mod configuration file, option 'texture_filename' not found");
    return std::nullopt;
  }
  if (!texture_filename_iter->second.is<std::string>())
  {
    ERROR_LOG_FMT(
        VIDEO,
        "Failed to load mod configuration file, option 'texture_filename' is not a string type");
    return std::nullopt;
  }
  std::string texture_info = texture_filename_iter->second.get<std::string>();

  const auto handle_fb_texture =
      [&texture_info](std::string_view type) -> std::optional<std::string> {
    const auto letter_n_pos = texture_info.find("_n");
    if (letter_n_pos == std::string::npos)
    {
      ERROR_LOG_FMT(VIDEO,
                    "Failed to load mod configuration file, value in 'texture_filename' "
                    "is {} without a count",
                    type);
      return std::nullopt;
    }

    const auto post_underscore = texture_info.find_first_of('_', letter_n_pos + 2);
    if (post_underscore == std::string::npos)
      return texture_info.erase(letter_n_pos, texture_info.size() - letter_n_pos);
    else
      return texture_info.erase(letter_n_pos, post_underscore - letter_n_pos);
  };

  if (texture_info.starts_with(EFB_DUMP_PREFIX))
    return handle_fb_texture("an efb");
  else if (texture_info.starts_with(XFB_DUMP_PREFIX))
    return handle_fb_texture("a xfb");
  return texture_info;
}
}  // namespace

void SerializeTargetToConfig(picojson::object& json_obj, const GraphicsTargetConfig& target)
{
  std::visit(overloaded{
                 [&](const DrawStartedTextureTarget& the_target) {
                   json_obj.emplace("type", "draw_started");
                   json_obj.emplace("texture_filename", the_target.m_texture_info_string);
                 },
                 [&](const LoadTextureTarget& the_target) {
                   json_obj.emplace("type", "load_texture");
                   json_obj.emplace("texture_filename", the_target.m_texture_info_string);
                 },
                 [&](const CreateTextureTarget& the_target) {
                   json_obj.emplace("type", "create_texture");
                   json_obj.emplace("texture_filename", the_target.m_texture_info_string);
                 },
                 [&](const EFBTarget& the_target) {
                   json_obj.emplace("type", "efb");
                   json_obj.emplace("texture_filename",
                                    fmt::format("{}_{}x{}_{}", EFB_DUMP_PREFIX, the_target.m_width,
                                                the_target.m_height,
                                                static_cast<int>(the_target.m_texture_format)));
                 },
                 [&](const XFBTarget& the_target) {
                   json_obj.emplace("type", "xfb");
                   json_obj.emplace("texture_filename",
                                    fmt::format("{}_{}x{}_{}", XFB_DUMP_PREFIX, the_target.m_width,
                                                the_target.m_height,
                                                static_cast<int>(the_target.m_texture_format)));
                 },
                 [&](const ProjectionTarget& the_target) {
                   const char* type_name = "3d";
                   if (the_target.m_projection_type == ProjectionType::Orthographic)
                     type_name = "2d";

                   json_obj.emplace("type", type_name);

                   if (the_target.m_texture_info_string)
                   {
                     json_obj.emplace("texture_filename", *the_target.m_texture_info_string);
                   }
                 },
             },
             target);
}

std::optional<GraphicsTargetConfig> DeserializeTargetFromConfig(const picojson::object& obj)
{
  const auto type_iter = obj.find("type");
  if (type_iter == obj.end())
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, option 'type' not found");
    return std::nullopt;
  }
  if (!type_iter->second.is<std::string>())
  {
    ERROR_LOG_FMT(VIDEO,
                  "Failed to load mod configuration file, option 'type' is not a string type");
    return std::nullopt;
  }
  const std::string& type = type_iter->second.get<std::string>();
  if (type == "draw_started")
  {
    std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj);
    if (!texture_info.has_value())
      return std::nullopt;

    DrawStartedTextureTarget target;
    target.m_texture_info_string = texture_info.value();
    return target;
  }
  else if (type == "load_texture")
  {
    std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj);
    if (!texture_info.has_value())
      return std::nullopt;

    LoadTextureTarget target;
    target.m_texture_info_string = texture_info.value();
    return target;
  }
  else if (type == "create_texture")
  {
    std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj);
    if (!texture_info.has_value())
      return std::nullopt;

    CreateTextureTarget target;
    target.m_texture_info_string = texture_info.value();
    return target;
  }
  else if (type == "efb")
  {
    return DeserializeFBTargetFromConfig<EFBTarget>(obj, EFB_DUMP_PREFIX);
  }
  else if (type == "xfb")
  {
    return DeserializeFBTargetFromConfig<XFBTarget>(obj, EFB_DUMP_PREFIX);
  }
  else if (type == "projection")
  {
    ProjectionTarget target;
    const auto texture_iter = obj.find("texture_filename");
    if (texture_iter != obj.end())
    {
      std::optional<std::string> texture_info = ExtractTextureFilenameForConfig(obj);
      if (!texture_info.has_value())
        return std::nullopt;
      target.m_texture_info_string = texture_info;
    }
    const auto value_iter = obj.find("value");
    if (value_iter == obj.end())
    {
      ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, option 'value' not found");
      return std::nullopt;
    }
    if (!value_iter->second.is<std::string>())
    {
      ERROR_LOG_FMT(VIDEO,
                    "Failed to load mod configuration file, option 'value' is not a string type");
      return std::nullopt;
    }
    const auto& value_str = value_iter->second.get<std::string>();
    if (value_str == "2d")
    {
      target.m_projection_type = ProjectionType::Orthographic;
    }
    else if (value_str == "3d")
    {
      target.m_projection_type = ProjectionType::Perspective;
    }
    else
    {
      ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, option 'value' is not a valid "
                           "value, valid values are: 2d, 3d");
      return std::nullopt;
    }
    return target;
  }
  else
  {
    ERROR_LOG_FMT(VIDEO,
                  "Failed to load mod configuration file, option 'type' is not a valid value");
  }
  return std::nullopt;
}

void SerializeTargetToProfile(picojson::object*, const GraphicsTargetConfig&)
{
  // Added for consistency, no functionality as of now
}

void DeserializeTargetFromProfile(const picojson::object&, GraphicsTargetConfig*)
{
  // Added for consistency, no functionality as of now
}