Put temp files in user://tmp/

This commit is contained in:
Sebastian Morr 2020-09-29 16:12:58 +02:00
parent 44214d2fdf
commit 78f324bf68
8 changed files with 59 additions and 31 deletions

View file

@ -28,7 +28,7 @@ func _on_item_selected():
var item = $FileTree.get_selected() var item = $FileTree.get_selected()
var file_path = item.get_text(0) var file_path = item.get_text(0)
shell.run("/tmp/fake-editor-noblock "+file_path) shell.run("%s/fake-editor-noblock %s" % [game.tmp_prefix_inside, file_path])
func very_best_sort(a,b): func very_best_sort(a,b):
# We're looking at the third character because all entries have the form # We're looking at the third character because all entries have the form

31
game.gd
View file

@ -1,6 +1,7 @@
extends Node extends Node
var tmp_prefix = _tmp_prefix() var tmp_prefix_outside = _tmp_prefix_outside()
var tmp_prefix_inside = _tmp_prefix_inside()
var global_shell var global_shell
var fake_editor var fake_editor
@ -8,6 +9,16 @@ var _file = "user://savegame.json"
var state = {} var state = {}
func _ready(): func _ready():
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() global_shell = Shell.new()
fake_editor = copy_file_to_game_env("fake-editor") fake_editor = copy_file_to_game_env("fake-editor")
copy_file_to_game_env("fake-editor-noblock") copy_file_to_game_env("fake-editor-noblock")
@ -40,20 +51,24 @@ func load_state() -> bool:
func copy_file_to_game_env(filename): func copy_file_to_game_env(filename):
# Copy fake-editor to tmp directory (because the original might be in a .pck file). # Copy fake-editor to tmp directory (because the original might be in a .pck file).
var file_outside = tmp_prefix + filename var file_outside = tmp_prefix_outside + filename
var file_inside = "/tmp/"+filename var file_inside = tmp_prefix_inside + filename
var content = helpers.read_file("res://scripts/"+filename) var content = helpers.read_file("res://scripts/"+filename)
helpers.write_file(file_outside, content) helpers.write_file(file_outside, content)
global_shell.run("chmod u+x " + file_inside) global_shell.run("chmod u+x " + file_inside)
return file_inside return file_inside
func _tmp_prefix(): func _tmp_prefix_inside():
var os = OS.get_name() var os = OS.get_name()
var path
if os == "X11": if os == "X11":
return "/tmp/" path = OS.get_user_data_dir()
elif os == "Windows": elif os == "Windows":
# For some reason, this command outputs a space in the end? We remove it. helpers.crash("Need to figure out Windows tmp_prefix_inside...")
# Also, Godot's default is to use forward slashes for everything.
return helpers.exec("echo", ["%TEMP%"]).replacen("\\", "/").replace(" \n", "/")
else: else:
helpers.crash("Unsupported OS: %s" % os) helpers.crash("Unsupported OS: %s" % os)
return path + "/tmp/"
func _tmp_prefix_outside():
return "user://tmp/"

View file

@ -5,8 +5,10 @@ var debug_file_io = false
# Crash the game and display the error message. # Crash the game and display the error message.
func crash(message): func crash(message):
push_error(message) push_error(message)
print("Fatal error: " + message) print("FATAL ERROR: " + message)
get_tree().quit() get_tree().quit()
# Oh, still here? Let's crash more violently, by calling a non-existing method.
get_tree().fatal_error()
# Run a simple command with arguments, blocking, using OS.execute. # Run a simple command with arguments, blocking, using OS.execute.
func exec(command, args=[], crash_on_fail=true): func exec(command, args=[], crash_on_fail=true):

40
main.gd
View file

@ -99,8 +99,8 @@ func load_level(id):
var level = levels[id] var level = levels[id]
var level_prefix = "res://levels/%s/" % chapter var level_prefix = "res://levels/%s/" % chapter
var goal_repository_path = "/tmp/goal/" var goal_repository_path = game.tmp_prefix_inside+"/repos/goal/"
var active_repository_path = "/tmp/active/" var active_repository_path = game.tmp_prefix_inside+"/repos/active/"
var goal_script = level_prefix+level+"/goal" var goal_script = level_prefix+level+"/goal"
var active_script = level_prefix+level+"/start" var active_script = level_prefix+level+"/start"
@ -121,15 +121,8 @@ func load_level(id):
# We're actually destroying stuff here. # We're actually destroying stuff here.
# Make sure that active_repository is in a temporary directory. # Make sure that active_repository is in a temporary directory.
var expected_prefix = "/tmp" _careful_delete(active_repository_path)
if active_repository_path.substr(0,4) != expected_prefix: _careful_delete(goal_repository_path)
helpers.crash("Refusing to delete directory %s that does not start with %s" % [active_repository_path, expected_prefix])
if goal_repository_path.substr(0,4) != expected_prefix:
helpers.crash("Refusing to delete directory %s that does not start with %s" % [goal_repository_path, expected_prefix])
# Danger zone!
game.global_shell.run("rm -rf '%s'" % active_repository_path)
game.global_shell.run("rm -rf '%s'" % goal_repository_path)
var goal_script_content = helpers.read_file(goal_script, "") var goal_script_content = helpers.read_file(goal_script, "")
var active_script_content = helpers.read_file(active_script, "") var active_script_content = helpers.read_file(active_script, "")
@ -140,7 +133,7 @@ func load_level(id):
active_repository.path = active_repository_path active_repository.path = active_repository_path
var win_script = level_prefix+level+"/win" var win_script = level_prefix+level+"/win"
var win_script_target = game.tmp_prefix+"/win" var win_script_target = game.tmp_prefix_outside+"/win"
var win_script_content = helpers.read_file(win_script, "exit 1\n") var win_script_content = helpers.read_file(win_script, "exit 1\n")
helpers.write_file(win_script_target, win_script_content) helpers.write_file(win_script_target, win_script_content)
@ -155,6 +148,23 @@ func load_level(id):
yield(t, "timeout") yield(t, "timeout")
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), false) AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), false)
# FIXME: Need to clean these up when switching levels somehow. # FIXME: Need to clean these up when switching levels somehow.
func _careful_delete(path_inside):
var expected_prefix
var os = OS.get_name()
if os == "X11":
expected_prefix = "/home/%s/.local/share/git-hydra/tmp/" % OS.get_environment("USER")
elif os == "Windows":
helpers.crash("Need to figure out delete_prefix on Windows")
else:
helpers.crash("Unsupported OS: %s" % os)
if path_inside.substr(0,expected_prefix.length()) != expected_prefix:
helpers.crash("Refusing to delete directory %s that does not start with %s" % [path_inside, expected_prefix])
else:
game.global_shell.run("rm -rf '%s'" % path_inside)
func reload_level(): func reload_level():
load_level(current_level) load_level(current_level)
@ -167,8 +177,8 @@ func construct_repo(script_content, path):
# Becase in an exported game, all assets are in a .pck file, we need to put # Becase in an exported game, all assets are in a .pck file, we need to put
# the script somewhere in the filesystem. # the script somewhere in the filesystem.
var script_path_outside = game.tmp_prefix+"/git-hydra-script" var script_path_outside = game.tmp_prefix_outside+"/git-hydra-script"
var script_path = "/tmp/git-hydra-script" var script_path_inside = game.tmp_prefix_inside+"/git-hydra-script"
helpers.write_file(script_path_outside, script_content) helpers.write_file(script_path_outside, script_content)
game.global_shell.run("mkdir " + path) game.global_shell.run("mkdir " + path)
@ -176,7 +186,7 @@ func construct_repo(script_content, path):
game.global_shell.run("git init") game.global_shell.run("git init")
game.global_shell.run("git symbolic-ref HEAD refs/heads/main") game.global_shell.run("git symbolic-ref HEAD refs/heads/main")
# Read stdin from /dev/null so that interactive commands don't block. # Read stdin from /dev/null so that interactive commands don't block.
game.global_shell.run("bash "+script_path+" </dev/null") game.global_shell.run("bash "+script_path_inside+" </dev/null")
func show_win_status(): func show_win_status():
next_level_button.show() next_level_button.show()

View file

@ -22,6 +22,7 @@ _global_script_class_icons={
config/name="git-hydra" config/name="git-hydra"
run/main_scene="res://main.tscn" run/main_scene="res://main.tscn"
config/use_custom_user_dir=true
[autoload] [autoload]

View file

@ -6,7 +6,7 @@ var _cwd
#signal output(text) #signal output(text)
func _init(): func _init():
_cwd = "/tmp" _cwd = game.tmp_prefix_inside
func cd(dir): func cd(dir):
_cwd = dir _cwd = dir
@ -30,7 +30,7 @@ func run(command):
var hacky_command = "" var hacky_command = ""
for variable in env: for variable in env:
hacky_command += "export %s='%s';" % [variable, env[variable]] hacky_command += "export %s='%s';" % [variable, env[variable]]
hacky_command += "cd '%s';" % _cwd hacky_command += "cd '%s' || exit 1;" % _cwd
hacky_command += command hacky_command += command
# Godot's OS.execute wraps each argument in double quotes before executing. # Godot's OS.execute wraps each argument in double quotes before executing.

View file

@ -112,7 +112,7 @@ func editor_closed():
input.grab_focus() input.grab_focus()
func check_win_condition(): func check_win_condition():
if repository.shell.run("bash /tmp/win 2>/dev/null >/dev/null && echo yes || echo no") == "yes\n": if repository.shell.run("bash %s/win 2>/dev/null >/dev/null && echo yes || echo no" % game.tmp_prefix_inside) == "yes\n":
main.show_win_status() main.show_win_status()
func regenerate_completions_menu(new_text): func regenerate_completions_menu(new_text):

View file

@ -15,13 +15,13 @@ func _process(_delta):
_client_connection = _server.take_connection() _client_connection = _server.take_connection()
var length = _client_connection.get_u8() var length = _client_connection.get_u8()
var filename = _client_connection.get_string(length) var filename = _client_connection.get_string(length)
filename = filename.replace("/tmp/active/", "") filename = filename.replace("%s/active/" % game.tmp_prefix_inside, "")
open(filename) open(filename)
func open(filename): func open(filename):
path = filename path = filename
var fixme_path = game.tmp_prefix+"/active/" var fixme_path = game.tmp_prefix_outside+"/active/"
var content = helpers.read_file(fixme_path+filename) var content = helpers.read_file(fixme_path+filename)
text = content text = content
@ -29,7 +29,7 @@ func open(filename):
grab_focus() grab_focus()
func save(): func save():
var fixme_path = game.tmp_prefix+"/active/" var fixme_path = game.tmp_prefix_outside+"/active/"
helpers.write_file(fixme_path+path, text) helpers.write_file(fixme_path+path, text)
close() close()