-
Notifications
You must be signed in to change notification settings - Fork 7
Custom file handler (Also workaround for issue with mixin paths in mixin configs) #66
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
026af67
2f29666
3dfb49c
9fa5b83
d32cf7a
1c08bad
637927d
c1429d8
0d0befe
1e6b535
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package io.github.pacifistmc.forgix.core.filehandlers; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @FunctionalInterface | ||
| public interface CustomFileHandler { | ||
| String handle(String fileName, String fileContent, Map<String, String> replacementPaths); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,41 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package io.github.pacifistmc.forgix.core.filehandlers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.google.gson.*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class MixinFileHandler implements CustomFileHandler { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String handle(String fileName, String fileContent, Map<String, String> replacementPaths) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JsonObject mixinJson = gson.fromJson(fileContent, JsonObject.class); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String packagePath = mixinJson.get("package").getAsString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JsonArray commonMixinPaths = mixinJson.getAsJsonArray("mixins"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JsonArray newCommonMixinPaths = new JsonArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (JsonElement mixinPath : commonMixinPaths) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String fullPath = packagePath + "." + mixinPath.getAsString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (replacementPaths.containsKey(fullPath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newCommonMixinPaths.add(replacementPaths.get(fullPath).substring(packagePath.length() + 1)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newCommonMixinPaths.add(mixinPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+14
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential StringIndexOutOfBoundsException and incorrect path handling. The substring operation at line 20 assumes the replacement path is longer than Example failure case:
Apply this diff to safely handle replacement paths: JsonArray newCommonMixinPaths = new JsonArray();
for (JsonElement mixinPath : commonMixinPaths) {
String fullPath = packagePath + "." + mixinPath.getAsString();
- if (replacementPaths.containsKey(fullPath))
- newCommonMixinPaths.add(replacementPaths.get(fullPath).substring(packagePath.length() + 1));
- else
+ if (replacementPaths.containsKey(fullPath)) {
+ String replacement = replacementPaths.get(fullPath);
+ String prefix = packagePath + ".";
+ if (replacement.startsWith(prefix) && replacement.length() > prefix.length()) {
+ newCommonMixinPaths.add(replacement.substring(prefix.length()));
+ } else {
+ // Replacement is in a different package or format; use the class name only
+ int lastDot = replacement.lastIndexOf('.');
+ newCommonMixinPaths.add(lastDot >= 0 ? replacement.substring(lastDot + 1) : replacement);
+ }
+ } else {
newCommonMixinPaths.add(mixinPath);
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mixinJson.add("mixins", newCommonMixinPaths); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ThePandaOliver marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JsonArray clientMixinPaths = mixinJson.getAsJsonArray("client"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JsonArray newClientMixinPaths = new JsonArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (JsonElement mixinPath : clientMixinPaths) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String fullPath = packagePath + "." + mixinPath.getAsString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (replacementPaths.containsKey(fullPath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newClientMixinPaths.add(replacementPaths.get(fullPath).substring(packagePath.length() + 1)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newClientMixinPaths.add(mixinPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+27
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential StringIndexOutOfBoundsException and incorrect path handling (client mixins). The same substring issue from lines 14-25 exists here for client mixins. Line 33 will throw Apply this diff to safely handle replacement paths: JsonArray newClientMixinPaths = new JsonArray();
for (JsonElement mixinPath : clientMixinPaths) {
String fullPath = packagePath + "." + mixinPath.getAsString();
- if (replacementPaths.containsKey(fullPath))
- newClientMixinPaths.add(replacementPaths.get(fullPath).substring(packagePath.length() + 1));
- else
+ if (replacementPaths.containsKey(fullPath)) {
+ String replacement = replacementPaths.get(fullPath);
+ String prefix = packagePath + ".";
+ if (replacement.startsWith(prefix) && replacement.length() > prefix.length()) {
+ newClientMixinPaths.add(replacement.substring(prefix.length()));
+ } else {
+ // Replacement is in a different package or format; use the class name only
+ int lastDot = replacement.lastIndexOf('.');
+ newClientMixinPaths.add(lastDot >= 0 ? replacement.substring(lastDot + 1) : replacement);
+ }
+ } else {
newClientMixinPaths.add(mixinPath);
+ }
} |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mixinJson.add("client", newClientMixinPaths); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ThePandaOliver marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JsonPrimitive mixinPlugin = mixinJson.getAsJsonPrimitive("plugin"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JsonPrimitive mixinRef = mixinJson.getAsJsonPrimitive("refmap"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return gson.toJson(mixinJson); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add null safety check for the "package" field.
The code will throw a
NullPointerExceptionif the "package" field is missing or null in the mixin configuration JSON. While this field should always be present in valid mixin configs, defensive coding requires validation.Apply this diff to add validation:
📝 Committable suggestion
🤖 Prompt for AI Agents