package emergencylanding.k.exst.mods;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import k.core.util.classes.ClassPathHack;
import emergencylanding.k.library.internalstate.ELEntity;
import emergencylanding.k.library.lwjgl.render.Render;
import emergencylanding.k.library.main.KMain;
import emergencylanding.k.library.util.LUtils;

public class Mods {
    private static ArrayList<IMod> loaded = new ArrayList<IMod>();

    public static void findAndLoad() {
        System.err.println("EL Mod System starting...");
        if (!injectModsFolder()) {
            System.err
                    .println("[WARNING] Mods folder does not exist or is a file, "
                            + "add it if you want mods to be loaded from there.");
        }
        ArrayList<IMod> injected = ModInjector.findAndInject();
        System.err.println("Loaded mods from classpath.");
        System.err.println("Letting game mess with mods list...");
        KMain inst = KMain.getInst();
        inst.loadMods(injected);
        System.err.println("Complete.");
        loaded.addAll(injected);
        System.err.println("Initializing mods...");
        for (IMod m : loaded) {
            try {
                m.init(inst);
            } catch (Exception e) {
                System.err.println("Error registering mod " + m.getClass()
                        + ":");
                e.printStackTrace();
                injected.remove(m);
            }
        }
        System.err.println("Complete.");
        loaded = injected;
        loaded.trimToSize();
        System.err.println("EL Mod System loaded.");
    }

    private static boolean injectModsFolder() {
        String topLevel = LUtils.TOP_LEVEL;
        File mods = new File(topLevel, "mods");
        if (!loadDirectory(mods)) {
            return false;
        }
        System.err.println("Injected '" + mods + "' into classpath.");
        return true;
    }

    private static boolean loadDirectory(File dir) {
        if (!dir.exists() || dir.isFile()) {
            return false;
        }

        File[] files = dir.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                loadDirectory(f);
                continue;
            }
            try {
                ClassPathHack.addFile(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public static void registerRenders(
            HashMap<Class<? extends ELEntity>, Render<? extends ELEntity>> classToRender) {
        for (IMod m : loaded) {
            HashMap<Class<? extends ELEntity>, Render<? extends ELEntity>> tmp = new HashMap<Class<? extends ELEntity>, Render<? extends ELEntity>>();
            m.registerRenders(tmp);
            classToRender.putAll(tmp);
        }
    }
}

@@ -102,43 +102,9 @@
 til.
-ArrayList;%0Aimport java.util.HashMap
+*
 ;%0A%0Ai
@@ -144,16 +144,69 @@
 thHack;%0A
+import emergencylanding.k.exst.modules.ModuleSystem;%0A
 import e
@@ -495,47 +495,257 @@
 ();%0A
-%0A    public static void findAndLoad() %7B
+    private static boolean loaded_mods = false;%0A%0A    public static void findAndLoad() %7B%0A        if (loaded_mods) %7B%0A            System.err%0A                    .println(%22Already loaded mod system, trying to load again?%22);%0A            return;%0A        %7D
 %0A   
@@ -1832,24 +1832,52 @@
 imToSize();%0A
+        loaded_mods = true;%0A
         Syst
@@ -1913,24 +1913,68 @@
  loaded.%22);%0A
+        ModuleSystem.loadModulesFromMods();%0A
     %7D%0A%0A    p
@@ -2909,24 +2909,174 @@
 ToRender) %7B%0A
+        if (!loaded_mods) %7B%0A            throw new IllegalStateException(%0A                    %22Registering renderers before loading mods!%22);%0A        %7D%0A
         for 
@@ -3336,11 +3336,271 @@
  %7D%0A    %7D
+%0A%0A    public static List%3CIMod%3E getLoadedMods() %7B%0A        if (!loaded_mods) %7B%0A            throw new IllegalStateException(%0A                    %22Getting loaded mods list before loading mods!%22);%0A        %7D%0A        return Collections.unmodifiableList(loaded);%0A    %7D
 %0A%7D%0A

Add checks and allow for list retreival