@@ -10,16 +10,16 @@ const MOD_LOADER_HOOKS_START_STRING := \
10
10
11
11
## finds function names used as setters and getters (excluding inline definitions)
12
12
## group 2 and 4 contain the xetter names
13
- static var regex_getter_setter := RegEx .create_from_string ("(.*?[sg]et\\ s*=\\ s*)(\\ w+)(\\ g<1>)?(\\ g<2>)?" )
13
+ var regex_getter_setter := RegEx .create_from_string ("(.*?[sg]et\\ s*=\\ s*)(\\ w+)(\\ g<1>)?(\\ g<2>)?" )
14
14
15
15
## finds every instance where super() is called
16
16
## returns only the super word, excluding the (, as match to make substitution easier
17
- static var regex_super_call := RegEx .create_from_string ("\\ bsuper(?=\\ s*\\ ()" )
17
+ var regex_super_call := RegEx .create_from_string ("\\ bsuper(?=\\ s*\\ ()" )
18
18
19
19
## matches the indented function body
20
20
## needs to start from the : of a function definition to work (offset)
21
21
## the body of a function is every line that is empty or starts with an indent or comment
22
- static var regex_func_body := RegEx .create_from_string ("(?smn)\\ N*(\\ n^(([\\ t #]+\\ N*)|$))*" )
22
+ var regex_func_body := RegEx .create_from_string ("(?smn)\\ N*(\\ n^(([\\ t #]+\\ N*)|$))*" )
23
23
24
24
25
25
var hashmap := {}
@@ -39,7 +39,7 @@ func process_script(path: String) -> String:
39
39
var class_prefix := str (hash (path ))
40
40
var method_store : Array [String ] = []
41
41
42
- var getters_setters := collect_getters_and_setters (source_code )
42
+ var getters_setters := collect_getters_and_setters (source_code , regex_getter_setter )
43
43
44
44
var moddable_methods := current_script .get_script_method_list ().filter (
45
45
func is_func_moddable (method : Dictionary ):
@@ -95,7 +95,14 @@ func process_script(path: String) -> String:
95
95
# including the methods from the scripts it extends,
96
96
# which leads to multiple entries in the list if they are overridden by the child script.
97
97
method_store .push_back (method .name )
98
- source_code = edit_vanilla_method (method .name , is_static , source_code , METHOD_PREFIX + class_prefix )
98
+ source_code = edit_vanilla_method (
99
+ method .name ,
100
+ is_static ,
101
+ source_code ,
102
+ regex_func_body ,
103
+ regex_super_call ,
104
+ METHOD_PREFIX + class_prefix
105
+ )
99
106
source_code_additions += "\n %s " % mod_loader_hook_string
100
107
101
108
# if we have some additions to the code, append them at the end
@@ -169,23 +176,39 @@ static func get_closing_paren_index(opening_paren_index: int, text: String) -> i
169
176
return closing_paren_index
170
177
171
178
172
- static func edit_vanilla_method (method_name : String , is_static : bool , text : String , prefix := METHOD_PREFIX , offset := 0 ) -> String :
179
+ static func edit_vanilla_method (
180
+ method_name : String ,
181
+ is_static : bool ,
182
+ text : String ,
183
+ regex_func_body : RegEx ,
184
+ regex_super_call : RegEx ,
185
+ prefix := METHOD_PREFIX ,
186
+ offset := 0
187
+ ) -> String :
173
188
var func_def := match_func_with_whitespace (method_name , text , offset )
174
189
175
190
if not func_def :
176
191
return text
177
192
178
193
if not is_top_level_func (text , func_def .get_start (), is_static ):
179
- return edit_vanilla_method (method_name , is_static , text , prefix , func_def .get_end ())
180
-
181
- text = fix_method_super (method_name , func_def .get_end (), text )
194
+ return edit_vanilla_method (
195
+ method_name ,
196
+ is_static ,
197
+ text ,
198
+ regex_func_body ,
199
+ regex_super_call ,
200
+ prefix ,
201
+ func_def .get_end ()
202
+ )
203
+
204
+ text = fix_method_super (method_name , func_def .get_end (), text , regex_func_body , regex_super_call )
182
205
text = text .erase (func_def .get_start (), func_def .get_end () - func_def .get_start ())
183
206
text = text .insert (func_def .get_start (), "func %s _%s (" % [prefix , method_name ])
184
207
185
208
return text
186
209
187
210
188
- static func fix_method_super (method_name : String , func_def_end : int , text : String , offset := 0 ) -> String :
211
+ static func fix_method_super (method_name : String , func_def_end : int , text : String , regex_func_body : RegEx , regex_super_call : RegEx , offset := 0 ) -> String :
189
212
var closing_paren_index := get_closing_paren_index (func_def_end , text )
190
213
var func_body_start_index := text .find (":" , closing_paren_index ) + 1
191
214
@@ -219,7 +242,8 @@ static func get_mod_loader_hook(
219
242
script_path : String ,
220
243
hash_before :int ,
221
244
hash_after :int ,
222
- method_prefix := METHOD_PREFIX ) -> String :
245
+ method_prefix := METHOD_PREFIX
246
+ ) -> String :
223
247
var type_string := " -> %s " % method_type if not method_type .is_empty () else ""
224
248
var static_string := "static " if is_static else ""
225
249
# Cannot use "self" inside a static function.
@@ -319,7 +343,7 @@ static func get_return_type_string(return_data: Dictionary) -> String:
319
343
return "%s%s " % [type_base , type_hint ]
320
344
321
345
322
- static func collect_getters_and_setters (text : String ) -> Dictionary :
346
+ static func collect_getters_and_setters (text : String , regex_getter_setter : RegEx ) -> Dictionary :
323
347
var result := {}
324
348
# a valid match has 2 or 4 groups, split into the method names and the rest of the line
325
349
# (var example: set = )(example_setter)(, get = )(example_getter)
0 commit comments