package xyz.openmodloader.modloader;

import com.google.gson.Gson;
import xyz.openmodloader.OpenModLoader;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ModLoader {
    public static List<ModContainer> MODS = new ArrayList<>();

    private File runDir = new File(".");
    private File modsDir = new File(runDir, "mods");
    private Gson gson = new Gson();

    public void loadMods() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        if (this.modsDir.exists()) {
            File[] files = this.modsDir.listFiles();
            if (files != null) {
                for (File mod : files) {
                    if (mod.getName().endsWith(".jar")) {
                        JarFile jar = new JarFile(mod);
                        for (JarEntry entry : Collections.list(jar.entries())) {
                            String fileName = entry.getName();
                            if (fileName.equals("mod.json")) {
                                this.loadMod(jar.getInputStream(entry));
                            }
                        }
                    }
                }
            }
        }

        URL roots = this.getClass().getClassLoader().getResource("");
        if (roots != null) {
            File root = new File(roots.getPath());
            File[] files = root.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.getName().equals("mod.json")) {
                        this.loadMod(new FileInputStream(file));
                    }
                }
            }
        }
    }

    public void loadMod(InputStream stream) {
        ModContainer container = this.gson.fromJson(new InputStreamReader(stream), ModContainer.class);
        OpenModLoader.INSTANCE.LOGGER.info("Found mod " + container.getName() + " (with id " + container.getModID() + ")");
        ModLoader.MODS.add(container);
    }

    public void registerMods() throws IllegalAccessException, InstantiationException {
        for (ModContainer mod : new ArrayList<>(MODS)) {
            try {
                mod.getInstance().onEnable();
            } catch (RuntimeException e) {
                System.err.println("An error occurred while enabling mod " + mod.getModID());
                e.printStackTrace();
                ModLoader.MODS.remove(mod);
            }
        }
    }
}

@@ -60,16 +60,122 @@
 n.Gson;%0A
+import com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;%0Aimport com.google.gson.JsonParser;%0A
 import x
@@ -607,16 +607,66 @@
  Gson();
+%0A    private JsonParser parser = new JsonParser();
 %0A%0A    pu
@@ -1979,16 +1979,21 @@
         
+List%3C
 ModConta
@@ -1996,16 +1996,17 @@
 ontainer
+%3E
  contain
@@ -2007,19 +2007,359 @@
 ontainer
- = 
+List = new ArrayList%3C%3E();%0A        JsonElement element = this.parser.parse(new InputStreamReader(stream));%0A        if (element.isJsonArray()) %7B%0A            for (JsonElement e : element.getAsJsonArray()) %7B%0A                containerList.add(this.gson.fromJson(e, ModContainer.class));%0A            %7D%0A        %7D else %7B%0A            containerList.add(
 this.gso
@@ -2419,18 +2419,88 @@
 r.class)
+)
 ;%0A
+        %7D%0A        for (ModContainer container : containerList) %7B%0A    
         
@@ -2615,32 +2615,36 @@
 + %22)%22);%0A        
+    
 ModLoader.MODS.a
@@ -2658,16 +2658,26 @@
 ainer);%0A
+        %7D%0A
     %7D%0A%0A 

Add support for multiple mods in one workspace