| 1 | // Copyright 2022 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.h" |
| 5 | |
| 6 | #include <fstream> |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
| 10 | #include <picojson.h> |
| 11 | |
| 12 | #include "Common/CommonPaths.h" |
| 13 | #include "Common/FileSearch.h" |
| 14 | #include "Common/FileUtil.h" |
| 15 | #include "Common/JsonUtil.h" |
| 16 | #include "Common/Logging/Log.h" |
| 17 | #include "Common/StringUtil.h" |
| 18 | |
| 19 | #include "VideoCommon/GraphicsModSystem/Config/GraphicsMod.h" |
| 20 | #include "VideoCommon/GraphicsModSystem/Constants.h" |
| 21 | #include "VideoCommon/HiresTextures.h" |
| 22 | |
| 23 | GraphicsModGroupConfig::GraphicsModGroupConfig(std::string game_id) : m_game_id(std::move(game_id)) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | GraphicsModGroupConfig::~GraphicsModGroupConfig() = default; |
| 28 | |
| 29 | GraphicsModGroupConfig::GraphicsModGroupConfig(const GraphicsModGroupConfig&) = default; |
| 30 | |
| 31 | GraphicsModGroupConfig::GraphicsModGroupConfig(GraphicsModGroupConfig&&) noexcept = default; |
| 32 | |
| 33 | GraphicsModGroupConfig& GraphicsModGroupConfig::operator=(const GraphicsModGroupConfig&) = default; |
| 34 | |
| 35 | GraphicsModGroupConfig& |
| 36 | GraphicsModGroupConfig::operator=(GraphicsModGroupConfig&&) noexcept = default; |
| 37 | |
| 38 | void GraphicsModGroupConfig::Load() |
| 39 | { |
| 40 | const std::string file_path = GetPath(); |
| 41 | |
| 42 | std::set<std::string> known_paths; |
| 43 | if (File::Exists(file_path)) |
| 44 | { |
| 45 | picojson::value root; |
| 46 | std::string error; |
| 47 | if (!JsonFromFile(file_path, &root, &error)) |
| 48 | { |
| 49 | ERROR_LOG_FMT(VIDEO, |
| 50 | "Failed to load graphics mod group json file '{}' due to parse error: {}", |
| 51 | file_path, error); |
| 52 | return; |
| 53 | } |
| 54 | if (!root.is<picojson::object>()) |
| 55 | { |
| 56 | ERROR_LOG_FMT( |
| 57 | VIDEO, |
| 58 | "Failed to load graphics mod group json file '{}' due to root not being an object!", |
| 59 | file_path); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const auto& mods = root.get("mods"); |
| 64 | if (mods.is<picojson::array>()) |
| 65 | { |
| 66 | for (const auto& mod_json : mods.get<picojson::array>()) |
| 67 | { |
| 68 | if (mod_json.is<picojson::object>()) |
| 69 | { |
| 70 | const auto& mod_json_obj = mod_json.get<picojson::object>(); |
| 71 | auto graphics_mod = GraphicsModConfig::Create(&mod_json_obj); |
| 72 | if (!graphics_mod) |
| 73 | { |
| 74 | continue; |
| 75 | } |
| 76 | graphics_mod->DeserializeFromProfile(mod_json_obj); |
| 77 | |
| 78 | auto mod_full_path = graphics_mod->GetAbsolutePath(); |
| 79 | known_paths.insert(std::move(mod_full_path)); |
| 80 | m_graphics_mods.push_back(std::move(*graphics_mod)); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const auto try_add_mod = [&known_paths, this](const std::string& dir, |
| 87 | GraphicsModConfig::Source source) { |
| 88 | const auto files = Common::DoFileSearch(dir, ".json", true); |
| 89 | for (const auto& file : files) |
| 90 | { |
| 91 | std::string basename; |
| 92 | SplitPath(file, nullptr, &basename, nullptr); |
| 93 | if (basename == "metadata") |
| 94 | { |
| 95 | auto file_copy = file; |
| 96 | UnifyPathSeparators(file_copy); |
| 97 | if (known_paths.contains(file_copy)) |
| 98 | return; |
| 99 | |
| 100 | if (auto mod = GraphicsModConfig::Create(file_copy, source)) |
| 101 | m_graphics_mods.push_back(std::move(*mod)); |
| 102 | } |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | const std::set<std::string> graphics_mod_user_directories = |
| 107 | GetTextureDirectoriesWithGameId(File::GetUserPath(D_GRAPHICSMOD_IDX), m_game_id); |
| 108 | |
| 109 | for (const auto& graphics_mod_directory : graphics_mod_user_directories) |
| 110 | { |
| 111 | try_add_mod(graphics_mod_directory, GraphicsModConfig::Source::User); |
| 112 | } |
| 113 | |
| 114 | const std::set<std::string> graphics_mod_system_directories = GetTextureDirectoriesWithGameId( |
| 115 | File::GetSysDirectory() + DOLPHIN_SYSTEM_GRAPHICS_MOD_DIR, m_game_id); |
| 116 | |
| 117 | for (const auto& graphics_mod_directory : graphics_mod_system_directories) |
| 118 | { |
| 119 | try_add_mod(graphics_mod_directory, GraphicsModConfig::Source::System); |
| 120 | } |
| 121 | |
| 122 | std::ranges::sort(m_graphics_mods, {}, &GraphicsModConfig::m_weight); |
| 123 | for (auto& mod : m_graphics_mods) |
| 124 | { |
| 125 | m_path_to_graphics_mod[mod.GetAbsolutePath()] = &mod; |
| 126 | } |
| 127 | |
| 128 | m_change_count++; |
| 129 | } |
| 130 | |
| 131 | void GraphicsModGroupConfig::Save() const |
| 132 | { |
| 133 | const std::string file_path = GetPath(); |
| 134 | std::ofstream json_stream; |
| 135 | File::OpenFStream(json_stream, file_path, std::ios_base::out); |
| 136 | if (!json_stream.is_open()) |
| 137 | { |
| 138 | ERROR_LOG_FMT(VIDEO, "Failed to open graphics mod group json file '{}' for writing", file_path); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | picojson::object serialized_root; |
| 143 | picojson::array serialized_mods; |
| 144 | for (const auto& mod : m_graphics_mods) |
| 145 | { |
| 146 | picojson::object serialized_mod; |
| 147 | mod.SerializeToProfile(&serialized_mod); |
| 148 | serialized_mods.emplace_back(std::move(serialized_mod)); |
| 149 | } |
| 150 | serialized_root.emplace("mods", std::move(serialized_mods)); |
| 151 | |
| 152 | const auto output = picojson::value{serialized_root}.serialize(true); |
| 153 | json_stream << output; |
| 154 | } |
| 155 | |
| 156 | void GraphicsModGroupConfig::SetChangeCount(u32 change_count) |
| 157 | { |
| 158 | m_change_count = change_count; |
| 159 | } |
| 160 | |
| 161 | u32 GraphicsModGroupConfig::GetChangeCount() const |
| 162 | { |
| 163 | return m_change_count; |
| 164 | } |
| 165 | |
| 166 | const std::vector<GraphicsModConfig>& GraphicsModGroupConfig::GetMods() const |
| 167 | { |
| 168 | return m_graphics_mods; |
| 169 | } |
| 170 | |
| 171 | std::vector<GraphicsModConfig>& GraphicsModGroupConfig::GetMods() |
| 172 | { |
| 173 | return m_graphics_mods; |
| 174 | } |
| 175 | |
| 176 | GraphicsModConfig* GraphicsModGroupConfig::GetMod(std::string_view absolute_path) const |
| 177 | { |
| 178 | if (const auto iter = m_path_to_graphics_mod.find(absolute_path); |
| 179 | iter != m_path_to_graphics_mod.end()) |
| 180 | { |
| 181 | return iter->second; |
| 182 | } |
| 183 | |
| 184 | return nullptr; |
| 185 | } |
| 186 | |
| 187 | const std::string& GraphicsModGroupConfig::GetGameID() const |
| 188 | { |
| 189 | return m_game_id; |
| 190 | } |
| 191 | |
| 192 | std::string GraphicsModGroupConfig::GetPath() const |
| 193 | { |
| 194 | const std::string game_mod_root = File::GetUserPath(D_CONFIG_IDX) + GRAPHICSMOD_CONFIG_DIR; |
| 195 | return fmt::format("{}/{}.json", game_mod_root, m_game_id); |
| 196 | } |