Skip to content

Commit 727e388

Browse files
committed
feat: 🚧 replace super() first working draft
1 parent 6cdb63c commit 727e388

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

addons/mod_loader/internal/mod_hook_preprocessor.gd

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static var regex_getter_setter: RegEx
99

1010
var hashmap := {}
1111
var previous_method := {}
12+
var last_valid_method := {}
1213

1314

1415
func process_begin() -> void:
@@ -37,6 +38,21 @@ func process_script(path: String) -> String:
3738
var method: Dictionary = script_method_list[i]
3839
var method_first_line_start := get_index_at_method_start(method.name, source_code)
3940

41+
if i == script_method_list.size() -1:
42+
# Check for super in last func
43+
# TODO: Add a better last method check
44+
var last_func_index := source_code.rfind("func ")
45+
if not last_func_index == -1:
46+
var code_between_end_and_last_func := source_code.substr(last_func_index)
47+
var supers := match_super_with_whitespace_all(code_between_end_and_last_func)
48+
for super_result in supers:
49+
print("super detected at end of SCRIPT!!!")
50+
var super_arg_string := get_super_arg_string(code_between_end_and_last_func, super_result.get_end() - 1)
51+
print("replace super(%s) with super.%s(%s)" % [super_arg_string, last_valid_method.name, super_arg_string])
52+
code_between_end_and_last_func = code_between_end_and_last_func.replace("super(%s)" % super_arg_string, "super.%s(%s)" % [last_valid_method.name, super_arg_string])
53+
source_code = source_code.erase(last_func_index, source_code.length() - last_func_index)
54+
source_code = source_code.insert(last_func_index, code_between_end_and_last_func)
55+
4056
if method_first_line_start == -1 or method.name in method_store:
4157
continue
4258

@@ -46,6 +62,8 @@ func process_script(path: String) -> String:
4662
if not is_func_moddable(method_first_line_start, source_code):
4763
continue
4864

65+
last_valid_method = method
66+
4967
if i > 0:
5068
# get_script_method_list() returns the methods in order they are in the source_code
5169
# we can use that to get the code between two funcs.
@@ -62,28 +80,21 @@ func process_script(path: String) -> String:
6280
if not method_end_match:
6381
print("No match for \"%s\"" % method.name)
6482

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())
83+
code_between_funcs = source_code.substr(method_start_match.get_start(), method_end_match.get_start() - method_start_match.get_start())
7184

7285
var supers := match_super_with_whitespace_all(code_between_funcs)
73-
print(supers.size())
86+
print(supers)
7487
for super_result in supers:
7588
print("super detected!")
7689
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)
90+
print("replace super(%s) with super.%s(%s)" % [super_arg_string, previous_method.name, super_arg_string])
7991
code_between_funcs = code_between_funcs.replace("super(%s)" % super_arg_string, "super.%s(%s)" % [previous_method.name, super_arg_string])
8092

81-
print("new_code: ")
93+
print("new_code: \n")
8294
print(code_between_funcs)
8395
source_code = source_code.erase(method_start_match.get_start(), method_end_match.get_start() - method_start_match.get_start())
8496
source_code = source_code.insert(method_start_match.get_start(), code_between_funcs)
8597

86-
8798
var type_string := get_return_type_string(method.return)
8899
var is_static := true if method.flags == METHOD_FLAG_STATIC + METHOD_FLAG_NORMAL else false
89100
var method_arg_string_with_defaults_and_types := get_function_parameters(method.name, source_code, is_static)

0 commit comments

Comments
 (0)