From ad8d8f50d1c48578c45778907c8a6ea111351de9 Mon Sep 17 00:00:00 2001 From: blinry Date: Thu, 7 Sep 2023 11:43:51 +0200 Subject: [PATCH] Some output from the Linux VM!! --- scenes/game.gd | 8 +++++- scenes/helpers.gd | 2 ++ scenes/shell.gd | 56 ++++++++++++++++------------------------- scenes/shell_command.gd | 8 ++++++ scenes/terminal.gd | 18 ++++++------- web/web-shell/script.js | 10 ++++---- 6 files changed, 53 insertions(+), 49 deletions(-) diff --git a/scenes/game.gd b/scenes/game.gd index 4d1642b..3a48e5c 100644 --- a/scenes/game.gd +++ b/scenes/game.gd @@ -1,6 +1,6 @@ extends Node -var tmp_prefix = OS.get_user_data_dir() + "/tmp/" +var tmp_prefix = get_tmp_prefix() var global_shell var fake_editor @@ -17,6 +17,12 @@ var state = {} var mutex +func get_tmp_prefix(): + if OS.get_name() == "Web": + return "/tmp/" + else: + OS.get_user_data_dir() + "/tmp/" + func _ready(): mutex = Mutex.new() load_state() diff --git a/scenes/helpers.gd b/scenes/helpers.gd index b4ac08f..b28fdb0 100644 --- a/scenes/helpers.gd +++ b/scenes/helpers.gd @@ -96,6 +96,8 @@ func careful_delete(path_inside): # Windows treats paths case-insensitively: expected_prefix = expected_prefix.to_lower() path_inside = path_inside.to_lower() + elif os == "Web": + expected_prefix = "/tmp" 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]) diff --git a/scenes/shell.gd b/scenes/shell.gd index 0989326..24b7ae5 100644 --- a/scenes/shell.gd +++ b/scenes/shell.gd @@ -12,7 +12,7 @@ func _init(): # Create required directories and move into the tmp directory. _cwd = "/tmp" run("mkdir -p '%s/repos'" % game.tmp_prefix) - _cwd = game.tmp_prefixls + _cwd = game.tmp_prefix func cd(dir): _cwd = dir @@ -38,7 +38,14 @@ func run_async(command, crash_on_fail=true): t.start(Callable(self, "run_async_thread").bind(shell_command)) return shell_command - + +func run_async_web(command, crash_on_fail=true): + var shell_command = ShellCommand.new() + shell_command.command = command + shell_command.crash_on_fail = crash_on_fail + run_async_thread(shell_command) + return shell_command + func run_async_thread(shell_command): var debug = false @@ -86,28 +93,26 @@ func run_async_thread(shell_command): helpers.write_file(script_path, hacky_command) result = helpers.exec(_shell_binary(), [script_path], crash_on_fail) elif _os == "Web": - hacky_command = hacky_command.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n") + #hacky_command = hacky_command.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n") #var js_code = "await run_in_vm('" + hacky_command + "')" #print(js_code) #var output = JavaScriptBridge.eval(js_code, true) - var output = JavaScriptBridge.eval("testy()") + #var output = JavaScriptBridge.eval("testy()") #print(hacky_command) - #var output = web_shell.run_in_vm(hacky_command) - - result = {} - result["output"] = output - result["exit_code"] = 0 + shell_command.js_callback = JavaScriptBridge.create_callback(Callable(shell_command, "callback")) + web_shell.run_in_vm(hacky_command).then(shell_command.js_callback) else: helpers.crash("Unimplemented OS: %s" % _os) - if debug: - print(result["output"]) - - shell_command.output = result["output"] - shell_command.exit_code = result["exit_code"] - shell_command.emit_signal("done") + if _os != "Web": + if debug: + print(result["output"]) + + shell_command.output = result["output"] + shell_command.exit_code = result["exit_code"] + shell_command.emit_signal("done") func _shell_binary(): if _os == "Linux" or _os == "OSX": @@ -117,25 +122,8 @@ func _shell_binary(): else: helpers.crash("Unsupported OS: %s" % _os) -#var _t -#func run_async(command): -# _t = Thread.new() -# _t.start(self, "run_async_thread", command) -# -#func run_async_thread(command): -# var port = 1000 + (randi() % 1000) -# var s = TCP_Server.new() -# s.listen(port) -# var _pid = OS.execute("ncat", ["127.0.0.1", str(port), "-c", command], false, [], true) -# while not s.is_connection_available(): -# pass -# var c = s.take_connection() -# while c.get_status() == StreamPeerTCP.STATUS_CONNECTED: -# read_from(c) -# OS.delay_msec(1000/30) -# read_from(c) -# c.disconnect_from_host() -# s.stop() +func callback(args): + print(args) func read_from(c): var total_available = c.get_available_bytes() diff --git a/scenes/shell_command.gd b/scenes/shell_command.gd index 7e65794..a8eabe8 100644 --- a/scenes/shell_command.gd +++ b/scenes/shell_command.gd @@ -8,7 +8,15 @@ var output var exit_code var crash_on_fail = true var thread +var js_callback # For JavaScriptBridge func _unused(): # This is just to suppress a warning about the signal never being emitted. emit_signal("done") + +func callback(_output): + #print(_output) + print(_output[0]) + output = _output[0] + exit_code = 0 + emit_signal("done") diff --git a/scenes/terminal.gd b/scenes/terminal.gd index 3cf033e..5390c6d 100644 --- a/scenes/terminal.gd +++ b/scenes/terminal.gd @@ -90,16 +90,16 @@ func send_command(command): shell.cd(repository.path) - #var cmd = shell.run_async(command, false) - #await cmd.done - #call_deferred("command_done", cmd) + var cmd = shell.run_async_web(command, false) + await cmd.done + call_deferred("command_done", cmd) - var output = shell.run(command, false) - var shell_command = ShellCommand.new() - shell_command.exit_code = 0 - shell_command.output = output - shell_command.command = command - command_done(shell_command) +# var output = shell.run(command, false) +# var shell_command = ShellCommand.new() +# shell_command.exit_code = 0 +# shell_command.output = output +# shell_command.command = command +# command_done(shell_command) func command_done(cmd): if cmd.exit_code == 0: diff --git a/web/web-shell/script.js b/web/web-shell/script.js index 8de3d63..bc66966 100644 --- a/web/web-shell/script.js +++ b/web/web-shell/script.js @@ -3,11 +3,11 @@ var emulator; // Whether or not to restore the VM state from a file. Set to false to perform a regular boot. let restoreState = true; -async function testy() { - return await new Promise((resolve, reject) => { +function testy(cmd) { + return new Promise((resolve, reject) => { setTimeout(() => { - resolve("testy"); - }, 1000); + resolve("testy!!" + cmd); + }, 100); }); } window.testy = testy; @@ -40,7 +40,7 @@ function run_in_vm(cmd) { }); } window.run_in_vm = run_in_vm; -window.web_shell = { run_in_vm: run_in_vm }; +window.web_shell = { run_in_vm, testy }; /*