summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModAsset.cpp
blob: 4b51c1c367cf54d47a4a7c28dfe7d52bfe78c7f8 (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
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

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

#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"

void GraphicsModAssetConfig::SerializeToConfig(picojson::object& json_obj) const
{
  json_obj["name"] = picojson::value{m_asset_id};

  picojson::object serialized_data;
  for (const auto& [name, path] : m_map)
  {
    serialized_data[name] = picojson::value{PathToString(path)};
  }
  json_obj["data"] = picojson::value{serialized_data};
}

bool GraphicsModAssetConfig::DeserializeFromConfig(const picojson::object& obj)
{
  auto name_iter = obj.find("name");
  if (name_iter == obj.end())
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset has no name");
    return false;
  }
  if (!name_iter->second.is<std::string>())
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset has a name "
                         "that is not a string");
    return false;
  }
  m_asset_id = name_iter->second.to_str();

  auto data_iter = obj.find("data");
  if (data_iter == obj.end())
  {
    ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset '{}' has no data",
                  m_asset_id);
    return false;
  }
  if (!data_iter->second.is<picojson::object>())
  {
    ERROR_LOG_FMT(VIDEO,
                  "Failed to load mod configuration file, specified asset '{}' has data "
                  "that is not an object",
                  m_asset_id);
    return false;
  }
  for (const auto& [key, value] : data_iter->second.get<picojson::object>())
  {
    if (!value.is<std::string>())
    {
      ERROR_LOG_FMT(VIDEO,
                    "Failed to load mod configuration file, specified asset '{}' has data "
                    "with a value for key '{}' that is not a string",
                    m_asset_id, key);
      return false;
    }
    m_map[key] = value.to_str();
  }

  return true;
}