Fixed: nullable patches

This commit is contained in:
Reider745
2025-03-21 06:14:27 +03:00
parent d2c38029c3
commit 6b0cc0862f
@@ -37,26 +37,33 @@ public class PatchPreLoader {
} }
public void init() { public void init() {
for(File patch : Objects.requireNonNull(patches.listFiles((dir, name) -> name.endsWith(".jar")))) { if(!patches.exists() && !patches.mkdir()) {
try { return;
final URLClassLoader mainLoader = new URLClassLoader(new URL[]{patch.toURL()}); }
final JSONObject description = new JSONObject(new String(mainLoader.getResourceAsStream("patch.json").readAllBytes()));
final JSONArray list = description.optJSONArray("hooks", null); final File[] files = patches.listFiles((dir, name) -> name.endsWith(".jar"));
if(list != null) if(files != null) {
for(int i = 0;i < list.length();i++) for (File patch : files) {
Main.INSTANCE.add(list.optString(i)); try {
final URLClassLoader mainLoader = new URLClassLoader(new URL[]{patch.toURL()});
final JSONObject description = new JSONObject(new String(mainLoader.getResourceAsStream("patch.json").readAllBytes()));
final String main = description.optString("main", null); final JSONArray list = description.optJSONArray("hooks", null);
if(main != null) { if (list != null)
Class<?> clazz = mainLoader.loadClass(main); for (int i = 0; i < list.length(); i++)
clazz.getMethod("onPreLoader").invoke(clazz.getConstructor().newInstance()); Main.INSTANCE.add(list.optString(i));
clazz = Main.INSTANCE.loader.getClassLoader().loadClass(main); final String main = description.optString("main", null);
clazz.getMethod("onMainLoader").invoke(clazz.getConstructor().newInstance()); if (main != null) {
Class<?> clazz = mainLoader.loadClass(main);
clazz.getMethod("onPreLoader").invoke(clazz.getConstructor().newInstance());
clazz = Main.INSTANCE.loader.getClassLoader().loadClass(main);
clazz.getMethod("onMainLoader").invoke(clazz.getConstructor().newInstance());
}
} catch (Exception e) {
e.printStackTrace();
} }
} catch (Exception e) {
e.printStackTrace();
} }
} }
} }