From 28a911ae6b3f609003132f67e3398a1b990723d9 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sun, 14 Feb 2021 20:47:00 -0600 Subject: [PATCH 1/3] InputCommon: Move DynamicInputTextureConfiguration to DynamicInputTextures::Configuration --- Source/Core/DolphinLib.props | 4 ++-- Source/Core/InputCommon/CMakeLists.txt | 4 ++-- .../DynamicInputTextureManager.cpp | 2 +- .../InputCommon/DynamicInputTextureManager.h | 7 +++++-- .../DITConfiguration.cpp} | 19 +++++++++---------- .../DITConfiguration.h} | 10 +++++----- 6 files changed, 24 insertions(+), 22 deletions(-) rename Source/Core/InputCommon/{DynamicInputTextureConfiguration.cpp => DynamicInputTextures/DITConfiguration.cpp} (94%) rename Source/Core/InputCommon/{DynamicInputTextureConfiguration.h => DynamicInputTextures/DITConfiguration.h} (85%) diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index 61688eb7b1..e89d64c009 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -475,7 +475,7 @@ - + @@ -1041,7 +1041,7 @@ - + diff --git a/Source/Core/InputCommon/CMakeLists.txt b/Source/Core/InputCommon/CMakeLists.txt index eb6c079f6b..693c6b8c28 100644 --- a/Source/Core/InputCommon/CMakeLists.txt +++ b/Source/Core/InputCommon/CMakeLists.txt @@ -1,6 +1,4 @@ add_library(inputcommon - DynamicInputTextureConfiguration.cpp - DynamicInputTextureConfiguration.h DynamicInputTextureManager.cpp DynamicInputTextureManager.h ImageOperations.cpp @@ -64,6 +62,8 @@ add_library(inputcommon ControlReference/ExpressionParser.h ControlReference/FunctionExpression.cpp ControlReference/FunctionExpression.h + DynamicInputTextures/DITConfiguration.cpp + DynamicInputTextures/DITConfiguration.h ) target_link_libraries(inputcommon diff --git a/Source/Core/InputCommon/DynamicInputTextureManager.cpp b/Source/Core/InputCommon/DynamicInputTextureManager.cpp index 31d0ee2aeb..602edc608d 100644 --- a/Source/Core/InputCommon/DynamicInputTextureManager.cpp +++ b/Source/Core/InputCommon/DynamicInputTextureManager.cpp @@ -12,7 +12,7 @@ #include "Core/ConfigManager.h" #include "Core/Core.h" -#include "InputCommon/DynamicInputTextureConfiguration.h" +#include "InputCommon/DynamicInputTextures/DITConfiguration.h" #include "VideoCommon/HiresTextures.h" #include "VideoCommon/RenderBase.h" diff --git a/Source/Core/InputCommon/DynamicInputTextureManager.h b/Source/Core/InputCommon/DynamicInputTextureManager.h index cd07854928..debd82f83e 100644 --- a/Source/Core/InputCommon/DynamicInputTextureManager.h +++ b/Source/Core/InputCommon/DynamicInputTextureManager.h @@ -11,7 +11,10 @@ namespace InputCommon { -class DynamicInputTextureConfiguration; +namespace DynamicInputTextures +{ +class Configuration; +} class DynamicInputTextureManager { public: @@ -21,7 +24,7 @@ public: void GenerateTextures(const IniFile::Section* sec, const std::string& controller_name); private: - std::vector m_configuration; + std::vector m_configuration; std::string m_config_type; }; } // namespace InputCommon diff --git a/Source/Core/InputCommon/DynamicInputTextureConfiguration.cpp b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp similarity index 94% rename from Source/Core/InputCommon/DynamicInputTextureConfiguration.cpp rename to Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp index d55099e856..4af1796ddc 100644 --- a/Source/Core/InputCommon/DynamicInputTextureConfiguration.cpp +++ b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp @@ -2,7 +2,7 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. -#include "InputCommon/DynamicInputTextureConfiguration.h" +#include "InputCommon/DynamicInputTextures/DITConfiguration.h" #include #include @@ -30,9 +30,9 @@ std::string GetStreamAsString(std::ifstream& stream) } } // namespace -namespace InputCommon +namespace InputCommon::DynamicInputTextures { -DynamicInputTextureConfiguration::DynamicInputTextureConfiguration(const std::string& json_file) +Configuration::Configuration(const std::string& json_file) { std::ifstream json_stream; File::OpenFStream(json_stream, json_file, std::ios_base::in); @@ -247,10 +247,10 @@ DynamicInputTextureConfiguration::DynamicInputTextureConfiguration(const std::st } } -DynamicInputTextureConfiguration::~DynamicInputTextureConfiguration() = default; +Configuration::~Configuration() = default; -bool DynamicInputTextureConfiguration::GenerateTextures(const IniFile::Section* sec, - const std::string& controller_name) const +bool Configuration::GenerateTextures(const IniFile::Section* sec, + const std::string& controller_name) const { bool any_dirty = false; for (const auto& texture_data : m_dynamic_input_textures) @@ -261,9 +261,8 @@ bool DynamicInputTextureConfiguration::GenerateTextures(const IniFile::Section* return any_dirty; } -bool DynamicInputTextureConfiguration::GenerateTexture( - const IniFile::Section* sec, const std::string& controller_name, - const DynamicInputTextureData& texture_data) const +bool Configuration::GenerateTexture(const IniFile::Section* sec, const std::string& controller_name, + const DynamicInputTextureData& texture_data) const { std::string device_name; if (!sec->Get("Device", &device_name)) @@ -381,4 +380,4 @@ bool DynamicInputTextureConfiguration::GenerateTexture( return false; } -} // namespace InputCommon +} // namespace InputCommon::DynamicInputTextures diff --git a/Source/Core/InputCommon/DynamicInputTextureConfiguration.h b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.h similarity index 85% rename from Source/Core/InputCommon/DynamicInputTextureConfiguration.h rename to Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.h index e5a4e722bf..5bf16168fa 100644 --- a/Source/Core/InputCommon/DynamicInputTextureConfiguration.h +++ b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.h @@ -12,13 +12,13 @@ #include "Common/IniFile.h" #include "InputCommon/ImageOperations.h" -namespace InputCommon +namespace InputCommon::DynamicInputTextures { -class DynamicInputTextureConfiguration +class Configuration { public: - explicit DynamicInputTextureConfiguration(const std::string& json_file); - ~DynamicInputTextureConfiguration(); + explicit Configuration(const std::string& json_file); + ~Configuration(); bool GenerateTextures(const IniFile::Section* sec, const std::string& controller_name) const; private: @@ -43,4 +43,4 @@ private: std::string m_base_path; bool m_valid = true; }; -} // namespace InputCommon +} // namespace InputCommon::DynamicInputTextures From 10127a045159fac34f3985db29a5ed715190cb15 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sun, 14 Feb 2021 21:20:47 -0600 Subject: [PATCH 2/3] InputCommon: Move DynamicInputTextureData to DynamicInputTextures::Data --- Source/Core/DolphinLib.props | 1 + Source/Core/InputCommon/CMakeLists.txt | 1 + .../DynamicInputTextures/DITConfiguration.cpp | 4 +-- .../DynamicInputTextures/DITConfiguration.h | 20 ++----------- .../DynamicInputTextures/DITData.h | 28 +++++++++++++++++++ 5 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 Source/Core/InputCommon/DynamicInputTextures/DITData.h diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index e89d64c009..cba5345f77 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -476,6 +476,7 @@ + diff --git a/Source/Core/InputCommon/CMakeLists.txt b/Source/Core/InputCommon/CMakeLists.txt index 693c6b8c28..445fa5bbf3 100644 --- a/Source/Core/InputCommon/CMakeLists.txt +++ b/Source/Core/InputCommon/CMakeLists.txt @@ -64,6 +64,7 @@ add_library(inputcommon ControlReference/FunctionExpression.h DynamicInputTextures/DITConfiguration.cpp DynamicInputTextures/DITConfiguration.h + DynamicInputTextures/DITData.h ) target_link_libraries(inputcommon diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp index 4af1796ddc..8cb5dd931d 100644 --- a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp +++ b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp @@ -93,7 +93,7 @@ Configuration::Configuration(const std::string& json_file) const auto output_textures = output_textures_json.get(); for (auto& [name, data] : output_textures) { - DynamicInputTextureData texture_data; + Data texture_data; texture_data.m_hires_texture_name = name; // Required fields @@ -262,7 +262,7 @@ bool Configuration::GenerateTextures(const IniFile::Section* sec, } bool Configuration::GenerateTexture(const IniFile::Section* sec, const std::string& controller_name, - const DynamicInputTextureData& texture_data) const + const Data& texture_data) const { std::string device_name; if (!sec->Get("Device", &device_name)) diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.h b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.h index 5bf16168fa..49e3a350f9 100644 --- a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.h +++ b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.h @@ -10,7 +10,7 @@ #include "Common/CommonTypes.h" #include "Common/IniFile.h" -#include "InputCommon/ImageOperations.h" +#include "InputCommon/DynamicInputTextures/DITData.h" namespace InputCommon::DynamicInputTextures { @@ -22,24 +22,10 @@ public: bool GenerateTextures(const IniFile::Section* sec, const std::string& controller_name) const; private: - struct DynamicInputTextureData - { - std::string m_image_name; - std::string m_hires_texture_name; - std::string m_generated_folder_name; - - using EmulatedKeyToRegionsMap = std::unordered_map>; - std::unordered_map m_emulated_controllers; - - using HostKeyToImagePath = std::unordered_map; - std::unordered_map m_host_devices; - bool m_preserve_aspect_ratio = true; - }; - bool GenerateTexture(const IniFile::Section* sec, const std::string& controller_name, - const DynamicInputTextureData& texture_data) const; + const Data& texture_data) const; - std::vector m_dynamic_input_textures; + std::vector m_dynamic_input_textures; std::string m_base_path; bool m_valid = true; }; diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITData.h b/Source/Core/InputCommon/DynamicInputTextures/DITData.h new file mode 100644 index 0000000000..b0c14a9bfb --- /dev/null +++ b/Source/Core/InputCommon/DynamicInputTextures/DITData.h @@ -0,0 +1,28 @@ +// Copyright 2021 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include + +#include "InputCommon/ImageOperations.h" + +namespace InputCommon::DynamicInputTextures +{ +struct Data +{ + std::string m_image_name; + std::string m_hires_texture_name; + std::string m_generated_folder_name; + + using EmulatedKeyToRegionsMap = std::unordered_map>; + std::unordered_map m_emulated_controllers; + + using HostKeyToImagePath = std::unordered_map; + std::unordered_map m_host_devices; + bool m_preserve_aspect_ratio = true; +}; +} // namespace InputCommon::DynamicInputTextures From 55ba1c7c9ca8569125339930d2f91531e7966a26 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sun, 14 Feb 2021 23:14:02 -0600 Subject: [PATCH 3/3] InputCommon: Move initial dynamic input texture configuration logic to a 'specification 1' function and load in a 'specification' attribute that defaults to 1 if not present (with 1 being the only valid value at the moment) --- Source/Core/DolphinLib.props | 2 + Source/Core/InputCommon/CMakeLists.txt | 2 + .../DynamicInputTextures/DITConfiguration.cpp | 218 +++--------------- .../DynamicInputTextures/DITSpecification.cpp | 201 ++++++++++++++++ .../DynamicInputTextures/DITSpecification.h | 18 ++ 5 files changed, 251 insertions(+), 190 deletions(-) create mode 100644 Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp create mode 100644 Source/Core/InputCommon/DynamicInputTextures/DITSpecification.h diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index cba5345f77..aaef97eaab 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -477,6 +477,7 @@ + @@ -1043,6 +1044,7 @@ + diff --git a/Source/Core/InputCommon/CMakeLists.txt b/Source/Core/InputCommon/CMakeLists.txt index 445fa5bbf3..576e87e711 100644 --- a/Source/Core/InputCommon/CMakeLists.txt +++ b/Source/Core/InputCommon/CMakeLists.txt @@ -65,6 +65,8 @@ add_library(inputcommon DynamicInputTextures/DITConfiguration.cpp DynamicInputTextures/DITConfiguration.h DynamicInputTextures/DITData.h + DynamicInputTextures/DITSpecification.cpp + DynamicInputTextures/DITSpecification.h ) target_link_libraries(inputcommon diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp index 8cb5dd931d..b057abff2b 100644 --- a/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp +++ b/Source/Core/InputCommon/DynamicInputTextures/DITConfiguration.cpp @@ -8,16 +8,15 @@ #include #include -#include #include #include "Common/CommonPaths.h" #include "Common/FileUtil.h" -#include "Common/IOFile.h" #include "Common/Logging/Log.h" #include "Common/StringUtil.h" #include "Core/ConfigManager.h" #include "InputCommon/ControllerEmu/ControllerEmu.h" +#include "InputCommon/DynamicInputTextures/DITSpecification.h" #include "InputCommon/ImageOperations.h" namespace @@ -43,8 +42,8 @@ Configuration::Configuration(const std::string& json_file) return; } - picojson::value out; - const auto error = picojson::parse(out, GetStreamAsString(json_stream)); + picojson::value root; + const auto error = picojson::parse(root, GetStreamAsString(json_stream)); if (!error.empty()) { @@ -54,197 +53,36 @@ Configuration::Configuration(const std::string& json_file) return; } - const picojson::value& output_textures_json = out.get("output_textures"); - if (!output_textures_json.is()) + SplitPath(json_file, &m_base_path, nullptr, nullptr); + + const picojson::value& specification_json = root.get("specification"); + u8 specification = 1; + if (specification_json.is()) { - ERROR_LOG_FMT( - VIDEO, - "Failed to load dynamic input json file '{}' because 'output_textures' is missing or " - "was not of type object", - json_file); + const double spec_from_json = specification_json.get(); + if (spec_from_json < static_cast(std::numeric_limits::min()) || + spec_from_json > static_cast(std::numeric_limits::max())) + { + ERROR_LOG_FMT( + VIDEO, + "Failed to load dynamic input json file '{}', specification '{}' is not within bounds", + json_file, spec_from_json); + m_valid = false; + return; + } + specification = static_cast(spec_from_json); + } + + if (specification != 1) + { + ERROR_LOG_FMT(VIDEO, + "Failed to load dynamic input json file '{}', specification '{}' is invalid", + json_file, specification); m_valid = false; return; } - const picojson::value& preserve_aspect_ratio_json = out.get("preserve_aspect_ratio"); - - bool preserve_aspect_ratio = true; - if (preserve_aspect_ratio_json.is()) - { - preserve_aspect_ratio = preserve_aspect_ratio_json.get(); - } - - const picojson::value& generated_folder_name_json = out.get("generated_folder_name"); - - const std::string& game_id = SConfig::GetInstance().GetGameID(); - std::string generated_folder_name = fmt::format("{}_Generated", game_id); - if (generated_folder_name_json.is()) - { - generated_folder_name = generated_folder_name_json.get(); - } - - const picojson::value& default_host_controls_json = out.get("default_host_controls"); - picojson::object default_host_controls; - if (default_host_controls_json.is()) - { - default_host_controls = default_host_controls_json.get(); - } - - const auto output_textures = output_textures_json.get(); - for (auto& [name, data] : output_textures) - { - Data texture_data; - texture_data.m_hires_texture_name = name; - - // Required fields - const picojson::value& image = data.get("image"); - const picojson::value& emulated_controls = data.get("emulated_controls"); - - if (!image.is() || !emulated_controls.is()) - { - ERROR_LOG_FMT(VIDEO, - "Failed to load dynamic input json file '{}' because required fields " - "'image', or 'emulated_controls' are either " - "missing or the incorrect type", - json_file); - m_valid = false; - return; - } - - texture_data.m_image_name = image.to_str(); - texture_data.m_preserve_aspect_ratio = preserve_aspect_ratio; - texture_data.m_generated_folder_name = generated_folder_name; - - SplitPath(json_file, &m_base_path, nullptr, nullptr); - - const std::string image_full_path = m_base_path + texture_data.m_image_name; - if (!File::Exists(image_full_path)) - { - ERROR_LOG_FMT(VIDEO, - "Failed to load dynamic input json file '{}' because the image '{}' " - "could not be loaded", - json_file, image_full_path); - m_valid = false; - return; - } - - const auto& emulated_controls_json = emulated_controls.get(); - for (auto& [emulated_controller_name, map] : emulated_controls_json) - { - if (!map.is()) - { - ERROR_LOG_FMT(VIDEO, - "Failed to load dynamic input json file '{}' because 'emulated_controls' " - "map key '{}' is incorrect type. Expected map ", - json_file, emulated_controller_name); - m_valid = false; - return; - } - - auto& key_to_regions = texture_data.m_emulated_controllers[emulated_controller_name]; - for (auto& [emulated_control, regions_array] : map.get()) - { - if (!regions_array.is()) - { - ERROR_LOG_FMT( - VIDEO, - "Failed to load dynamic input json file '{}' because emulated controller '{}' " - "key '{}' has incorrect value type. Expected array ", - json_file, emulated_controller_name, emulated_control); - m_valid = false; - return; - } - - std::vector region_rects; - for (auto& region : regions_array.get()) - { - Rect r; - if (!region.is()) - { - ERROR_LOG_FMT( - VIDEO, - "Failed to load dynamic input json file '{}' because emulated controller '{}' " - "key '{}' has a region with the incorrect type. Expected array ", - json_file, emulated_controller_name, emulated_control); - m_valid = false; - return; - } - - auto region_offsets = region.get(); - - if (region_offsets.size() != 4) - { - ERROR_LOG_FMT( - VIDEO, - "Failed to load dynamic input json file '{}' because emulated controller '{}' " - "key '{}' has a region that does not have 4 offsets (left, top, right, " - "bottom).", - json_file, emulated_controller_name, emulated_control); - m_valid = false; - return; - } - - if (!std::all_of(region_offsets.begin(), region_offsets.end(), - [](picojson::value val) { return val.is(); })) - { - ERROR_LOG_FMT( - VIDEO, - "Failed to load dynamic input json file '{}' because emulated controller '{}' " - "key '{}' has a region that has the incorrect offset type.", - json_file, emulated_controller_name, emulated_control); - m_valid = false; - return; - } - - r.left = static_cast(region_offsets[0].get()); - r.top = static_cast(region_offsets[1].get()); - r.right = static_cast(region_offsets[2].get()); - r.bottom = static_cast(region_offsets[3].get()); - region_rects.push_back(r); - } - key_to_regions.insert_or_assign(emulated_control, std::move(region_rects)); - } - } - - // Default to the default controls but overwrite if the creator - // has provided something specific - picojson::object host_controls = default_host_controls; - const picojson::value& host_controls_json = data.get("host_controls"); - if (host_controls_json.is()) - { - host_controls = host_controls_json.get(); - } - - if (host_controls.empty()) - { - ERROR_LOG_FMT(VIDEO, - "Failed to load dynamic input json file '{}' because field " - "'host_controls' is missing ", - json_file); - m_valid = false; - return; - } - - for (auto& [host_device, map] : host_controls) - { - if (!map.is()) - { - ERROR_LOG_FMT(VIDEO, - "Failed to load dynamic input json file '{}' because 'host_controls' " - "map key '{}' is incorrect type ", - json_file, host_device); - m_valid = false; - return; - } - auto& host_control_to_imagename = texture_data.m_host_devices[host_device]; - for (auto& [host_control, image_name] : map.get()) - { - host_control_to_imagename.insert_or_assign(host_control, image_name.to_str()); - } - } - - m_dynamic_input_textures.emplace_back(std::move(texture_data)); - } + m_valid = ProcessSpecificationV1(root, m_dynamic_input_textures, m_base_path, json_file); } Configuration::~Configuration() = default; diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp b/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp new file mode 100644 index 0000000000..f176115f0e --- /dev/null +++ b/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.cpp @@ -0,0 +1,201 @@ +// Copyright 2021 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "InputCommon/DynamicInputTextures/DITSpecification.h" + +#include + +#include "Common/FileUtil.h" +#include "Common/IOFile.h" +#include "Common/Logging/Log.h" +#include "Core/ConfigManager.h" + +namespace InputCommon::DynamicInputTextures +{ +bool ProcessSpecificationV1(picojson::value& root, std::vector& input_textures, + const std::string& base_path, const std::string& json_file) +{ + const picojson::value& output_textures_json = root.get("output_textures"); + if (!output_textures_json.is()) + { + ERROR_LOG_FMT( + VIDEO, + "Failed to load dynamic input json file '{}' because 'output_textures' is missing or " + "was not of type object", + json_file); + return false; + } + + const picojson::value& preserve_aspect_ratio_json = root.get("preserve_aspect_ratio"); + + bool preserve_aspect_ratio = true; + if (preserve_aspect_ratio_json.is()) + { + preserve_aspect_ratio = preserve_aspect_ratio_json.get(); + } + + const picojson::value& generated_folder_name_json = root.get("generated_folder_name"); + + const std::string& game_id = SConfig::GetInstance().GetGameID(); + std::string generated_folder_name = fmt::format("{}_Generated", game_id); + if (generated_folder_name_json.is()) + { + generated_folder_name = generated_folder_name_json.get(); + } + + const picojson::value& default_host_controls_json = root.get("default_host_controls"); + picojson::object default_host_controls; + if (default_host_controls_json.is()) + { + default_host_controls = default_host_controls_json.get(); + } + + const auto output_textures = output_textures_json.get(); + for (auto& [name, data] : output_textures) + { + Data texture_data; + texture_data.m_hires_texture_name = name; + + // Required fields + const picojson::value& image = data.get("image"); + const picojson::value& emulated_controls = data.get("emulated_controls"); + + if (!image.is() || !emulated_controls.is()) + { + ERROR_LOG_FMT(VIDEO, + "Failed to load dynamic input json file '{}' because required fields " + "'image', or 'emulated_controls' are either " + "missing or the incorrect type", + json_file); + return false; + } + + texture_data.m_image_name = image.to_str(); + texture_data.m_preserve_aspect_ratio = preserve_aspect_ratio; + texture_data.m_generated_folder_name = generated_folder_name; + + const std::string image_full_path = base_path + texture_data.m_image_name; + if (!File::Exists(image_full_path)) + { + ERROR_LOG_FMT(VIDEO, + "Failed to load dynamic input json file '{}' because the image '{}' " + "could not be loaded", + json_file, image_full_path); + return false; + } + + const auto& emulated_controls_json = emulated_controls.get(); + for (auto& [emulated_controller_name, map] : emulated_controls_json) + { + if (!map.is()) + { + ERROR_LOG_FMT(VIDEO, + "Failed to load dynamic input json file '{}' because 'emulated_controls' " + "map key '{}' is incorrect type. Expected map ", + json_file, emulated_controller_name); + return false; + } + + auto& key_to_regions = texture_data.m_emulated_controllers[emulated_controller_name]; + for (auto& [emulated_control, regions_array] : map.get()) + { + if (!regions_array.is()) + { + ERROR_LOG_FMT( + VIDEO, + "Failed to load dynamic input json file '{}' because emulated controller '{}' " + "key '{}' has incorrect value type. Expected array ", + json_file, emulated_controller_name, emulated_control); + return false; + } + + std::vector region_rects; + for (auto& region : regions_array.get()) + { + Rect r; + if (!region.is()) + { + ERROR_LOG_FMT( + VIDEO, + "Failed to load dynamic input json file '{}' because emulated controller '{}' " + "key '{}' has a region with the incorrect type. Expected array ", + json_file, emulated_controller_name, emulated_control); + return false; + } + + auto region_offsets = region.get(); + + if (region_offsets.size() != 4) + { + ERROR_LOG_FMT( + VIDEO, + "Failed to load dynamic input json file '{}' because emulated controller '{}' " + "key '{}' has a region that does not have 4 offsets (left, top, right, " + "bottom).", + json_file, emulated_controller_name, emulated_control); + return false; + } + + if (!std::all_of(region_offsets.begin(), region_offsets.end(), + [](picojson::value val) { return val.is(); })) + { + ERROR_LOG_FMT( + VIDEO, + "Failed to load dynamic input json file '{}' because emulated controller '{}' " + "key '{}' has a region that has the incorrect offset type.", + json_file, emulated_controller_name, emulated_control); + return false; + } + + r.left = static_cast(region_offsets[0].get()); + r.top = static_cast(region_offsets[1].get()); + r.right = static_cast(region_offsets[2].get()); + r.bottom = static_cast(region_offsets[3].get()); + region_rects.push_back(r); + } + key_to_regions.insert_or_assign(emulated_control, std::move(region_rects)); + } + } + + // Default to the default controls but overwrite if the creator + // has provided something specific + picojson::object host_controls = default_host_controls; + const picojson::value& host_controls_json = data.get("host_controls"); + if (host_controls_json.is()) + { + host_controls = host_controls_json.get(); + } + + if (host_controls.empty()) + { + ERROR_LOG_FMT(VIDEO, + "Failed to load dynamic input json file '{}' because field " + "'host_controls' is missing ", + json_file); + return false; + } + + for (auto& [host_device, map] : host_controls) + { + if (!map.is()) + { + ERROR_LOG_FMT(VIDEO, + "Failed to load dynamic input json file '{}' because 'host_controls' " + "map key '{}' is incorrect type ", + json_file, host_device); + return false; + } + auto& host_control_to_imagename = texture_data.m_host_devices[host_device]; + for (auto& [host_control, image_name] : map.get()) + { + host_control_to_imagename.insert_or_assign(host_control, image_name.to_str()); + } + } + + input_textures.emplace_back(std::move(texture_data)); + } + + return true; +} +} // namespace InputCommon::DynamicInputTextures diff --git a/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.h b/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.h new file mode 100644 index 0000000000..2a04ab81d7 --- /dev/null +++ b/Source/Core/InputCommon/DynamicInputTextures/DITSpecification.h @@ -0,0 +1,18 @@ +// Copyright 2021 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include + +#include "InputCommon/DynamicInputTextures/DITData.h" + +namespace InputCommon::DynamicInputTextures +{ +bool ProcessSpecificationV1(picojson::value& root, std::vector& input_textures, + const std::string& base_path, const std::string& json_file); +}