Skip to content

Commit 6d47f36

Browse files
committed
feat: 🚧 WIP: replace super()
1 parent 976c923 commit 6d47f36

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

addons/mod_loader/_export_plugin/export_plugin.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extends EditorExportPlugin
22

33
const ModHookPreprocessorScript := preload("res://addons/mod_loader/internal/mod_hook_preprocessor.gd")
4-
static var ModHookPreprocessor
4+
static var ModHookPreprocessor: ModLoaderHookPreprocessor
55

66

77
func _get_name() -> String:

addons/mod_loader/internal/mod_hook_preprocessor.gd

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
class_name ModLoaderHookPreprocessor
12
extends Object
23

34
const REQUIRE_EXPLICIT_ADDITION := false
@@ -7,6 +8,7 @@ const HASH_COLLISION_ERROR := "MODDING EXPORT ERROR: Hash collision between %s a
78
static var regex_getter_setter: RegEx
89

910
var hashmap := {}
11+
var previous_method := {}
1012

1113

1214
func process_begin() -> void:
@@ -29,8 +31,18 @@ func process_script(path: String) -> String:
2931

3032
var getters_setters := collect_getters_and_setters(source_code)
3133

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+
3344
var method_first_line_start := get_index_at_method_start(method.name, source_code)
45+
3446
if method_first_line_start == -1 or method.name in method_store:
3547
continue
3648

@@ -130,7 +142,7 @@ static func get_function_parameters(method_name: String, text: String, is_static
130142
# Extract the substring between the parentheses
131143
var param_string := text.substr(opening_paren_index + 1, closing_paren_index - opening_paren_index - 1)
132144

133-
# Clean whitespace characters (spaces, newlines, tabs)
145+
# Clean trailing characters and whitespace
134146
param_string = param_string.strip_edges()\
135147
.replace(" ", "")\
136148
.replace("\n", "")\
@@ -164,6 +176,15 @@ static func match_func_with_whitespace(method_name: String, text: String, offset
164176
return func_with_whitespace.search(text, offset)
165177

166178

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+
167188
static func get_mod_loader_hook(
168189
method_name: String,
169190
method_arg_string_names_only: String,
@@ -289,3 +310,54 @@ static func collect_getters_and_setters(text: String) -> Dictionary:
289310
result[mat.get_string(4)] = null
290311

291312
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

Comments
 (0)