Have fake-editor send the content of the file itself

So we don't have to fetch the content from the outside.
This commit is contained in:
Sebastian Morr 2020-10-26 21:29:11 +01:00
parent 8f70770107
commit 448ed89ead
5 changed files with 41 additions and 50 deletions

View file

@ -11,20 +11,12 @@ var _file = "user://savegame.json"
var state = {} var state = {}
func _ready(): 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() 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() load_state()
func _initial_state(): func _initial_state():
@ -50,11 +42,8 @@ func load_state():
state[key] = new_state[key] state[key] = new_state[key]
savegame.close() savegame.close()
func copy_file_to_game_env(filename): # filename is relative to the tmp directory!
# Copy fake-editor to tmp directory (because the original might be in a .pck file). func create_file_in_game_env(filename, content):
var file_outside = tmp_prefix + filename global_shell.cd(tmp_prefix)
var file_inside = tmp_prefix + filename # Quoted HERE doc doesn't do any substitutions inside.
var content = helpers.read_file("res://scripts/"+filename) global_shell.run("cat > '%s' <<'HEREHEREHERE'\n%s\nHEREHEREHERE" % [filename, content])
helpers.write_file(file_outside, content)
global_shell.run("chmod u+x " + '"'+file_inside+'"')
return file_inside

View file

@ -7,6 +7,9 @@ var _cwd
var _os = OS.get_name() var _os = OS.get_name()
func _init(): 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 _cwd = game.tmp_prefix
func cd(dir): func cd(dir):

View file

@ -15,34 +15,31 @@ func _ready():
func _process(_delta): func _process(_delta):
if _server.is_connection_available(): if _server.is_connection_available():
_client_connection = _server.take_connection() _client_connection = _server.take_connection()
var length = _client_connection.get_u8() var length = _client_connection.get_u32()
var filename = _client_connection.get_string(length) 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): func _input(event):
if event.is_action_pressed("save"): if event.is_action_pressed("save"):
save() save()
func open(filename): func open(content):
path = filename
var fixme_path = game.tmp_prefix+"repos/"
var content = helpers.read_file(fixme_path+filename)
text = content text = content
show() show()
grab_focus() grab_focus()
func save(): func save():
if visible: if visible:
var fixme_path = game.tmp_prefix+"repos/"
# Add a newline to the end of the file if there is none. # 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": if text.length() > 0 and text.substr(text.length()-1, 1) != "\n":
text += "\n" text += "\n"
helpers.write_file(fixme_path+path, text) _client_connection.put_string(text)
close() close()
emit_signal("saved") emit_signal("saved")

View file

@ -11,10 +11,28 @@ $socket = IO::Socket::INET->new(PeerAddr => "127.0.0.1",
my $absolute_path = File::Spec->rel2abs($ARGV[0]); my $absolute_path = File::Spec->rel2abs($ARGV[0]);
# Send the length of the first argument as a byte. # 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. # Send the first argument as a string.
$socket->send($absolute_path); $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. # This call is intended to block, we're waiting for Godot to close the connection.
my $reply; my $reply;
$socket->read($reply, 1000); $socket->read($reply, 1000);

View file

@ -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);