1
+ class_name ModLoaderHookPreprocessor
1
2
extends Object
2
3
3
4
const REQUIRE_EXPLICIT_ADDITION := false
@@ -7,6 +8,7 @@ const HASH_COLLISION_ERROR := "MODDING EXPORT ERROR: Hash collision between %s a
7
8
static var regex_getter_setter : RegEx
8
9
9
10
var hashmap := {}
11
+ var previous_method := {}
10
12
11
13
12
14
func process_begin () -> void :
@@ -29,8 +31,18 @@ func process_script(path: String) -> String:
29
31
30
32
var getters_setters := collect_getters_and_setters (source_code )
31
33
32
- for method in current_script .get_script_method_list ():
34
+ var script_method_list := current_script .get_script_method_list ()
35
+
36
+ for i in script_method_list .size ():
37
+ var method : Dictionary = script_method_list [i ]
38
+
39
+ if i > 0 :
40
+ # get_script_method_list() returns the methods in order they are in the source_code
41
+ # we can use that to get the code between two funcs.
42
+ previous_method = script_method_list [i - 1 ]
43
+
33
44
var method_first_line_start := get_index_at_method_start (method .name , source_code )
45
+
34
46
if method_first_line_start == - 1 or method .name in method_store :
35
47
continue
36
48
@@ -130,7 +142,7 @@ static func get_function_parameters(method_name: String, text: String, is_static
130
142
# Extract the substring between the parentheses
131
143
var param_string := text .substr (opening_paren_index + 1 , closing_paren_index - opening_paren_index - 1 )
132
144
133
- # Clean whitespace characters (spaces, newlines, tabs)
145
+ # Clean trailing characters and whitespace
134
146
param_string = param_string .strip_edges ()\
135
147
.replace (" " , "" )\
136
148
.replace ("\n " , "" )\
@@ -164,6 +176,15 @@ static func match_func_with_whitespace(method_name: String, text: String, offset
164
176
return func_with_whitespace .search (text , offset )
165
177
166
178
179
+ # TODO: Check for comment
180
+ static func match_super_with_whitespace_all (text : String , offset := 0 ) -> Array [RegExMatch ]:
181
+ var func_with_whitespace := RegEx .new ()
182
+ func_with_whitespace .compile ("super\\ s*\\ (" )
183
+
184
+ # Search for the function definition
185
+ return func_with_whitespace .search_all (text , offset )
186
+
187
+
167
188
static func get_mod_loader_hook (
168
189
method_name : String ,
169
190
method_arg_string_names_only : String ,
@@ -289,3 +310,54 @@ static func collect_getters_and_setters(text: String) -> Dictionary:
289
310
result [mat .get_string (4 )] = null
290
311
291
312
return result
313
+
314
+
315
+ static func get_string_between_functions (method_name_start : String , method_name_end : String , text : String ) -> void :
316
+ var method_start_match := match_func_with_whitespace (method_name_start , text )
317
+ var method_end_match := match_func_with_whitespace (method_name_end , text )
318
+
319
+ return text .substr (method_start_match .get_start (), method_end_match .get_start ())
320
+
321
+
322
+ static func get_super_string (method_name : String , text : String , is_static : bool , offset := 0 ) -> String :
323
+ var result := match_func_with_whitespace (method_name , text , offset )
324
+ if result == null :
325
+ return ""
326
+
327
+ # Find the index of the opening parenthesis
328
+ var opening_paren_index := result .get_end () - 1
329
+ if opening_paren_index == - 1 :
330
+ return ""
331
+
332
+ if not is_top_level_func (text , result .get_start (), is_static ):
333
+ return get_function_parameters (method_name , text , is_static , result .get_end ())
334
+
335
+ # Use a stack to match parentheses
336
+ var stack := []
337
+ var closing_paren_index := opening_paren_index
338
+ while closing_paren_index < text .length ():
339
+ var char := text [closing_paren_index ]
340
+ if char == '(' :
341
+ stack .push_back ('(' )
342
+ elif char == ')' :
343
+ stack .pop_back ()
344
+ if stack .size () == 0 :
345
+ break
346
+ closing_paren_index += 1
347
+
348
+ # If the stack is not empty, that means there's no matching closing parenthesis
349
+ if stack .size () != 0 :
350
+ return ""
351
+
352
+ # Extract the substring between the parentheses
353
+ var param_string := text .substr (opening_paren_index + 1 , closing_paren_index - opening_paren_index - 1 )
354
+
355
+ # Clean trailing characters and whitespace
356
+ param_string = param_string .strip_edges ()\
357
+ .replace (" " , "" )\
358
+ .replace ("\n " , "" )\
359
+ .replace ("\t " , "" )\
360
+ .replace ("," , ", " )\
361
+ .replace (":" , ": " )
362
+
363
+ return param_string
0 commit comments