From 448ed89eada01a06a1eeab2f36443aeb0126e4aa Mon Sep 17 00:00:00 2001 From: Sebastian Morr Date: Mon, 26 Oct 2020 21:29:11 +0100 Subject: [PATCH] Have fake-editor send the content of the file itself So we don't have to fetch the content from the outside. --- scenes/game.gd | 31 ++++++++++--------------------- scenes/shell.gd | 3 +++ scenes/text_editor.gd | 21 +++++++++------------ scripts/fake-editor | 20 +++++++++++++++++++- scripts/fake-editor-noblock | 16 ---------------- 5 files changed, 41 insertions(+), 50 deletions(-) delete mode 100755 scripts/fake-editor-noblock diff --git a/scenes/game.gd b/scenes/game.gd index b14c3be..5cca835 100644 --- a/scenes/game.gd +++ b/scenes/game.gd @@ -11,20 +11,12 @@ var _file = "user://savegame.json" var state = {} func _ready(): - var dir = Directory.new() - var repo_dir = tmp_prefix+"repos/" - if not dir.dir_exists(tmp_prefix): - var err = dir.make_dir(tmp_prefix) - if err != OK: - helpers.crash("Could not create temporary directory %s." % tmp_prefix) - if not dir.dir_exists(repo_dir): - var err = dir.make_dir(repo_dir) - if err != OK: - helpers.crash("Could not create temporary directory %s." % repo_dir) - global_shell = Shell.new() - fake_editor = copy_file_to_game_env("fake-editor") - copy_file_to_game_env("fake-editor-noblock") + + create_file_in_game_env("fake-editor", helpers.read_file("res://scripts/fake-editor")) + fake_editor = tmp_prefix + "fake-editor" + global_shell.run("chmod u+x '%s'" % fake_editor) + load_state() func _initial_state(): @@ -50,11 +42,8 @@ func load_state(): state[key] = new_state[key] savegame.close() -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_prefix + 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 +# filename is relative to the tmp directory! +func create_file_in_game_env(filename, content): + global_shell.cd(tmp_prefix) + # Quoted HERE doc doesn't do any substitutions inside. + global_shell.run("cat > '%s' <<'HEREHEREHERE'\n%s\nHEREHEREHERE" % [filename, content]) diff --git a/scenes/shell.gd b/scenes/shell.gd index a64fdf5..7a9a308 100644 --- a/scenes/shell.gd +++ b/scenes/shell.gd @@ -7,6 +7,9 @@ var _cwd var _os = OS.get_name() func _init(): + # Create required directories and move into the tmp directory. + _cwd = "/tmp" + run("mkdir -p '%s/repos'" % game.tmp_prefix) _cwd = game.tmp_prefix func cd(dir): diff --git a/scenes/text_editor.gd b/scenes/text_editor.gd index 350e7e4..e449fc6 100644 --- a/scenes/text_editor.gd +++ b/scenes/text_editor.gd @@ -15,34 +15,31 @@ func _ready(): func _process(_delta): if _server.is_connection_available(): _client_connection = _server.take_connection() - var length = _client_connection.get_u8() + var length = _client_connection.get_u32() var filename = _client_connection.get_string(length) - filename = filename.replace("%srepos/" % game.tmp_prefix, "") - open(filename) + + length = _client_connection.get_u32() + var content = _client_connection.get_string(length) + + open(content) func _input(event): if event.is_action_pressed("save"): save() -func open(filename): - path = filename - - var fixme_path = game.tmp_prefix+"repos/" - var content = helpers.read_file(fixme_path+filename) +func open(content): text = content - show() grab_focus() func save(): if visible: - var fixme_path = game.tmp_prefix+"repos/" - # Add a newline to the end of the file if there is none. if text.length() > 0 and text.substr(text.length()-1, 1) != "\n": text += "\n" - helpers.write_file(fixme_path+path, text) + _client_connection.put_string(text) + close() emit_signal("saved") diff --git a/scripts/fake-editor b/scripts/fake-editor index 223c058..9e5a929 100755 --- a/scripts/fake-editor +++ b/scripts/fake-editor @@ -11,10 +11,28 @@ $socket = IO::Socket::INET->new(PeerAddr => "127.0.0.1", my $absolute_path = File::Spec->rel2abs($ARGV[0]); # Send the length of the first argument as a byte. -$socket->send(chr(length($absolute_path))); +$socket->send(pack("L", length($absolute_path))); # Send the first argument as a string. $socket->send($absolute_path); +# Get and send the content of the file, prefixed by its length. +my $content = do{local(@ARGV,$/)=$absolute_path;<>}; +$socket->send(pack("L", length($content))); +$socket->send($content); + +# Get size of written content. This blocks until the players hits save or close. +my $length_str; +$socket->recv($length_str, 4); +my $length = unpack("L", $length_str); +my $new_content = ""; +$socket->recv($new_content, $length); + +# Write content back into the file. +my $handle; +open ($handle,'>',$absolute_path) or die("Error opening file"); +print $handle $new_content; +close ($handle) or die ("Error closing file"); + # This call is intended to block, we're waiting for Godot to close the connection. my $reply; $socket->read($reply, 1000); diff --git a/scripts/fake-editor-noblock b/scripts/fake-editor-noblock deleted file mode 100755 index 4f1fec4..0000000 --- a/scripts/fake-editor-noblock +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env perl - -use IO::Socket; -use File::Spec; - -$socket = IO::Socket::INET->new(PeerAddr => "127.0.0.1", - PeerPort => 1234, - Proto => "tcp", - Type => SOCK_STREAM); - -my $absolute_path = File::Spec->rel2abs($ARGV[0]); - -# Send the length of string as a byte. -$socket->send(chr(length($absolute_path))); -# Send the first argument as a string. -$socket->send($absolute_path);