Skip to content

Commit 4018d23

Browse files
committed
Config: Finish wiring up Regex app overrides
This wasn't quite wired up exactly how we wanted it. It was previously matching against the opaque file config handle, which can be anything. Instead compare it to the appname that now gets passed over to it for matching. This allows us to do the following: ``` { "Config": { "ProfileStats": "1", "X87ReducedPrecision": "1", "TSOEnabled": "1", "VectorTSOEnabled": "0", "MemcpySetTSOEnabled": "0", "HalfBarrierTSOEnabled":"1", "MaxInst": "500", "Multiblock": "1" }, "AppOverrides" : { "setup*" : { "Comment": [ "292030 - The Witcher 3: Wild Hunt" ], "X87ReducedPrecision": "0" } } } ``` Based on #121 which needs to get merged first. Code Review Code Review: Class deletion
1 parent 81d4e8f commit 4018d23

3 files changed

Lines changed: 37 additions & 59 deletions

File tree

FEXCore/Source/Utils/WildcardMatcher.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,21 @@
33
#include <FEXCore/Utils/WildcardMatcher.h>
44

55
namespace FEXCore::Utils::Wildcard {
6-
class WildcardMatcher {
7-
private:
8-
std::string_view pattern;
9-
std::string_view text;
10-
11-
public:
12-
WildcardMatcher(std::string_view pattern, std::string_view text)
13-
: pattern(pattern)
14-
, text(text) {}
15-
16-
bool matchHelper(size_t p_idx, size_t t_idx);
17-
};
18-
19-
bool WildcardMatcher::matchHelper(size_t p_idx, size_t t_idx) {
6+
static bool matchHelper(std::string_view pattern, std::string_view text, size_t p_idx, size_t t_idx) {
207
if (p_idx == pattern.size()) {
218
// Pattern exhausted
229
return (t_idx == text.size());
2310
} else if (pattern[p_idx] == '*') {
2411
// Wildcard: Try matching zero characters, or one or more characters
25-
return matchHelper(p_idx + 1, t_idx) || (t_idx < text.size() && matchHelper(p_idx, t_idx + 1));
12+
return matchHelper(pattern, text, p_idx + 1, t_idx) || (t_idx < text.size() && matchHelper(pattern, text, p_idx, t_idx + 1));
2613
} else {
2714
// Match normally
28-
return (t_idx < text.size() && pattern[p_idx] == text[t_idx] && matchHelper(p_idx + 1, t_idx + 1));
15+
return (t_idx < text.size() && pattern[p_idx] == text[t_idx] && matchHelper(pattern, text, p_idx + 1, t_idx + 1));
2916
}
3017
}
3118

3219
bool Matches(std::string_view pattern, std::string_view text) {
33-
WildcardMatcher matcher(pattern, text);
34-
return matcher.matchHelper(0, 0);
20+
return matchHelper(pattern, text, 0, 0);
3521
}
3622

3723
} // namespace FEXCore::Utils::Wildcard

Source/Common/Config.cpp

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
namespace FEX::Config {
3131
namespace JSON {
32-
static void LoadJSonConfig(const fextl::string& Config, std::function<void(const char* Name, const char* ConfigString)> Func) {
32+
static void LoadJSonConfig(const fextl::string& Config, std::optional<fextl::string> AppName,
33+
std::function<void(const char* Name, const char* ConfigString)> Func) {
3334
fextl::vector<char> Data;
3435
if (!FEXCore::FileLoading::LoadFile(Data, Config)) {
3536
return;
@@ -52,26 +53,17 @@ namespace JSON {
5253
fextl::vector<const json_t*> ConfigBlocks;
5354
ConfigBlocks.push_back(ConfigList);
5455

55-
const json_t* OverrideList = json_getProperty(json, "AppOverrides");
56-
if (OverrideList) {
57-
for (const json_t* Item = json_getChild(OverrideList); Item != nullptr; Item = json_getSibling(Item)) {
58-
const char* ItemName = json_getName(Item);
59-
const json_t* OverrideNamedList = json_getProperty(OverrideList, ItemName);
60-
61-
if (!ItemName) {
62-
LogMan::Msg::EFmt("JSON file '{}': Couldn't get config name for an item", Config);
63-
break;
64-
}
65-
66-
if (!OverrideNamedList) {
67-
LogMan::Msg::EFmt("JSON file '{}': Couldn't get value for config item '{}'", Config, ItemName);
68-
break;
69-
}
70-
71-
// Find the first match, then break
72-
if (FEXCore::Utils::Wildcard::Matches(ItemName, Config)) {
73-
ConfigBlocks.push_back(OverrideNamedList);
74-
break;
56+
if (AppName) {
57+
const json_t* OverrideList = json_getProperty(json, "AppOverrides");
58+
if (OverrideList) {
59+
for (const json_t* Item = json_getChild(OverrideList); Item != nullptr; Item = json_getSibling(Item)) {
60+
const char* AppPattern = json_getName(Item);
61+
62+
// Find the first match, then break
63+
if (FEXCore::Utils::Wildcard::Matches(AppPattern, *AppName)) {
64+
ConfigBlocks.push_back(Item);
65+
break;
66+
}
7567
}
7668
}
7769
}
@@ -81,11 +73,6 @@ namespace JSON {
8173
const char* ConfigName = json_getName(ConfigItem);
8274
const char* ConfigString = json_getValue(ConfigItem);
8375

84-
if (!ConfigName) {
85-
LogMan::Msg::EFmt("JSON file '{}': Couldn't get config name for an item", Config);
86-
return;
87-
}
88-
8976
if (!ConfigString) {
9077
LogMan::Msg::EFmt("JSON file '{}': Couldn't get value for config item '{}'", Config, ConfigName);
9178
return;
@@ -196,22 +183,24 @@ class OptionMapper : public FEXCore::Config::Layer {
196183

197184
class MainLoader final : public OptionMapper {
198185
public:
199-
explicit MainLoader(FEXCore::Config::LayerType Type);
200-
explicit MainLoader(fextl::string ConfigFile);
186+
explicit MainLoader(FEXCore::Config::LayerType Type, std::optional<fextl::string> AppName = std::nullopt);
187+
explicit MainLoader(fextl::string ConfigFile, std::optional<fextl::string> AppName = std::nullopt);
201188
explicit MainLoader(FEXCore::Config::LayerType Type, std::string_view ConfigFile);
202189

203190
void Load() override;
204191

205192
private:
193+
std::optional<fextl::string> AppName;
206194
fextl::string Config;
207195
};
208196

209197
class AppLoader final : public OptionMapper {
210198
public:
211-
explicit AppLoader(const fextl::string& Filename, FEXCore::Config::LayerType Type);
199+
explicit AppLoader(const fextl::string& AppName, FEXCore::Config::LayerType Type);
212200
void Load();
213201

214202
private:
203+
const fextl::string AppName;
215204
fextl::string Config;
216205
};
217206

@@ -250,12 +239,14 @@ void OptionMapper::MapNameToOption(const char* ConfigName, const char* ConfigStr
250239
#include <FEXCore/Config/ConfigOptions.inl>
251240
}
252241

253-
MainLoader::MainLoader(FEXCore::Config::LayerType Type)
242+
MainLoader::MainLoader(FEXCore::Config::LayerType Type, std::optional<fextl::string> AppName)
254243
: OptionMapper(Type)
244+
, AppName {AppName}
255245
, Config {FEXCore::Config::GetConfigFileLocation(Type == FEXCore::Config::LayerType::LAYER_GLOBAL_MAIN)} {}
256246

257-
MainLoader::MainLoader(fextl::string ConfigFile)
247+
MainLoader::MainLoader(fextl::string ConfigFile, std::optional<fextl::string> AppName)
258248
: OptionMapper(FEXCore::Config::LayerType::LAYER_MAIN)
249+
, AppName {AppName}
259250
, Config {std::move(ConfigFile)} {}
260251

261252

@@ -265,21 +256,22 @@ MainLoader::MainLoader(FEXCore::Config::LayerType Type, std::string_view ConfigF
265256

266257
void MainLoader::Load() {
267258
SetCurrentConfigFile(Config);
268-
JSON::LoadJSonConfig(Config, [this](const char* Name, const char* ConfigString) { MapNameToOption(Name, ConfigString); });
259+
JSON::LoadJSonConfig(Config, AppName, [this](const char* Name, const char* ConfigString) { MapNameToOption(Name, ConfigString); });
269260
}
270261

271-
AppLoader::AppLoader(const fextl::string& Filename, FEXCore::Config::LayerType Type)
272-
: OptionMapper(Type) {
262+
AppLoader::AppLoader(const fextl::string& AppName, FEXCore::Config::LayerType Type)
263+
: OptionMapper(Type)
264+
, AppName {AppName} {
273265
const bool Global = Type == FEXCore::Config::LayerType::LAYER_GLOBAL_STEAM_APP || Type == FEXCore::Config::LayerType::LAYER_GLOBAL_APP;
274-
Config = FEXCore::Config::GetApplicationConfig(Filename, Global);
266+
Config = FEXCore::Config::GetApplicationConfig(AppName, Global);
275267

276268
// Immediately load so we can reload the meta layer
277269
Load();
278270
}
279271

280272
void AppLoader::Load() {
281273
SetCurrentConfigFile(Config);
282-
JSON::LoadJSonConfig(Config, [this](const char* Name, const char* ConfigString) { MapNameToOption(Name, ConfigString); });
274+
JSON::LoadJSonConfig(Config, AppName, [this](const char* Name, const char* ConfigString) { MapNameToOption(Name, ConfigString); });
283275
}
284276

285277
EnvLoader::EnvLoader(char* const _envp[])
@@ -349,11 +341,11 @@ fextl::unique_ptr<FEXCore::Config::Layer> CreateGlobalMainLayer() {
349341
return fextl::make_unique<MainLoader>(FEXCore::Config::LayerType::LAYER_GLOBAL_MAIN);
350342
}
351343

352-
fextl::unique_ptr<FEXCore::Config::Layer> CreateMainLayer(const fextl::string* File) {
344+
fextl::unique_ptr<FEXCore::Config::Layer> CreateMainLayer(const fextl::string* File, std::optional<fextl::string> AppName) {
353345
if (File) {
354-
return fextl::make_unique<MainLoader>(*File);
346+
return fextl::make_unique<MainLoader>(*File, std::move(AppName));
355347
} else {
356-
return fextl::make_unique<MainLoader>(FEXCore::Config::LayerType::LAYER_MAIN);
348+
return fextl::make_unique<MainLoader>(FEXCore::Config::LayerType::LAYER_MAIN, std::move(AppName));
357349
}
358350
}
359351

@@ -490,7 +482,7 @@ void LoadConfig(fextl::string ProgramName, char** const envp, const PortableInfo
490482
if (!IsPortable) {
491483
FEXCore::Config::AddLayer(CreateGlobalMainLayer());
492484
}
493-
FEXCore::Config::AddLayer(CreateMainLayer());
485+
FEXCore::Config::AddLayer(CreateMainLayer(nullptr, ProgramName.empty() ? std::nullopt : std::optional {ProgramName}));
494486

495487
if (!ProgramName.empty()) {
496488
if (!IsPortable) {

Source/Common/Config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fextl::unique_ptr<FEXCore::Config::Layer> CreateGlobalMainLayer();
8181
*
8282
* @return unique_ptr for that layer
8383
*/
84-
fextl::unique_ptr<FEXCore::Config::Layer> CreateMainLayer(const fextl::string* File = nullptr);
84+
fextl::unique_ptr<FEXCore::Config::Layer> CreateMainLayer(const fextl::string* File = nullptr, std::optional<fextl::string> AppName = std::nullopt);
8585
fextl::unique_ptr<FEXCore::Config::Layer> CreateUserOverrideLayer(std::string_view AppConfig);
8686

8787
/**

0 commit comments

Comments
 (0)