Skip to content

fix: Use counter instead of stack in get_closing_paren_index() #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions addons/mod_loader/internal/mod_hook_preprocessor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,21 @@ static func get_function_parameters(method_name: String, text: String, is_static


static func get_closing_paren_index(opening_paren_index: int, text: String) -> int:
# Use a stack to match parentheses
var stack := []
# Use a stack counter to match parentheses
var stack := 0
var closing_paren_index := opening_paren_index
while closing_paren_index < text.length():
var char := text[closing_paren_index]
if char == '(':
stack.push_back('(')
stack += 1
elif char == ')':
stack.pop_back()
if stack.size() == 0:
stack -= 1
if stack == 0:
break
closing_paren_index += 1

# If the stack is not empty, that means there's no matching closing parenthesis
if stack.size() != 0:
if stack != 0:
return -1

return closing_paren_index
Expand Down Expand Up @@ -222,6 +222,8 @@ static func edit_vanilla_method(

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

var func_body := regex_func_body.search(text, func_body_start_index)
Expand Down