Skip to content

Commit 28a653c

Browse files
committed
move other utility methods to ModLoaderUtils
1 parent 34420bd commit 28a653c

File tree

2 files changed

+91
-117
lines changed

2 files changed

+91
-117
lines changed

addons/mod_loader/mod_loader.gd

Lines changed: 8 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ var _saved_objects = []
8181

8282
func _init():
8383
# if mods are not enabled - don't load mods
84-
if REQUIRE_CMD_LINE && (!_check_cmd_line_arg("--enable-mods")):
84+
if REQUIRE_CMD_LINE and not ModLoaderUtils.is_running_with_command_line_arg("--enable-mods"):
8585
return
8686

8787
# Log game install dir
88-
ModLoaderUtils.log_info(str("game_install_directory: ", _get_local_folder_dir()), LOG_NAME)
88+
ModLoaderUtils.log_info("game_install_directory: %s" % ModLoaderUtils.get_local_folder_dir(), LOG_NAME)
8989

9090
# check if we want to use a different mods path that is provided as a command line argument
91-
var cmd_line_mod_path = _get_cmd_line_arg("--mods-path")
91+
var cmd_line_mod_path := ModLoaderUtils.get_cmd_line_arg_value("--mods-path")
9292
if cmd_line_mod_path != "":
9393
os_mods_path_override = cmd_line_mod_path
9494
ModLoaderUtils.log_info("The path mods are loaded from has been changed via the CLI arg `--mods-path`, to: " + cmd_line_mod_path, LOG_NAME)
9595

9696
# Check for the CLI arg that overrides the configs path
97-
var cmd_line_configs_path = _get_cmd_line_arg("--configs-path")
97+
var cmd_line_configs_path = ModLoaderUtils.get_cmd_line_arg_value("--configs-path")
9898
if cmd_line_configs_path != "":
9999
os_configs_path_override = cmd_line_configs_path
100100
ModLoaderUtils.log_info("The path configs are loaded from has been changed via the CLI arg `--configs-path`, to: " + cmd_line_configs_path, LOG_NAME)
@@ -154,7 +154,7 @@ func _init():
154154
# (UNPACKED_DIR)
155155
func _load_mod_zips():
156156
# Path to the games mod folder
157-
var game_mod_folder_path = _get_local_folder_dir("mods")
157+
var game_mod_folder_path = ModLoaderUtils.get_local_folder_dir("mods")
158158

159159
var dir = Directory.new()
160160
if dir.open(game_mod_folder_path) != OK:
@@ -257,7 +257,7 @@ func _setup_mods():
257257
# Load mod config JSONs from res://configs
258258
func _load_mod_configs():
259259
var found_configs_count = 0
260-
var configs_path = _get_local_folder_dir("configs")
260+
var configs_path = ModLoaderUtils.get_local_folder_dir("configs")
261261

262262
# CLI override, set with `--configs-path="C://path/configs"`
263263
# (similar to os_mods_path_override)
@@ -306,7 +306,7 @@ func _load_mod_configs():
306306
# which depends on the name used in a given mod ZIP (eg "mods-unpacked/Folder-Name")
307307
func _init_mod_data(mod_folder_path):
308308
# The file name should be a valid mod id
309-
var dir_name = _get_file_name(mod_folder_path, false, true)
309+
var dir_name = ModLoaderUtils.get_file_name_from_path(mod_folder_path, false, true)
310310

311311
# Path to the mod in UNPACKED_DIR (eg "res://mods-unpacked/My-Mod")
312312
var local_mod_path = str(UNPACKED_DIR, dir_name)
@@ -320,7 +320,7 @@ func _init_mod_data(mod_folder_path):
320320
# operation if a mod has a large number of files (eg. Brotato's Invasion mod,
321321
# which has ~1,000 files). That's why it's disabled by default
322322
if DEBUG_ENABLE_STORING_FILEPATHS:
323-
mod.file_paths = _get_flat_view_dict(local_mod_path)
323+
mod.file_paths = ModLoaderUtils.get_flat_view_dict(local_mod_path)
324324

325325

326326
# Run dependency checks on a mod, checking any dependencies it lists in its
@@ -403,113 +403,6 @@ func _init_mod(mod: ModData):
403403
add_child(mod_main_instance, true)
404404

405405

406-
# Utils (Mod Loader)
407-
# =============================================================================
408-
409-
# Util functions used in the mod loading process
410-
411-
# Check if the provided command line argument was present when launching the game
412-
func _check_cmd_line_arg(argument) -> bool:
413-
for arg in OS.get_cmdline_args():
414-
if arg == argument:
415-
return true
416-
417-
return false
418-
419-
# Get the command line argument value if present when launching the game
420-
func _get_cmd_line_arg(argument) -> String:
421-
for arg in OS.get_cmdline_args():
422-
if arg.find("=") > -1:
423-
var key_value = arg.split("=")
424-
# True if the checked argument matches a user-specified arg key
425-
# (eg. checking `--mods-path` will match with `--mods-path="C://mods"`
426-
if key_value[0] == argument:
427-
return key_value[1]
428-
429-
return ""
430-
431-
# Get the path to a local folder. Primarily used to get the (packed) mods
432-
# folder, ie "res://mods" or the OS's equivalent, as well as the configs path
433-
func _get_local_folder_dir(subfolder:String = ""):
434-
var game_install_directory = OS.get_executable_path().get_base_dir()
435-
436-
if OS.get_name() == "OSX":
437-
game_install_directory = game_install_directory.get_base_dir().get_base_dir()
438-
439-
# Fix for running the game through the Godot editor (as the EXE path would be
440-
# the editor's own EXE, which won't have any mod ZIPs)
441-
# if OS.is_debug_build():
442-
if OS.has_feature("editor"):
443-
game_install_directory = "res://"
444-
445-
return game_install_directory.plus_file(subfolder)
446-
447-
448-
func _get_file_name(path, is_lower_case = true, is_no_extension = false):
449-
var file_name = path.get_file()
450-
451-
if(is_lower_case):
452-
file_name = file_name.to_lower()
453-
454-
if(is_no_extension):
455-
var file_extension = file_name.get_extension()
456-
file_name = file_name.replace(str(".",file_extension), '')
457-
458-
return file_name
459-
460-
461-
# Get a flat array of all files in the target directory. This was needed in the
462-
# original version of this script, before becoming deprecated. It may still be
463-
# used if DEBUG_ENABLE_STORING_FILEPATHS is true.
464-
# Source: https://gist.github.com/willnationsdev/00d97aa8339138fd7ef0d6bd42748f6e
465-
func _get_flat_view_dict(p_dir = "res://", p_match = "", p_match_is_regex = false):
466-
var regex = null
467-
if p_match_is_regex:
468-
regex = RegEx.new()
469-
regex.compile(p_match)
470-
if not regex.is_valid():
471-
return []
472-
473-
var dirs = [p_dir]
474-
var first = true
475-
var data = []
476-
while not dirs.empty():
477-
var dir = Directory.new()
478-
var dir_name = dirs.back()
479-
dirs.pop_back()
480-
481-
if dir.open(dir_name) == OK:
482-
dir.list_dir_begin()
483-
var file_name = dir.get_next()
484-
while file_name != "":
485-
if not dir_name == "res://":
486-
first = false
487-
# ignore hidden, temporary, or system content
488-
if not file_name.begins_with(".") and not file_name.get_extension() in ["tmp", "import"]:
489-
# If a directory, then add to list of directories to visit
490-
if dir.current_is_dir():
491-
dirs.push_back(dir.get_current_dir() + "/" + file_name)
492-
# If a file, check if we already have a record for the same name
493-
else:
494-
var path = dir.get_current_dir() + ("/" if not first else "") + file_name
495-
# grab all
496-
if not p_match:
497-
data.append(path)
498-
# grab matching strings
499-
elif not p_match_is_regex and file_name.find(p_match, 0) != -1:
500-
data.append(path)
501-
# grab matching regex
502-
else:
503-
var regex_match = regex.search(path)
504-
if regex_match != null:
505-
data.append(path)
506-
# Move on to the next file in this directory
507-
file_name = dir.get_next()
508-
# We've exhausted all files in this directory. Close the iterator.
509-
dir.list_dir_end()
510-
return data
511-
512-
513406
# Helpers
514407
# =============================================================================
515408

addons/mod_loader/mod_loader_utils.gd

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ static func is_running_with_command_line_arg(argument: String) -> bool:
127127
# Get the command line argument value if present when launching the game
128128
static func get_cmd_line_arg_value(argument: String) -> String:
129129
for arg in OS.get_cmdline_args():
130-
if arg.find("=") > -1:
131-
var key_value = arg.split("=")
130+
if (arg as String).find("=") > -1:
131+
var key_value := (arg as String).split("=")
132132
# True if the checked argument matches a user-specified arg key
133133
# (eg. checking `--mods-path` will match with `--mods-path="C://mods"`
134134
if key_value[0] == argument:
@@ -147,3 +147,84 @@ static func get_date_time_string() -> String:
147147
]
148148

149149

150+
# Get the path to a local folder. Primarily used to get the (packed) mods
151+
# folder, ie "res://mods" or the OS's equivalent, as well as the configs path
152+
static func get_local_folder_dir(subfolder: String = "") -> String:
153+
var game_install_directory := OS.get_executable_path().get_base_dir()
154+
155+
if OS.get_name() == "OSX":
156+
game_install_directory = game_install_directory.get_base_dir().get_base_dir()
157+
158+
# Fix for running the game through the Godot editor (as the EXE path would be
159+
# the editor's own EXE, which won't have any mod ZIPs)
160+
# if OS.is_debug_build():
161+
if OS.has_feature("editor"):
162+
game_install_directory = "res://"
163+
164+
return game_install_directory.plus_file(subfolder)
165+
166+
167+
# Provide a path, get the file name at the end of the path
168+
static func get_file_name_from_path(path: String, make_lower_case := true, remove_extension := false) -> String:
169+
var file_name := path.get_file()
170+
171+
if make_lower_case:
172+
file_name = file_name.to_lower()
173+
174+
if remove_extension:
175+
file_name = file_name.trim_suffix("." + file_name.get_extension())
176+
177+
return file_name
178+
179+
180+
# Get a flat array of all files in the target directory. This was needed in the
181+
# original version of this script, before becoming deprecated. It may still be
182+
# used if DEBUG_ENABLE_STORING_FILEPATHS is true.
183+
# Source: https://gist.github.com/willnationsdev/00d97aa8339138fd7ef0d6bd42748f6e
184+
static func get_flat_view_dict(p_dir := "res://", p_match := "", p_match_is_regex := false) -> Array:
185+
var regex: RegEx
186+
if p_match_is_regex:
187+
regex = RegEx.new()
188+
regex.compile(p_match)
189+
if not regex.is_valid():
190+
return []
191+
192+
var dirs := [p_dir]
193+
var first := true
194+
var data := []
195+
while not dirs.empty():
196+
var dir := Directory.new()
197+
var dir_name: String = dirs.back()
198+
dirs.pop_back()
199+
200+
if dir.open(dir_name) == OK:
201+
dir.list_dir_begin()
202+
var file_name := dir.get_next()
203+
while file_name != "":
204+
if not dir_name == "res://":
205+
first = false
206+
# ignore hidden, temporary, or system content
207+
if not file_name.begins_with(".") and not file_name.get_extension() in ["tmp", "import"]:
208+
# If a directory, then add to list of directories to visit
209+
if dir.current_is_dir():
210+
dirs.push_back(dir.get_current_dir().plus_file(file_name))
211+
# If a file, check if we already have a record for the same name
212+
else:
213+
var path := dir.get_current_dir() + ("/" if not first else "") + file_name
214+
# grab all
215+
if not p_match:
216+
data.append(path)
217+
# grab matching strings
218+
elif not p_match_is_regex and file_name.find(p_match, 0) != -1:
219+
data.append(path)
220+
# grab matching regex
221+
else:
222+
var regex_match := regex.search(path)
223+
if regex_match != null:
224+
data.append(path)
225+
# Move on to the next file in this directory
226+
file_name = dir.get_next()
227+
# We've exhausted all files in this directory. Close the iterator.
228+
dir.list_dir_end()
229+
return data
230+

0 commit comments

Comments
 (0)