Skip to content

Commit

Permalink
Fix injectFunction crashing when there are no functions
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Jan 11, 2025
1 parent 4592fb1 commit 0f7b50e
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/org/taumc/glsl/Transformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,26 @@ public void injectVariable(String code) {
public void injectFunction(String code) {
var insert = ShaderParser.parseSnippet(code, GLSLParser::external_declaration);


if (function == null) {
if (functionDefinitions.get(0).getParent() instanceof GLSLParser.External_declarationContext list) {
if (!functionDefinitions.isEmpty() && functionDefinitions.get(0).getParent() instanceof GLSLParser.External_declarationContext list) {
function = list;
}
}

ParserRuleContext parent;

if (function != null) {
var parent = function.getParent();
int i = parent.children.indexOf(function);
parent.children.add(i, insert);
insert.setParent(parent);
scanNode(insert);
ParseTreeWalker.DEFAULT.walk(new InjectorPoint(this, true), insert);
parent = function.getParent();
} else {
parent = this.root;
}

int i = function != null ? parent.children.indexOf(function) : parent.children.size();
parent.children.add(i, insert);
insert.setParent(parent);
scanNode(insert);
ParseTreeWalker.DEFAULT.walk(new InjectorPoint(this, true), insert);
}

public void injectAtEnd(String code) {
Expand Down

0 comments on commit 0f7b50e

Please sign in to comment.