@@ -23,20 +23,19 @@ func _export_file(path: String, type: String, features: PackedStringArray) -> vo
23
23
if path .begins_with ("res://addons" ) or path .begins_with ("res://mods-unpacked" ):
24
24
return
25
25
26
- if type != "GDScript" :
26
+ if type != "GDScript" :
27
27
return
28
28
29
29
var current_script := load (path ) as GDScript
30
30
var source_code := current_script .source_code
31
31
var source_code_additions := ""
32
32
33
- # we need to stop all vanilla methods from forming inheritance chains
34
- # since the generated methods will fulfill inheritance requirements
33
+ # We need to stop all vanilla methods from forming inheritance chains,
34
+ # since the generated methods will fulfill inheritance requirements
35
35
var class_prefix := str (hash (path ))
36
36
var method_store : Array [String ] = []
37
- var mod_loader_hooks_start_string := """
38
- # ModLoader Hooks - The following code has been automatically added by the Godot Mod Loader export plugin.
39
- """
37
+ var mod_loader_hooks_start_string := \
38
+ "\n # ModLoader Hooks - The following code has been automatically added by the Godot Mod Loader export plugin.\n "
40
39
41
40
var getters_setters := collect_getters_and_setters (source_code )
42
41
@@ -109,24 +108,18 @@ static func get_function_arg_name_string(args: Array) -> String:
109
108
110
109
111
110
static func get_function_parameters (method_name : String , text : String , is_static : bool , offset := 0 ) -> String :
112
- # Regular expression to match the function definition with arbitrary whitespace
113
- var pattern := "func\\ s+" + method_name + "\\ s*\\ ("
114
- var regex := RegEx .new ()
115
- regex .compile (pattern )
116
-
117
- # Search for the function definition
118
- var result := regex .search (text , offset )
111
+ var result := match_func_with_whitespace (method_name , text , offset )
119
112
if result == null :
120
113
return ""
121
114
122
- if not is_top_level_func (text , result .get_start (), is_static ):
123
- return get_function_parameters (method_name , text , is_static , result .get_end ())
124
-
125
115
# Find the index of the opening parenthesis
126
116
var opening_paren_index := result .get_end () - 1
127
117
if opening_paren_index == - 1 :
128
118
return ""
129
119
120
+ if not is_top_level_func (text , result .get_start (), is_static ):
121
+ return get_function_parameters (method_name , text , is_static , result .get_end ())
122
+
130
123
# Use a stack to match parentheses
131
124
var stack := []
132
125
var closing_paren_index := opening_paren_index
@@ -147,31 +140,38 @@ static func get_function_parameters(method_name: String, text: String, is_static
147
140
# Extract the substring between the parentheses
148
141
var param_string := text .substr (opening_paren_index + 1 , closing_paren_index - opening_paren_index - 1 )
149
142
150
- # Remove all whitespace characters (spaces, newlines, tabs) from the parameter string
151
- param_string = param_string .strip_edges ()
143
+ # Clean whitespace characters (spaces, newlines, tabs)
144
+ param_string = param_string .strip_edges ()\
145
+ .replace (" " , "" )\
146
+ .replace ("\n " , "" )\
147
+ .replace ("\t " , "" )\
148
+ .replace ("," , ", " )\
149
+ .replace (":" , ": " )
152
150
153
151
return param_string
154
152
155
153
156
154
static func prefix_method_name (method_name : String , is_static : bool , text : String , prefix := METHOD_PREFIX , offset := 0 ) -> String :
157
- # Regular expression to match the function definition with arbitrary whitespace
158
- var pattern := "func\\ s+%s \\ s*\\ (" % method_name
159
- var regex := RegEx .new ()
160
- regex .compile (pattern )
155
+ var result := match_func_with_whitespace (method_name , text , offset )
161
156
162
- var result := regex .search (text , offset )
157
+ if not result :
158
+ return text
163
159
164
- if result :
165
- if not is_top_level_func (text , result .get_start (), is_static ):
166
- return prefix_method_name (method_name , is_static , text , prefix , result .get_end ())
160
+ if not is_top_level_func (text , result .get_start (), is_static ):
161
+ return prefix_method_name (method_name , is_static , text , prefix , result .get_end ())
167
162
168
- text = text .erase (result .get_start (), result .get_end () - result .get_start ())
169
- text = text .insert (result .get_start (), "func %s _%s (" % [prefix , method_name ])
163
+ text = text .erase (result .get_start (), result .get_end () - result .get_start ())
164
+ text = text .insert (result .get_start (), "func %s _%s (" % [prefix , method_name ])
165
+
166
+ return text
170
167
171
- return text
172
- else :
173
- print ("WHAT?!" )
174
- return text
168
+
169
+ static func match_func_with_whitespace (method_name : String , text : String , offset := 0 ) -> RegExMatch :
170
+ var func_with_whitespace := RegEx .new ()
171
+ func_with_whitespace .compile ("func\\ s+%s \\ s*\\ (" % method_name )
172
+
173
+ # Search for the function definition
174
+ return func_with_whitespace .search (text , offset )
175
175
176
176
177
177
static func get_mod_loader_hook (
@@ -215,6 +215,7 @@ static func get_mod_loader_hook(
215
215
"%HOOK_ID_AFTER%" : hash_after ,
216
216
})
217
217
218
+
218
219
static func get_previous_line_to (text : String , index : int ) -> String :
219
220
if index <= 0 or index >= text .length ():
220
221
return ""
@@ -249,12 +250,7 @@ static func is_func_moddable(method_start_idx, text) -> bool:
249
250
250
251
251
252
static func get_index_at_method_start (method_name : String , text : String ) -> int :
252
- # Regular expression to match the function definition with arbitrary whitespace
253
- var pattern := "func\\ s+%s \\ s*\\ (" % method_name
254
- var regex := RegEx .new ()
255
- regex .compile (pattern )
256
-
257
- var result := regex .search (text )
253
+ var result := match_func_with_whitespace (method_name , text )
258
254
259
255
if result :
260
256
return text .find ("\n " , result .get_end ())
0 commit comments