[3PP] Replace cpptoml with tomlplusplus

This commit is contained in:
Gliniak 2024-02-26 09:30:54 +01:00
parent f6b5424a9f
commit 20f3ffbe13
8 changed files with 47 additions and 56 deletions

6
.gitmodules vendored
View file

@ -31,9 +31,9 @@
[submodule "third_party/capstone"] [submodule "third_party/capstone"]
path = third_party/capstone path = third_party/capstone
url = https://github.com/xenia-project/capstone.git url = https://github.com/xenia-project/capstone.git
[submodule "third_party/cpptoml"] [submodule "third_party/tomlplusplus"]
path = third_party/cpptoml path = third_party/tomlplusplus
url = https://github.com/skystrife/cpptoml.git url = https://github.com/marzer/tomlplusplus.git
[submodule "third_party/cxxopts"] [submodule "third_party/cxxopts"]
path = third_party/cxxopts path = third_party/cxxopts
url = https://github.com/jarro2783/cxxopts.git url = https://github.com/jarro2783/cxxopts.git

View file

@ -246,7 +246,7 @@ workspace("xenia")
include("third_party/dxbc.lua") include("third_party/dxbc.lua")
include("third_party/discord-rpc.lua") include("third_party/discord-rpc.lua")
include("third_party/cxxopts.lua") include("third_party/cxxopts.lua")
include("third_party/cpptoml.lua") include("third_party/tomlplusplus.lua")
include("third_party/FFmpeg/premake5.lua") include("third_party/FFmpeg/premake5.lua")
include("third_party/fmt.lua") include("third_party/fmt.lua")
include("third_party/glslang-spirv.lua") include("third_party/glslang-spirv.lua")

View file

@ -101,7 +101,7 @@ void ParseLaunchArguments(int& argc, char**& argv,
} }
} }
namespace toml { namespace toml_internal {
std::string EscapeBasicString(const std::string_view view) { std::string EscapeBasicString(const std::string_view view) {
std::string result; std::string result;
@ -202,7 +202,7 @@ std::string EscapeString(const std::string_view view) {
if (xe::utf8::find_any_of(view, escape_chars) == std::string_view::npos) { if (xe::utf8::find_any_of(view, escape_chars) == std::string_view::npos) {
return "'" + std::string(view) + "'"; return "'" + std::string(view) + "'";
} else { } else {
return "\"" + toml::EscapeBasicString(view) + "\""; return "\"" + toml_internal::EscapeBasicString(view) + "\"";
} }
} else { } else {
// multi line // multi line
@ -210,11 +210,12 @@ std::string EscapeString(const std::string_view view) {
xe::utf8::find_first_of(view, u8"'''") == std::string_view::npos) { xe::utf8::find_first_of(view, u8"'''") == std::string_view::npos) {
return "'''\n" + std::string(view) + "'''"; return "'''\n" + std::string(view) + "'''";
} else { } else {
return u8"\"\"\"\n" + toml::EscapeMultilineBasicString(view) + u8"\"\"\""; return u8"\"\"\"\n" + toml_internal::EscapeMultilineBasicString(view) +
u8"\"\"\"";
} }
} }
} }
} // namespace toml } // namespace toml_internal
} // namespace cvar } // namespace cvar

View file

@ -15,9 +15,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "third_party/cpptoml/include/cpptoml.h"
#include "third_party/cxxopts/include/cxxopts.hpp" #include "third_party/cxxopts/include/cxxopts.hpp"
#include "third_party/fmt/include/fmt/format.h" #include "third_party/fmt/include/fmt/format.h"
#include "third_party/tomlplusplus/include/toml++/toml.hpp"
#include "xenia/base/assert.h" #include "xenia/base/assert.h"
#include "xenia/base/filesystem.h" #include "xenia/base/filesystem.h"
#include "xenia/base/platform.h" #include "xenia/base/platform.h"
@ -29,7 +29,7 @@
namespace cvar { namespace cvar {
namespace toml { namespace toml_internal {
std::string EscapeString(const std::string_view str); std::string EscapeString(const std::string_view str);
} }
@ -48,8 +48,8 @@ class IConfigVar : virtual public ICommandVar {
virtual const std::string& category() const = 0; virtual const std::string& category() const = 0;
virtual bool is_transient() const = 0; virtual bool is_transient() const = 0;
virtual std::string config_value() const = 0; virtual std::string config_value() const = 0;
virtual void LoadConfigValue(std::shared_ptr<cpptoml::base> result) = 0; virtual void LoadConfigValue(const toml::node* result) = 0;
virtual void LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) = 0; virtual void LoadGameConfigValue(const toml::node* result) = 0;
virtual void ResetConfigValueToDefault() = 0; virtual void ResetConfigValueToDefault() = 0;
}; };
@ -87,8 +87,8 @@ class ConfigVar : public CommandVar<T>, virtual public IConfigVar {
const std::string& category() const override; const std::string& category() const override;
bool is_transient() const override; bool is_transient() const override;
void AddToLaunchOptions(cxxopts::Options* options) override; void AddToLaunchOptions(cxxopts::Options* options) override;
void LoadConfigValue(std::shared_ptr<cpptoml::base> result) override; void LoadConfigValue(const toml::node* result) override;
void LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) override; void LoadGameConfigValue(const toml::node* result) override;
void SetConfigValue(T val); void SetConfigValue(T val);
void SetGameConfigValue(T val); void SetGameConfigValue(T val);
// Changes the actual value used to the one specified, and also makes it the // Changes the actual value used to the one specified, and also makes it the
@ -146,24 +146,24 @@ inline void CommandVar<std::filesystem::path>::LoadFromLaunchOptions(
SetCommandLineValue(value); SetCommandLineValue(value);
} }
template <class T> template <class T>
void ConfigVar<T>::LoadConfigValue(std::shared_ptr<cpptoml::base> result) { void ConfigVar<T>::LoadConfigValue(const toml::node* result) {
SetConfigValue(*cpptoml::get_impl<T>(result)); SetConfigValue(result->value<T>().value());
} }
template <> template <>
inline void ConfigVar<std::filesystem::path>::LoadConfigValue( inline void ConfigVar<std::filesystem::path>::LoadConfigValue(
std::shared_ptr<cpptoml::base> result) { const toml::node* result) {
SetConfigValue( SetConfigValue(
xe::utf8::fix_path_separators(*cpptoml::get_impl<std::string>(result))); xe::utf8::fix_path_separators(result->as_string()->value_or("")));
} }
template <class T> template <class T>
void ConfigVar<T>::LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) { void ConfigVar<T>::LoadGameConfigValue(const toml::node* result) {
SetGameConfigValue(*cpptoml::get_impl<T>(result)); SetGameConfigValue(result->value<T>().value());
} }
template <> template <>
inline void ConfigVar<std::filesystem::path>::LoadGameConfigValue( inline void ConfigVar<std::filesystem::path>::LoadGameConfigValue(
std::shared_ptr<cpptoml::base> result) { const toml::node* result) {
SetGameConfigValue( SetGameConfigValue(
xe::utf8::fix_path_separators(*cpptoml::get_impl<std::string>(result))); xe::utf8::fix_path_separators(result->as_string()->value_or("")));
} }
template <class T> template <class T>
CommandVar<T>::CommandVar(const char* name, T* default_value, CommandVar<T>::CommandVar(const char* name, T* default_value,
@ -215,12 +215,12 @@ inline std::string CommandVar<bool>::ToString(bool val) {
} }
template <> template <>
inline std::string CommandVar<std::string>::ToString(std::string val) { inline std::string CommandVar<std::string>::ToString(std::string val) {
return toml::EscapeString(val); return toml_internal::EscapeString(val);
} }
template <> template <>
inline std::string CommandVar<std::filesystem::path>::ToString( inline std::string CommandVar<std::filesystem::path>::ToString(
std::filesystem::path val) { std::filesystem::path val) {
return toml::EscapeString( return toml_internal::EscapeString(
xe::utf8::fix_path_separators(xe::path_to_utf8(val), '/')); xe::utf8::fix_path_separators(xe::path_to_utf8(val), '/'));
} }

View file

@ -9,8 +9,8 @@
#include "config.h" #include "config.h"
#include "third_party/cpptoml/include/cpptoml.h"
#include "third_party/fmt/include/fmt/format.h" #include "third_party/fmt/include/fmt/format.h"
#include "third_party/tomlplusplus/include/toml++/toml.hpp"
#include "xenia/base/assert.h" #include "xenia/base/assert.h"
#include "xenia/base/cvar.h" #include "xenia/base/cvar.h"
#include "xenia/base/filesystem.h" #include "xenia/base/filesystem.h"
@ -18,23 +18,8 @@
#include "xenia/base/string.h" #include "xenia/base/string.h"
#include "xenia/base/string_buffer.h" #include "xenia/base/string_buffer.h"
std::shared_ptr<cpptoml::table> ParseFile( toml::parse_result ParseFile(const std::filesystem::path& filename) {
const std::filesystem::path& filename) { return toml::parse_file(xe::path_to_utf8(filename));
std::ifstream file(filename);
if (!file.is_open()) {
throw cpptoml::parse_exception(xe::path_to_utf8(filename) +
" could not be opened for parsing");
}
// since cpptoml can't parse files with a UTF-8 BOM we need to skip them
char bom[3];
file.read(bom, sizeof(bom));
if (file.fail() || bom[0] != '\xEF' || bom[1] != '\xBB' || bom[2] != '\xBF') {
file.clear();
file.seekg(0);
}
cpptoml::parser p(file);
return p.parse();
} }
CmdVar(config, "", "Specifies the target config to load."); CmdVar(config, "", "Specifies the target config to load.");
@ -51,14 +36,13 @@ std::filesystem::path config_folder;
std::filesystem::path config_path; std::filesystem::path config_path;
std::string game_config_suffix = ".config.toml"; std::string game_config_suffix = ".config.toml";
std::shared_ptr<cpptoml::table> ParseConfig( toml::parse_result ParseConfig(const std::filesystem::path& config_path) {
const std::filesystem::path& config_path) {
try { try {
return ParseFile(config_path); return ParseFile(config_path);
} catch (cpptoml::parse_exception e) { } catch (toml::parse_error& e) {
xe::FatalError(fmt::format("Failed to parse config file '{}':\n\n{}", xe::FatalError(fmt::format("Failed to parse config file '{}':\n\n{}",
xe::path_to_utf8(config_path), e.what())); xe::path_to_utf8(config_path), e.what()));
return nullptr; return toml::parse_result();
} }
} }
@ -67,7 +51,7 @@ void ReadConfig(const std::filesystem::path& file_path,
if (!cvar::ConfigVars) { if (!cvar::ConfigVars) {
return; return;
} }
const auto config = ParseConfig(file_path); const toml::table config = ParseConfig(file_path);
// Loading an actual global config file that exists - if there's no // Loading an actual global config file that exists - if there's no
// defaults_date in it, it's very old (before updating was added at all, thus // defaults_date in it, it's very old (before updating was added at all, thus
// all defaults need to be updated). // all defaults need to be updated).
@ -77,9 +61,12 @@ void ReadConfig(const std::filesystem::path& file_path,
defaults_date_cvar->SetConfigValue(0); defaults_date_cvar->SetConfigValue(0);
for (auto& it : *cvar::ConfigVars) { for (auto& it : *cvar::ConfigVars) {
auto config_var = static_cast<cvar::IConfigVar*>(it.second); auto config_var = static_cast<cvar::IConfigVar*>(it.second);
auto config_key = config_var->category() + "." + config_var->name(); toml::path config_key =
if (config->contains_qualified(config_key)) { toml::path(config_var->category() + "." + config_var->name());
config_var->LoadConfigValue(config->get_qualified(config_key));
const auto config_key_node = config.at_path(config_key);
if (config_key_node) {
config_var->LoadConfigValue(config_key_node.node());
} }
} }
uint32_t config_defaults_date = defaults_date_cvar->GetTypedConfigValue(); uint32_t config_defaults_date = defaults_date_cvar->GetTypedConfigValue();
@ -96,9 +83,12 @@ void ReadGameConfig(const std::filesystem::path& file_path) {
const auto config = ParseConfig(file_path); const auto config = ParseConfig(file_path);
for (auto& it : *cvar::ConfigVars) { for (auto& it : *cvar::ConfigVars) {
auto config_var = static_cast<cvar::IConfigVar*>(it.second); auto config_var = static_cast<cvar::IConfigVar*>(it.second);
auto config_key = config_var->category() + "." + config_var->name(); toml::path config_key =
if (config->contains_qualified(config_key)) { toml::path(config_var->category() + "." + config_var->name());
config_var->LoadGameConfigValue(config->get_qualified(config_key));
const auto config_key_node = config.at_path(config_key);
if (config_key_node) {
config_var->LoadConfigValue(config_key_node.node());
} }
} }
XELOGI("Loaded game config: {}", xe::path_to_utf8(file_path)); XELOGI("Loaded game config: {}", xe::path_to_utf8(file_path));

1
third_party/cpptoml vendored

@ -1 +0,0 @@
Subproject commit fededad7169e538ca47e11a9ee9251bc361a9a65

1
third_party/tomlplusplus vendored Submodule

@ -0,0 +1 @@
Subproject commit 30172438cee64926dc41fdd9c11fb3ba5b2ba9de

View file

@ -1,5 +1,5 @@
group("third_party") group("third_party")
project("cpptoml") project("tomlplusplus")
uuid("1e86cc51-3f8b-476d-9249-3b200424846b") uuid("1e86cc51-3f8b-476d-9249-3b200424846b")
if os.istarget("android") then if os.istarget("android") then
-- ndk-build only supports StaticLib and SharedLib. -- ndk-build only supports StaticLib and SharedLib.
@ -9,5 +9,5 @@ project("cpptoml")
end end
language("C++") language("C++")
files({ files({
"cpptoml/include/cpptoml.h", "tomlplusplus/include/toml++/toml.hpp"
}) })