Skip to content

Commit

Permalink
Make PathHelper into a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
qrrk committed Mar 16, 2022
1 parent 1015f23 commit 3aac773
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 86 deletions.
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ default_bus_layout=""
Status="*res://scripts/status.gd"
Settings="*res://scripts/settings_manager.gd"
Geom="*res://scripts/window_geometry.gd"
Paths="*res://scripts/path_helper.gd"

[display]

Expand Down
10 changes: 1 addition & 9 deletions scenes/Catapult.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=39 format=2]
[gd_scene load_steps=38 format=2]

[ext_resource path="res://icons/info.svg" type="Texture" id=1]
[ext_resource path="res://icons/download.svg" type="Texture" id=2]
Expand Down Expand Up @@ -30,7 +30,6 @@
[ext_resource path="res://scripts/FontManager.gd" type="Script" id=28]
[ext_resource path="res://scenes/FontSizeHelpDialog.tscn" type="PackedScene" id=29]
[ext_resource path="res://icons/help.svg" type="Texture" id=30]
[ext_resource path="res://scripts/PathHelper.gd" type="Script" id=31]
[ext_resource path="res://scenes/ChangelogDialog.tscn" type="PackedScene" id=32]
[ext_resource path="res://scripts/BackupsUI.gd" type="Script" id=33]
[ext_resource path="res://scripts/BackupManager.gd" type="Script" id=34]
Expand Down Expand Up @@ -1259,12 +1258,6 @@ __meta__ = {
"_editor_description_": "Installs fonts from launcher's internal storage to the game directory, reads, manipulates and saves font-related settings."
}

[node name="PathHelper" type="Node" parent="."]
script = ExtResource( 31 )
__meta__ = {
"_editor_description_": "The centralized location for all path resolutions in the launcher."
}

[node name="Backups" type="Node" parent="."]
script = ExtResource( 34 )
__meta__ = {
Expand Down Expand Up @@ -1363,7 +1356,6 @@ __meta__ = {
[connection signal="soundpack_deletion_started" from="Sound" to="." method="_on_soundpack_operation_started"]
[connection signal="soundpack_installation_finished" from="Sound" to="." method="_on_soundpack_operation_finished"]
[connection signal="soundpack_installation_started" from="Sound" to="." method="_on_soundpack_operation_started"]
[connection signal="status_message" from="PathHelper" to="." method="_on_status_message"]
[connection signal="backup_creation_finished" from="Backups" to="." method="_on_backup_operation_finished"]
[connection signal="backup_creation_started" from="Backups" to="." method="_on_backup_operation_started"]
[connection signal="backup_restoration_finished" from="Backups" to="." method="_on_backup_operation_finished"]
Expand Down
17 changes: 8 additions & 9 deletions scripts/BackupManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ signal backup_deletion_started
signal backup_deletion_finished

onready var _fshelper := $"../FSHelper"
onready var _path := $"../PathHelper"

var available = null setget , _get_available

Expand All @@ -20,13 +19,13 @@ func backup_current(backup_name: String) -> void:
Status.post(tr("msg_backing_up_saves") % backup_name)
emit_signal("backup_creation_started")

var dest_dir = _path.save_backups.plus_file(backup_name)
var dest_dir = Paths.save_backups.plus_file(backup_name)
var d = Directory.new()

if not d.dir_exists(dest_dir):
d.make_dir(dest_dir)
for world in _fshelper.list_dir(_path.savegames):
_fshelper.copy_dir(_path.savegames.plus_file(world), dest_dir)
for world in _fshelper.list_dir(Paths.savegames):
_fshelper.copy_dir(Paths.savegames.plus_file(world), dest_dir)
yield(_fshelper, "copy_dir_done")

Status.post(tr("msg_backup_created"))
Expand Down Expand Up @@ -66,11 +65,11 @@ func refresh_available():

available = []

if not Directory.new().dir_exists(_path.save_backups):
if not Directory.new().dir_exists(Paths.save_backups):
return

for backup in _fshelper.list_dir(_path.save_backups):
var path = _path.save_backups.plus_file(backup)
for backup in _fshelper.list_dir(Paths.save_backups):
var path = Paths.save_backups.plus_file(backup)
available.append(get_save_summary(path))


Expand All @@ -81,7 +80,7 @@ func restore(backup_index: int) -> void:
Status.post(tr("msg_restoring_backup") % backup_name)

var source_dir = available[backup_index]["path"]
var dest_dir = _path.savegames
var dest_dir = Paths.savegames

emit_signal("backup_restoration_started")

Expand All @@ -105,7 +104,7 @@ func restore(backup_index: int) -> void:
func delete(backup_name: String) -> void:
# Delete a backup.

var target_dir = _path.save_backups.plus_file(backup_name)
var target_dir = Paths.save_backups.plus_file(backup_name)
emit_signal("backup_deletion_started")

if Directory.new().dir_exists(target_dir):
Expand Down
21 changes: 10 additions & 11 deletions scripts/Catapult.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ onready var _tabs = $Main/Tabs
onready var _mods = $Mods
onready var _releases = $Releases
onready var _fshelper = $FSHelper
onready var _path = $PathHelper
onready var _installer = $ReleaseInstaller
onready var _btn_install = $Main/Tabs/Game/BtnInstall
onready var _btn_refresh = $Main/Tabs/Game/Builds/BtnRefresh
Expand Down Expand Up @@ -60,10 +59,10 @@ func _ready() -> void:
func _unpack_utils() -> void:

var d = Directory.new()
var unzip_exe = _path.utils_dir.plus_file("unzip.exe")
var unzip_exe = Paths.utils_dir.plus_file("unzip.exe")
if (OS.get_name() == "Windows") and (not d.file_exists(unzip_exe)):
if not d.dir_exists(_path.utils_dir):
d.make_dir(_path.utils_dir)
if not d.dir_exists(Paths.utils_dir):
d.make_dir(Paths.utils_dir)
Status.post(tr("msg_unpacking_unzip"))
d.copy("res://utils/unzip.exe", unzip_exe)

Expand Down Expand Up @@ -247,14 +246,14 @@ func _get_release_key() -> String:

func _on_GameDir_pressed() -> void:

var gamedir = _path.game_dir
var gamedir = Paths.game_dir
if Directory.new().dir_exists(gamedir):
OS.shell_open(gamedir)


func _on_UserDir_pressed() -> void:

var userdir = _path.userdata
var userdir = Paths.userdata
if Directory.new().dir_exists(userdir):
OS.shell_open(userdir)

Expand Down Expand Up @@ -323,7 +322,7 @@ func _on_BtnPlay_pressed() -> void:

func _on_BtnResume_pressed() -> void:

var lastworld: String = _path.config.plus_file("lastworld.json")
var lastworld: String = Paths.config.plus_file("lastworld.json")
if Directory.new().file_exists(lastworld):
var f = File.new()
f.open(lastworld, File.READ)
Expand All @@ -336,15 +335,15 @@ func _start_game(world := "") -> void:

match OS.get_name():
"X11":
var params := ["--userdir", _path.userdata + "/"]
var params := ["--userdir", Paths.userdata + "/"]
if world != "":
params.append_array(["--world", world])
OS.execute(_path.game_dir.plus_file("cataclysm-launcher"), params, false)
OS.execute(Paths.game_dir.plus_file("cataclysm-launcher"), params, false)
"Windows":
var world_str := ""
if world != "":
world_str = "--world \"%s\"" % world
var command = "cd /d %s && start cataclysm-tiles.exe --userdir %s %s" % [_path.game_dir, _path.userdata, world_str]
var command = "cd /d %s && start cataclysm-tiles.exe --userdir %s %s" % [Paths.game_dir, Paths.userdata, world_str]
OS.execute("cmd", ["/C", command], false)


Expand All @@ -358,7 +357,7 @@ func _refresh_currently_installed() -> void:
_lbl_build.text = info[game]["name"]
_btn_install.text = tr("btn_update")
_btn_play.disabled = false
_btn_resume.disabled = not (Directory.new().file_exists(_path.config.plus_file("lastworld.json")))
_btn_resume.disabled = not (Directory.new().file_exists(Paths.config.plus_file("lastworld.json")))
_btn_game_dir.visible = true
_btn_user_dir.visible = true
if (_lst_builds.selected != -1) and (_lst_builds.selected < len(releases)):
Expand Down
13 changes: 6 additions & 7 deletions scripts/Debug.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ extends VBoxContainer


onready var _fshelper = $"/root/Catapult/FSHelper"
onready var _path = $"/root/Catapult/PathHelper"
onready var _mods = $"../../../Mods"
onready var _sound = $"../../../Sound"
onready var _totd = $"/root/Catapult/TOTD"
Expand All @@ -13,7 +12,7 @@ func _on_Button_pressed() -> void:
# Test modinfo parsing.

var message = "Found mods:"
var mods_dir = _path.mods_stock
var mods_dir = Paths.mods_stock

Status.post("Looking for mods in %s" % mods_dir)

Expand All @@ -29,7 +28,7 @@ func _on_Button2_pressed() -> void:
# Test soundpack parsing.

var message = "Found soundpacks:"
var sound_dir = _path.sound_user
var sound_dir = Paths.sound_user

Status.post("Looking for soundpacks in %s" % sound_dir)

Expand All @@ -44,7 +43,7 @@ func _on_Button2_pressed() -> void:
func _on_Button3_pressed():

var d = Directory.new()
var dir = _path.own_dir.plus_file("testdir")
var dir = Paths.own_dir.plus_file("testdir")
d.make_dir(dir)

var command_linux = {
Expand Down Expand Up @@ -88,7 +87,7 @@ func _on_Button4_pressed() -> void:

func _on_Button5_pressed() -> void:

var path = _path.own_dir
var path = Paths.own_dir
Status.post("Listing directory %s..." % path, Enums.MSG_DEBUG)
yield(get_tree().create_timer(0.1), "timeout")

Expand All @@ -108,10 +107,10 @@ func _on_Button7_pressed() -> void:

var msg = "PathHelper properties:"

for prop in _path.get_property_list():
for prop in Paths.get_property_list():
var name = prop["name"]
if (prop["type"] == 4):
msg += "\n%s: %s" % [name, _path.get(name)]
msg += "\n%s: %s" % [name, Paths.get(name)]

Status.post(msg, Enums.MSG_DEBUG)

Expand Down
4 changes: 1 addition & 3 deletions scripts/FilesystemHelper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var _platform: String = ""
var last_extract_result: int = 0 setget , _get_last_extract_result
# Stores the exit code of the last extract operation (0 if successful).

onready var _path = $"../PathHelper"


func _enter_tree() -> void:

Expand Down Expand Up @@ -159,7 +157,7 @@ func extract(path: String, dest_dir: String) -> void:
# Extracts a .zip or .tar.gz archive using the system utilities on Linux
# and bundled unzip.exe from InfoZip on Windows.

var unzip_exe = _path.utils_dir.plus_file("unzip.exe")
var unzip_exe = Paths.utils_dir.plus_file("unzip.exe")

var command_linux_zip = {
"name": "unzip",
Expand Down
16 changes: 7 additions & 9 deletions scripts/FontManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ extends Node

const _DEFAULT_FONTS = ["data/font/Terminus.ttf", "data/font/unifont.ttf"]

onready var _path := $"../PathHelper"

var _game_options: Array = []

var available_fonts: Array = [] setget , _get_available_fonts
Expand Down Expand Up @@ -62,15 +60,15 @@ func load_available_fonts() -> void:

func font_config_file_exists() -> bool:

var config_file: String = _path.config.plus_file("fonts.json")
var config_file: String = Paths.config.plus_file("fonts.json")

return Directory.new().file_exists(config_file)


func load_font_config() -> void:

var result: Dictionary = {}
var config_file: String = _path.config.plus_file("fonts.json")
var config_file: String = Paths.config.plus_file("fonts.json")

if Directory.new().file_exists(config_file):
var f := File.new()
Expand All @@ -93,14 +91,14 @@ func load_font_config() -> void:

func options_file_exists() -> bool:

var options_file: String = _path.config.plus_file("options.json")
var options_file: String = Paths.config.plus_file("options.json")

return Directory.new().file_exists(options_file)


func load_game_options() -> void:

var options_file: String = _path.config.plus_file("options.json")
var options_file: String = Paths.config.plus_file("options.json")

if Directory.new().file_exists(options_file):
var f := File.new()
Expand All @@ -121,7 +119,7 @@ func load_game_options() -> void:

func _write_font_config() -> void:

var config_file: String = _path.config.plus_file("fonts.json")
var config_file: String = Paths.config.plus_file("fonts.json")

var f = File.new()
var err = f.open(config_file, File.WRITE)
Expand All @@ -135,7 +133,7 @@ func _write_font_config() -> void:

func write_game_options() -> void:

var options_file: String = _path.config.plus_file("options.json")
var options_file: String = Paths.config.plus_file("options.json")

var f = File.new()
var err = f.open(options_file, File.WRITE)
Expand All @@ -153,7 +151,7 @@ func _install_font(font_index: int) -> bool:
var d := Directory.new()
var font_file = available_fonts[font_index]["file"]
var source := "res://fonts/ingame".plus_file(font_file)
var dest: String = _path.font_user.plus_file(font_file)
var dest: String = Paths.font_user.plus_file(font_file)
var err = d.copy(source, dest)

if err:
Expand Down
6 changes: 2 additions & 4 deletions scripts/InstallProbe.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ extends Node

const _INFO_FILENAME = "catapult_install_info.json"

onready var _path := $"../PathHelper"


func create_info_file(location: String, name: String) -> void:

Expand Down Expand Up @@ -40,11 +38,11 @@ func probe_installed_games() -> Dictionary:
var result = {}
var d = Directory.new()

var path_dda = _path.own_dir + "/dda/current/" + _INFO_FILENAME
var path_dda = Paths.own_dir + "/dda/current/" + _INFO_FILENAME
if d.file_exists(path_dda):
result["dda"] = _load_json(path_dda)

var path_bn = _path.own_dir + "/bn/current/" + _INFO_FILENAME
var path_bn = Paths.own_dir + "/bn/current/" + _INFO_FILENAME
if d.file_exists(path_bn):
result["bn"] = _load_json(path_bn)

Expand Down
Loading

0 comments on commit 3aac773

Please sign in to comment.