Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate data from executable #90

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion project.godot

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions scenes/Catapult.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ margin_bottom = 100.0
[node name="Tabs" type="TabContainer" parent="Main" groups=["disable_during_backup_operations", "disable_during_mod_operations", "disable_during_release_operations", "disable_during_soundpack_operations", "disable_while_fetching_releases"]]
margin_top = 104.0
margin_right = 592.0
margin_bottom = 547.0
margin_bottom = 573.0
tab_align = 0
script = ExtResource( 16 )

Expand Down Expand Up @@ -1194,6 +1194,26 @@ value = 100.0
editable = false
suffix = "%"

[node name="InstDir" type="HBoxContainer" parent="Main/Tabs/Settings"]
margin_top = 404.0
margin_right = 577.0
margin_bottom = 428.0
hint_tooltip = "tooltip_num_prs_to_request"

[node name="Label" type="Label" parent="Main/Tabs/Settings/InstDir"]
margin_right = 120.0
margin_bottom = 24.0
text = "lbl_installation_dir"

[node name="leInstDir" type="LineEdit" parent="Main/Tabs/Settings/InstDir"]
margin_left = 493.0
margin_right = 577.0
margin_bottom = 24.0
size_flags_horizontal = 10
middle_mouse_paste_enabled = false
placeholder_text = "placeholder_installation_dir"
caret_blink = true

[node name="Debug" type="VBoxContainer" parent="Main/Tabs"]
visible = false
anchor_right = 1.0
Expand Down Expand Up @@ -1284,7 +1304,7 @@ size_flags_horizontal = 4
text = "Print screen info"

[node name="Log" type="RichTextLabel" parent="Main"]
margin_top = 551.0
margin_top = 577.0
margin_right = 592.0
margin_bottom = 692.0
focus_mode = 2
Expand Down Expand Up @@ -1400,6 +1420,7 @@ __meta__ = {
[connection signal="value_changed" from="Main/Tabs/Settings/NumPrs/sbNumPRs" to="Main/Tabs/Settings" method="_on_sbNumPRs_value_changed"]
[connection signal="toggled" from="Main/Tabs/Settings/ScaleOverride/cbScaleOverrideEnable" to="Main/Tabs/Settings" method="_on_cbScaleOverrideEnable_toggled"]
[connection signal="value_changed" from="Main/Tabs/Settings/ScaleOverride/sbScaleOverride" to="Main/Tabs/Settings" method="_on_sbScaleOverride_value_changed"]
[connection signal="text_changed" from="Main/Tabs/Settings/InstDir/leInstDir" to="Main/Tabs/Settings" method="_on_leInstDir_text_changed"]
[connection signal="pressed" from="Main/Tabs/Debug/Button" to="Main/Tabs/Debug" method="_on_Button_pressed"]
[connection signal="pressed" from="Main/Tabs/Debug/Button2" to="Main/Tabs/Debug" method="_on_Button2_pressed"]
[connection signal="pressed" from="Main/Tabs/Debug/Button3" to="Main/Tabs/Debug" method="_on_Button3_pressed"]
Expand Down
2 changes: 1 addition & 1 deletion scripts/Catapult.gd
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,6 @@ func _activate_easter_egg() -> void:

Status.rainbow_text = true

for i in range(20):
for _i in range(20):
Status.post(tr("msg_easter_egg_activated"))
yield(get_tree().create_timer(0.1), "timeout")
4 changes: 2 additions & 2 deletions scripts/Debug.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func _on_Button2_pressed() -> void:
func _on_Button3_pressed():

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

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

func _on_Button5_pressed() -> void:

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

Expand Down
4 changes: 2 additions & 2 deletions scripts/ModManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ func retrieve_kenan_pack() -> void:
emit_signal("modpack_retrieval_started")
Status.post(tr("msg_getting_kenan_pack") % game.to_upper())

Downloader.download_file(pack["url"], Paths.own_dir, pack["filename"])
Downloader.download_file(pack["url"], Paths.catapult_dir, pack["filename"])
yield(Downloader, "download_finished")

var archive = Paths.own_dir.plus_file(pack["filename"])
var archive = Paths.catapult_dir.plus_file(pack["filename"])
if Directory.new().file_exists(archive):
FS.extract(archive, Paths.tmp_dir)
yield(FS, "extract_done")
Expand Down
6 changes: 3 additions & 3 deletions scripts/ReleaseInstaller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func install_release(release_info: Dictionary, game: String, update_in: String =
else:
Status.post(tr("msg_installing_game") % release_info["name"])

Downloader.download_file(release_info["url"], Paths.own_dir, release_info["filename"])
Downloader.download_file(release_info["url"], Paths.catapult_dir, release_info["filename"])
yield(Downloader, "download_finished")

var archive: String = Paths.own_dir.plus_file(release_info["filename"])
var archive: String = Paths.catapult_dir.plus_file(release_info["filename"])
if Directory.new().file_exists(archive):

FS.extract(archive, Paths.tmp_dir)
Expand All @@ -41,7 +41,7 @@ func install_release(release_info: Dictionary, game: String, update_in: String =
FS.rm_dir(target_dir)
yield(FS, "rm_dir_done")
else:
target_dir = Paths.next_install_dir
target_dir = Paths.next_data_dir

FS.move_dir(extracted_root, target_dir)
yield(FS, "move_dir_done")
Expand Down
7 changes: 7 additions & 0 deletions scripts/SettingsUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func _ready() -> void:
$ShowDebug.pressed = Settings.read("debug_mode")
$NumReleases/sbNumReleases.value = Settings.read("num_releases_to_request") as int
$NumPrs/sbNumPRs.value = Settings.read("num_prs_to_request") as int
var inst_dir = Settings.read("installation_dir")
$InstDir/leInstDir.text = inst_dir if inst_dir else ""

$ScaleOverride/cbScaleOverrideEnable.pressed = Settings.read("ui_scale_override_enabled")
$ScaleOverride/sbScaleOverride.editable = Settings.read("ui_scale_override_enabled")
Expand Down Expand Up @@ -123,6 +125,11 @@ func _on_sbNumPRs_value_changed(value: float) -> void:
Settings.store("num_prs_to_request", str(value))


func _on_leInstDir_text_changed(new_text: String) -> void:

Settings.store("installation_dir", new_text)


func _on_cbScaleOverrideEnable_toggled(button_pressed: bool) -> void:

Settings.store("ui_scale_override_enabled", button_pressed)
Expand Down
4 changes: 2 additions & 2 deletions scripts/SoundpackManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ func install_pack(soundpack_index: int, from_file = null, reinstall = false, kee
if from_file:
archive = from_file
else:
Downloader.download_file(pack["url"], Paths.own_dir, pack["filename"])
Downloader.download_file(pack["url"], Paths.catapult_dir, pack["filename"])
yield(Downloader, "download_finished")
archive = Paths.own_dir.plus_file(pack["filename"])
archive = Paths.catapult_dir.plus_file(pack["filename"])
if not Directory.new().file_exists(archive):
Status.post(tr("msg_sound_download_failed"), Enums.MSG_ERROR)
emit_signal("soundpack_installation_finished")
Expand Down
2 changes: 1 addition & 1 deletion scripts/SoundpacksUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func _on_ConfirmManualDownload_confirmed() -> void:
var pack = _sound.SOUNDPACKS[_available_list.get_selected_items()[0]]

OS.shell_open(pack["url"])
_dlg_file.current_dir = Paths.own_dir
_dlg_file.current_dir = Paths.catapult_dir
_dlg_file.popup_centered_ratio(0.9)


Expand Down
109 changes: 96 additions & 13 deletions scripts/path_helper.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ extends Node

signal status_message

var own_dir: String setget , _get_own_dir
var catapult_dir: String setget _set_catapult_dir , _get_catapult_dir
var settings_dir: String setget _set_settings_dir , _get_settings_dir
var installs_summary: Dictionary setget , _get_installs_summary
var game_dir: String setget , _get_game_dir
var next_install_dir: String setget , _get_next_install_dir
var next_data_dir: String setget , _get_next_data_dir
var userdata: String setget , _get_userdata_dir
var config: String setget , _get_config_dir
var savegames: String setget , _get_savegame_dir
Expand All @@ -30,19 +31,101 @@ var _last_active_install_name := ""
var _last_active_install_dir := ""


func _get_own_dir() -> String:
func _init() -> void:

catapult_dir = _determine_catapult_dir()
settings_dir = _determine_settings_dir()

Settings.load_settings(settings_dir)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed, it is called automatically when Settings.read is called for the first time.

var inst_dir_setting = Settings.read("installation_dir")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be a hard-coded default value for installation_dir, just like for all the other settings. Returning null here creates all sorts of problems. For example, calling replace() below on what is potentially a null. Consider an empty string as the default.

inst_dir_setting = inst_dir_setting.replace("~", OS.get_environment("HOME"))

if inst_dir_setting != null:
var d = Directory.new()

var error = 0
if not d.dir_exists(inst_dir_setting):
error = d.make_dir_recursive(inst_dir_setting)

if not error:
catapult_dir = inst_dir_setting


func _determine_catapult_dir() -> String:

for arg in OS.get_cmdline_args():
if arg.find("=") > -1: # if arg is a key-value pair
var arg_pair = arg.split("=")
if arg_pair[0] == "--catapult_dir_abs":
var dirname = arg_pair[1]

if not dirname.is_abs_path():
# relative paths are not supported, fallback to executable path
Status.post(tr("msg_path_not_absolute") % dirname, Enums.MSG_ERROR)
break

# Ensure that catapult_dir_abs exists
var d = Directory.new()
if not d.dir_exists(dirname):
var error = d.make_dir_recursive(dirname)
if error:
# something went wrong, fallback to executable path
Status.post(tr("msg_cannot_create_target_dir") % [dirname, error], Enums.MSG_ERROR)
break

return dirname

return OS.get_executable_path().get_base_dir()


func _determine_settings_dir() -> String:

var global_settings_dir: String

match OS.get_name():
"X11":
var xdg_config_home = OS.get_environment("XDG_CONFIG_HOME")
var config_home = xdg_config_home if not xdg_config_home.empty() else OS.get_environment("HOME").plus_file(".config")
global_settings_dir = config_home.plus_file("catapult/")
"Windows":
global_settings_dir = OS.get_environment("USERPROFILE").plus_file("AppData/Local/Catapult/")
_:
return catapult_dir

if Settings.dir_contains_settings(global_settings_dir):
return global_settings_dir
else:
return catapult_dir


func _set_catapult_dir(path: String) -> void:

catapult_dir = path


func _get_catapult_dir() -> String:

return catapult_dir


func _set_settings_dir(path: String) -> void:

settings_dir = path


func _get_settings_dir() -> String:

return settings_dir


func _get_installs_summary() -> Dictionary:

var result = {}
var d = Directory.new()

for game in ["dda", "bn"]:
var installs = {}
var base_dir = Paths.own_dir.plus_file(game)
var base_dir = Paths.catapult_dir.plus_file(game)
for subdir in FS.list_dir(base_dir):
var info_file = base_dir.plus_file(subdir).plus_file(Helpers.INFO_FILENAME)
if d.file_exists(info_file):
Expand All @@ -66,7 +149,7 @@ func _get_game_dir() -> String:
var active_name = Settings.read("active_install_" + Settings.read("game"))

if active_name == "":
return _get_next_install_dir()
return _get_next_data_dir()
elif active_name == _last_active_install_name:
return _last_active_install_dir
else:
Expand All @@ -76,7 +159,7 @@ func _get_game_dir() -> String:
func _find_active_game_dir() -> String:

var d = Directory.new()
var base_dir = _get_own_dir().plus_file(Settings.read("game"))
var base_dir = _get_catapult_dir().plus_file(Settings.read("game"))
for subdir in FS.list_dir(base_dir):
var curr_dir = base_dir.plus_file(subdir)
var info_file = curr_dir.plus_file("catapult_install_info.json")
Expand All @@ -89,11 +172,11 @@ func _find_active_game_dir() -> String:
return ""


func _get_next_install_dir() -> String:
func _get_next_data_dir() -> String:
# Finds a suitable directory name for a new game installation in the
# multi-install system. The names follow the pattern "game0, game1, ..."

var base_dir := _get_own_dir().plus_file(Settings.read("game"))
var base_dir := _get_catapult_dir().plus_file(Settings.read("game"))
var dir_number := 0
var d := Directory.new()
while d.dir_exists(base_dir.plus_file("game" + str(dir_number))):
Expand All @@ -103,7 +186,7 @@ func _get_next_install_dir() -> String:

func _get_userdata_dir() -> String:

return _get_own_dir().plus_file(Settings.read("game")).plus_file("userdata")
return _get_catapult_dir().plus_file(Settings.read("game")).plus_file("userdata")


func _get_config_dir() -> String:
Expand Down Expand Up @@ -168,19 +251,19 @@ func _get_graveyard_dir() -> String:

func _get_modrepo_dir() -> String:

return _get_own_dir().plus_file(Settings.read("game")).plus_file("mod_repo")
return _get_catapult_dir().plus_file(Settings.read("game")).plus_file("mod_repo")


func _get_tmp_dir() -> String:

return _get_own_dir().plus_file(Settings.read("game")).plus_file("tmp")
return _get_catapult_dir().plus_file(Settings.read("game")).plus_file("tmp")


func _get_utils_dir() -> String:

return _get_own_dir().plus_file("utils")
return _get_catapult_dir().plus_file("utils")


func _get_save_backups_dir() -> String:

return _get_own_dir().plus_file(Settings.read("game")).plus_file("save_backups")
return _get_catapult_dir().plus_file(Settings.read("game")).plus_file("save_backups")
15 changes: 11 additions & 4 deletions scripts/settings_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ func _exit_tree() -> void:
_write_to_file(_current, _settings_file)


func _load() -> void:
func load_settings(path: String = Paths.settings_dir) -> void:

_settings_file = Paths.own_dir.plus_file(_SETTINGS_FILENAME)
_settings_file = path.plus_file(_SETTINGS_FILENAME)

if File.new().file_exists(_settings_file):
_current = _read_from_file(_settings_file)

else:
_current = _HARDCODED_DEFAULTS
Status.post(tr("msg_creating_settings") % _SETTINGS_FILENAME)
Expand Down Expand Up @@ -82,10 +81,18 @@ func _write_to_file(data: Dictionary, path: String) -> void:
f.close()


func dir_contains_settings(path: String) ->bool:

var d = Directory.new()
d.open(path)

return d.file_exists(_SETTINGS_FILENAME)


func read(setting_name: String):

if len(_current) == 0:
_load()
load_settings()

if not setting_name in _current:
if setting_name in _HARDCODED_DEFAULTS:
Expand Down
3 changes: 3 additions & 0 deletions text/cs/path_helper.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"keys","en"
,
"msg_path_not_absolute","%s is not an absolute path. Falling back to executable path"
16 changes: 16 additions & 0 deletions text/cs/path_helper.csv.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[remap]

importer="csv_translation"
type="Translation"

[deps]

files=[ "res://text/cs/path_helper.en.translation" ]

source_file="res://text/cs/path_helper.csv"
dest_files=[ "res://text/cs/path_helper.en.translation" ]

[params]

compress=true
delimiter=0
3 changes: 3 additions & 0 deletions text/en/path_helper.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"keys","en"
,
"msg_path_not_absolute","%s is not an absolute path. Falling back to executable path"
Loading