2021-10-23 14:03:18 +00:00
|
|
|
/*
|
2022-01-06 23:38:21 +00:00
|
|
|
Copyright 2021-2022 Peter Repukat - FlatspotSoftware
|
2021-10-23 14:03:18 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <fstream>
|
2021-11-12 12:28:10 +00:00
|
|
|
#include <regex>
|
2021-10-23 14:03:18 +00:00
|
|
|
#include <string>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <spdlog/spdlog.h>
|
2022-07-13 20:53:57 +00:00
|
|
|
#include <locale>
|
|
|
|
#include <codecvt>
|
2021-10-23 14:03:18 +00:00
|
|
|
|
|
|
|
namespace Settings {
|
|
|
|
|
|
|
|
inline struct Launch {
|
|
|
|
bool launch = false;
|
|
|
|
std::wstring launchPath;
|
|
|
|
std::wstring launchAppArgs;
|
|
|
|
bool closeOnExit = true;
|
2022-03-14 19:13:07 +00:00
|
|
|
bool waitForChildProcs = true;
|
2021-11-12 12:28:10 +00:00
|
|
|
bool isUWP = false;
|
2021-10-23 14:03:18 +00:00
|
|
|
} launch;
|
|
|
|
|
|
|
|
inline struct Devices {
|
|
|
|
bool hideDevices = true;
|
2022-03-07 14:26:16 +00:00
|
|
|
bool realDeviceIds = false;
|
2021-10-23 14:03:18 +00:00
|
|
|
} devices;
|
|
|
|
|
|
|
|
inline struct Window {
|
|
|
|
bool windowMode = false;
|
|
|
|
int maxFps = 0;
|
|
|
|
float scale = 0.f;
|
2022-03-05 15:47:07 +00:00
|
|
|
bool disableOverlay = false;
|
2021-10-23 14:03:18 +00:00
|
|
|
} window;
|
|
|
|
|
2022-03-05 15:47:07 +00:00
|
|
|
inline struct Controller {
|
2022-09-20 18:07:52 +00:00
|
|
|
int maxControllers = 1;
|
2022-07-26 10:01:56 +00:00
|
|
|
bool allowDesktopConfig = false;
|
2022-06-06 18:23:05 +00:00
|
|
|
bool emulateDS4 = false;
|
2022-03-05 15:47:07 +00:00
|
|
|
} controller;
|
|
|
|
|
2022-09-23 10:00:58 +00:00
|
|
|
inline bool extendedLogging = false;
|
2022-09-20 18:00:50 +00:00
|
|
|
|
2022-09-20 12:29:47 +00:00
|
|
|
inline std::filesystem::path settings_path_ = "";
|
|
|
|
|
2021-11-12 12:28:10 +00:00
|
|
|
inline bool checkIsUwp(const std::wstring& launch_path)
|
|
|
|
{
|
2022-04-09 13:29:58 +00:00
|
|
|
if (launch_path.find(L"://") != std::wstring::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-12 12:28:10 +00:00
|
|
|
std::wsmatch m;
|
|
|
|
if (!std::regex_search(launch_path, m, std::wregex(L"^.{1,3}:"))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-13 20:53:57 +00:00
|
|
|
inline void Parse(std::wstring arg1)
|
2021-10-23 14:03:18 +00:00
|
|
|
{
|
2022-07-13 20:53:57 +00:00
|
|
|
if (!arg1.ends_with(L".json")) {
|
|
|
|
arg1 += L".json";
|
2021-11-04 11:59:02 +00:00
|
|
|
}
|
2021-10-28 18:42:48 +00:00
|
|
|
std::filesystem::path path(arg1);
|
|
|
|
if (path.has_extension() && !std::filesystem::exists(path)) {
|
|
|
|
path = std::filesystem::temp_directory_path()
|
2022-03-05 15:47:07 +00:00
|
|
|
.parent_path()
|
|
|
|
.parent_path()
|
|
|
|
.parent_path();
|
2021-10-28 18:42:48 +00:00
|
|
|
|
|
|
|
path /= "Roaming";
|
|
|
|
path /= "GlosSI";
|
|
|
|
path /= "Targets";
|
|
|
|
path /= arg1;
|
|
|
|
}
|
2021-10-23 14:03:18 +00:00
|
|
|
std::ifstream json_file;
|
2021-10-28 18:42:48 +00:00
|
|
|
json_file.open(path);
|
2021-10-23 14:03:18 +00:00
|
|
|
if (!json_file.is_open()) {
|
2022-07-13 20:53:57 +00:00
|
|
|
spdlog::error(L"Couldn't open settings file {}", path.wstring());
|
2021-10-23 14:03:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-20 12:29:47 +00:00
|
|
|
settings_path_ = path;
|
2021-10-23 14:03:18 +00:00
|
|
|
|
2022-09-22 18:44:56 +00:00
|
|
|
auto safeParseValue = [](const auto& object, const auto& key, auto& value) {
|
2021-10-23 14:03:18 +00:00
|
|
|
try {
|
2022-04-09 13:29:58 +00:00
|
|
|
if (object.is_null() || object.empty() || object.at(key).empty() || object.at(key).is_null()) {
|
2021-10-23 14:03:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
value = object[key];
|
2022-03-05 15:47:07 +00:00
|
|
|
}
|
|
|
|
catch (const nlohmann::json::exception& e) {
|
2022-09-20 12:29:47 +00:00
|
|
|
spdlog::warn("Err parsing \"{}\"; {}", key, e.what());
|
2021-10-23 14:03:18 +00:00
|
|
|
}
|
2022-04-09 13:29:58 +00:00
|
|
|
catch (const std::exception& e) {
|
2022-09-20 12:29:47 +00:00
|
|
|
spdlog::warn("Err parsing \"{}\"; {}", key, e.what());
|
2022-04-09 13:29:58 +00:00
|
|
|
}
|
2021-10-23 14:03:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto safeWStringParse = [&safeParseValue](const auto& object, const auto& key, std::wstring& value) {
|
|
|
|
std::string meh;
|
|
|
|
safeParseValue(object, key, meh);
|
|
|
|
if (!meh.empty()) {
|
2022-07-13 20:53:57 +00:00
|
|
|
// This assumes that char is utf8 and wchar_t is utf16, which is guaranteed on Windows.
|
|
|
|
value = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(meh);
|
2021-10-23 14:03:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-22 18:44:56 +00:00
|
|
|
const auto json = nlohmann::json::parse(json_file);
|
|
|
|
int version;
|
|
|
|
safeParseValue(json, "version" ,version);
|
|
|
|
if (version != 1) { // TODO: versioning stuff
|
|
|
|
spdlog::warn("Config version doesn't match application version.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make this as much generic as fits in about the same amount of code if one would parse every value separately.
|
|
|
|
|
2021-10-23 14:03:18 +00:00
|
|
|
if (auto launchconf = json["launch"]; launchconf.is_object()) {
|
|
|
|
safeParseValue(launchconf, "launch", launch.launch);
|
|
|
|
safeWStringParse(launchconf, "launchPath", launch.launchPath);
|
|
|
|
safeWStringParse(launchconf, "launchAppArgs", launch.launchAppArgs);
|
|
|
|
safeParseValue(launchconf, "closeOnExit", launch.closeOnExit);
|
2021-11-12 13:49:25 +00:00
|
|
|
safeParseValue(launchconf, "waitForChildProcs", launch.waitForChildProcs);
|
2021-10-23 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto devconf = json["devices"]; devconf.is_object()) {
|
|
|
|
safeParseValue(devconf, "hideDevices", devices.hideDevices);
|
2022-03-07 14:26:16 +00:00
|
|
|
safeParseValue(devconf, "realDeviceIds", devices.realDeviceIds);
|
2021-10-23 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto winconf = json["window"]; winconf.is_object()) {
|
|
|
|
safeParseValue(winconf, "windowMode", window.windowMode);
|
|
|
|
safeParseValue(winconf, "maxFps", window.maxFps);
|
|
|
|
safeParseValue(winconf, "scale", window.scale);
|
2022-03-05 15:47:07 +00:00
|
|
|
safeParseValue(winconf, "disableOverlay", window.disableOverlay);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto controllerConf = json["controller"]; controllerConf.is_object()) {
|
|
|
|
safeParseValue(controllerConf, "maxControllers", controller.maxControllers);
|
2022-03-14 19:13:43 +00:00
|
|
|
safeParseValue(controllerConf, "allowDesktopConfig", controller.allowDesktopConfig);
|
2022-06-06 18:23:05 +00:00
|
|
|
safeParseValue(controllerConf, "emulateDS4", controller.emulateDS4);
|
2021-10-23 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 18:44:56 +00:00
|
|
|
safeParseValue(json, "extendedLogging", extendedLogging);
|
2022-09-20 18:00:50 +00:00
|
|
|
|
2021-10-23 14:03:18 +00:00
|
|
|
json_file.close();
|
2021-11-04 12:07:07 +00:00
|
|
|
|
2022-09-24 16:34:34 +00:00
|
|
|
spdlog::debug("Read config file \"{}\"; config: {}", path.string(), json.dump());
|
2021-11-12 12:28:10 +00:00
|
|
|
|
|
|
|
if (launch.launch) {
|
|
|
|
launch.isUWP = checkIsUwp(launch.launchPath);
|
|
|
|
}
|
2021-10-23 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 12:29:47 +00:00
|
|
|
inline void StoreSettings()
|
|
|
|
{
|
|
|
|
nlohmann::json json;
|
|
|
|
json["version"] = 1;
|
|
|
|
json["launch"]["launch"] = launch.launch;
|
|
|
|
json["launch"]["launchPath"] = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(launch.launchPath);
|
|
|
|
json["launch"]["launchAppArgs"] = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(launch.launchAppArgs);
|
|
|
|
json["launch"]["closeOnExit"] = launch.closeOnExit;
|
|
|
|
json["launch"]["waitForChildProcs"] = launch.waitForChildProcs;
|
|
|
|
json["devices"]["hideDevices"] = devices.hideDevices;
|
|
|
|
json["devices"]["realDeviceIds"] = devices.realDeviceIds;
|
|
|
|
json["window"]["windowMode"] = window.windowMode;
|
|
|
|
json["window"]["maxFps"] = window.maxFps;
|
|
|
|
json["window"]["scale"] = window.scale;
|
|
|
|
json["window"]["disableOverlay"] = window.disableOverlay;
|
|
|
|
json["controller"]["maxControllers"] = controller.maxControllers;
|
|
|
|
json["controller"]["allowDesktopConfig"] = controller.allowDesktopConfig;
|
|
|
|
json["controller"]["emulateDS4"] = controller.emulateDS4;
|
|
|
|
|
2022-09-20 18:00:50 +00:00
|
|
|
json["extendedLogging"] = extendedLogging;
|
|
|
|
|
2022-09-20 12:29:47 +00:00
|
|
|
std::ofstream json_file;
|
|
|
|
json_file.open(settings_path_);
|
|
|
|
if (!json_file.is_open()) {
|
|
|
|
spdlog::error(L"Couldn't open settings file {}", settings_path_.wstring());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
json_file << json.dump(4);
|
|
|
|
json_file.close();
|
|
|
|
}
|
|
|
|
|
2021-10-23 14:03:18 +00:00
|
|
|
} // namespace Settings
|