[D3D12] Switch from gflags to cvars

This commit is contained in:
Triang3l
2019-08-03 16:53:23 +03:00
127 changed files with 959 additions and 647 deletions

56
src/xenia/base/cvar.cc Normal file
View File

@@ -0,0 +1,56 @@
#include "cvar.h"
namespace cvar {
cxxopts::Options options("xenia", "Xbox 360 Emulator");
std::map<std::string, ICommandVar*>* CmdVars;
std::map<std::string, IConfigVar*>* ConfigVars;
void PrintHelpAndExit() {
std::cout << options.help({""}) << std::endl;
std::cout << "For the full list of command line arguments, see xenia.cfg."
<< std::endl;
exit(0);
}
void ParseLaunchArguments(int argc, char** argv) {
options.add_options()("help", "Prints help and exit.");
if (!CmdVars) CmdVars = new std::map<std::string, ICommandVar*>();
if (!ConfigVars) ConfigVars = new std::map<std::string, IConfigVar*>();
for (auto& it : *CmdVars) {
auto cmdVar = it.second;
cmdVar->AddToLaunchOptions(&options);
}
std::vector<IConfigVar*> vars;
for (const auto& s : *ConfigVars) vars.push_back(s.second);
for (auto& it : *ConfigVars) {
auto configVar = it.second;
configVar->AddToLaunchOptions(&options);
}
try {
options.positional_help("[Path to .iso/.xex]");
options.parse_positional({"target"});
auto result = options.parse(argc, argv);
if (result.count("help")) {
PrintHelpAndExit();
}
for (auto& it : *CmdVars) {
auto cmdVar = static_cast<ICommandVar*>(it.second);
if (result.count(cmdVar->GetName())) {
cmdVar->LoadFromLaunchOptions(&result);
}
}
for (auto& it : *ConfigVars) {
auto configVar = static_cast<IConfigVar*>(it.second);
if (result.count(configVar->GetName())) {
configVar->LoadFromLaunchOptions(&result);
}
}
} catch (const cxxopts::OptionException& e) {
std::cout << e.what() << std::endl;
PrintHelpAndExit();
}
}
} // namespace cvar

258
src/xenia/base/cvar.h Normal file
View File

@@ -0,0 +1,258 @@
#ifndef XENIA_CVAR_H_
#define XENIA_CVAR_H_
#include <map>
#include <string>
#include "cpptoml/include/cpptoml.h"
#include "cxxopts/include/cxxopts.hpp"
#include "xenia/base/string_util.h"
namespace cvar {
class ICommandVar {
public:
virtual ~ICommandVar() = default;
virtual std::string GetName() = 0;
virtual std::string GetDescription() = 0;
virtual void UpdateValue() = 0;
virtual void AddToLaunchOptions(cxxopts::Options* options) = 0;
virtual void LoadFromLaunchOptions(cxxopts::ParseResult* result) = 0;
};
class IConfigVar : virtual public ICommandVar {
public:
virtual std::string GetCategory() = 0;
virtual std::string GetConfigValue() = 0;
virtual void LoadConfigValue(std::shared_ptr<cpptoml::base> result) = 0;
virtual void LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) = 0;
};
template <class T>
class CommandVar : virtual public ICommandVar {
public:
CommandVar<T>(const char* name, T* defaultValue, const char* description);
std::string GetName() override;
std::string GetDescription() override;
void AddToLaunchOptions(cxxopts::Options* options) override;
void LoadFromLaunchOptions(cxxopts::ParseResult* result) override;
T* GetCurrentValue() { return currentValue_; }
protected:
std::string name_;
T defaultValue_;
T* currentValue_;
std::unique_ptr<T> commandLineValue_;
std::string description_;
T Convert(std::string val);
static std::string ToString(T val);
void SetValue(T val);
void SetCommandLineValue(T val);
void UpdateValue() override;
};
#pragma warning(push)
#pragma warning(disable : 4250)
template <class T>
class ConfigVar : public CommandVar<T>, virtual public IConfigVar {
public:
ConfigVar<T>(const char* name, T* defaultValue, const char* description,
const char* category);
std::string GetConfigValue() override;
std::string GetCategory() 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 SetConfigValue(T val);
void SetGameConfigValue(T val);
private:
std::string category_;
std::unique_ptr<T> configValue_ = nullptr;
std::unique_ptr<T> gameConfigValue_ = nullptr;
void UpdateValue() override;
};
#pragma warning(pop)
template <class T>
std::string CommandVar<T>::GetName() {
return name_;
}
template <class T>
std::string CommandVar<T>::GetDescription() {
return description_;
}
template <class T>
void CommandVar<T>::AddToLaunchOptions(cxxopts::Options* options) {
options->add_options()(this->name_, this->description_, cxxopts::value<T>());
}
template <class T>
void ConfigVar<T>::AddToLaunchOptions(cxxopts::Options* options) {
options->add_options(category_)(this->name_, this->description_,
cxxopts::value<T>());
}
template <class T>
void CommandVar<T>::LoadFromLaunchOptions(cxxopts::ParseResult* result) {
T value = (*result)[this->name_].template as<T>();
SetCommandLineValue(value);
}
template <class T>
void ConfigVar<T>::LoadConfigValue(std::shared_ptr<cpptoml::base> result) {
SetConfigValue(*cpptoml::get_impl<T>(result));
}
template <class T>
void ConfigVar<T>::LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) {
SetGameConfigValue(*cpptoml::get_impl<T>(result));
}
template <class T>
CommandVar<T>::CommandVar(const char* name, T* defaultValue,
const char* description)
: name_(name),
defaultValue_(*defaultValue),
description_(description),
currentValue_(defaultValue) {}
template <class T>
ConfigVar<T>::ConfigVar(const char* name, T* defaultValue,
const char* description, const char* category)
: CommandVar<T>(name, defaultValue, description), category_(category) {}
template <class T>
void CommandVar<T>::UpdateValue() {
if (this->commandLineValue_) return this->SetValue(*this->commandLineValue_);
return this->SetValue(defaultValue_);
}
template <class T>
void ConfigVar<T>::UpdateValue() {
if (this->commandLineValue_) return this->SetValue(*this->commandLineValue_);
if (this->gameConfigValue_) return this->SetValue(*this->gameConfigValue_);
if (this->configValue_) return this->SetValue(*this->configValue_);
return this->SetValue(this->defaultValue_);
}
template <class T>
T CommandVar<T>::Convert(std::string val) {
return xe::string_util::from_string<T>(val);
}
template <>
inline std::string CommandVar<std::string>::Convert(std::string val) {
return val;
}
template <>
inline std::string CommandVar<bool>::ToString(bool val) {
return val ? "true" : "false";
}
template <>
inline std::string CommandVar<std::string>::ToString(std::string val) {
return "\"" + val + "\"";
}
template <class T>
std::string CommandVar<T>::ToString(T val) {
return std::to_string(val);
}
template <class T>
void CommandVar<T>::SetValue(T val) {
*currentValue_ = val;
}
template <class T>
std::string ConfigVar<T>::GetCategory() {
return category_;
}
template <class T>
std::string ConfigVar<T>::GetConfigValue() {
if (this->configValue_) return this->ToString(*this->configValue_);
return this->ToString(this->defaultValue_);
}
template <class T>
void CommandVar<T>::SetCommandLineValue(const T val) {
this->commandLineValue_ = std::make_unique<T>(val);
this->UpdateValue();
}
template <class T>
void ConfigVar<T>::SetConfigValue(T val) {
this->configValue_ = std::make_unique<T>(val);
this->UpdateValue();
}
template <class T>
void ConfigVar<T>::SetGameConfigValue(T val) {
this->gameConfigValue_ = std::make_unique<T>(val);
this->UpdateValue();
}
extern std::map<std::string, ICommandVar*>* CmdVars;
extern std::map<std::string, IConfigVar*>* ConfigVars;
inline void AddConfigVar(IConfigVar* cv) {
if (!ConfigVars) ConfigVars = new std::map<std::string, IConfigVar*>();
ConfigVars->insert(std::pair<std::string, IConfigVar*>(cv->GetName(), cv));
}
inline void AddCommandVar(ICommandVar* cv) {
if (!CmdVars) CmdVars = new std::map<std::string, ICommandVar*>();
CmdVars->insert(std::pair<std::string, ICommandVar*>(cv->GetName(), cv));
}
void ParseLaunchArguments(int argc, char** argv);
template <typename T>
T* define_configvar(const char* name, T* defaultValue, const char* description,
const char* category) {
IConfigVar* cfgVar =
new ConfigVar<T>(name, defaultValue, description, category);
AddConfigVar(cfgVar);
return defaultValue;
}
template <typename T>
T* define_cmdvar(const char* name, T* defaultValue, const char* description) {
ICommandVar* cmdVar = new CommandVar<T>(name, defaultValue, description);
AddCommandVar(cmdVar);
return defaultValue;
}
#define DEFINE_double(name, defaultValue, description, category) \
DEFINE_CVar(name, defaultValue, description, category, double)
#define DEFINE_int32(name, defaultValue, description, category) \
DEFINE_CVar(name, defaultValue, description, category, int32_t)
#define DEFINE_uint64(name, defaultValue, description, category) \
DEFINE_CVar(name, defaultValue, description, category, uint64_t)
#define DEFINE_string(name, defaultValue, description, category) \
DEFINE_CVar(name, defaultValue, description, category, std::string)
#define DEFINE_bool(name, defaultValue, description, category) \
DEFINE_CVar(name, defaultValue, description, category, bool)
#define DEFINE_CVar(name, defaultValue, description, category, type) \
namespace cvars { \
type name = defaultValue; \
} \
namespace cv { \
static auto cv_##name = \
cvar::define_configvar(#name, &cvars::name, description, category); \
}
// CmdVars can only be strings for now, we don't need any others
#define CmdVar(name, defaultValue, description) \
namespace cvars { \
std::string name = defaultValue; \
} \
namespace cv { \
static auto cv_##name = \
cvar::define_cmdvar(#name, &cvars::name, description); \
}
#define DECLARE_double(name) DECLARE_CVar(name, double)
#define DECLARE_bool(name) DECLARE_CVar(name, bool)
#define DECLARE_string(name) DECLARE_CVar(name, std::string)
#define DECLARE_int32(name) DECLARE_CVar(name, int32_t)
#define DECLARE_uint64(name) DECLARE_CVar(name, uint64_t)
#define DECLARE_CVar(name, type) \
namespace cvars { \
extern type name; \
}
} // namespace cvar
#endif // XENIA_CVAR_H_

View File

@@ -9,8 +9,6 @@
#include "xenia/base/logging.h"
#include <gflags/gflags.h>
#include <atomic>
#include <cinttypes>
#include <cstdarg>
@@ -19,6 +17,7 @@
#include <vector>
#include "xenia/base/atomic.h"
#include "xenia/base/cvar.h"
#include "xenia/base/debugging.h"
#include "xenia/base/filesystem.h"
#include "xenia/base/main.h"
@@ -26,6 +25,7 @@
#include "xenia/base/memory.h"
#include "xenia/base/ring_buffer.h"
#include "xenia/base/threading.h"
//#include "xenia/base/cvar.h"
// For MessageBox:
// TODO(benvanik): generic API? logging_win.cc?
@@ -35,12 +35,15 @@
DEFINE_string(
log_file, "",
"Logs are written to the given file (specify stdout for command line)");
DEFINE_bool(log_debugprint, false, "Dump the log to DebugPrint.");
DEFINE_bool(flush_log, true, "Flush log file after each log line batch.");
"Logs are written to the given file (specify stdout for command line)",
"Logging");
DEFINE_bool(log_debugprint, false, "Dump the log to DebugPrint.", "Logging");
DEFINE_bool(flush_log, true, "Flush log file after each log line batch.",
"Logging");
DEFINE_int32(
log_level, 2,
"Maximum level to be logged. (0=error, 1=warning, 2=info, 3=debug)");
"Maximum level to be logged. (0=error, 1=warning, 2=info, 3=debug)",
"Logging");
namespace xe {
@@ -52,16 +55,16 @@ thread_local std::vector<char> log_format_buffer_(64 * 1024);
class Logger {
public:
explicit Logger(const std::wstring& app_name) : running_(true) {
if (FLAGS_log_file.empty()) {
if (cvars::log_file.empty()) {
// Default to app name.
auto file_path = app_name + L".log";
xe::filesystem::CreateParentFolder(file_path);
file_ = xe::filesystem::OpenFile(file_path, "wt");
} else {
if (FLAGS_log_file == "stdout") {
if (cvars::log_file == "stdout") {
file_ = stdout;
} else {
auto file_path = xe::to_wstring(FLAGS_log_file.c_str());
auto file_path = xe::to_wstring(cvars::log_file);
xe::filesystem::CreateParentFolder(file_path);
file_ = xe::filesystem::OpenFile(file_path, "wt");
}
@@ -81,7 +84,7 @@ class Logger {
void AppendLine(uint32_t thread_id, LogLevel level, const char prefix_char,
const char* buffer, size_t buffer_length) {
if (static_cast<int32_t>(level) > FLAGS_log_level) {
if (static_cast<int32_t>(level) > cvars::log_level) {
// Discard this line.
return;
}
@@ -148,7 +151,7 @@ class Logger {
fwrite(buf, 1, size, file_);
}
if (FLAGS_log_debugprint) {
if (cvars::log_debugprint) {
debugging::DebugPrint("%.*s", size, buf);
}
}
@@ -214,7 +217,7 @@ class Logger {
read_head_ = rb.read_offset();
}
if (did_write) {
if (FLAGS_flush_log) {
if (cvars::flush_log) {
fflush(file_);
}

View File

@@ -7,10 +7,9 @@
******************************************************************************
*/
#include "xenia/base/cvar.h"
#include "xenia/base/main.h"
#include <gflags/gflags.h>
#include "xenia/base/logging.h"
#include "xenia/base/string.h"
@@ -23,10 +22,7 @@ bool has_console_attached() { return true; }
extern "C" int main(int argc, char** argv) {
auto entry_info = xe::GetEntryInfo();
google::SetUsageMessage(std::string("usage: ") +
xe::to_string(entry_info.usage));
google::SetVersionString("1.0");
google::ParseCommandLineFlags(&argc, &argv, true);
cvar::ParseLaunchArguments(argc, argv);
std::vector<std::wstring> args;
for (int n = 0; n < argc; n++) {
@@ -39,6 +35,5 @@ extern "C" int main(int argc, char** argv) {
// Call app-provided entry point.
int result = entry_info.entry_point(args);
google::ShutDownCommandLineFlags();
return result;
}

View File

@@ -10,7 +10,6 @@
#include "xenia/base/main.h"
#include <fcntl.h>
#include <gflags/gflags.h>
#include <io.h>
#include <cstdlib>
@@ -18,6 +17,7 @@
// Autogenerated by `xb premake`.
#include "build/version.h"
#include "xenia/base/filesystem.h"
#include "xenia/base/logging.h"
#include "xenia/base/platform_win.h"
#include "xenia/base/string.h"
@@ -25,9 +25,10 @@
#include "third_party/xbyak/xbyak/xbyak_util.h"
#include <bcrypt.h>
#include "xenia/base/cvar.h"
DEFINE_bool(win32_high_freq, true,
"Requests high performance from the NT kernel");
"Requests high performance from the NT kernel", "Kernel");
namespace xe {
@@ -88,7 +89,6 @@ int Main() {
auto entry_info = xe::GetEntryInfo();
// Convert command line to an argv-like format so we can share code/use
// gflags.
auto command_line = GetCommandLineW();
int argc;
wchar_t** argv = CommandLineToArgvW(command_line, &argc);
@@ -96,11 +96,7 @@ int Main() {
return 1;
}
google::SetUsageMessage(std::string("usage: ") +
xe::to_string(entry_info.usage));
google::SetVersionString("1.0");
// Convert all args to narrow, as gflags doesn't support wchar.
// Convert all args to narrow, as cxxopts doesn't support wchar.
int argca = argc;
char** argva = reinterpret_cast<char**>(alloca(sizeof(char*) * argca));
for (int n = 0; n < argca; n++) {
@@ -109,8 +105,7 @@ int Main() {
std::wcstombs(argva[n], argv[n], len + 1);
}
// Parse flags; this may delete some of them.
google::ParseCommandLineFlags(&argc, &argva, true);
cvar::ParseLaunchArguments(argca, argva);
// Widen all remaining flags and convert to usable strings.
std::vector<std::wstring> args;
@@ -143,7 +138,7 @@ int Main() {
XE_BUILD_DATE);
// Request high performance timing.
if (FLAGS_win32_high_freq) {
if (cvars::win32_high_freq) {
RequestHighPerformance();
}
@@ -151,7 +146,6 @@ int Main() {
int result = entry_info.entry_point(args);
xe::ShutdownLogging();
google::ShutDownCommandLineFlags();
LocalFree(argv);
return result;
}

View File

@@ -7,9 +7,6 @@ project("xenia-base")
language("C++")
defines({
})
includedirs({
project_root.."/third_party/gflags/src",
})
local_platform_files()
removefiles({"main_*.cc"})
files({

View File

@@ -7,8 +7,6 @@
******************************************************************************
*/
#include <gflags/gflags.h>
#include <string>
// NOTE: this must be included before microprofile as macro expansion needs
@@ -30,6 +28,7 @@
#include "third_party/microprofile/microprofile.h"
#include "xenia/base/assert.h"
#include "xenia/base/cvar.h"
#include "xenia/base/profiling.h"
#include "xenia/ui/window.h"
@@ -42,7 +41,7 @@
#include "xenia/ui/microprofile_drawer.h"
#endif // XE_OPTION_PROFILING_UI
DEFINE_bool(show_profiler, false, "Show profiling UI by default.");
DEFINE_bool(show_profiler, false, "Show profiling UI by default.", "Other");
namespace xe {
@@ -77,7 +76,7 @@ void Profiler::Initialize() {
g_MicroProfileUI.bShowSpikes = true;
g_MicroProfileUI.nOpacityBackground = 0x40u << 24;
g_MicroProfileUI.nOpacityForeground = 0xc0u << 24;
if (FLAGS_show_profiler) {
if (cvars::show_profiler) {
MicroProfileSetDisplayMode(1);
}
#else

View File

@@ -24,13 +24,13 @@ namespace string_util {
inline std::string to_hex_string(uint32_t value) {
char buffer[21];
std::snprintf(buffer, sizeof(buffer), "%08" PRIX32, value);
snprintf(buffer, sizeof(buffer), "%08" PRIX32, value);
return std::string(buffer);
}
inline std::string to_hex_string(uint64_t value) {
char buffer[21];
std::snprintf(buffer, sizeof(buffer), "%016" PRIX64, value);
snprintf(buffer, sizeof(buffer), "%016" PRIX64, value);
return std::string(buffer);
}
@@ -54,8 +54,8 @@ inline std::string to_hex_string(double value) {
inline std::string to_hex_string(const vec128_t& value) {
char buffer[128];
std::snprintf(buffer, sizeof(buffer), "[%.8X, %.8X, %.8X, %.8X]",
value.u32[0], value.u32[1], value.u32[2], value.u32[3]);
snprintf(buffer, sizeof(buffer), "[%.8X, %.8X, %.8X, %.8X]", value.u32[0],
value.u32[1], value.u32[2], value.u32[3]);
return std::string(buffer);
}
@@ -66,7 +66,7 @@ inline std::string to_hex_string(const __m128& value) {
char buffer[128];
float f[4];
_mm_storeu_ps(f, value);
std::snprintf(
snprintf(
buffer, sizeof(buffer), "[%.8X, %.8X, %.8X, %.8X]",
*reinterpret_cast<uint32_t*>(&f[0]), *reinterpret_cast<uint32_t*>(&f[1]),
*reinterpret_cast<uint32_t*>(&f[2]), *reinterpret_cast<uint32_t*>(&f[3]));
@@ -77,15 +77,22 @@ inline std::string to_string(const __m128& value) {
char buffer[128];
float f[4];
_mm_storeu_ps(f, value);
std::snprintf(buffer, sizeof(buffer), "(%F, %F, %F, %F)", f[0], f[1], f[2],
f[3]);
snprintf(buffer, sizeof(buffer), "(%F, %F, %F, %F)", f[0], f[1], f[2], f[3]);
return std::string(buffer);
}
#endif
template <typename T>
inline T from_string(const char* value, bool force_hex = false);
inline T from_string(const char* value, bool force_hex = false) {
// Missing implementation for converting type T to string
throw;
}
template <>
inline bool from_string<bool>(const char* value, bool force_hex) {
return std::strcmp(value, "true") == 0 || value[0] == '1';
}
template <>
inline int32_t from_string<int32_t>(const char* value, bool force_hex) {

View File

@@ -2,9 +2,6 @@ project_root = "../../../.."
include(project_root.."/tools/build")
test_suite("xenia-base-tests", project_root, ".", {
includedirs = {
project_root.."/third_party/gflags/src",
},
links = {
"xenia-base",
},

View File

@@ -266,8 +266,8 @@ static inline vec128_t vec128b(uint8_t x0, uint8_t x1, uint8_t x2, uint8_t x3,
inline std::string to_string(const vec128_t& value) {
char buffer[128];
std::snprintf(buffer, sizeof(buffer), "(%g, %g, %g, %g)", value.x, value.y,
value.z, value.w);
snprintf(buffer, sizeof(buffer), "(%g, %g, %g, %g)", value.x, value.y,
value.z, value.w);
return std::string(buffer);
}