From af0ed416f5534d3b85d3920decb365a30f6536f4 Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 14 Jan 2021 22:58:48 +0000 Subject: [PATCH] Implement loading / saving of new shortcuts --- src/openrct2-ui/input/ShortcutManager.cpp | 48 +++++++++++++++++++++++ src/openrct2/core/Json.cpp | 12 ++++++ src/openrct2/core/Json.hpp | 3 ++ 3 files changed, 63 insertions(+) diff --git a/src/openrct2-ui/input/ShortcutManager.cpp b/src/openrct2-ui/input/ShortcutManager.cpp index c3ef9571a4..79d26c270f 100644 --- a/src/openrct2-ui/input/ShortcutManager.cpp +++ b/src/openrct2-ui/input/ShortcutManager.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -606,6 +607,29 @@ void ShortcutManager::LoadLegacyBindings(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()); + } + else if (value.is_array()) + { + for (auto& subValue : value) + { + shortcut->Current.emplace_back(subValue.get()); + } + } + } + } } void ShortcutManager::SaveUserBindings() @@ -623,6 +647,30 @@ void ShortcutManager::SaveUserBindings() 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) diff --git a/src/openrct2/core/Json.cpp b/src/openrct2/core/Json.cpp index 6c44360f04..39572fddbf 100644 --- a/src/openrct2/core/Json.cpp +++ b/src/openrct2/core/Json.cpp @@ -42,6 +42,12 @@ namespace 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) { // Serialise JSON @@ -52,6 +58,12 @@ namespace Json 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 json; diff --git a/src/openrct2/core/Json.hpp b/src/openrct2/core/Json.hpp index 92946c136c..0ab783cfcc 100644 --- a/src/openrct2/core/Json.hpp +++ b/src/openrct2/core/Json.hpp @@ -10,6 +10,7 @@ #pragma once #include "../common.h" +#include "FileSystem.hpp" #include #include @@ -34,6 +35,7 @@ namespace Json * @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 fs::path& path, size_t maxSize = MAX_JSON_SIZE); /** * 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 */ 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