36 lines
1.6 KiB
GDScript
36 lines
1.6 KiB
GDScript
|
|
extends RefCounted
|
||
|
|
const MAX_RESOURCE=10_000_000
|
||
|
|
# A save belongs to exactly one exported build. Settings use a separate file.
|
||
|
|
static func defaults(build_id: String) -> Dictionary:
|
||
|
|
return {"build_id":build_id,"coins":0,"essence":0,"done":0,"speed":0,"power":0,"auto":false,"development":{}}
|
||
|
|
|
||
|
|
static func decode(data, build_id: String) -> Dictionary:
|
||
|
|
var result=defaults(build_id)
|
||
|
|
if not data is Dictionary or data.get("build_id","")!=build_id:
|
||
|
|
return result
|
||
|
|
for key in ["coins","essence","done","speed","power"]:
|
||
|
|
var value=data.get(key,0)
|
||
|
|
if typeof(value) in [TYPE_INT,TYPE_FLOAT] and is_finite(float(value)):
|
||
|
|
var limit=3 if key=="speed" else (2 if key=="power" else MAX_RESOURCE)
|
||
|
|
result[key]=clampi(int(value),0,limit)
|
||
|
|
result["auto"]=data.get("auto",false)==true
|
||
|
|
var dev=data.get("development",{})
|
||
|
|
if dev is Dictionary:
|
||
|
|
for key in ["tier","selected","xp","points","action","leadership"]:
|
||
|
|
var value=dev.get(key,0)
|
||
|
|
if typeof(value) in [TYPE_INT,TYPE_FLOAT] and is_finite(float(value)):
|
||
|
|
result.development[key]=clampi(int(value),0,3 if key in ["tier","selected","action","leadership"] else MAX_RESOURCE)
|
||
|
|
var workshop=data.get("workshop",{})
|
||
|
|
if workshop is Dictionary:result["workshop"]=workshop
|
||
|
|
return result
|
||
|
|
|
||
|
|
static func read_save(path: String, build_id: String) -> Dictionary:
|
||
|
|
if not FileAccess.file_exists(path): return defaults(build_id)
|
||
|
|
return decode(JSON.parse_string(FileAccess.get_file_as_string(path)),build_id)
|
||
|
|
|
||
|
|
static func write_save(path: String, data: Dictionary) -> Error:
|
||
|
|
var file=FileAccess.open(path,FileAccess.WRITE)
|
||
|
|
if not file: return FileAccess.get_open_error()
|
||
|
|
file.store_string(JSON.stringify(data))
|
||
|
|
return OK
|