2020-01-29 20:25:13 +01:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
var _file = "user://savegame.json"
|
|
|
|
var state = {}
|
|
|
|
|
|
|
|
func _ready():
|
2020-08-24 16:48:30 +02:00
|
|
|
load_state()
|
|
|
|
|
2020-01-29 20:25:13 +01:00
|
|
|
func _initial_state():
|
2020-08-24 16:48:30 +02:00
|
|
|
return {}
|
|
|
|
|
2020-01-29 20:25:13 +01:00
|
|
|
func save_state() -> bool:
|
2020-08-24 16:48:30 +02:00
|
|
|
var savegame = File.new()
|
|
|
|
|
|
|
|
savegame.open(_file, File.WRITE)
|
|
|
|
savegame.store_line(to_json(state))
|
|
|
|
savegame.close()
|
|
|
|
return true
|
|
|
|
|
2020-01-29 20:25:13 +01:00
|
|
|
func load_state() -> bool:
|
2020-08-24 16:48:30 +02:00
|
|
|
var savegame = File.new()
|
|
|
|
if not savegame.file_exists(_file):
|
|
|
|
return false
|
|
|
|
|
|
|
|
savegame.open(_file, File.READ)
|
|
|
|
|
|
|
|
state = _initial_state()
|
|
|
|
var new_state = parse_json(savegame.get_line())
|
|
|
|
for key in new_state:
|
|
|
|
state[key] = new_state[key]
|
|
|
|
savegame.close()
|
|
|
|
return true
|
2020-09-01 18:26:43 +02:00
|
|
|
|
|
|
|
func run(command):
|
|
|
|
var output = []
|
|
|
|
OS.execute(command, [], true, output, true)
|
|
|
|
# Remove trailing newline.
|
|
|
|
return output[0].substr(0,len(output[0])-1)
|