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...");
        injectModsFolder();
        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 void injectModsFolder() {
        String topLevel = LUtils.TOP_LEVEL;
        File mods = new File(topLevel, "mods");
        try {
            ClassPathHack.addFile(mods);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        System.err.println("Injected '" + mods + "' into classpath.");
    }

    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);
        }
    }
}

@@ -573,24 +573,29 @@
 %22);%0A        
+if (!
 injectModsFo
@@ -600,17 +600,239 @@
 Folder()
-;
+) %7B%0A            System.err%0A                    .println(%22%5BWARNING%5D Mods folder does not exist or is a file, %22%0A                            + %22add it if you want mods to be loaded from there.%22);%0A%0A            return;%0A        %7D
 %0A       
@@ -1707,20 +1707,23 @@
  static 
-void
+boolean
  injectM
@@ -1828,38 +1828,348 @@
 mods%22);%0A        
-try %7B%0A
+if (!mods.exists() %7C%7C mods.isFile()) %7B%0A            return false;%0A        %7D%0A%0A        File%5B%5D files = mods.listFiles();%0A        for (File f : files) %7B%0A            if (f.isDirectory()) %7B%0A                // add support for recursive directory searching later%0A                continue;%0A            %7D%0A            try %7B%0A    
             Clas
@@ -2190,15 +2190,16 @@
 ile(
-mods);%0A
+f);%0A    
     
@@ -2232,32 +2232,36 @@
 ) %7B%0A            
+    
 e.printStackTrac
@@ -2269,39 +2269,33 @@
 ();%0A            
-return;
+%7D
 %0A        %7D%0A     
@@ -2356,24 +2356,45 @@
 asspath.%22);%0A
+        return true;%0A
     %7D%0A%0A    p

Fix injectModsFolder