2019-08-17 19:40:58 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-02-15 02:47:00 +00:00
|
|
|
#include "InputCommon/DynamicInputTextures/DITConfiguration.h"
|
2019-08-17 19:40:58 +00:00
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <picojson.h>
|
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
2021-02-15 05:14:02 +00:00
|
|
|
#include "InputCommon/DynamicInputTextures/DITSpecification.h"
|
2019-08-17 19:40:58 +00:00
|
|
|
#include "InputCommon/ImageOperations.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::string GetStreamAsString(std::ifstream& stream)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << stream.rdbuf();
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2021-02-15 02:47:00 +00:00
|
|
|
namespace InputCommon::DynamicInputTextures
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2021-02-15 02:47:00 +00:00
|
|
|
Configuration::Configuration(const std::string& json_file)
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
|
|
|
std::ifstream json_stream;
|
|
|
|
File::OpenFStream(json_stream, json_file, std::ios_base::in);
|
|
|
|
if (!json_stream.is_open())
|
|
|
|
{
|
2020-10-22 21:49:29 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Failed to load dynamic input json file '{}'", json_file);
|
2019-08-17 19:40:58 +00:00
|
|
|
m_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:14:02 +00:00
|
|
|
picojson::value root;
|
|
|
|
const auto error = picojson::parse(root, GetStreamAsString(json_stream));
|
2019-08-17 19:40:58 +00:00
|
|
|
|
|
|
|
if (!error.empty())
|
|
|
|
{
|
2020-10-22 21:49:29 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Failed to load dynamic input json file '{}' due to parse error: {}",
|
|
|
|
json_file, error);
|
2019-08-17 19:40:58 +00:00
|
|
|
m_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:14:02 +00:00
|
|
|
SplitPath(json_file, &m_base_path, nullptr, nullptr);
|
2019-08-17 19:40:58 +00:00
|
|
|
|
2021-02-15 05:14:02 +00:00
|
|
|
const picojson::value& specification_json = root.get("specification");
|
|
|
|
u8 specification = 1;
|
|
|
|
if (specification_json.is<double>())
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2021-02-15 05:14:02 +00:00
|
|
|
const double spec_from_json = specification_json.get<double>();
|
|
|
|
if (spec_from_json < static_cast<double>(std::numeric_limits<u8>::min()) ||
|
|
|
|
spec_from_json > static_cast<double>(std::numeric_limits<u8>::max()))
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2021-02-15 05:14:02 +00:00
|
|
|
ERROR_LOG_FMT(
|
|
|
|
VIDEO,
|
|
|
|
"Failed to load dynamic input json file '{}', specification '{}' is not within bounds",
|
|
|
|
json_file, spec_from_json);
|
2019-08-17 19:40:58 +00:00
|
|
|
m_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
2021-02-15 05:14:02 +00:00
|
|
|
specification = static_cast<u8>(spec_from_json);
|
|
|
|
}
|
2019-08-17 19:40:58 +00:00
|
|
|
|
2021-02-15 05:14:02 +00:00
|
|
|
if (specification != 1)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(VIDEO,
|
|
|
|
"Failed to load dynamic input json file '{}', specification '{}' is invalid",
|
|
|
|
json_file, specification);
|
|
|
|
m_valid = false;
|
|
|
|
return;
|
2019-08-17 19:40:58 +00:00
|
|
|
}
|
2021-02-15 05:14:02 +00:00
|
|
|
|
|
|
|
m_valid = ProcessSpecificationV1(root, m_dynamic_input_textures, m_base_path, json_file);
|
2019-08-17 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 02:47:00 +00:00
|
|
|
Configuration::~Configuration() = default;
|
2019-08-17 19:40:58 +00:00
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
bool Configuration::GenerateTextures(const IniFile& file,
|
|
|
|
const std::vector<std::string>& controller_names) const
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
|
|
|
bool any_dirty = false;
|
|
|
|
for (const auto& texture_data : m_dynamic_input_textures)
|
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
any_dirty |= GenerateTexture(file, controller_names, texture_data);
|
2019-08-17 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 05:04:33 +00:00
|
|
|
return any_dirty;
|
2019-08-17 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
bool Configuration::GenerateTexture(const IniFile& file,
|
|
|
|
const std::vector<std::string>& controller_names,
|
2021-02-15 03:20:47 +00:00
|
|
|
const Data& texture_data) const
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2020-12-05 19:50:11 +00:00
|
|
|
// Two copies of the loaded texture
|
|
|
|
// The first one is used as a fallback if a key or device isn't mapped
|
|
|
|
// the second one is used as the final image to write to the textures directory
|
|
|
|
const auto original_image = LoadImage(m_base_path + texture_data.m_image_name);
|
|
|
|
auto image_to_write = original_image;
|
2019-08-17 19:40:58 +00:00
|
|
|
|
2020-12-05 19:50:11 +00:00
|
|
|
bool dirty = false;
|
2021-02-27 22:41:50 +00:00
|
|
|
|
|
|
|
for (const auto& controller_name : controller_names)
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
auto* sec = file.GetSection(controller_name);
|
|
|
|
if (!sec)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-17 19:40:58 +00:00
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
std::string device_name;
|
|
|
|
if (!sec->Get("Device", &device_name))
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
auto emulated_controls_iter = texture_data.m_emulated_controllers.find(controller_name);
|
|
|
|
if (emulated_controls_iter == texture_data.m_emulated_controllers.end())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-05 19:50:11 +00:00
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
bool device_found = true;
|
|
|
|
auto host_devices_iter = texture_data.m_host_devices.find(device_name);
|
|
|
|
if (host_devices_iter == texture_data.m_host_devices.end())
|
2020-12-05 19:50:11 +00:00
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
// If we fail to find our exact device,
|
|
|
|
// it's possible the creator doesn't care (single player game)
|
|
|
|
// and has used a wildcard for any device
|
|
|
|
host_devices_iter = texture_data.m_host_devices.find("");
|
|
|
|
|
|
|
|
if (host_devices_iter == texture_data.m_host_devices.end())
|
|
|
|
{
|
|
|
|
device_found = false;
|
|
|
|
}
|
2020-12-05 19:50:11 +00:00
|
|
|
}
|
2021-02-27 22:41:50 +00:00
|
|
|
|
|
|
|
for (auto& [emulated_key, rects] : emulated_controls_iter->second)
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
if (!device_found)
|
|
|
|
{
|
|
|
|
// If we get here, that means the controller is set to a
|
|
|
|
// device not exposed to the pack
|
|
|
|
dirty = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string host_key;
|
|
|
|
sec->Get(emulated_key, &host_key);
|
2019-08-17 19:40:58 +00:00
|
|
|
|
2021-02-27 22:41:50 +00:00
|
|
|
const auto input_image_iter = host_devices_iter->second.find(host_key);
|
|
|
|
if (input_image_iter == host_devices_iter->second.end())
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto host_key_image = LoadImage(m_base_path + input_image_iter->second);
|
|
|
|
|
|
|
|
for (const auto& rect : rects)
|
2019-08-17 19:40:58 +00:00
|
|
|
{
|
2021-02-27 22:41:50 +00:00
|
|
|
InputCommon::ImagePixelData pixel_data;
|
|
|
|
if (host_key_image->width == rect.GetWidth() &&
|
|
|
|
host_key_image->height == rect.GetHeight())
|
|
|
|
{
|
|
|
|
pixel_data = *host_key_image;
|
|
|
|
}
|
|
|
|
else if (texture_data.m_preserve_aspect_ratio)
|
|
|
|
{
|
|
|
|
pixel_data =
|
|
|
|
ResizeKeepAspectRatio(ResizeMode::Nearest, *host_key_image, rect.GetWidth(),
|
|
|
|
rect.GetHeight(), Pixel{0, 0, 0, 0});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixel_data =
|
|
|
|
Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyImageRegion(pixel_data, *image_to_write,
|
|
|
|
Rect{0, 0, rect.GetWidth(), rect.GetHeight()}, rect);
|
|
|
|
dirty = true;
|
2019-08-17 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirty)
|
|
|
|
{
|
|
|
|
const std::string& game_id = SConfig::GetInstance().GetGameID();
|
|
|
|
const auto hi_res_folder =
|
|
|
|
File::GetUserPath(D_HIRESTEXTURES_IDX) + texture_data.m_generated_folder_name;
|
|
|
|
if (!File::IsDirectory(hi_res_folder))
|
|
|
|
{
|
|
|
|
File::CreateDir(hi_res_folder);
|
|
|
|
}
|
2020-12-05 19:50:11 +00:00
|
|
|
WriteImage(hi_res_folder + DIR_SEP + texture_data.m_hires_texture_name, *image_to_write);
|
2019-08-17 19:40:58 +00:00
|
|
|
|
|
|
|
const auto game_id_folder = hi_res_folder + DIR_SEP + "gameids";
|
|
|
|
if (!File::IsDirectory(game_id_folder))
|
|
|
|
{
|
|
|
|
File::CreateDir(game_id_folder);
|
|
|
|
}
|
|
|
|
File::CreateEmptyFile(game_id_folder + DIR_SEP + game_id + ".txt");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-15 02:47:00 +00:00
|
|
|
} // namespace InputCommon::DynamicInputTextures
|