[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"]
path = third_party/capstone
url = https://github.com/xenia-project/capstone.git
[submodule "third_party/cpptoml"]
path = third_party/cpptoml
url = https://github.com/skystrife/cpptoml.git
[submodule "third_party/tomlplusplus"]
path = third_party/tomlplusplus
url = https://github.com/marzer/tomlplusplus.git
[submodule "third_party/cxxopts"]
path = third_party/cxxopts
url = https://github.com/jarro2783/cxxopts.git

View file

@ -246,7 +246,7 @@ workspace("xenia")
include("third_party/dxbc.lua")
include("third_party/discord-rpc.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/fmt.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 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) {
return "'" + std::string(view) + "'";
} else {
return "\"" + toml::EscapeBasicString(view) + "\"";
return "\"" + toml_internal::EscapeBasicString(view) + "\"";
}
} else {
// 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) {
return "'''\n" + std::string(view) + "'''";
} else {
return u8"\"\"\"\n" + toml::EscapeMultilineBasicString(view) + u8"\"\"\"";
return u8"\"\"\"\n" + toml_internal::EscapeMultilineBasicString(view) +
u8"\"\"\"";
}
}
}
} // namespace toml
} // namespace toml_internal
} // namespace cvar

View file

@ -15,9 +15,9 @@
#include <string>
#include <vector>
#include "third_party/cpptoml/include/cpptoml.h"
#include "third_party/cxxopts/include/cxxopts.hpp"
#include "third_party/fmt/include/fmt/format.h"
#include "third_party/tomlplusplus/include/toml++/toml.hpp"
#include "xenia/base/assert.h"
#include "xenia/base/filesystem.h"
#include "xenia/base/platform.h"
@ -29,7 +29,7 @@
namespace cvar {
namespace toml {
namespace toml_internal {
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 bool is_transient() const = 0;
virtual std::string config_value() const = 0;
virtual void LoadConfigValue(std::shared_ptr<cpptoml::base> result) = 0;
virtual void LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) = 0;
virtual void LoadConfigValue(const toml::node* result) = 0;
virtual void LoadGameConfigValue(const toml::node* result) = 0;
virtual void ResetConfigValueToDefault() = 0;
};
@ -87,8 +87,8 @@ class ConfigVar : public CommandVar<T>, virtual public IConfigVar {
const std::string& category() const override;
bool is_transient() const override;
void AddToLaunchOptions(cxxopts::Options* options) override;
void LoadConfigValue(std::shared_ptr<cpptoml::base> result) override;
void LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) override;
void LoadConfigValue(const toml::node* result) override;
void LoadGameConfigValue(const toml::node* result) override;
void SetConfigValue(T val);
void SetGameConfigValue(T val);
// 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);
}
template <class T>
void ConfigVar<T>::LoadConfigValue(std::shared_ptr<cpptoml::base> result) {
SetConfigValue(*cpptoml::get_impl<T>(result));
void ConfigVar<T>::LoadConfigValue(const toml::node* result) {
SetConfigValue(result->value<T>().value());
}
template <>
inline void ConfigVar<std::filesystem::path>::LoadConfigValue(
std::shared_ptr<cpptoml::base> result) {
const toml::node* result) {
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>
void ConfigVar<T>::LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) {
SetGameConfigValue(*cpptoml::get_impl<T>(result));
void ConfigVar<T>::LoadGameConfigValue(const toml::node* result) {
SetGameConfigValue(result->value<T>().value());
}
template <>
inline void ConfigVar<std::filesystem::path>::LoadGameConfigValue(
std::shared_ptr<cpptoml::base> result) {
const toml::node* result) {
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>
CommandVar<T>::CommandVar(const char* name, T* default_value,
@ -215,12 +215,12 @@ inline std::string CommandVar<bool>::ToString(bool val) {
}
template <>
inline std::string CommandVar<std::string>::ToString(std::string val) {
return toml::EscapeString(val);
return toml_internal::EscapeString(val);
}
template <>
inline std::string CommandVar<std::filesystem::path>::ToString(
std::filesystem::path val) {
return toml::EscapeString(
return toml_internal::EscapeString(
xe::utf8::fix_path_separators(xe::path_to_utf8(val), '/'));
}

View file

@ -9,8 +9,8 @@
#include "config.h"
#include "third_party/cpptoml/include/cpptoml.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/cvar.h"
#include "xenia/base/filesystem.h"
@ -18,23 +18,8 @@
#include "xenia/base/string.h"
#include "xenia/base/string_buffer.h"
std::shared_ptr<cpptoml::table> ParseFile(
const std::filesystem::path& 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();
toml::parse_result ParseFile(const std::filesystem::path& filename) {
return toml::parse_file(xe::path_to_utf8(filename));
}
CmdVar(config, "", "Specifies the target config to load.");
@ -51,14 +36,13 @@ std::filesystem::path config_folder;
std::filesystem::path config_path;
std::string game_config_suffix = ".config.toml";
std::shared_ptr<cpptoml::table> ParseConfig(
const std::filesystem::path& config_path) {
toml::parse_result ParseConfig(const std::filesystem::path& config_path) {
try {
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::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) {
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
// defaults_date in it, it's very old (before updating was added at all, thus
// all defaults need to be updated).
@ -77,9 +61,12 @@ void ReadConfig(const std::filesystem::path& file_path,
defaults_date_cvar->SetConfigValue(0);
for (auto& it : *cvar::ConfigVars) {
auto config_var = static_cast<cvar::IConfigVar*>(it.second);
auto config_key = config_var->category() + "." + config_var->name();
if (config->contains_qualified(config_key)) {
config_var->LoadConfigValue(config->get_qualified(config_key));
toml::path config_key =
toml::path(config_var->category() + "." + config_var->name());
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();
@ -96,9 +83,12 @@ void ReadGameConfig(const std::filesystem::path& file_path) {
const auto config = ParseConfig(file_path);
for (auto& it : *cvar::ConfigVars) {
auto config_var = static_cast<cvar::IConfigVar*>(it.second);
auto config_key = config_var->category() + "." + config_var->name();
if (config->contains_qualified(config_key)) {
config_var->LoadGameConfigValue(config->get_qualified(config_key));
toml::path config_key =
toml::path(config_var->category() + "." + config_var->name());
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));

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