Skip to content

Commit 11d9a00

Browse files
committed
refactor: 🚧 general code cleanup
1 parent 0f907e6 commit 11d9a00

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

addons/mod_loader/_export_plugin/export_plugin.gd

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ func _export_file(path: String, type: String, features: PackedStringArray) -> vo
2323
if path.begins_with("res://addons") or path.begins_with("res://mods-unpacked"):
2424
return
2525

26-
if type != "GDScript":
26+
if type != "GDScript":
2727
return
2828

2929
var current_script := load(path) as GDScript
3030
var source_code := current_script.source_code
3131
var source_code_additions := ""
3232

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
3535
var class_prefix := str(hash(path))
3636
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"
4039

4140
var getters_setters := collect_getters_and_setters(source_code)
4241

@@ -109,24 +108,18 @@ static func get_function_arg_name_string(args: Array) -> String:
109108

110109

111110
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)
119112
if result == null:
120113
return ""
121114

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-
125115
# Find the index of the opening parenthesis
126116
var opening_paren_index := result.get_end() - 1
127117
if opening_paren_index == -1:
128118
return ""
129119

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+
130123
# Use a stack to match parentheses
131124
var stack := []
132125
var closing_paren_index := opening_paren_index
@@ -147,31 +140,38 @@ static func get_function_parameters(method_name: String, text: String, is_static
147140
# Extract the substring between the parentheses
148141
var param_string := text.substr(opening_paren_index + 1, closing_paren_index - opening_paren_index - 1)
149142

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(":", ": ")
152150

153151
return param_string
154152

155153

156154
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)
161156

162-
var result := regex.search(text, offset)
157+
if not result:
158+
return text
163159

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())
167162

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
170167

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)
175175

176176

177177
static func get_mod_loader_hook(
@@ -215,6 +215,7 @@ static func get_mod_loader_hook(
215215
"%HOOK_ID_AFTER%" : hash_after,
216216
})
217217

218+
218219
static func get_previous_line_to(text: String, index: int) -> String:
219220
if index <= 0 or index >= text.length():
220221
return ""
@@ -249,12 +250,7 @@ static func is_func_moddable(method_start_idx, text) -> bool:
249250

250251

251252
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)
258254

259255
if result:
260256
return text.find("\n", result.get_end())

0 commit comments

Comments
 (0)