Skip to content

Commit 7c78a92

Browse files
committed
refactor: ♻️ remove static from regex vars
1 parent 8343f52 commit 7c78a92

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

addons/mod_loader/internal/mod_hook_preprocessor.gd

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ const MOD_LOADER_HOOKS_START_STRING := \
1010

1111
## finds function names used as setters and getters (excluding inline definitions)
1212
## 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>)?")
1414

1515
## finds every instance where super() is called
1616
## 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*\\()")
1818

1919
## matches the indented function body
2020
## needs to start from the : of a function definition to work (offset)
2121
## 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*)|$))*")
2323

2424

2525
var hashmap := {}
@@ -39,7 +39,7 @@ func process_script(path: String) -> String:
3939
var class_prefix := str(hash(path))
4040
var method_store: Array[String] = []
4141

42-
var getters_setters := collect_getters_and_setters(source_code)
42+
var getters_setters := collect_getters_and_setters(source_code, regex_getter_setter)
4343

4444
var moddable_methods := current_script.get_script_method_list().filter(
4545
func is_func_moddable(method: Dictionary):
@@ -95,7 +95,14 @@ func process_script(path: String) -> String:
9595
# including the methods from the scripts it extends,
9696
# which leads to multiple entries in the list if they are overridden by the child script.
9797
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+
)
99106
source_code_additions += "\n%s" % mod_loader_hook_string
100107

101108
#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
169176
return closing_paren_index
170177

171178

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:
173188
var func_def := match_func_with_whitespace(method_name, text, offset)
174189

175190
if not func_def:
176191
return text
177192

178193
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)
182205
text = text.erase(func_def.get_start(), func_def.get_end() - func_def.get_start())
183206
text = text.insert(func_def.get_start(), "func %s_%s(" % [prefix, method_name])
184207

185208
return text
186209

187210

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:
189212
var closing_paren_index := get_closing_paren_index(func_def_end, text)
190213
var func_body_start_index := text.find(":", closing_paren_index) +1
191214

@@ -219,7 +242,8 @@ static func get_mod_loader_hook(
219242
script_path: String,
220243
hash_before:int,
221244
hash_after:int,
222-
method_prefix := METHOD_PREFIX) -> String:
245+
method_prefix := METHOD_PREFIX
246+
) -> String:
223247
var type_string := " -> %s" % method_type if not method_type.is_empty() else ""
224248
var static_string := "static " if is_static else ""
225249
# Cannot use "self" inside a static function.
@@ -319,7 +343,7 @@ static func get_return_type_string(return_data: Dictionary) -> String:
319343
return "%s%s" % [type_base, type_hint]
320344

321345

322-
static func collect_getters_and_setters(text: String) -> Dictionary:
346+
static func collect_getters_and_setters(text: String, regex_getter_setter: RegEx) -> Dictionary:
323347
var result := {}
324348
# a valid match has 2 or 4 groups, split into the method names and the rest of the line
325349
# (var example: set = )(example_setter)(, get = )(example_getter)

0 commit comments

Comments
 (0)