Skip to content

Commit c3e6603

Browse files
authored
fix: 🐛 use reader.get_files().has() before 4.2 (#545)
Moved engine version-related functionality to `_ModLoaderGodot` and added `@tool` to `_ModLoaderGodot` to allow access in the editor. Added a version check to `_ModLoaderFile.file_exists_in_zip()` because `ZIPReader.file_exists()` was introduced in Godot 4.2.
1 parent 39f6059 commit c3e6603

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

addons/mod_loader/internal/file.gd

+5-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ static func file_exists_in_zip(zip_path: String, path: String) -> bool:
194194
var reader := zip_reader_open(zip_path)
195195
if not reader:
196196
return false
197-
return reader.file_exists(path.trim_prefix("res://"))
197+
198+
if _ModLoaderGodot.is_version_below(_ModLoaderGodot.ENGINE_VERSION_HEX_4_2_0):
199+
return reader.get_files().has(path.trim_prefix("res://"))
200+
else:
201+
return reader.file_exists(path.trim_prefix("res://"))
198202

199203

200204
static func get_mod_dir_name_in_zip(zip_path: String) -> String:

addons/mod_loader/internal/godot.gd

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@tool
12
class_name _ModLoaderGodot
23
extends Object
34

@@ -8,6 +9,11 @@ extends Object
89
const LOG_NAME := "ModLoader:Godot"
910
const AUTOLOAD_CONFIG_HELP_MSG := "To configure your autoloads, go to Project > Project Settings > Autoload."
1011

12+
const ENGINE_VERSION_HEX_4_2_2 := 0x040202
13+
const ENGINE_VERSION_HEX_4_2_0 := 0x040200
14+
15+
static var engine_version_hex: int = Engine.get_version_info().hex
16+
1117

1218
# Check autoload positions:
1319
# Ensure 1st autoload is `ModLoaderStore`, and 2nd is `ModLoader`.
@@ -100,3 +106,11 @@ static func get_autoload_index(autoload_name: String) -> int:
100106
var autoload_index := autoloads.find(autoload_name)
101107

102108
return autoload_index
109+
110+
111+
static func is_version_below(version_hex: int) -> bool:
112+
return engine_version_hex < version_hex
113+
114+
115+
static func is_version_above(version_hex: int) -> bool:
116+
return engine_version_hex > version_hex

addons/mod_loader/internal/mod_hook_preprocessor.gd

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ const HASH_COLLISION_ERROR := \
1414
"MODDING HOOKS ERROR: Hash collision between %s and %s. The collision can be resolved by renaming one of the methods or changing their script's path."
1515
const MOD_LOADER_HOOKS_START_STRING := \
1616
"\n# ModLoader Hooks - The following code has been automatically added by the Godot Mod Loader."
17-
const ENGINE_VERSION_HEX_4_2_2 := 0x040202
1817

1918
## \\bfunc\\b\\s+ -> Match the word 'func' and one or more whitespace characters
2019
## \\b%s\\b -> the function name
2120
## (?:.*\\n*)*?\\s*\\( -> Match any character between zero and unlimited times, but be lazy
2221
## and only do this until a '(' is found.
2322
const REGEX_MATCH_FUNC_WITH_WHITESPACE := "\\bfunc\\b\\s+\\b%s\\b(?:.*\\n*)*?\\s*\\("
2423

25-
static var engine_version_hex: int = Engine.get_version_info().hex
26-
2724
## finds function names used as setters and getters (excluding inline definitions)
2825
## group 2 and 4 contain the setter/getter names
2926
var regex_getter_setter := RegEx.create_from_string("(.*?[sg]et\\s*=\\s*)(\\w+)(\\g<1>)?(\\g<2>)?")
@@ -337,7 +334,7 @@ func edit_vanilla_method(
337334

338335

339336
func fix_method_super(method_name: String, func_body: RegExMatch, text: String) -> String:
340-
if engine_version_hex < ENGINE_VERSION_HEX_4_2_2:
337+
if _ModLoaderGodot.is_version_below(_ModLoaderGodot.ENGINE_VERSION_HEX_4_2_2):
341338
return fix_method_super_before_4_2_2(method_name, func_body, text)
342339

343340
return regex_super_call.sub(

0 commit comments

Comments
 (0)