oh-my-git/game.gd

73 lines
1.9 KiB
GDScript3
Raw Permalink Normal View History

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()
var global_shell
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/"))
global_shell = Shell.new()
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": []}
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()
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()
func copy_file_to_game_env(filename):
# 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
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+'"')
return file_inside
2020-09-29 16:12:58 +02:00
func _tmp_prefix_inside():
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()
elif os == "Windows":
2020-09-29 16:12:58 +02:00
helpers.crash("Need to figure out Windows tmp_prefix_inside...")
else:
helpers.crash("Unsupported OS: %s" % os)
2020-09-29 16:12:58 +02:00
return path + "/tmp/"
func _tmp_prefix_outside():
return "user://tmp/"