From 2a0e2e46bb963444908951c3d3760a02f970702c Mon Sep 17 00:00:00 2001 From: Sebastian Morr Date: Tue, 1 Sep 2020 19:20:51 +0200 Subject: [PATCH] Use scripts to create levels, add level selector --- game.gd | 28 ++++++++++++++++ levels/blobs/goal | 6 ++++ levels/blobs/start | 0 levels/first/goal | 1 + levels/first/start | 0 main.gd | 79 ++++++++++++++++++++++++++++++++-------------- main.tscn | 11 +++++++ repository.gd | 26 ++++++++++++--- terminal.gd | 11 ++----- 9 files changed, 124 insertions(+), 38 deletions(-) create mode 100644 levels/blobs/goal create mode 100644 levels/blobs/start create mode 100644 levels/first/goal create mode 100644 levels/first/start diff --git a/game.gd b/game.gd index dd51227..770307b 100644 --- a/game.gd +++ b/game.gd @@ -31,8 +31,36 @@ func load_state() -> bool: savegame.close() return true +# Run a simple command given as a string, blocking, using execute. func run(command): + print("run: "+command) var output = [] OS.execute(command, [], true, output, true) # Remove trailing newline. return output[0].substr(0,len(output[0])-1) + +func sh(command, wd="/tmp/"): + print("sh in "+wd+": "+command) + var cwd = game.run("pwd") + var output = [] + + var hacky_command = command + hacky_command = "cd '"+wd+"';"+hacky_command + hacky_command = "export EDITOR=fake-editor;"+hacky_command + hacky_command = "export PATH=\"$PATH\":"+cwd+"/scripts;"+hacky_command + OS.execute("/bin/sh", ["-c", hacky_command], true, output, true) + return output[0] + +func read_file(path): + var file = File.new() + file.open(path, File.READ) + var content = file.get_as_text() + file.close() + return content + +func write_file(path, content): + var file = File.new() + file.open(path, File.WRITE) + file.store_string(content) + file.close() + return true diff --git a/levels/blobs/goal b/levels/blobs/goal new file mode 100644 index 0000000..5533642 --- /dev/null +++ b/levels/blobs/goal @@ -0,0 +1,6 @@ +echo "Hi" > file1 +echo "Ho" > file1 +echo "Hu" > file1 +git hash-object -w file1 +git hash-object -w file2 +git hash-object -w file3 diff --git a/levels/blobs/start b/levels/blobs/start new file mode 100644 index 0000000..e69de29 diff --git a/levels/first/goal b/levels/first/goal new file mode 100644 index 0000000..d5e174d --- /dev/null +++ b/levels/first/goal @@ -0,0 +1 @@ +git commit --allow-empty -m "Emtpy commit" diff --git a/levels/first/start b/levels/first/start new file mode 100644 index 0000000..e69de29 diff --git a/main.gd b/main.gd index 3bc915e..7c17922 100644 --- a/main.gd +++ b/main.gd @@ -6,21 +6,63 @@ var server var client_connection func _ready(): - var level = "first" - var goal_repository_path = "goal" - var start_repository_path = "start" - var active_repository_path = "active" - var active_prefix = "/tmp/" - var levels_prefix = game.run("pwd")+"/levels/"+level+"/" - - OS.execute("rm", ["-r", active_prefix+active_repository_path], true) - OS.execute("cp", ["-ra", levels_prefix+start_repository_path, active_prefix+active_repository_path], true) - $GoalRepository.path = levels_prefix+goal_repository_path - $ActiveRepository.path = active_prefix+active_repository_path + # Initialize level select. + var options = $LevelSelect.get_popup() + for level in list_levels(): + options.add_item(level) + options.connect("id_pressed", self, "load_level") + # Initialize TCP server for fake editor. server = TCP_Server.new() server.listen(1234) +func list_levels(): + var levels = [] + var dir = Directory.new() + dir.open("levels") + dir.list_dir_begin() + + while true: + var file = dir.get_next() + if file == "": + break + elif not file.begins_with("."): + levels.append(file) + + dir.list_dir_end() + return levels + +func load_level(id): + var levels = list_levels() + + var level = levels[id] + var cwd = game.run("pwd") + var tmp_prefix = "/tmp/" + var level_prefix = cwd+"/levels/" + + var goal_repository_path = tmp_prefix+"goal/" + var active_repository_path = tmp_prefix+"active/" + var goal_script = level_prefix+level+"/goal" + var active_script = level_prefix+level+"/start" + + OS.execute("rm", ["-r", active_repository_path], true) + OS.execute("rm", ["-r", goal_repository_path], true) + construct_repo(goal_script, goal_repository_path) + construct_repo(active_script, active_repository_path) + + $GoalRepository.path = goal_repository_path + $ActiveRepository.path = active_repository_path + +func construct_repo(script, path): + print(path) + game.sh("mkdir "+path) + game.sh("git init", path) + var commands = game.read_file(script).split("\n") + print(commands) + for command in commands: + print(command) + game.sh(command, path) + func _process(delta): if server.is_connection_available(): client_connection = server.take_connection() @@ -50,23 +92,12 @@ func _process(delta): func read_commit_message(): $CommitMessage.show() $Terminal/Input.editable = false - var file_path = "/tmp/githydragit/.git/COMMIT_EDITMSG" - var file = File.new() - file.open(file_path, File.READ) - var content = file.get_as_text() - file.close() - $CommitMessage.text = content + $CommitMessage.text = game.read_file("/tmp/githydragit/.git/COMMIT_EDITMSG") func save_commit_message(): - var file = File.new() - var file_path = "/tmp/githydragit/.git/COMMIT_EDITMSG" - file.open(file_path, File.WRITE) - var content = $CommitMessage.text - file.store_string(content) - file.close() + game.write_file("/tmp/githydragit/.git/COMMIT_EDITMSG", $CommitMessage.text) print("disconnect") client_connection.disconnect_from_host() $Terminal/Input.editable = true $CommitMessage.text = "" $CommitMessage.hide() - diff --git a/main.tscn b/main.tscn index 74eaff1..f192577 100644 --- a/main.tscn +++ b/main.tscn @@ -57,5 +57,16 @@ size = Vector2( 500, 1000 ) [node name="ActiveRepository" parent="." instance=ExtResource( 3 )] position = Vector2( 704.399, 17.2594 ) size = Vector2( 500, 1000 ) + +[node name="LevelSelect" type="MenuButton" parent="."] +margin_left = 11.5584 +margin_top = 11.1303 +margin_right = 109.558 +margin_bottom = 31.1303 +text = "Select level..." +flat = false +__meta__ = { +"_edit_use_anchors_": false +} [connection signal="text_entered" from="Terminal/Input" to="Terminal" method="send_command"] [connection signal="pressed" from="CommitMessage/Button" to="." method="save_commit_message"] diff --git a/repository.gd b/repository.gd index b7cab10..5dfecc9 100644 --- a/repository.gd +++ b/repository.gd @@ -10,14 +10,20 @@ func _ready(): func _process(delta): if path: - update_head() - update_refs() - update_index() - update_objects() - apply_forces() + update_everything() + +func update_everything(): + update_head() + update_refs() + update_index() + update_objects() + apply_forces() func set_path(new_path): path = new_path + for o in objects.values(): + o.queue_free() + objects = {} func get_path(): return path @@ -126,6 +132,16 @@ func update_head(): add_child(n) var n = objects["HEAD"] n.children = {symref_target("HEAD"): ""} + if not objects.has(symref_target("HEAD")): + var n2 = node.instance() + var r = symref_target("HEAD") + n2.id = r + n2.type = "ref" + n2.content = "" + n2.repository = self + n2.position = random_position() + objects[r] = n2 + add_child(n2) func all_objects(): return git("cat-file --batch-check=%(objectname) --batch-all-objects", true) diff --git a/terminal.gd b/terminal.gd index 3bc73d6..b55518c 100644 --- a/terminal.gd +++ b/terminal.gd @@ -7,15 +7,8 @@ func send_command(command): thread.start(self, "run_command_in_a_thread", command) func run_command_in_a_thread(command): - var cwd = game.run("pwd") - var output = [] - - var hacky_command = command - hacky_command = "cd /tmp/githydragit;"+hacky_command - hacky_command = "export EDITOR=fake-editor;"+hacky_command - hacky_command = "export PATH=\"$PATH\":"+cwd+"/scripts;"+hacky_command - OS.execute("/bin/sh", ["-c", hacky_command], true, output, true) + var output = game.sh(command, "/tmp/active") $Input.text = "" - $Output.text = $Output.text + "$ " + command + "\n" + output[0] + $Output.text = $Output.text + "$ " + command + "\n" + output $Output.scroll_vertical = 999999