76 lines
2.8 KiB
Java
76 lines
2.8 KiB
Java
package main;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.net.URL;
|
|
import java.net.URLClassLoader;
|
|
import java.util.ArrayList;
|
|
|
|
import basics.BasicMod;
|
|
import manager.SettingManager;
|
|
import mod_quicklaunch.QuickLaunch;
|
|
|
|
public class ModLoader {
|
|
public static ArrayList<BasicMod> mods = new ArrayList<BasicMod>();
|
|
private BufferedReader bfr;
|
|
|
|
public void init() {
|
|
System.out.println("ModLoader: Start init");
|
|
File modfile = new File(SettingManager.getJarDirectory()+File.separator+"mods.txt");
|
|
try {
|
|
if (!modfile.exists()) {
|
|
System.out.println("ModLoader: No mod file exists");
|
|
System.out.println("ModLoader: Starting for the first time?");
|
|
modfile.createNewFile();
|
|
System.out.println("ModLoader: Mod file: mods.txt created");
|
|
System.out.println("ModLoader: Ready to install mods");
|
|
}
|
|
this.bfr = new BufferedReader(new FileReader(modfile));
|
|
}
|
|
catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
System.out.println("ModLoader: Exit init");
|
|
}
|
|
|
|
public void addMods() {
|
|
mods.add(new QuickLaunch());
|
|
String nextMod = null;
|
|
try {
|
|
nextMod = this.bfr.readLine();
|
|
}
|
|
catch (IOException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
while (nextMod != null) {
|
|
try {
|
|
System.out.println("Trying for "+nextMod+".jar");
|
|
File modJar = new File(SettingManager.getJarDirectory()+File.separator+nextMod+".jar");
|
|
System.out.println(modJar.exists() ? "Found "+nextMod : "Not found");
|
|
ClassLoader cl = new URLClassLoader(new URL[] {modJar.toURL()}, Thread.currentThread().getContextClassLoader());
|
|
mods.add((BasicMod)cl.loadClass("mod_"+nextMod.toLowerCase()+"."+nextMod).getConstructors()[0].newInstance(new Object[0]));
|
|
nextMod = bfr.readLine();
|
|
} catch (InstantiationException | IOException | SecurityException | InvocationTargetException | IllegalArgumentException | IllegalAccessException e) {
|
|
e.printStackTrace();
|
|
System.out.println("Did not work");
|
|
} catch (ClassNotFoundException e) {
|
|
System.err.println("'" + nextMod + "'" + " is not installed");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void initMods() {
|
|
for (BasicMod mod : mods) {
|
|
mod.init();
|
|
}
|
|
}
|
|
}
|