Skip to content

Commit 6cdb63c

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

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

addons/mod_loader/internal/mod_hook_preprocessor.gd

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func process_script(path: String) -> String:
2121
var current_script := load(path) as GDScript
2222
var source_code := current_script.source_code
2323
var source_code_additions := ""
24-
24+
print(path)
25+
print("----------------------------")
2526
# We need to stop all vanilla methods from forming inheritance chains,
2627
# since the generated methods will fulfill inheritance requirements
2728
var class_prefix := str(hash(path))
@@ -30,17 +31,10 @@ func process_script(path: String) -> String:
3031
"\n# ModLoader Hooks - The following code has been automatically added by the Godot Mod Loader export plugin."
3132

3233
var getters_setters := collect_getters_and_setters(source_code)
33-
3434
var script_method_list := current_script.get_script_method_list()
3535

3636
for i in script_method_list.size():
3737
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-
4438
var method_first_line_start := get_index_at_method_start(method.name, source_code)
4539

4640
if method_first_line_start == -1 or method.name in method_store:
@@ -52,6 +46,44 @@ func process_script(path: String) -> String:
5246
if not is_func_moddable(method_first_line_start, source_code):
5347
continue
5448

49+
if i > 0:
50+
# get_script_method_list() returns the methods in order they are in the source_code
51+
# we can use that to get the code between two funcs.
52+
previous_method = script_method_list[i - 1]
53+
if not previous_method.name.begins_with("@"):
54+
var method_name_start := "%s%s_%s" % [METHOD_PREFIX, class_prefix, previous_method.name]
55+
var method_start_match := match_func_with_whitespace(method_name_start, source_code)
56+
var method_end_match := match_func_with_whitespace(method.name, source_code)
57+
var code_between_funcs := ""
58+
59+
if not method_start_match:
60+
print("No match for \"%s\"" % previous_method.name)
61+
62+
if not method_end_match:
63+
print("No match for \"%s\"" % method.name)
64+
65+
# TODO: Add a better last method check
66+
if source_code.find("func ", method_end_match.get_end()) == -1:
67+
print("last method! -> %s" % method.name)
68+
code_between_funcs = source_code.substr(method_end_match.get_start())
69+
else:
70+
code_between_funcs = source_code.substr(method_start_match.get_start(), method_end_match.get_start() - method_start_match.get_start())
71+
72+
var supers := match_super_with_whitespace_all(code_between_funcs)
73+
print(supers.size())
74+
for super_result in supers:
75+
print("super detected!")
76+
var super_arg_string := get_super_arg_string(code_between_funcs, super_result.get_end() - 1)
77+
print("super_arg_string")
78+
print(super_arg_string)
79+
code_between_funcs = code_between_funcs.replace("super(%s)" % super_arg_string, "super.%s(%s)" % [previous_method.name, super_arg_string])
80+
81+
print("new_code: ")
82+
print(code_between_funcs)
83+
source_code = source_code.erase(method_start_match.get_start(), method_end_match.get_start() - method_start_match.get_start())
84+
source_code = source_code.insert(method_start_match.get_start(), code_between_funcs)
85+
86+
5587
var type_string := get_return_type_string(method.return)
5688
var is_static := true if method.flags == METHOD_FLAG_STATIC + METHOD_FLAG_NORMAL else false
5789
var method_arg_string_with_defaults_and_types := get_function_parameters(method.name, source_code, is_static)
@@ -312,26 +344,24 @@ static func collect_getters_and_setters(text: String) -> Dictionary:
312344
return result
313345

314346

315-
static func get_string_between_functions(method_name_start: String, method_name_end: String, text: String) -> void:
347+
static func get_string_between_functions(method_name_start: String, method_name_end: String, text: String) -> String:
316348
var method_start_match := match_func_with_whitespace(method_name_start, text)
317349
var method_end_match := match_func_with_whitespace(method_name_end, text)
318350

319-
return text.substr(method_start_match.get_start(), method_end_match.get_start())
320-
351+
print("method_name_start: %s - method_name_end: %s" % [method_name_start, method_name_end])
321352

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:
353+
if not method_start_match:
354+
print("No match for \"%s\"" % method_name_start)
325355
return ""
326356

327-
# Find the index of the opening parenthesis
328-
var opening_paren_index := result.get_end() - 1
329-
if opening_paren_index == -1:
357+
if not method_end_match:
358+
print("No match for \"%s\"" % method_end_match)
330359
return ""
331360

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())
361+
return text.substr(method_start_match.get_start(), method_end_match.get_start() - method_start_match.get_start())
334362

363+
364+
static func get_super_arg_string(text: String, opening_paren_index := 0) -> String:
335365
# Use a stack to match parentheses
336366
var stack := []
337367
var closing_paren_index := opening_paren_index
@@ -350,14 +380,4 @@ static func get_super_string(method_name: String, text: String, is_static: bool,
350380
return ""
351381

352382
# 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
383+
return text.substr(opening_paren_index + 1, closing_paren_index - opening_paren_index - 1)

0 commit comments

Comments
 (0)