-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Mod Loader class that will search for jars in the mods folder and execute the constructor on the class with the same name as the jar file. Jar classes will have access to everything in the game, but there isn't a Mod API or useful functions for adding levels yet. See Test.jar and Test.java for brief example.
- Loading branch information
1 parent
3daa187
commit 6298e77
Showing
5 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
true | ||
true | ||
4 | ||
13 | ||
12 | ||
9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import edu.ncsu.feddgame.GameInstance; | ||
import edu.ncsu.feddgame.level.Level2; | ||
|
||
public class Test { | ||
|
||
public Test(){ | ||
//System.out.println("Test loaded"); | ||
GameInstance.scenes.add(new Level2()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package edu.ncsu.feddgame; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Hashtable; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
public class ModLoader extends ClassLoader{ | ||
private String jarPath = "mods/"; | ||
private ArrayList<String> jars = new ArrayList<String>(); | ||
@SuppressWarnings("rawtypes") | ||
private Hashtable<String, Class> classes = new Hashtable<String, Class>(); | ||
|
||
public ModLoader(){ | ||
super(ModLoader.class.getClassLoader()); | ||
loadMods(); | ||
} | ||
|
||
public void loadMods(){ | ||
File folder = new File(jarPath); | ||
File[] jarFiles = folder.listFiles(); | ||
for (int i = 0; i < jarFiles.length; i++){ | ||
if (jarFiles[i].isFile()){ | ||
jars.add(jarFiles[i].getName()); | ||
} | ||
} | ||
for (String s : jars){ | ||
try { | ||
loadClass(s.substring(0, s.length()-4),jarPath + s).newInstance(); //Loads the class by the same name as the jar and constructs it | ||
} catch (Exception e) {} | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public Class loadClass(String className, String jarFile){ | ||
try{ | ||
return findClass(className, jarFile); | ||
}catch (Exception e){ | ||
return null; | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public Class findClass(String className, String jarFile) { | ||
byte classByte[]; | ||
Class result = null; | ||
JarFile jar = null; | ||
try { | ||
jar = new JarFile(jarFile); | ||
JarEntry entry = jar.getJarEntry(className + ".class"); | ||
InputStream is = jar.getInputStream(entry); | ||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); | ||
int nextValue = is.read(); | ||
while (-1 != nextValue) { | ||
byteStream.write(nextValue); | ||
nextValue = is.read(); | ||
} | ||
|
||
classByte = byteStream.toByteArray(); | ||
result = defineClass(className, classByte, 0, classByte.length, null); | ||
classes.put(className, result); | ||
return result; | ||
} catch (Exception e) { | ||
return null; | ||
}finally { | ||
try { | ||
jar.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
} |