GraphicsModAsset.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2023 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoCommon/GraphicsModSystem/Config/GraphicsModAsset.h"
  4. #include "Common/Logging/Log.h"
  5. #include "Common/StringUtil.h"
  6. void GraphicsModAssetConfig::SerializeToConfig(picojson::object& json_obj) const
  7. {
  8. json_obj.emplace("name", m_asset_id);
  9. picojson::object serialized_data;
  10. for (const auto& [name, path] : m_map)
  11. {
  12. serialized_data.emplace(name, PathToString(path));
  13. }
  14. json_obj.emplace("data", std::move(serialized_data));
  15. }
  16. bool GraphicsModAssetConfig::DeserializeFromConfig(const picojson::object& obj)
  17. {
  18. auto name_iter = obj.find("name");
  19. if (name_iter == obj.end())
  20. {
  21. ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset has no name");
  22. return false;
  23. }
  24. if (!name_iter->second.is<std::string>())
  25. {
  26. ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset has a name "
  27. "that is not a string");
  28. return false;
  29. }
  30. m_asset_id = name_iter->second.to_str();
  31. auto data_iter = obj.find("data");
  32. if (data_iter == obj.end())
  33. {
  34. ERROR_LOG_FMT(VIDEO, "Failed to load mod configuration file, specified asset '{}' has no data",
  35. m_asset_id);
  36. return false;
  37. }
  38. if (!data_iter->second.is<picojson::object>())
  39. {
  40. ERROR_LOG_FMT(VIDEO,
  41. "Failed to load mod configuration file, specified asset '{}' has data "
  42. "that is not an object",
  43. m_asset_id);
  44. return false;
  45. }
  46. for (const auto& [key, value] : data_iter->second.get<picojson::object>())
  47. {
  48. if (!value.is<std::string>())
  49. {
  50. ERROR_LOG_FMT(VIDEO,
  51. "Failed to load mod configuration file, specified asset '{}' has data "
  52. "with a value for key '{}' that is not a string",
  53. m_asset_id, key);
  54. return false;
  55. }
  56. m_map[key] = value.to_str();
  57. }
  58. return true;
  59. }