mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-24 00:03:11 +01:00
Implement loading / saving of new shortcuts
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
#include <openrct2/PlatformEnvironment.h>
|
#include <openrct2/PlatformEnvironment.h>
|
||||||
#include <openrct2/core/DataSerialiser.h>
|
#include <openrct2/core/DataSerialiser.h>
|
||||||
#include <openrct2/core/FileSystem.hpp>
|
#include <openrct2/core/FileSystem.hpp>
|
||||||
|
#include <openrct2/core/Json.hpp>
|
||||||
#include <openrct2/core/String.hpp>
|
#include <openrct2/core/String.hpp>
|
||||||
#include <openrct2/interface/Window.h>
|
#include <openrct2/interface/Window.h>
|
||||||
#include <openrct2/localisation/Language.h>
|
#include <openrct2/localisation/Language.h>
|
||||||
@@ -606,6 +607,29 @@ void ShortcutManager::LoadLegacyBindings(const fs::path& path)
|
|||||||
|
|
||||||
void ShortcutManager::LoadUserBindings(const fs::path& path)
|
void ShortcutManager::LoadUserBindings(const fs::path& path)
|
||||||
{
|
{
|
||||||
|
auto root = Json::ReadFromFile(path);
|
||||||
|
if (root.is_object())
|
||||||
|
{
|
||||||
|
for (auto it = root.begin(); it != root.end(); ++it)
|
||||||
|
{
|
||||||
|
const auto& key = it.key();
|
||||||
|
const auto& value = it.value();
|
||||||
|
|
||||||
|
const auto& shortcut = GetShortcut(key);
|
||||||
|
shortcut->Current.clear();
|
||||||
|
if (value.is_string())
|
||||||
|
{
|
||||||
|
shortcut->Current.emplace_back(value.get<std::string>());
|
||||||
|
}
|
||||||
|
else if (value.is_array())
|
||||||
|
{
|
||||||
|
for (auto& subValue : value)
|
||||||
|
{
|
||||||
|
shortcut->Current.emplace_back(subValue.get<std::string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShortcutManager::SaveUserBindings()
|
void ShortcutManager::SaveUserBindings()
|
||||||
@@ -623,6 +647,30 @@ void ShortcutManager::SaveUserBindings()
|
|||||||
|
|
||||||
void ShortcutManager::SaveUserBindings(const fs::path& path)
|
void ShortcutManager::SaveUserBindings(const fs::path& path)
|
||||||
{
|
{
|
||||||
|
json_t root;
|
||||||
|
if (fs::exists(path))
|
||||||
|
{
|
||||||
|
root = Json::ReadFromFile(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& shortcut : Shortcuts)
|
||||||
|
{
|
||||||
|
auto& jShortcut = root[shortcut.Id];
|
||||||
|
if (shortcut.Current.size() == 1)
|
||||||
|
{
|
||||||
|
jShortcut = shortcut.Current[0].ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
jShortcut = nlohmann::json::array();
|
||||||
|
for (const auto& binding : shortcut.Current)
|
||||||
|
{
|
||||||
|
jShortcut.push_back(binding.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Json::WriteToFile(path, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view ShortcutManager::GetLegacyShortcutId(size_t index)
|
std::string_view ShortcutManager::GetLegacyShortcutId(size_t index)
|
||||||
|
|||||||
@@ -42,6 +42,12 @@ namespace Json
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
json_t ReadFromFile(const fs::path& path, size_t maxSize)
|
||||||
|
{
|
||||||
|
auto path8 = path.u8string();
|
||||||
|
return ReadFromFile(path8.c_str(), maxSize);
|
||||||
|
}
|
||||||
|
|
||||||
void WriteToFile(const utf8* path, const json_t& jsonData, int indentSize)
|
void WriteToFile(const utf8* path, const json_t& jsonData, int indentSize)
|
||||||
{
|
{
|
||||||
// Serialise JSON
|
// Serialise JSON
|
||||||
@@ -52,6 +58,12 @@ namespace Json
|
|||||||
fs.Write(jsonOutput.data(), jsonOutput.size());
|
fs.Write(jsonOutput.data(), jsonOutput.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WriteToFile(const fs::path& path, const json_t& jsonData, int indentSize)
|
||||||
|
{
|
||||||
|
auto path8 = path.u8string();
|
||||||
|
WriteToFile(path8.c_str(), jsonData, indentSize);
|
||||||
|
}
|
||||||
|
|
||||||
json_t FromString(std::string_view raw)
|
json_t FromString(std::string_view raw)
|
||||||
{
|
{
|
||||||
json_t json;
|
json_t json;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
|
#include "FileSystem.hpp"
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -34,6 +35,7 @@ namespace Json
|
|||||||
* @note This function will throw an exception if the JSON file cannot be parsed
|
* @note This function will throw an exception if the JSON file cannot be parsed
|
||||||
*/
|
*/
|
||||||
json_t ReadFromFile(const utf8* path, size_t maxSize = MAX_JSON_SIZE);
|
json_t ReadFromFile(const utf8* path, size_t maxSize = MAX_JSON_SIZE);
|
||||||
|
json_t ReadFromFile(const fs::path& path, size_t maxSize = MAX_JSON_SIZE);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read JSON file and parse the contents
|
* Read JSON file and parse the contents
|
||||||
@@ -42,6 +44,7 @@ namespace Json
|
|||||||
* @param indentSize The number of spaces in an indent, or removes whitespace on -1
|
* @param indentSize The number of spaces in an indent, or removes whitespace on -1
|
||||||
*/
|
*/
|
||||||
void WriteToFile(const utf8* path, const json_t& jsonData, int indentSize = 4);
|
void WriteToFile(const utf8* path, const json_t& jsonData, int indentSize = 4);
|
||||||
|
void WriteToFile(const fs::path& path, const json_t& jsonData, int indentSize = 4);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse JSON from a string
|
* Parse JSON from a string
|
||||||
|
|||||||
Reference in New Issue
Block a user