Skip to content

Commit bfa3e24

Browse files
committed
[Config] Gracefully handle type mismatches in cvars.
Detect cvar type mismatch and reset to default value, as well as show message to the user that the cvar from the config has been updated.
1 parent e342744 commit bfa3e24

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/xenia/base/cvar.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cxxopts::Options options("xenia", "Xbox 360 Emulator");
3030
std::map<std::string, ICommandVar*>* CmdVars;
3131
std::map<std::string, IConfigVar*>* ConfigVars;
3232
std::multimap<uint32_t, const IConfigVarUpdate*>* IConfigVarUpdate::updates_;
33+
std::vector<std::string>* config_type_mismatch_warnings = nullptr;
3334

3435
void PrintHelpAndExit() {
3536
std::cout << options.help({""}) << std::endl;

src/xenia/base/cvar.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ namespace toml_internal {
3333
std::string EscapeString(const std::string_view str);
3434
}
3535

36+
// Track config values that had type mismatches during loading
37+
extern std::vector<std::string>* config_type_mismatch_warnings;
38+
3639
class ICommandVar {
3740
public:
3841
virtual ~ICommandVar() = default;
@@ -147,7 +150,16 @@ inline void CommandVar<std::filesystem::path>::LoadFromLaunchOptions(
147150
}
148151
template <class T>
149152
void ConfigVar<T>::LoadConfigValue(const toml::node* result) {
150-
SetConfigValue(result->value<T>().value());
153+
auto value_opt = result->value<T>();
154+
if (value_opt) {
155+
SetConfigValue(value_opt.value());
156+
} else {
157+
// Type mismatch - track for warning
158+
if (!config_type_mismatch_warnings) {
159+
config_type_mismatch_warnings = new std::vector<std::string>();
160+
}
161+
config_type_mismatch_warnings->push_back(this->name_);
162+
}
151163
}
152164
template <>
153165
inline void ConfigVar<std::filesystem::path>::LoadConfigValue(
@@ -157,7 +169,16 @@ inline void ConfigVar<std::filesystem::path>::LoadConfigValue(
157169
}
158170
template <class T>
159171
void ConfigVar<T>::LoadGameConfigValue(const toml::node* result) {
160-
SetGameConfigValue(result->value<T>().value());
172+
auto value_opt = result->value<T>();
173+
if (value_opt) {
174+
SetGameConfigValue(value_opt.value());
175+
} else {
176+
// Type mismatch - track for warning
177+
if (!config_type_mismatch_warnings) {
178+
config_type_mismatch_warnings = new std::vector<std::string>();
179+
}
180+
config_type_mismatch_warnings->push_back(this->name_);
181+
}
161182
}
162183
template <>
163184
inline void ConfigVar<std::filesystem::path>::LoadGameConfigValue(

src/xenia/config.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "xenia/base/logging.h"
1717
#include "xenia/base/string.h"
1818
#include "xenia/base/string_buffer.h"
19+
#include "xenia/base/system.h"
1920

2021
toml::parse_result ParseFile(const std::filesystem::path& filename) {
2122
return toml::parse_file(xe::path_to_utf8(filename));
@@ -118,6 +119,26 @@ void ReadConfig(const std::filesystem::path& file_path,
118119
cvar::IConfigVarUpdate::ApplyUpdates(config_defaults_date);
119120
}
120121

122+
// Check for type mismatch warnings
123+
if (cvar::config_type_mismatch_warnings &&
124+
!cvar::config_type_mismatch_warnings->empty()) {
125+
std::string warning_message =
126+
"The following config values had invalid types and have been reset to "
127+
"defaults:\n\n";
128+
for (const auto& name : *cvar::config_type_mismatch_warnings) {
129+
warning_message += " - " + name + "\n";
130+
}
131+
warning_message +=
132+
"\nPlease check your config file. The config will be saved with the "
133+
"correct types.";
134+
135+
xe::ShowSimpleMessageBox(xe::SimpleMessageBoxType::Warning,
136+
warning_message);
137+
138+
// Clear warnings
139+
cvar::config_type_mismatch_warnings->clear();
140+
}
141+
121142
XELOGI("Loaded config: {}", file_path);
122143
}
123144

0 commit comments

Comments
 (0)