From 13ffa5fdfb8f6a4e137981cf2ee37a70a4023456 Mon Sep 17 00:00:00 2001 From: Hilligans Date: Tue, 26 Mar 2024 01:26:55 -0600 Subject: [PATCH] Old mod loader code cleanup --- .../dev/hilligans/ourcraft/GameInstance.java | 2 - .../dev/hilligans/ourcraft/item/Item.java | 2 +- .../ourcraft/mod/handler/ModLoader.java | 191 ---------------- .../mod/handler/content/ContentPack.java | 168 -------------- .../mod/handler/content/ModContent.java | 210 +----------------- .../ourcraft/mod/handler/content/ModList.java | 104 --------- .../loader/LoaderExceptionHandler.java | 4 +- 7 files changed, 6 insertions(+), 675 deletions(-) delete mode 100644 src/main/java/dev/hilligans/ourcraft/mod/handler/ModLoader.java diff --git a/src/main/java/dev/hilligans/ourcraft/GameInstance.java b/src/main/java/dev/hilligans/ourcraft/GameInstance.java index 7f24b7f2..19620041 100644 --- a/src/main/java/dev/hilligans/ourcraft/GameInstance.java +++ b/src/main/java/dev/hilligans/ourcraft/GameInstance.java @@ -29,7 +29,6 @@ import dev.hilligans.ourcraft.mod.handler.EventBus; import dev.hilligans.ourcraft.mod.handler.content.ModList; import dev.hilligans.ourcraft.mod.handler.events.common.RegistryClearEvent; -import dev.hilligans.ourcraft.mod.handler.ModLoader; import dev.hilligans.ourcraft.mod.handler.pipeline.InstanceLoaderPipeline; import dev.hilligans.ourcraft.network.Protocol; import dev.hilligans.ourcraft.recipe.helper.RecipeView; @@ -61,7 +60,6 @@ public class GameInstance { public final EventBus EVENT_BUS = new EventBus(); - public final ModLoader MOD_LOADER = new ModLoader(this); public final Logger LOGGER = new Logger("", ""); public final ResourceManager RESOURCE_MANAGER = new ResourceManager(this); public final ModContent OURCRAFT = new ModContent("ourcraft",this).addClassLoader(new URLClassLoader(new URL[]{Ourcraft.class.getProtectionDomain().getCodeSource().getLocation()})).addMainClass(Ourcraft.class); diff --git a/src/main/java/dev/hilligans/ourcraft/item/Item.java b/src/main/java/dev/hilligans/ourcraft/item/Item.java index f89654e3..37954ed8 100644 --- a/src/main/java/dev/hilligans/ourcraft/item/Item.java +++ b/src/main/java/dev/hilligans/ourcraft/item/Item.java @@ -29,7 +29,7 @@ public class Item implements IRecipeComponent, IRegistryElement { public Item(String name, ItemProperties itemProperties) { this.name = name; this.itemProperties = itemProperties; - this.modID = Ourcraft.GAME_INSTANCE.MOD_LOADER.mod; + // this.modID = Ourcraft.GAME_INSTANCE.MOD_LOADER.mod; id = Items.getNextId(); } diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/ModLoader.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/ModLoader.java deleted file mode 100644 index 73539173..00000000 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/ModLoader.java +++ /dev/null @@ -1,191 +0,0 @@ -package dev.hilligans.ourcraft.mod.handler; - -import dev.hilligans.ourcraft.GameInstance; -import dev.hilligans.ourcraft.data.primitives.Triplet; -import dev.hilligans.ourcraft.mod.handler.content.ModContainer; -import dev.hilligans.ourcraft.mod.handler.content.ModContent; -import dev.hilligans.ourcraft.resource.dataloader.FolderResourceDirectory; -import dev.hilligans.ourcraft.resource.dataloader.ResourceDirectory; -import dev.hilligans.ourcraft.resource.dataloader.ZipResourceDirectory; -import dev.hilligans.ourcraft.resource.loaders.ResourceLoader; -import dev.hilligans.ourcraft.util.Settings; -import org.json.JSONObject; - -import java.io.File; -import java.io.FileInputStream; -import java.lang.annotation.Annotation; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; - -public class ModLoader { - - public String mod = "ourcraft"; - public GameInstance gameInstance; - public boolean suspended = false; - public HashMap,String,Boolean>> mainClasses = new HashMap<>(); - - public ModLoader(GameInstance gameInstance) { - this.gameInstance = gameInstance; - } - - public ModContent requestMod(String modID) { - return new ModContent(modID, gameInstance); - } - - public void loadDefaultMods() { - //System.out.println("Java " + System.getProperty("java.version")); - loadAllMods(new File("mods/")); - if(true) { - // loadClasses1(new File("target/classes/"), ""); - } - - if(new File("ourcraft-1.0.3-jar-with-dependencies.jar").exists()) { - gameInstance.DATA_LOADER.addJar("ourcraft-1.0.3-jar-with-dependencies.jar", "ourcraft"); - } else if(new File("ourcraft-1.0.3.jar").exists()) { - gameInstance.DATA_LOADER.addJar("ourcraft-1.0.3.jar", "ourcraft"); - } else { - gameInstance.DATA_LOADER.addFolder("target/classes/", "ourcraft"); - } - //gameInstance.CONTENT_PACK.load(); - } - - public void loadAllMods(File folder) { - File[] mods = folder.listFiles(); - if(mods != null) { - for (File mod : mods) { - loadMod(mod); - } - } - } - - public void loadClasses(File folder, String topName) { - File[] mods = folder.listFiles(); - if(mods != null) { - for (File mod : mods) { - if(mod.isDirectory()) { - loadClasses(mod, topName + mod.getName() + "."); - } else if(mod.getName().endsWith(".class")) { - try { - URLClassLoader child = new URLClassLoader(new URL[]{mod.toURI().toURL()}, this.getClass().getClassLoader()); - Class testClass = child.loadClass(topName + mod.getName().substring(0,mod.getName().length() - 6)); - // Class testClass = Class.forName(topName + mod.getName().substring(0,mod.getName().length() - 6),false,child); - String modID = getModID(testClass); - if(modID != null) { - System.out.println("yes"); - this.mod = modID; - ModContent modContent = requestMod(modID); - modContent.isJar = false; - modContent.path = folder.getPath(); - FolderResourceDirectory resourceDirectory = new FolderResourceDirectory(new File("target/classes/")); - JSONObject jsonObject = getContent(resourceDirectory); - if(jsonObject != null) { - modContent.readData(jsonObject); - } - modContent.mainClass = testClass; - modContent.addClassLoader(child); - mainClasses.put(modID,new Triplet<>(testClass,mod.getAbsolutePath(),false)); - gameInstance.DATA_LOADER.add(modID, resourceDirectory); - //gameInstance.MOD_LIST.registerMod(modContent); - } - } catch (Exception ignored) {} - } - } - } - } - - - - public boolean loadMod(File file) { - try { - URLClassLoader child = new URLClassLoader(new URL[]{file.toURI().toURL()}, this.getClass().getClassLoader()); - ArrayList classNames = getClassNames(file); - for(String name : classNames) { - Class testClass = Class.forName(name,false,child); - String modID = getModID(testClass); - if(modID != null) { - ModContent modContent = requestMod(modID); - modContent.path = name; - ZipResourceDirectory zipResourceDirectory = new ZipResourceDirectory(new ZipFile(file.getPath()), file.getPath()); - JSONObject jsonObject = getContent(zipResourceDirectory); - if(jsonObject != null) { - modContent.readData(jsonObject); - } - mod = modID; - testClass = Class.forName(name,true,child); - mainClasses.put(modID,new Triplet<>(testClass,file.getAbsolutePath(),true)); - modContent.mainClass = testClass; - modContent.addClassLoader(child); - gameInstance.DATA_LOADER.add(modID, zipResourceDirectory); - gameInstance.CONTENT_PACK.registerModContent(modContent); - return true; - } - } - } catch (Exception e) { - mod = ""; - return false; - } - mod = ""; - return true; - } - - public JSONObject getContent(ResourceDirectory resourceDirectory) throws Exception { - try { - return new JSONObject(ResourceLoader.toString(resourceDirectory.get("mod.json"))); - //return new JSONObject(WorldLoader.readString(classLoader.getResource("mod.json").openStream())); - } catch (Exception e) { - if(!Settings.loadModsWithoutInfo) { - mod = ""; - throw new Exception("Cannot load file"); - } - } - return null; - } - - //https://stackoverflow.com/questions/15720822/how-to-get-names-of-classes-inside-a-jar-file - public ArrayList getClassNames(File file) { - ArrayList classNames = new ArrayList(); - try { - ZipInputStream zip = new ZipInputStream(new FileInputStream(file.getPath())); - for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { - if (!entry.isDirectory() && entry.getName().endsWith(".class")) { - String className = entry.getName().replace('/', '.'); - classNames.add(className.substring(0, className.length() - ".class".length())); - } - } - } catch (Exception ignored) {} - return classNames; - } - - public > T testClass(Class clazz) { - if(ModClass.class.isAssignableFrom(clazz)) { - return (T)clazz; - } - return null; - } - - public String getModID(Class val) { - for(Annotation annotation : val.getAnnotations()) { - if(annotation.annotationType().equals(Mod.class)) { - if(((Mod)annotation).modID().equals("")) { - System.out.println("MainClass " + val.toString() + " has no modID specified, will not load"); - return null; - } else { - return ((Mod)annotation).modID(); - } - } - } - return null; - } - - public synchronized boolean isSuspended() { - return suspended; - } - public synchronized void suspend() { - suspended = true; - } -} diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java index c5fab291..26de356a 100644 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java +++ b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java @@ -29,8 +29,6 @@ public class ContentPack { public GameInstance gameInstance; public HashMap mods = new HashMap<>(); - public ArrayList modList = new ArrayList<>(); - public HashMap loadedMods = new HashMap<>(); public HashMap modStates = new HashMap<>(); public HashMap shouldLoad = new HashMap<>(); @@ -38,162 +36,6 @@ public ContentPack(GameInstance gameInstance) { this.gameInstance = gameInstance; } - public void load() { - for(String string : mods.keySet()) { - recursivelyLoad(string); - shouldLoad.put(string,true); - } - } - - //private boolean waiting = false; - - public void generateData() { - //gameInstance.REBUILDING.set(true); - ///TODO Could potentially rebuild when rendering but very unlikely - //if(!Settings.isServer && !(ClientMain.getClient() == null || ClientMain.getClient().rendering)) { - // waiting = true; - //} else { - // rebuild(); - //} - } - - public void registerModContent(ModContent modContent) { - mods.put(modContent.getModID(),modContent); - modList.add(modContent); - - gameInstance.RESOURCE_MANAGER.classLoaders.add(modContent.classLoader); - gameInstance.MOD_LOADER.mainClasses.computeIfAbsent(modContent.getModID(),a -> new Triplet<>(modContent.mainClass, modContent.mainClass.getProtectionDomain().getCodeSource().getLocation().getPath(), false)); - } - - public void registerModContent(ModContainer modContent) { - - gameInstance.RESOURCE_MANAGER.classLoaders.add(modContent.classLoader); - gameInstance.MOD_LOADER.mainClasses.computeIfAbsent(modContent.getModID(),a -> new Triplet<>(modContent.modClass.getClass(), modContent.modClass.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(), false)); - } - - ///TODO use Blocks, Items, Entity Instances so the game can still render while its being reupdated. - - public void buildVital() { - built = true; - for(String string : mods.keySet()) { - if(shouldLoad.get(string)) { - ModContent mod = mods.get(string); - for (ResourceLoader resourceLoader : mod.resourceLoaders) { - gameInstance.RESOURCE_LOADER.add(resourceLoader); - } - } - } - } - - public boolean built = false; - - private void rebuild() { - // gameInstance.clear(); - // gameInstance.RESOURCE_MANAGER.clearData(); - - if(!built) { - // buildVital(); - } - - for(String string : mods.keySet()) { - if(shouldLoad.get(string)) { - ModContent mod = mods.get(string); - for(RegistryLoader registryLoader : mod.registryLoaders) { - if(registryLoader.rerunOnInstanceClear) { - // registryLoader.run(); - } - // gameInstance.registerRegistryLoader(registryLoader); - } - } - } - - //TODO Mods must be registered in order of dependencies - for(String string : mods.keySet()) { - if(shouldLoad.get(string)) { - ModContent mod = mods.get(string); - for(SoundBuffer soundBuffer : mod.sounds) { - //gameInstance.registerSound(soundBuffer); - } - for (Texture texture : mod.textures) { - //gameInstance.registerTextures(texture); - } - for (Block block : mod.blocks) { - //gameInstance.registerBlock(block); - } - for (Item item : mod.items) { - //gameInstance.registerItem(item); - } - for(IModel model : mod.models) { - //gameInstance.RESOURCE_MANAGER.putModel(model.getPath(),model); - } - for(Protocol protocol : mod.protocols.values()) { - //gameInstance.PROTOCOLS.put(protocol.protocolName,protocol); - } - for(ScreenBuilder screenBuilder : mod.screenBuilders) { - //gameInstance.registerScreenBuilder(screenBuilder); - } - for(Feature feature : mod.features) { - //gameInstance.registerFeature(feature); - } - for(IGraphicsEngine graphicsEngine : mod.graphicsEngines) { - //gameInstance.registerGraphicsEngine(graphicsEngine); - } - for(RenderTarget renderTarget : mod.renderTargets) { - //gameInstance.registerRenderTarget(renderTarget); - } - for(RenderPipeline renderPipeline : mod.renderPipelines) { - //gameInstance.registerRenderPipeline(renderPipeline); - } - for(RenderTaskSource renderTask : mod.renderTasks) { - //gameInstance.registerRenderTask(renderTask); - } - for(VertexFormat vertexFormat : mod.vertexFormats) { - //gameInstance.registerVertexFormat(vertexFormat); - } - for(InputHandlerProvider provider : mod.inputHandlerProviders) { - //gameInstance.registerInputHandlerProviders(provider); - } - for(Input input : mod.keybinds) { - //gameInstance.registerKeybind(input); - } - for(ShaderSource shaderSource : mod.shaders) { - //gameInstance.registerShader(shaderSource); - } - for(ILayoutEngine layoutEngine : mod.layoutEngines) { - //gameInstance.registerLayoutEngine(layoutEngine); - } - } - } - for(Registry registry : gameInstance.REGISTRIES.ELEMENTS) { - for(Object o : registry.ELEMENTS) { - if(o instanceof IRegistryElement) { - // ((IRegistryElement) o).load(gameInstance); - } - } - } - built = false; - } - - public void recursivelyLoad(String modID) { - if(!mods.containsKey(modID)) { - System.err.println("Missing Dependency: " + modID); - } - ModContent modContent = mods.get(modID); - if(loadedMods.getOrDefault(modID,false)) { - return; - } - for(ModDependency modDependency : modContent.dependencies) { - if(!loadedMods.getOrDefault(modDependency.modID,false)) { - recursivelyLoad(modDependency.modID); - } - } - try { - modContent.load(); - } catch (Exception e) { - e.printStackTrace(); - } - loadedMods.put(modID,true); - } public String[] getModList() { @@ -214,16 +56,6 @@ public void putMod(ModContent modContent) { shouldLoad.put(modContent.getModID(),true); } - public void releaseMod(String mod) { - ModContent modContent = mods.remove(mod); - gameInstance.RESOURCE_MANAGER.classLoaders.remove(modContent.classLoader); - try { - modContent.classLoader.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - public void loadCachedMod(String name, Client client) { ModContent modContent = ModContent.readLocal(name,gameInstance, client); if(modContent != null) { diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java index 58b4491f..46ce99b7 100644 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java +++ b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java @@ -49,34 +49,14 @@ public class ModContent { public ModDependency[] dependencies = new ModDependency[0]; public int version = -1; public String description = ""; - public boolean isJar = true; public String[] authors = new String[0]; public String path = ""; public ArrayList blocks = new ArrayList<>(); public ArrayList items = new ArrayList<>(); - public ArrayList textures = new ArrayList<>(); - public ArrayList sounds = new ArrayList<>(); - public ArrayList models = new ArrayList<>(); - public ArrayList tags = new ArrayList<>(); - public ArrayList> resourceLoaders = new ArrayList<>(); - public ArrayList biomes = new ArrayList<>(); - public ArrayList toolLevels = new ArrayList<>(); - public ArrayList registryLoaders = new ArrayList<>(); public ArrayList screenBuilders = new ArrayList<>(); - public ArrayList features = new ArrayList<>(); - public ArrayList> graphicsEngines = new ArrayList<>(); - public ArrayList renderTargets = new ArrayList<>(); - public ArrayList renderPipelines = new ArrayList<>(); - public ArrayList renderTasks = new ArrayList<>(); - public ArrayList vertexFormats = new ArrayList<>(); - public ArrayList inputHandlerProviders = new ArrayList<>(); - public ArrayList keybinds = new ArrayList<>(); - public ArrayList shaders = new ArrayList<>(); - public ArrayList> layoutEngines = new ArrayList<>(); - - public HashMap protocols = new HashMap<>(); + public ArrayList shaders = new ArrayList<>(); public boolean loaded = false; public boolean shouldLoad = true; @@ -132,11 +112,6 @@ public void load() throws Exception { loaded = true; } - public void invokeRegistryLoaders() { - for(RegistryLoader registryLoader : registryLoaders) { - registryLoader.runInit(); - } - } public void registerBlock(Block block) { // block.setModContent(this); @@ -145,102 +120,7 @@ public void registerBlock(Block block) { items.add(new BlockItem(block.name,block,modID).setModContent(this)); } - public void registerBlocks(Block... blocks) { - for(Block block : blocks) { - registerBlock(block); - } - } - - public void registerItem(Item item) { - items.add(item); - item.source = this; - } - - public void registerItems(Item... items) { - for(Item item : items) { - registerItem(item); - } - } - - public void registerSound(SoundBuffer soundBuffer) { - sounds.add(soundBuffer); - //soundBuffer.source = this; - } - - public void registerSounds(SoundBuffer... soundBuffers) { - for(SoundBuffer soundBuffer : soundBuffers) { - registerSound(soundBuffer); - } - } - - public void registerTexture(Texture... textures) { - for(Texture texture : textures) { - texture.assignOwner(this); - this.textures.add(texture); - } - } - - public void registerModel(IModel... models) { - for(IModel iModel : models) { - this.models.add(iModel); - } - } - - @SafeVarargs - public final void registerPacket(Supplier... packets) { - for(Supplier packet : packets) { - Protocol protocol = protocols.computeIfAbsent("Play", Protocol::new); - protocol.register(packet); - } - } - - @SafeVarargs - public final void registerPacket(String protocolName, Supplier... packets) { - for(Supplier packet : packets) { - Protocol protocol = protocols.computeIfAbsent(protocolName, Protocol::new); - protocol.register(packet); - } - } - - @SafeVarargs - public final void registerPacket(String protocolName, int id, Supplier... packets) { - for(Supplier packet : packets) { - Protocol protocol = protocols.computeIfAbsent(protocolName, Protocol::new); - protocol.register(packet,id); - } - } - - public void registerResourceLoader(ResourceLoader... resourceLoaders) { - for(ResourceLoader resourceLoader : resourceLoaders) { - this.resourceLoaders.add(resourceLoader); - resourceLoader.gameInstance = gameInstance; - } - } - - public void registerBiome(Biome... biomes) { - for(Biome biome : biomes) { - //biome.source = this; - this.biomes.add(biome); - } - } - - public void registerFeature(Feature... features) { - for(Feature feature : features) { - feature.setModContent(this); - this.features.add(feature); - } - } - public void registerToolLevel(ToolLevel... toolLevels) { - this.toolLevels.addAll(List.of(toolLevels)); - } - - public void registerRegistryLoader(RegistryLoader... registryLoaders) { - for(RegistryLoader registryLoader : registryLoaders) { - registryLoader.gameInstance = gameInstance; - this.registryLoaders.add(registryLoader); - } - } public void registerScreenBuilder(ScreenBuilder... screenBuilders) { for(ScreenBuilder screenBuilder : screenBuilders) { @@ -249,68 +129,6 @@ public void registerScreenBuilder(ScreenBuilder... screenBuilders) { } } - public void registerGraphicsEngine(IGraphicsEngine... graphicsEngines) { - for(IGraphicsEngine graphicsEngine : graphicsEngines) { - graphicsEngine.assignOwner(this); - this.graphicsEngines.add(graphicsEngine); - } - } - - public void registerRenderTarget(RenderTarget... renderTargets) { - for(RenderTarget renderTarget : renderTargets) { - renderTarget.assignOwner(this); - } - this.renderTargets.addAll(Arrays.asList(renderTargets)); - } - - public void registerRenderPipelines(RenderPipeline... renderPipelines) { - for(RenderPipeline renderPipeline : renderPipelines) { - renderPipeline.assignOwner(this); - } - this.renderPipelines.addAll(Arrays.asList(renderPipelines)); - } - - public void registerRenderTask(RenderTaskSource... renderTasks) { - for(RenderTaskSource renderTask : renderTasks) { - renderTask.assignOwner(this); - } - this.renderTasks.addAll(Arrays.asList(renderTasks)); - } - - public void registerVertexFormat(VertexFormat... vertexFormats) { - for(VertexFormat vertexFormat : vertexFormats) { - vertexFormat.assignOwner(this); - } - this.vertexFormats.addAll(Arrays.asList(vertexFormats)); - } - - public void registerInputHandlerProviders(InputHandlerProvider... providers) { - for(InputHandlerProvider provider : providers) { - provider.assignOwner(this); - } - this.inputHandlerProviders.addAll(Arrays.asList(providers)); - } - - public void registerKeybinds(Input... inputs) { - for(Input input : inputs) { - input.assignOwner(this); - } - this.keybinds.addAll(Arrays.asList(inputs)); - } - - public void registerShader(ShaderSource... shaderSources) { - for(ShaderSource shaderSource : shaderSources) { - shaderSource.assignOwner(this); - } - this.shaders.addAll(Arrays.asList(shaderSources)); - } - - public void registerLayoutEngine(ILayoutEngine... layoutEngines) { - for(ILayoutEngine layoutEngine : layoutEngines) { - layoutEngine.assignOwner(this); - } - this.layoutEngines.addAll(Arrays.asList(layoutEngines)); - } public void putData(IByteArray byteArray) { byteArray.writeInt(version); @@ -318,7 +136,7 @@ public void putData(IByteArray byteArray) { byteArray.writeUTF16(description); byteArray.writeUTF16(Util.toString(authors)); byteArray.writeUTF16(Util.toString(getDependencies())); - byteArray.writeInt(models.size()); + // byteArray.writeInt(models.size()); /* for(IModel iModel : models) { byteArray.writeString(iModel.getPath()); @@ -434,28 +252,4 @@ public void readData(JSONObject jsonObject) { public GameInstance getGameInstance() { return gameInstance; } - - public CoreExtensionView getCoreView() { - return new CoreExtensionView(null); - } - - @Override - public String toString() { - return "ModContent{" + - "modID='" + modID + '\'' + - ", mod=" + mod + - ", mainClass=" + mainClass + - ", classLoader=" + classLoader + - ", dependencies=" + Arrays.toString(dependencies) + - ", version=" + version + - ", description='" + description + '\'' + - ", authors=" + Arrays.toString(authors) + - ", blocks=" + blocks + - ", items=" + items + - ", textures=" + textures + - ", sounds=" + sounds + - ", models=" + models + - ", loaded=" + loaded + - '}'; - } } diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModList.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModList.java index c41d6426..d9fde1ba 100644 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModList.java +++ b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModList.java @@ -137,96 +137,6 @@ public boolean loadMod(File file) { return true; } - /* - public void loadClasses(File folder, String topName) { - File[] mods = folder.listFiles(); - if(mods != null) { - for (File mod : mods) { - if(mod.isDirectory()) { - loadClasses(mod, topName + mod.getName() + "."); - } else if(mod.getName().endsWith(".class")) { - try { - URLClassLoader child = new URLClassLoader(new URL[]{mod.toURI().toURL()}, this.getClass().getClassLoader()); - Class testClass = child.loadClass(topName + mod.getName().substring(0,mod.getName().length() - 6)); - // Class testClass = Class.forName(topName + mod.getName().substring(0,mod.getName().length() - 6),false,child); - String modID = getModID(testClass); - if(modID != null) { - System.out.println("yes"); - this.mod = modID; - ModContent modContent = requestMod(modID); - modContent.isJar = false; - modContent.path = folder.getPath(); - FolderResourceDirectory resourceDirectory = new FolderResourceDirectory(new File("target/classes/")); - JSONObject jsonObject = getContent(resourceDirectory); - if(jsonObject != null) { - modContent.readData(jsonObject); - } - modContent.mainClass = testClass; - modContent.addClassLoader(child); - mainClasses.put(modID,new Triplet<>(testClass,mod.getAbsolutePath(),false)); - gameInstance.DATA_LOADER.add(modID, resourceDirectory); - gameInstance.CONTENT_PACK.registerModContent(modContent); - } - } catch (Exception ignored) {} - } - } - } - } - - */ - - - /*public boolean loadMod(File file) { - try { - URLClassLoader child = new URLClassLoader(new URL[]{file.toURI().toURL()}, this.getClass().getClassLoader()); - ArrayList classNames = getClassNames(file); - for(String name : classNames) { - Class testClass = Class.forName(name,false,child); - String modID = getModID(testClass); - if(modID != null) { - ModContent modContent = requestMod(modID); - modContent.path = name; - ZipResourceDirectory zipResourceDirectory = new ZipResourceDirectory(new ZipFile(file.getPath()), file.getPath()); - JSONObject jsonObject = getContent(zipResourceDirectory); - if(jsonObject != null) { - modContent.readData(jsonObject); - } - mod = modID; - testClass = Class.forName(name,true,child); - mainClasses.put(modID,new Triplet<>(testClass,file.getAbsolutePath(),true)); - modContent.mainClass = testClass; - modContent.addClassLoader(child); - gameInstance.DATA_LOADER.add(modID, zipResourceDirectory); - gameInstance.CONTENT_PACK.registerModContent(modContent); - return true; - } - } - } catch (Exception e) { - mod = ""; - return false; - } - mod = ""; - return true; - } - - - - public JSONObject getContent(ResourceDirectory resourceDirectory) throws Exception { - try { - return new JSONObject(ResourceLoader.toString(resourceDirectory.get("mod.json"))); - //return new JSONObject(WorldLoader.readString(classLoader.getResource("mod.json").openStream())); - } catch (Exception e) { - if(!Settings.loadModsWithoutInfo) { - mod = ""; - throw new Exception("Cannot load file"); - } - } - return null; - } - - */ - - //https://stackoverflow.com/questions/15720822/how-to-get-names-of-classes-inside-a-jar-file public ArrayList getClassNames(File file) { ArrayList classNames = new ArrayList(); @@ -241,18 +151,4 @@ public ArrayList getClassNames(File file) { } catch (Exception ignored) {} return classNames; } - - public String getModID(Class val) { - for(Annotation annotation : val.getAnnotations()) { - if(annotation.annotationType().equals(Mod.class)) { - if(((Mod)annotation).modID().equals("")) { - System.out.println("MainClass " + val.toString() + " has no modID specified, will not load"); - return null; - } else { - return ((Mod)annotation).modID(); - } - } - } - return null; - } } diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/exception/loader/LoaderExceptionHandler.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/exception/loader/LoaderExceptionHandler.java index ccc10c9d..6ed0febf 100644 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/exception/loader/LoaderExceptionHandler.java +++ b/src/main/java/dev/hilligans/ourcraft/mod/handler/exception/loader/LoaderExceptionHandler.java @@ -15,7 +15,9 @@ public LoaderExceptionHandler() { @Override public boolean handleException(Exception exception, @Nullable IContext context, CrashReport crashReport) { if(context instanceof LoadingCrashContext crashContext) { - gameInstance.MOD_LOADER.suspend(); + //TODO update + + // gameInstance.MOD_LOADER.suspend(); int maxNameLength = 0; int maxVersionLength = 0; int maxSourceLength = 0;