oh-my-git/main.gd

168 lines
5.2 KiB
GDScript3
Raw Normal View History

extends Control
2020-03-18 15:23:38 +01:00
2020-03-18 20:03:17 +01:00
var dragged = null
2020-09-24 10:10:14 +02:00
#var level_set = "top-down"
var level_set = "bottom-up"
var current_level = 0
export(NodePath) var terminal_path
onready var terminal = get_node(terminal_path)
2020-09-14 19:36:58 +02:00
onready var input = terminal.input
onready var output = terminal.output
export(NodePath) var goal_repository_path
export(NodePath) var active_repository_path
onready var goal_repository = get_node(goal_repository_path)
onready var active_repository = get_node(active_repository_path)
export(NodePath) var level_select_path
onready var level_select = get_node(level_select_path)
export(NodePath) var next_level_button_path
onready var next_level_button = get_node(next_level_button_path)
export(NodePath) var level_name_path
onready var level_name = get_node(level_name_path)
export(NodePath) var level_description_path
onready var level_description = get_node(level_description_path)
export(NodePath) var level_congrats_path
onready var level_congrats = get_node(level_congrats_path)
2020-09-01 21:25:24 +02:00
2020-03-18 15:23:38 +01:00
func _ready():
2020-09-04 14:57:37 +02:00
# Initialize level select.
var options = level_select.get_popup()
repopulate_levels()
2020-09-04 14:57:37 +02:00
options.connect("id_pressed", self, "load_level")
# Load first level.
load_level(0)
input.grab_focus()
func list_levels():
2020-09-04 14:57:37 +02:00
var levels = []
var dir = Directory.new()
dir.open("res://levels/%s" % level_set)
2020-09-04 14:57:37 +02:00
dir.list_dir_begin()
2020-09-04 14:57:37 +02:00
while true:
var file = dir.get_next()
if file == "":
break
elif not file.begins_with(".") and file != "sequence":
2020-09-04 14:57:37 +02:00
levels.append(file)
2020-09-04 14:57:37 +02:00
dir.list_dir_end()
levels.sort()
var final_level_sequence = []
var level_sequence = Array(game.read_file("res://levels/%s/sequence" % level_set, "").split("\n"))
for level in level_sequence:
if level == "":
continue
if not levels.has(level):
push_error("Level '%s' is specified in the sequence, but could not be found" % level)
levels.erase(level)
final_level_sequence.push_back(level)
final_level_sequence += levels
return final_level_sequence
func load_level(id):
next_level_button.hide()
level_congrats.hide()
level_description.show()
current_level = id
2020-09-04 14:57:37 +02:00
var levels = list_levels()
var level = levels[id]
var level_prefix = "res://levels/%s/" % level_set
2020-09-04 14:57:37 +02:00
var goal_repository_path = "/tmp/goal/"
var active_repository_path = "/tmp/active/"
2020-09-04 14:57:37 +02:00
var goal_script = level_prefix+level+"/goal"
var active_script = level_prefix+level+"/start"
2020-09-15 12:36:22 +02:00
var description_file = level_prefix+level+"/description"
var description = game.read_file(description_file, "no description")
# Surround all lines indented with four spaces with [code] tags.
var monospace_regex = RegEx.new()
monospace_regex.compile("\n (.*)\n")
description = monospace_regex.sub(description, "\n [code]$1[/code]\n", true)
level_description.bbcode_text = description
var congrats_file = level_prefix+level+"/congrats"
var congrats = game.read_file(congrats_file, "Good job, you solved the level!\n\nFeel free to try a few more things or click 'Next Level'.")
level_congrats.bbcode_text = congrats
level_name.text = level
2020-09-04 14:57:37 +02:00
# We're actually destroying stuff here.
# Make sure that active_repository is in a temporary directory.
var expected_prefix = "/tmp"
if active_repository_path.substr(0,4) != expected_prefix:
push_error("Refusing to delete a directory that does not start with %s" % expected_prefix)
get_tree().quit()
if goal_repository_path.substr(0,4) != expected_prefix:
push_error("Refusing to delete a directory that does not start with %s" % expected_prefix)
get_tree().quit()
# Danger zone!
game.global_shell.run("rm -rf '%s'" % active_repository_path)
game.global_shell.run("rm -rf '%s'" % goal_repository_path)
2020-09-15 12:36:22 +02:00
var goal_script_content = game.read_file(goal_script, "")
var active_script_content = game.read_file(active_script, "")
construct_repo(active_script_content +"\n"+ goal_script_content, goal_repository_path)
construct_repo(active_script_content, active_repository_path)
2020-09-04 14:57:37 +02:00
goal_repository.path = goal_repository_path
active_repository.path = active_repository_path
2020-09-10 12:03:46 +02:00
var win_script = level_prefix+level+"/win"
var win_script_target = game.tmp_prefix+"/win"
2020-09-15 12:36:22 +02:00
var win_script_content = game.read_file(win_script, "exit 1\n")
game.write_file(win_script_target, win_script_content)
terminal.clear()
2020-09-13 21:55:24 +02:00
func reload_level():
load_level(current_level)
func load_next_level():
current_level = (current_level + 1) % list_levels().size()
load_level(current_level)
2020-09-10 12:03:46 +02:00
2020-09-15 12:36:22 +02:00
func construct_repo(script_content, path):
# Becase in an exported game, all assets are in a .pck file, we need to put
# the script somewhere in the filesystem.
2020-09-15 12:36:22 +02:00
var script_path_outside = game.tmp_prefix+"/git-hydra-script"
var script_path = "/tmp/git-hydra-script"
2020-09-15 12:36:22 +02:00
game.write_file(script_path_outside, script_content)
game.global_shell.run("mkdir " + path)
game.global_shell.cd(path)
game.global_shell.run("git init")
2020-09-15 12:36:22 +02:00
game.global_shell.run("git symbolic-ref HEAD refs/heads/main")
2020-09-24 10:25:38 +02:00
# Read stdin from /dev/null so that interactive commands don't block.
game.global_shell.run("bash "+script_path+" </dev/null")
2020-09-04 14:57:37 +02:00
func show_win_status():
next_level_button.show()
level_description.hide()
level_congrats.show()
func repopulate_levels():
var options = level_select.get_popup()
options.clear()
for level in list_levels():
options.add_item(level)