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