make all targets compile text-based pref handling in prefs.cpp
mac replays use preference std::map
This commit is contained in:
@@ -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" />
|
||||
|
@@ -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">
|
||||
|
@@ -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" />
|
||||
|
@@ -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">
|
||||
|
@@ -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 */; };
|
||||
@@ -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>"; };
|
||||
@@ -1277,6 +1279,7 @@
|
||||
9185BDA11EA041570027C346 /* tools */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
415EEEAF2D5534A500B47408 /* prefs.cpp */,
|
||||
413FE08E2CECFAFF000D97DC /* winutil.cpp */,
|
||||
9103DC652C6A406600849E60 /* cli.hpp */,
|
||||
D384F6A22C1B9D4000A806C3 /* replay.cpp */,
|
||||
@@ -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 */,
|
||||
|
@@ -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>"; };
|
||||
@@ -1216,6 +1217,7 @@
|
||||
9185BDA11EA041570027C346 /* tools */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
415EEEB12D5595B300B47408 /* prefs.cpp */,
|
||||
413FE08D2CECFAA4000D97DC /* winutil.cpp */,
|
||||
D384F6A12C1B9CE900A806C3 /* replay.cpp */,
|
||||
91C688E70FD702B9000F6D01 /* cursors.mac.mm */,
|
||||
|
@@ -8,6 +8,7 @@ tools = Split("""
|
||||
keymods.cpp
|
||||
replay.cpp
|
||||
winutil.cpp
|
||||
prefs.cpp
|
||||
../alchemy.cpp
|
||||
../damage.cpp
|
||||
../enchant.cpp
|
||||
@@ -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)
|
||||
|
||||
|
@@ -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};
|
||||
@@ -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);
|
||||
@@ -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);
|
@@ -39,11 +39,6 @@ NSDictionary* prefsToRecord = @{
|
||||
@"UIScale": @(kFloat),
|
||||
@"UIScaleMap": @(kFloat)
|
||||
};
|
||||
// Some legacy preferences influenced RNG and must be
|
||||
// known by replays
|
||||
NSDictionary* prefsToReplay = @{
|
||||
@"DrawTerrainFrills": @(kBool)
|
||||
};
|
||||
|
||||
bool prefsLoaded = false;
|
||||
|
||||
@@ -62,44 +57,40 @@ static NSString* convertValue(std::string value) {
|
||||
}
|
||||
|
||||
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];
|
||||
@@ -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;
|
||||
@@ -117,91 +108,23 @@ std::vector<int> get_iarray_pref(std::string keypath) {
|
||||
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];
|
||||
@@ -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];
|
||||
}
|
||||
|
Reference in New Issue
Block a user