Skip to content

Commit

Permalink
make all targets compile text-based pref handling in prefs.cpp
Browse files Browse the repository at this point in the history
mac replays use preference std::map
  • Loading branch information
NQNStudios authored and CelticMinstrel committed Feb 14, 2025
1 parent 1d25b44 commit 3bd8874
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 100 deletions.
2 changes: 1 addition & 1 deletion proj/vs2013/Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
<ClCompile Include="..\..\..\src\tools\cursors.win.cpp" />
<ClCompile Include="..\..\..\src\tools\drawable_manager.cpp" />
<ClCompile Include="..\..\..\src\tools\menu_accel.win.cpp" />
<ClCompile Include="..\..\..\src\tools\prefs.win.cpp" />
<ClCompile Include="..\..\..\src\tools\prefs.cpp" />
<ClCompile Include="..\..\..\src\tools\replay.cpp" />
<ClCompile Include="..\..\..\src\tools\undo.cpp" />
<ClCompile Include="..\..\..\src\tools\keymods.cpp" />
Expand Down
2 changes: 1 addition & 1 deletion proj/vs2013/Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@
<ClCompile Include="..\..\..\src\tools\undo.cpp">
<Filter>Tools</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\tools\prefs.win.cpp">
<ClCompile Include="..\..\..\src\tools\prefs.cpp">
<Filter>Tools</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\tools\menu_accel.win.cpp">
Expand Down
2 changes: 1 addition & 1 deletion proj/vs2017/Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@
<ClCompile Include="..\..\..\src\tools\drawable_manager.cpp" />
<ClCompile Include="..\..\..\src\tools\framerate_limiter.cpp" />
<ClCompile Include="..\..\..\src\tools\menu_accel.win.cpp" />
<ClCompile Include="..\..\..\src\tools\prefs.win.cpp" />
<ClCompile Include="..\..\..\src\tools\prefs.cpp" />
<ClCompile Include="..\..\..\src\tools\replay.cpp" />
<ClCompile Include="..\..\..\src\tools\undo.cpp" />
<ClCompile Include="..\..\..\src\tools\keymods.cpp" />
Expand Down
2 changes: 1 addition & 1 deletion proj/vs2017/Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@
<ClCompile Include="..\..\..\src\tools\menu_accel.win.cpp">
<Filter>Tools</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\tools\prefs.win.cpp">
<ClCompile Include="..\..\..\src\tools\prefs.cpp">
<Filter>Tools</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\tools\undo.cpp">
Expand Down
4 changes: 4 additions & 0 deletions proj/xc12/BoE.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
413AAF662D38A4A0002E9BF1 /* creature.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 914698FE1A747C4500F20F5E /* creature.cpp */; };
413AAF672D38A4A5002E9BF1 /* living.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 914698FB1A7362D900F20F5E /* living.cpp */; };
413FE08F2CECFAFF000D97DC /* winutil.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 413FE08E2CECFAFF000D97DC /* winutil.cpp */; };
415EEEB02D5534A500B47408 /* prefs.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 415EEEAF2D5534A500B47408 /* prefs.cpp */; };
91034D211B225E4A008F01C1 /* scen.appleevents.mm in Sources */ = {isa = PBXBuildFile; fileRef = 91034D201B225E49008F01C1 /* scen.appleevents.mm */; };
911A14031B8FAFC600900FD9 /* town_read.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91C2A6EC1B8FA91400346948 /* town_read.cpp */; };
911A14041B8FB00300900FD9 /* talk_read.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91C2A6EE1B8FAA8E00346948 /* talk_read.cpp */; };
Expand Down Expand Up @@ -616,6 +617,7 @@
2BF04B090BF51924006C0831 /* boe.town.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = boe.town.cpp; sourceTree = "<group>"; wrapsLines = 1; };
2BF04B0A0BF51924006C0831 /* boe.town.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = boe.town.hpp; sourceTree = "<group>"; };
413FE08E2CECFAFF000D97DC /* winutil.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = winutil.cpp; sourceTree = "<group>"; };
415EEEAF2D5534A500B47408 /* prefs.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = prefs.cpp; sourceTree = "<group>"; };
91034D201B225E49008F01C1 /* scen.appleevents.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = scen.appleevents.mm; sourceTree = "<group>"; };
9103DC652C6A406600849E60 /* cli.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = cli.hpp; sourceTree = "<group>"; };
910BBA170FB8BECA001E34EA /* dialog.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = dialog.hpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1277,6 +1279,7 @@
9185BDA11EA041570027C346 /* tools */ = {
isa = PBXGroup;
children = (
415EEEAF2D5534A500B47408 /* prefs.cpp */,
413FE08E2CECFAFF000D97DC /* winutil.cpp */,
9103DC652C6A406600849E60 /* cli.hpp */,
D384F6A22C1B9D4000A806C3 /* replay.cpp */,
Expand Down Expand Up @@ -2093,6 +2096,7 @@
919CC26E1B3773E000273FDA /* pict.cpp in Sources */,
919CC26F1B3773E700273FDA /* stack.cpp in Sources */,
915473C62C7FCDBB00EB1C94 /* alchemy.cpp in Sources */,
415EEEB02D5534A500B47408 /* prefs.cpp in Sources */,
919CC2701B3773EC00273FDA /* scrollbar.cpp in Sources */,
919CC2711B3773F300273FDA /* cursors.mac.mm in Sources */,
919CC2721B3773F800273FDA /* fileio.cpp in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions proj/xc4/BoE.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@
2BF04B090BF51924006C0831 /* boe.town.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = boe.town.cpp; sourceTree = "<group>"; wrapsLines = 1; };
2BF04B0A0BF51924006C0831 /* boe.town.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = boe.town.hpp; sourceTree = "<group>"; };
413FE08D2CECFAA4000D97DC /* winutil.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = winutil.cpp; sourceTree = "<group>"; };
415EEEB12D5595B300B47408 /* prefs.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = prefs.cpp; sourceTree = "<group>"; };
91034D201B225E49008F01C1 /* scen.appleevents.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = scen.appleevents.mm; sourceTree = "<group>"; };
910BBA170FB8BECA001E34EA /* dialog.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = dialog.hpp; sourceTree = "<group>"; };
910BBA180FB8BECA001E34EA /* dialog.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = dialog.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1216,6 +1217,7 @@
9185BDA11EA041570027C346 /* tools */ = {
isa = PBXGroup;
children = (
415EEEB12D5595B300B47408 /* prefs.cpp */,
413FE08D2CECFAA4000D97DC /* winutil.cpp */,
D384F6A12C1B9CE900A806C3 /* replay.cpp */,
91C688E70FD702B9000F6D01 /* cursors.mac.mm */,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tools = Split("""
keymods.cpp
replay.cpp
winutil.cpp
prefs.cpp
../alchemy.cpp
../damage.cpp
../enchant.cpp
Expand All @@ -34,7 +35,6 @@ elif str(platform) == "win32":
tools.extend(Glob("../fileio/*.win.cpp"))
elif str(platform) == "posix":
tools.extend(Glob("*.linux.cpp"))
tools.append("prefs.win.cpp")

tools_obj = env.StaticLibrary("#build/lib/tools", tools)

Expand Down
43 changes: 43 additions & 0 deletions src/tools/prefs.win.cpp → src/tools/prefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,61 @@ std::map<std::string,boost::any> prefs;
using iarray = std::vector<int>;
static bool prefsLoaded = false, prefsDirty = false;

// These can make the use of platform-specific overloads DRY
#ifdef SFML_SYSTEM_MACOS
#define SET_PREF_MAC(type) void set_pref_mac(std::string keypath, type value); if(!was_replaying){ set_pref_mac(keypath, value); return; }
#define GET_PREF_MAC(type) type get_##type##_pref_mac(std::string keypath, type fallback); if(!was_replaying){ return get_##type##_pref_mac(keypath, fallback); }
#else
#define SET_PREF_MAC(type)
#define GET_PREF_MAC(type)
#endif

void set_pref(std::string keypath, bool value) {
SET_PREF_MAC(bool);
prefsDirty = true;
prefs[keypath] = value;
}

bool get_bool_pref(std::string keypath, bool fallback) {
GET_PREF_MAC(bool);
if(prefs.find(keypath) == prefs.end()) return fallback;
if(prefs[keypath].type() == typeid(bool)) return boost::any_cast<bool>(prefs[keypath]);
return fallback;
}

void set_pref(std::string keypath, int value) {
SET_PREF_MAC(int);
prefsDirty = true;
prefs[keypath] = value;
}

int get_int_pref(std::string keypath, int fallback) {
GET_PREF_MAC(int);
if(prefs.find(keypath) == prefs.end()) return fallback;
if(prefs[keypath].type() == typeid(int)) return boost::any_cast<int>(prefs[keypath]);
return fallback;
}

void set_pref(std::string keypath, double value) {
SET_PREF_MAC(double);
prefsDirty = true;
prefs[keypath] = value;
}

double get_float_pref(std::string keypath, double fallback) {
GET_PREF_MAC(double);
if(prefs.find(keypath) == prefs.end()) return fallback;
if(prefs[keypath].type() == typeid(double)) return boost::any_cast<double>(prefs[keypath]);
return fallback;
}

void append_iarray_pref(std::string keypath, int value) {
#ifdef SFML_SYSTEM_MACOS
void append_iarray_pref_mac(std::string keypath, int value);
if(!was_replaying){
append_iarray_pref_mac(keypath, value);
}
#endif
prefsDirty = true;
if(prefs.find(keypath) == prefs.end() || prefs[keypath].type() != typeid(iarray))
prefs[keypath] = iarray{value};
Expand All @@ -62,23 +83,39 @@ void append_iarray_pref(std::string keypath, int value) {
}

std::vector<int> get_iarray_pref(std::string keypath) {
#ifdef SFML_SYSTEM_MACOS
std::vector<int> get_iarray_pref_mac(std::string keypath);
if(!was_replaying){
return get_iarray_pref_mac(keypath);
}
#endif
if(prefs.find(keypath) == prefs.end()) return {};
if(prefs[keypath].type() == typeid(iarray)) return boost::any_cast<iarray&>(prefs[keypath]);
return {};
}

void set_pref(std::string keypath, std::string value) {
SET_PREF_MAC(std::string);
prefsDirty = true;
prefs[keypath] = value;
}

std::string get_string_pref(std::string keypath, std::string fallback) {
using std::string;
GET_PREF_MAC(string);
if(prefs.find(keypath) == prefs.end()) return fallback;
if(prefs[keypath].type() == typeid(std::string)) return boost::any_cast<std::string>(prefs[keypath]);
return fallback;
}

void clear_pref(std::string keypath) {
#ifdef SFML_SYSTEM_MACOS
void clear_pref_mac(std::string keypath);
if(!was_replaying){
clear_pref_mac(keypath);
return;
}
#endif
prefsDirty = true;
auto iter = prefs.find(keypath);
if(iter != prefs.end()) prefs.erase(iter);
Expand Down Expand Up @@ -190,6 +227,12 @@ static bool load_prefs(fs::path fpath) {

extern fs::path tempDir;
bool sync_prefs() {
#ifdef SFML_SYSTEM_MACOS
bool sync_prefs_mac();
if(!was_replaying){
return sync_prefs_mac();
}
#endif
fs::path prefsPath = tempDir.parent_path() / "bladesprefs.ini";
if(prefsLoaded) return save_prefs(prefsPath);
else return load_prefs(prefsPath);
Expand Down
108 changes: 13 additions & 95 deletions src/tools/prefs.mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ typedef NS_ENUM(NSInteger) {
@"UIScale": @(kFloat),
@"UIScaleMap": @(kFloat)
};
// Some legacy preferences influenced RNG and must be
// known by replays
NSDictionary* prefsToReplay = @{
@"DrawTerrainFrills": @(kBool)
};

bool prefsLoaded = false;

Expand All @@ -62,44 +57,40 @@ typedef NS_ENUM(NSInteger) {
}

static NSUserDefaults* getCurrentDefaults() {
if(replaying){
return replayUserDefaults;
}else{
return [NSUserDefaults standardUserDefaults];
}
return [NSUserDefaults standardUserDefaults];
}

void set_pref(std::string keypath, bool value) {
void set_pref_mac(std::string keypath, bool value) {
[getCurrentDefaults() setBool: value forKey: convertKey(keypath)];
}

bool get_bool_pref(std::string keypath, bool fallback) {
bool get_bool_pref_mac(std::string keypath, bool fallback) {
id val = [getCurrentDefaults() objectForKey: convertKey(keypath)];
if([val isKindOfClass: [NSNumber class]]) return [val boolValue];
return fallback;
}

void set_pref(std::string keypath, int value) {
void set_pref_mac(std::string keypath, int value) {
[getCurrentDefaults() setInteger: value forKey: convertKey(keypath)];
}

int get_int_pref(std::string keypath, int fallback) {
int get_int_pref_mac(std::string keypath, int fallback) {
id val = [getCurrentDefaults() objectForKey: convertKey(keypath)];
if([val isKindOfClass: [NSNumber class]]) return [val intValue];
return fallback;
}

void set_pref(std::string keypath, double value) {
void set_pref_mac(std::string keypath, double value) {
[getCurrentDefaults() setDouble: value forKey: convertKey(keypath)];
}

double get_float_pref(std::string keypath, double fallback) {
double get_double_pref_mac(std::string keypath, double fallback) {
id val = [getCurrentDefaults() objectForKey: convertKey(keypath)];
if([val isKindOfClass: [NSNumber class]]) return [val doubleValue];
return fallback;
}

void append_iarray_pref(std::string keypath, int value) {
void append_iarray_pref_mac(std::string keypath, int value) {
NSString* key = convertKey(keypath);
NSArray* list = [getCurrentDefaults() arrayForKey: key];
NSNumber* num = [NSNumber numberWithInt: value];
Expand All @@ -108,7 +99,7 @@ void append_iarray_pref(std::string keypath, int value) {
else [getCurrentDefaults() setObject: [list arrayByAddingObject: num] forKey: key];
}

std::vector<int> get_iarray_pref(std::string keypath) {
std::vector<int> get_iarray_pref_mac(std::string keypath) {
NSArray* list = [getCurrentDefaults() arrayForKey: convertKey(keypath)];
if(list == nil) return {};
std::vector<int> result;
Expand All @@ -117,91 +108,23 @@ void append_iarray_pref(std::string keypath, int value) {
return result;
}

void set_pref(std::string keypath, std::string val) {
void set_pref_mac(std::string keypath, std::string val) {
NSString* key = convertKey(keypath);
NSString* value = convertValue(val);
[getCurrentDefaults() setObject: value forKey: key];
}

std::string get_string_pref(std::string keypath, std::string fallback) {
std::string get_string_pref_mac(std::string keypath, std::string fallback) {
NSString* val = [getCurrentDefaults() stringForKey: convertKey(keypath)];
if(val == nil) return fallback;
return convertValue(val);
}

void clear_pref(std::string keypath) {
void clear_pref_mac(std::string keypath) {
[getCurrentDefaults() setValue: nil forKey: convertKey(keypath)];
}

// When replaying, load the preferences from the action log into a
// non-synchronized UserDefaults object
static bool load_prefs(std::istream& istream) {
std::string line;
while(std::getline(istream, line)) {
if(!istream) {
perror("Error reading preferences");
return false;
}
if(line[0] == '#') continue;
int eq = line.find_first_of('=');
if(eq == std::string::npos) {
printf("Error reading preferences: line is not a comment and lacks an = sign:\n\t%s\n", line.c_str());
return false;
}
int key_end = line.find_last_not_of(' ', eq - 1), val_beg = line.find_first_not_of(' ', eq + 1);
if(val_beg == std::string::npos) {
printf("Error reading preferences: line is missing value:\n\t%s\n", line.c_str());
return false;
}
if(key_end == std::string::npos) {
printf("Error reading preferences: line is missing key:\n\t%s\n", line.c_str());
return false;
}

std::string key = line.substr(0, key_end + 1), val = line.substr(val_beg);
NSString* pref_key = [NSString stringWithUTF8String: key.c_str()];
NSInteger type;
// Skip obsolete preferences from legacy replays
if([prefsToRecord valueForKey: pref_key] == nil){
if([prefsToReplay valueForKey: pref_key] == nil){
continue;
}else{
type = [prefsToReplay[pref_key] integerValue];
}
}else{
type = [prefsToRecord[pref_key] integerValue];
}
switch((int)type) {
case kBool:
if(val == "true") set_pref(key, true);
else if(val == "false") set_pref(key, false);
break;
case kIArray:
if(val[0] == '[') {
int arr_end = val.find_first_of(']');
std::istringstream arr_vals(val.substr(1, arr_end - 1));
int i = 0;
clear_pref(key);
while(arr_vals >> i) append_iarray_pref(key, i);
}
break;
case kFloat:
set_pref(key, boost::lexical_cast<double>(val));
break;
case kInt:
set_pref(key, boost::lexical_cast<int>(val));
break;
// NOTE: The core game currently has no string preferences, so the recording system doesn't need to know
// about them for now.
case kString: break;
}
}

prefsLoaded = true;
return true;
}

bool sync_prefs() {
bool sync_prefs_mac() {
if(recording && !prefsLoaded){
std::ostringstream prefs_recording;
NSUserDefaults* standard = [NSUserDefaults standardUserDefaults];
Expand Down Expand Up @@ -245,11 +168,6 @@ bool sync_prefs() {
}
record_action("load_prefs", prefs_recording.str(), true);
prefsLoaded = true;
}else if(replaying && !prefsLoaded){
replayUserDefaults = [[NSUserDefaults alloc] init];
Element& prefs_action = pop_next_action("load_prefs");
std::istringstream istream(prefs_action.GetText());
return load_prefs(istream);
}
return [[NSUserDefaults standardUserDefaults] synchronize];
}
Expand Down

0 comments on commit 3bd8874

Please sign in to comment.