WIP: Mutex and length fix in net-test

This commit is contained in:
blinry 2021-02-26 12:14:07 +01:00
parent cebe42ba21
commit b13e9baf4e
5 changed files with 63 additions and 39 deletions

View file

@ -58,9 +58,9 @@ boot_splash/bg_color=Color( 0, 0, 0, 1 )
[autoload] [autoload]
game="*res://scenes/game.tscn"
helpers="*res://scenes/helpers.gd" helpers="*res://scenes/helpers.gd"
levels="*res://scenes/levels.gd" levels="*res://scenes/levels.gd"
game="*res://scenes/game.tscn"
[display] [display]

View file

@ -15,16 +15,29 @@ var skipped_title = false
var _file = "user://savegame.json" var _file = "user://savegame.json"
var state = {} var state = {}
var mutex
func _ready(): func _ready():
mutex = Mutex.new()
load_state()
if OS.has_feature("standalone"): if OS.has_feature("standalone"):
get_tree().set_auto_accept_quit(false) get_tree().set_auto_accept_quit(false)
else: else:
game.toggle_music() game.toggle_music()
start_remote_shell() start_remote_shell()
yield(get_tree().create_timer(0.1), "timeout")
global_shell = Shell.new() global_shell = Shell.new()
# var cmd = global_shell.run("echo hi")
# print(cmd)
# cmd = global_shell.run("seq 1 10")
# print(cmd)
# cmd = global_shell.run("ls")
# print(cmd)
# helpers.crash(":)")
if global_shell.run("command -v git &>/dev/null && echo yes || echo no") == "no\n": if global_shell.run("command -v git &>/dev/null && echo yes || echo no") == "no\n":
game.skipped_title = true game.skipped_title = true
get_tree().change_scene("res://scenes/no_git.tscn") get_tree().change_scene("res://scenes/no_git.tscn")
@ -34,8 +47,6 @@ func _ready():
copy_script_to_game_env("fake-editor") copy_script_to_game_env("fake-editor")
copy_script_to_game_env("hint") copy_script_to_game_env("hint")
load_state()
func start_remote_shell(): func start_remote_shell():
var user_dir = ProjectSettings.globalize_path("user://") var user_dir = ProjectSettings.globalize_path("user://")
var script_content = helpers.read_file("res://scripts/net-test") var script_content = helpers.read_file("res://scripts/net-test")
@ -129,4 +140,9 @@ func toggle_music():
func shell_test(command): func shell_test(command):
return $ShellServer.send(command) mutex.lock()
print("go")
var response = $ShellServer.send(command)
print("stop")
mutex.unlock()
return response

View file

@ -57,40 +57,40 @@ func run_async_thread(shell_command):
hacky_command += "cd '%s' || exit 1;" % _cwd hacky_command += "cd '%s' || exit 1;" % _cwd
hacky_command += command hacky_command += command
print(hacky_command)
var result var result
if _os == "X11" or _os == "OSX": var shell_command_internal = game.shell_test(hacky_command)
# Godot's OS.execute wraps each argument in double quotes before executing # if _os == "X11" or _os == "OSX":
# on Linux and macOS. # # Godot's OS.execute wraps each argument in double quotes before executing
# Because we want to be in a single-quote context, where nothing is evaluated, # # on Linux and macOS.
# we end those double quotes and start a single quoted string. For each single # # Because we want to be in a single-quote context, where nothing is evaluated,
# quote appearing in our string, we close the single quoted string, and add # # we end those double quotes and start a single quoted string. For each single
# a double quoted string containing the single quote. Ooooof! # # quote appearing in our string, we close the single quoted string, and add
# # # a double quoted string containing the single quote. Ooooof!
# Example: The string # #
# # # Example: The string
# test 'fu' "bla" blubb # #
# # # test 'fu' "bla" blubb
# becomes # #
# # # becomes
# "'test '"'"'fu'"'"' "bla" blubb" # #
# # "'test '"'"'fu'"'"' "bla" blubb"
#
# hacky_command = '"\''+hacky_command.replace("'", "'\"'\"'")+'\'"'
# result = helpers.exec(_shell_binary(), ["-c", hacky_command], crash_on_fail)
# elif _os == "Windows":
# # On Windows, if the command contains a newline (even if inside a string),
# # execution will end. To avoid that, we first write the command to a file,
# # and run that file with bash.
# var script_path = game.tmp_prefix + "command" + str(randi())
# helpers.write_file(script_path, hacky_command)
# result = helpers.exec(_shell_binary(), [script_path], crash_on_fail)
# else:
# helpers.crash("Unimplemented OS: %s" % _os)
hacky_command = '"\''+hacky_command.replace("'", "'\"'\"'")+'\'"' shell_command.output = shell_command_internal.output
result = helpers.exec(_shell_binary(), ["-c", hacky_command], crash_on_fail) shell_command.exit_code = shell_command_internal.exit_code
elif _os == "Windows":
# On Windows, if the command contains a newline (even if inside a string),
# execution will end. To avoid that, we first write the command to a file,
# and run that file with bash.
var script_path = game.tmp_prefix + "command" + str(randi())
helpers.write_file(script_path, hacky_command)
result = helpers.exec(_shell_binary(), [script_path], crash_on_fail)
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") shell_command.emit_signal("done")
func _shell_binary(): func _shell_binary():

View file

@ -44,6 +44,8 @@ func send(text):
var shell_command = ShellCommand.new() var shell_command = ShellCommand.new()
shell_command.command = text shell_command.command = text
shell_command.output = response shell_command.output = response
print("response:")
print(response)
shell_command.exit_code = exit_code shell_command.exit_code = exit_code
return shell_command return shell_command
else: else:

View file

@ -28,13 +28,19 @@ while(true) {
} }
print("still connected"); print("still connected");
my $s2; my $s2;
$socket->recv($s2, ord($len)); $socket->recv($s2, $actual_len);
print($s2); print($s2);
STDOUT->flush(); STDOUT->flush();
my $script;
open($script, ">", "/tmp/omgscript") or die $!;
#$s3 = 'export HOME=\'/home/blinry/.local/share/Oh My Git/tmp/\';export PATH=\'/home/blinry/.local/share/Oh My Git/tmp/:\'"$PATH";cd \'/home/blinry/.local/share/Oh My Git/tmp/repos/yours/\' || exit 1;find . -type f -not -path \'*/\\.git/*\'';
print $script $s2;
close($script);
$s = ""; $s = "";
$command = $s2 . "\necho MAGIC\n"; $command = ". /tmp/omgscript" . "\necho MAGIC\n";
print $in $command; print $in $command;
inner_while: while (true) { inner_while: while (true) {