Skip to content

Commit

Permalink
Added Mod Loader
Browse files Browse the repository at this point in the history
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
thejereman13 committed Nov 28, 2016
1 parent 3daa187 commit 6298e77
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
Binary file added mods/Test.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion saveGame.fd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
true
true
4
13
12
9
11 changes: 11 additions & 0 deletions src/Test.java
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());
}

}
10 changes: 6 additions & 4 deletions src/edu/ncsu/feddgame/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ public class GameInstance {
add(new Level8());
add(new Level9());
add(new Level10());
if (demoMode)
add(new DemoLevel());
add(new GameComplete());
}};

public static int levNum = 0; // Start with 0
Expand Down Expand Up @@ -108,6 +105,7 @@ public GameInstance() {
*/
private void setup() {
SaveData.readData();
new ModLoader();
objectManager = new ObjectManager();

// If glfw fails to initialize, throw exception
Expand All @@ -120,6 +118,10 @@ private void setup() {
glfwWindowHint(GLFW_STENCIL_BITS, 4);

window = new Window(800, 800, "Laser Amazer", false);

if (demoMode)
scenes.add(new DemoLevel());
scenes.add(new GameComplete());
}

/**
Expand Down Expand Up @@ -159,7 +161,7 @@ private void renderLoop() {
//
setLevel(0); // Set the starting level
//

new Thread(() -> logicLoop()).start(); // Run the logic in a separate thread

glfwSetWindowSize(window.window, 1200, 800);
Expand Down
76 changes: 76 additions & 0 deletions src/edu/ncsu/feddgame/ModLoader.java
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) {
}
}
}
}

0 comments on commit 6298e77

Please sign in to comment.