2020-01-29 20:25:13 +01:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
var _file = "user://savegame.json"
|
|
|
|
var state = {}
|
|
|
|
|
|
|
|
func _ready():
|
2020-09-04 14:57:37 +02:00
|
|
|
load_state()
|
|
|
|
|
2020-01-29 20:25:13 +01:00
|
|
|
func _initial_state():
|
2020-09-04 14:57:37 +02:00
|
|
|
return {}
|
|
|
|
|
2020-01-29 20:25:13 +01:00
|
|
|
func save_state() -> bool:
|
2020-09-04 14:57:37 +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-09-04 14:57:37 +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
|
|
|
|
2020-09-01 19:20:51 +02:00
|
|
|
# Run a simple command given as a string, blocking, using execute.
|
2020-09-01 18:26:43 +02:00
|
|
|
func run(command):
|
2020-09-04 14:57:37 +02:00
|
|
|
print("run: "+command)
|
|
|
|
var output = []
|
|
|
|
OS.execute(command, [], true, output, true)
|
|
|
|
# Remove trailing newline.
|
|
|
|
return output[0].substr(0,len(output[0])-1)
|
2020-09-01 19:20:51 +02:00
|
|
|
|
|
|
|
func sh(command, wd="/tmp/"):
|
2020-09-04 14:57:37 +02:00
|
|
|
print("sh in "+wd+": "+command)
|
|
|
|
var cwd = game.run("pwd")
|
|
|
|
var output = []
|
|
|
|
|
|
|
|
var hacky_command = command
|
|
|
|
hacky_command = "cd '"+wd+"';"+hacky_command
|
|
|
|
hacky_command = "export EDITOR=fake-editor;"+hacky_command
|
|
|
|
hacky_command = "export PATH=\"$PATH\":"+cwd+"/scripts;"+hacky_command
|
|
|
|
OS.execute("/bin/sh", ["-c", hacky_command], true, output, true)
|
|
|
|
return output[0]
|
|
|
|
|
2020-09-04 11:40:42 +02:00
|
|
|
func script(filename, wd="/tmp/"):
|
2020-09-04 14:57:37 +02:00
|
|
|
print("sh script in "+wd+": "+filename)
|
|
|
|
var cwd = game.run("pwd")
|
|
|
|
var output = []
|
|
|
|
|
|
|
|
var hacky_command = "/bin/sh " + filename
|
|
|
|
hacky_command = "cd '"+wd+"';"+hacky_command
|
|
|
|
OS.execute("/bin/sh", ["-c", hacky_command], true, output, true)
|
|
|
|
return output[0]
|
2020-09-01 19:20:51 +02:00
|
|
|
|
|
|
|
func read_file(path):
|
2020-09-04 14:57:37 +02:00
|
|
|
print("read "+path)
|
|
|
|
var file = File.new()
|
|
|
|
file.open(path, File.READ)
|
|
|
|
var content = file.get_as_text()
|
|
|
|
file.close()
|
|
|
|
return content
|
2020-09-01 19:20:51 +02:00
|
|
|
|
|
|
|
func write_file(path, content):
|
2020-09-04 14:57:37 +02:00
|
|
|
var file = File.new()
|
|
|
|
file.open(path, File.WRITE)
|
|
|
|
file.store_string(content)
|
|
|
|
file.close()
|
|
|
|
return true
|