From 4099030fcd3810dafe0692a282f1617bfb4a1053 Mon Sep 17 00:00:00 2001 From: Sebastian Morr Date: Wed, 9 Sep 2020 17:59:56 +0200 Subject: [PATCH] Use bundled sh.exe, expand %TEMP% per-user --- game.gd | 33 ++++++++++++++++++++++++++++++--- main.gd | 9 +++++---- shell.gd | 24 +++--------------------- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/game.gd b/game.gd index d745bdb..dc306d5 100644 --- a/game.gd +++ b/game.gd @@ -9,9 +9,10 @@ func _ready(): global_shell = Shell.new() # Copy fake-editor to tmp directory (because the original might be in a .pck file). - fake_editor = tmp_prefix + "fake-editor" + var fake_editor_outside = tmp_prefix + "fake-editor" + fake_editor = "/tmp/fake-editor" var content = game.read_file("res://scripts/fake-editor") - write_file('C:\\Users\\1\\AppData\\Local\\Temp\\fake-editor', content) + write_file(fake_editor_outside, content) global_shell.run("chmod u+x " + fake_editor) func read_file(path): @@ -34,5 +35,31 @@ func write_file(path, content): func _tmp_prefix(): var os = OS.get_name() - return "/tmp/" + if os == "X11": + return "/tmp/" + 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 exec("echo", ["%TEMP%"]).replacen("\\", "/").replace(" \n", "") + else: + push_error("Unsupported OS") + get_tree().quit() +# Run a simple command with arguments, blocking, using OS.execute. +func exec(command, args=[]): + var debug = true + + if debug: + print("exec: %s [%s]" % [command, PoolStringArray(args).join(", ")]) + + var output = [] + var exit_code = OS.execute(command, args, true, output, true) + output = output[0] + + if exit_code != 0: + push_error("OS.execute failed: %s [%s] Output: %s" % [command, PoolStringArray(args).join(", "), output]) + + if debug: + print(output) + + return output diff --git a/main.gd b/main.gd index ac0b10e..b9f1972 100644 --- a/main.gd +++ b/main.gd @@ -48,8 +48,8 @@ func load_level(id): var level = levels[id] var level_prefix = "res://levels/" - var goal_repository_path = game.tmp_prefix+"goal/" - var active_repository_path = game.tmp_prefix+"active/" + var goal_repository_path = "/tmp/goal/" + var active_repository_path = "/tmp/active/" var goal_script = level_prefix+level+"/goal" var active_script = level_prefix+level+"/start" @@ -80,8 +80,9 @@ func construct_repo(script, path): # Becase in an exported game, all assets are in a .pck file, we need to put # the script somewhere in the filesystem. var content = game.read_file(script) - var script_path = game.tmp_prefix+"/git-hydra-script" - game.write_file('C:\\Users\\1\\AppData\\Local\\Temp\\git-hydra-script', content) + var script_path_outside = game.tmp_prefix+"/git-hydra-script" + var script_path = "/tmp/git-hydra-script" + game.write_file(script_path_outside, content) game.global_shell.run("mkdir " + path) game.global_shell.cd(path) diff --git a/shell.gd b/shell.gd index b8904dc..122b760 100644 --- a/shell.gd +++ b/shell.gd @@ -6,7 +6,7 @@ var _cwd signal output(text) func _init(): - _cwd = game.tmp_prefix + _cwd = "/tmp" func cd(dir): _cwd = dir @@ -28,7 +28,7 @@ func run(command): hacky_command += "cd '%s';" % _cwd hacky_command += command - var output = _exec(_shell_binary(), ["-c", hacky_command]) + var output = game.exec(_shell_binary(), ["-c", hacky_command]) if debug: print(output) @@ -41,7 +41,7 @@ func _shell_binary(): if os == "X11": return "sh" elif os == "Windows": - return "sh.exe" + return "dependencies\\windows\\git\\bin\\sh.exe" else: push_error("Unsupported OS") get_tree().quit() @@ -69,21 +69,3 @@ func run_async_thread(command): c.disconnect_from_host() s.stop() -# Run a simple command with arguments, blocking, using OS.execute. -func _exec(command, args=[]): - var debug = true - - if debug: - print("exec: %s [%s]" % [command, PoolStringArray(args).join(", ")]) - - var output = [] - var exit_code = OS.execute(command, args, true, output, true) - output = output[0] - - if exit_code != 0: - push_error("OS.execute failed: %s [%s] Output: %s" % [command, PoolStringArray(args).join(", "), output]) - - if debug: - print(output) - - return output