Skip to content

Commit 903cbeb

Browse files
authored
✔ run setup based on position in autoloads (#105)
1 parent 0b76416 commit 903cbeb

File tree

1 file changed

+27
-48
lines changed

1 file changed

+27
-48
lines changed

addons/mod_loader/mod_loader_setup.gd

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,34 @@ var is_setup_create_override_cfg : bool = modloaderutils.is_running_with_command
3939

4040

4141
func _init() -> void:
42-
try_setup_modloader()
43-
var _changescene_error: int = change_scene(ProjectSettings.get_setting("application/run/main_scene"))
42+
modloaderutils.log_debug("ModLoader setup initialized", LOG_NAME)
4443

45-
46-
# Set up the ModLoader, if it hasn't been set up yet
47-
func try_setup_modloader() -> void:
4844
# Avoid doubling the setup work
49-
if is_loader_setup_applied():
50-
modloaderutils.log_info("ModLoader is available, mods can be loaded!", LOG_NAME)
51-
OS.set_window_title("%s (Modded)" % ProjectSettings.get_setting("application/config/name"))
45+
# Checks if the ModLoader Node is in the root of the scene tree
46+
# and if the IS_LOADER_SETUP_APPLIED project setting is there
47+
if modloaderutils.get_autoload_index("ModLoader") == 0:
48+
modded_start()
5249
return
5350

54-
setup_file_data()
5551
setup_modloader()
5652

57-
# If the loader is set up, but the override is not applied yet,
58-
# prompt the user to quit and restart the game.
59-
if is_loader_set_up() and not is_loader_setup_applied():
60-
modloaderutils.log_info("ModLoader is set up, but the game needs to be restarted", LOG_NAME)
61-
ProjectSettings.set_setting(settings.IS_LOADER_SETUP_APPLIED, true)
6253

63-
if is_setup_create_override_cfg:
64-
handle_override_cfg()
65-
else:
66-
handle_project_binary()
54+
# ModLoader already setup - switch to the main scene
55+
func modded_start() -> void:
56+
modloaderutils.log_info("ModLoader is available, mods can be loaded!", LOG_NAME)
6757

68-
match true:
69-
# If the --only-setup cli argument is passed, quit with exit code 0
70-
is_only_setup:
71-
quit(0)
72-
# If no cli argument is passed, show message with OS.alert() and user has to restart the game
73-
_:
74-
OS.alert("The Godot ModLoader has been set up. Restart the game to apply the changes. Confirm to quit.")
75-
quit(0)
58+
OS.set_window_title("%s (Modded)" % ProjectSettings.get_setting("application/config/name"))
59+
60+
var _error_change_scene_main := change_scene(ProjectSettings.get_setting("application/run/main_scene"))
7661

7762

7863
# Set up the ModLoader as an autoload and register the other global classes.
79-
# Saved as override.cfg besides the game executable to extend the existing project settings
8064
func setup_modloader() -> void:
8165
modloaderutils.log_info("Setting up ModLoader", LOG_NAME)
8266

67+
# Setup path and file_name dict with all required paths and file names.
68+
setup_file_data()
69+
8370
# Register all new helper classes as global
8471
modloaderutils.register_global_classes_from_array(new_global_classes)
8572

@@ -96,7 +83,17 @@ func setup_modloader() -> void:
9683
else:
9784
handle_project_binary()
9885

99-
modloaderutils.log_info("ModLoader setup complete", LOG_NAME)
86+
# ModLoader is set up. A game restart is required to apply the ProjectSettings.
87+
modloaderutils.log_info("ModLoader is set up, a game restart is required.", LOG_NAME)
88+
89+
match true:
90+
# If the --only-setup cli argument is passed, quit with exit code 0
91+
is_only_setup:
92+
quit(0)
93+
# If no cli argument is passed, show message with OS.alert() and user has to restart the game
94+
_:
95+
OS.alert("The Godot ModLoader has been set up. Restart the game to apply the changes. Confirm to quit.")
96+
quit(0)
10097

10198

10299
# Reorders the autoloads in the project settings, to get the ModLoader on top.
@@ -122,11 +119,13 @@ func reorder_autoloads() -> void:
122119

123120
# Saves the ProjectSettings to a override.cfg file in the base game directory.
124121
func handle_override_cfg() -> void:
122+
modloaderutils.log_debug("using the override.cfg file", LOG_NAME)
125123
var _save_custom_error: int = ProjectSettings.save_custom(modloaderutils.get_override_path())
126124

127125

128126
# Creates the project.binary file, adds it to the pck and removes the no longer needed project.binary file.
129127
func handle_project_binary() -> void:
128+
modloaderutils.log_debug("injecting the project.binary file", LOG_NAME)
130129
create_project_binary()
131130
inject_project_binary()
132131
clean_up_project_binary_file()
@@ -179,23 +178,3 @@ func setup_file_data() -> void:
179178

180179
modloaderutils.log_debug_json_print("path: ", path, LOG_NAME)
181180
modloaderutils.log_debug_json_print("file_name: ", file_name, LOG_NAME)
182-
183-
184-
func is_loader_set_up() -> bool:
185-
return is_project_setting_true(settings.IS_LOADER_SET_UP)
186-
187-
188-
func is_loader_setup_applied() -> bool:
189-
if not root.get_node_or_null("/root/ModLoader") == null:
190-
if not is_project_setting_true(settings.IS_LOADER_SETUP_APPLIED):
191-
modloaderutils.log_info("ModLoader is already set up. No self setup required.", LOG_NAME)
192-
return true
193-
return false
194-
195-
196-
static func is_project_setting_true(project_setting: String) -> bool:
197-
return ProjectSettings.has_setting(project_setting) and\
198-
ProjectSettings.get_setting(project_setting)
199-
200-
201-

0 commit comments

Comments
 (0)