From 78f324bf6854fc80620e1231eab1142670a984a5 Mon Sep 17 00:00:00 2001 From: Sebastian Morr Date: Tue, 29 Sep 2020 16:12:58 +0200 Subject: [PATCH] Put temp files in user://tmp/ --- file_browser.gd | 2 +- game.gd | 31 +++++++++++++++++++++++-------- helpers.gd | 4 +++- main.gd | 40 +++++++++++++++++++++++++--------------- project.godot | 1 + shell.gd | 4 ++-- terminal.gd | 2 +- text_editor.gd | 6 +++--- 8 files changed, 59 insertions(+), 31 deletions(-) diff --git a/file_browser.gd b/file_browser.gd index 1d85100..d49463c 100644 --- a/file_browser.gd +++ b/file_browser.gd @@ -28,7 +28,7 @@ func _on_item_selected(): var item = $FileTree.get_selected() 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): # We're looking at the third character because all entries have the form diff --git a/game.gd b/game.gd index b407a9d..de7accd 100644 --- a/game.gd +++ b/game.gd @@ -1,6 +1,7 @@ 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 fake_editor @@ -8,6 +9,16 @@ var _file = "user://savegame.json" var state = {} 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() fake_editor = copy_file_to_game_env("fake-editor") copy_file_to_game_env("fake-editor-noblock") @@ -40,20 +51,24 @@ func load_state() -> bool: func copy_file_to_game_env(filename): # Copy fake-editor to tmp directory (because the original might be in a .pck file). - var file_outside = tmp_prefix + filename - var file_inside = "/tmp/"+filename + 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) global_shell.run("chmod u+x " + file_inside) return file_inside -func _tmp_prefix(): +func _tmp_prefix_inside(): var os = OS.get_name() + var path if os == "X11": - return "/tmp/" + path = OS.get_user_data_dir() elif os == "Windows": - # For some reason, this command outputs a space in the end? We remove it. - # Also, Godot's default is to use forward slashes for everything. - return helpers.exec("echo", ["%TEMP%"]).replacen("\\", "/").replace(" \n", "/") + helpers.crash("Need to figure out Windows tmp_prefix_inside...") else: helpers.crash("Unsupported OS: %s" % os) + + return path + "/tmp/" + +func _tmp_prefix_outside(): + return "user://tmp/" diff --git a/helpers.gd b/helpers.gd index 9f54496..2f23c30 100644 --- a/helpers.gd +++ b/helpers.gd @@ -5,8 +5,10 @@ var debug_file_io = false # Crash the game and display the error message. func crash(message): push_error(message) - print("Fatal error: " + message) + print("FATAL ERROR: " + message) 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. func exec(command, args=[], crash_on_fail=true): diff --git a/main.gd b/main.gd index 7f2ba2e..eb37dab 100644 --- a/main.gd +++ b/main.gd @@ -99,8 +99,8 @@ func load_level(id): var level = levels[id] var level_prefix = "res://levels/%s/" % chapter - var goal_repository_path = "/tmp/goal/" - var active_repository_path = "/tmp/active/" + var goal_repository_path = game.tmp_prefix_inside+"/repos/goal/" + var active_repository_path = game.tmp_prefix_inside+"/repos/active/" var goal_script = level_prefix+level+"/goal" var active_script = level_prefix+level+"/start" @@ -121,15 +121,8 @@ func load_level(id): # We're actually destroying stuff here. # Make sure that active_repository is in a temporary directory. - var expected_prefix = "/tmp" - if active_repository_path.substr(0,4) != expected_prefix: - 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) + _careful_delete(active_repository_path) + _careful_delete(goal_repository_path) var goal_script_content = helpers.read_file(goal_script, "") var active_script_content = helpers.read_file(active_script, "") @@ -140,7 +133,7 @@ func load_level(id): active_repository.path = active_repository_path 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") helpers.write_file(win_script_target, win_script_content) @@ -155,6 +148,23 @@ func load_level(id): yield(t, "timeout") AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), false) # 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(): 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 # the script somewhere in the filesystem. - var script_path_outside = game.tmp_prefix+"/git-hydra-script" - var script_path = "/tmp/git-hydra-script" + var script_path_outside = game.tmp_prefix_outside+"/git-hydra-script" + var script_path_inside = game.tmp_prefix_inside+"/git-hydra-script" helpers.write_file(script_path_outside, script_content) 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 symbolic-ref HEAD refs/heads/main") # Read stdin from /dev/null so that interactive commands don't block. - game.global_shell.run("bash "+script_path+" /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() func regenerate_completions_menu(new_text): diff --git a/text_editor.gd b/text_editor.gd index e179ba7..7740cdc 100644 --- a/text_editor.gd +++ b/text_editor.gd @@ -15,13 +15,13 @@ func _process(_delta): _client_connection = _server.take_connection() var length = _client_connection.get_u8() var filename = _client_connection.get_string(length) - filename = filename.replace("/tmp/active/", "") + filename = filename.replace("%s/active/" % game.tmp_prefix_inside, "") open(filename) func open(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) text = content @@ -29,7 +29,7 @@ func open(filename): grab_focus() func save(): - var fixme_path = game.tmp_prefix+"/active/" + var fixme_path = game.tmp_prefix_outside+"/active/" helpers.write_file(fixme_path+path, text) close()