2020-01-29 20:25:13 +01:00
|
|
|
extends Node
|
|
|
|
|
2020-09-29 16:12:58 +02:00
|
|
|
var tmp_prefix_outside = _tmp_prefix_outside()
|
|
|
|
var tmp_prefix_inside = _tmp_prefix_inside()
|
2020-09-08 16:36:52 +02:00
|
|
|
var global_shell
|
2020-09-09 11:48:42 +02:00
|
|
|
var fake_editor
|
2020-01-29 20:25:13 +01:00
|
|
|
|
2020-09-28 16:18:06 +02:00
|
|
|
var _file = "user://savegame.json"
|
|
|
|
var state = {}
|
|
|
|
|
2020-01-29 20:25:13 +01:00
|
|
|
func _ready():
|
2020-09-29 16:12:58 +02:00
|
|
|
var dir = Directory.new()
|
|
|
|
if not dir.dir_exists(tmp_prefix_outside):
|
|
|
|
var err = dir.make_dir(tmp_prefix_outside)
|
|
|
|
if err != OK:
|
|
|
|
helpers.crash("Could not create temporary directory %s." % tmp_prefix_outside)
|
|
|
|
if not dir.dir_exists(tmp_prefix_outside+"/repos/"):
|
|
|
|
var err = dir.make_dir(tmp_prefix_outside+"/repos/")
|
|
|
|
if err != OK:
|
|
|
|
helpers.crash("Could not create temporary directory %s." % (tmp_prefix_outside+"/repos/"))
|
|
|
|
|
2020-09-08 16:36:52 +02:00
|
|
|
global_shell = Shell.new()
|
2020-09-22 13:15:36 +02:00
|
|
|
fake_editor = copy_file_to_game_env("fake-editor")
|
|
|
|
copy_file_to_game_env("fake-editor-noblock")
|
2020-09-28 16:18:06 +02:00
|
|
|
load_state()
|
|
|
|
|
|
|
|
func _initial_state():
|
|
|
|
return {"history": []}
|
|
|
|
|
2020-09-29 17:52:31 +02:00
|
|
|
func save_state():
|
2020-09-28 16:18:06 +02:00
|
|
|
var savegame = File.new()
|
|
|
|
|
|
|
|
savegame.open(_file, File.WRITE)
|
|
|
|
savegame.store_line(to_json(state))
|
|
|
|
savegame.close()
|
|
|
|
|
2020-09-29 17:52:31 +02:00
|
|
|
func load_state():
|
2020-09-28 16:18:06 +02:00
|
|
|
var savegame = File.new()
|
|
|
|
if not savegame.file_exists(_file):
|
|
|
|
save_state()
|
|
|
|
|
|
|
|
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()
|
2020-09-22 13:15:36 +02:00
|
|
|
|
|
|
|
func copy_file_to_game_env(filename):
|
2020-09-09 11:48:42 +02:00
|
|
|
# Copy fake-editor to tmp directory (because the original might be in a .pck file).
|
2020-09-29 16:12:58 +02:00
|
|
|
var file_outside = tmp_prefix_outside + filename
|
|
|
|
var file_inside = tmp_prefix_inside + filename
|
2020-09-29 14:53:00 +02:00
|
|
|
var content = helpers.read_file("res://scripts/"+filename)
|
|
|
|
helpers.write_file(file_outside, content)
|
2020-10-06 16:50:31 +02:00
|
|
|
global_shell.run("chmod u+x " + '"'+file_inside+'"')
|
2020-09-22 13:15:36 +02:00
|
|
|
return file_inside
|
2020-09-01 19:20:51 +02:00
|
|
|
|
2020-09-29 16:12:58 +02:00
|
|
|
func _tmp_prefix_inside():
|
2020-09-09 11:48:42 +02:00
|
|
|
var os = OS.get_name()
|
2020-09-29 16:12:58 +02:00
|
|
|
var path
|
2020-10-06 16:50:31 +02:00
|
|
|
if os == "X11" or os == "OSX":
|
2020-09-29 16:12:58 +02:00
|
|
|
path = OS.get_user_data_dir()
|
2020-09-09 17:59:56 +02:00
|
|
|
elif os == "Windows":
|
2020-09-29 16:12:58 +02:00
|
|
|
helpers.crash("Need to figure out Windows tmp_prefix_inside...")
|
2020-09-09 17:59:56 +02:00
|
|
|
else:
|
2020-09-29 14:53:00 +02:00
|
|
|
helpers.crash("Unsupported OS: %s" % os)
|
2020-09-29 16:12:58 +02:00
|
|
|
|
|
|
|
return path + "/tmp/"
|
|
|
|
|
|
|
|
func _tmp_prefix_outside():
|
|
|
|
return "user://tmp/"
|