Skip to content
This repository has been archived by the owner on Nov 13, 2022. It is now read-only.

Commit

Permalink
-- Upgrades to EngineLoader. Speed improvement on smaller programs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Torbuntu committed Aug 27, 2019
1 parent 84640ae commit 07dca26
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion assets/Programs/Shapes/program.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ type = Demo
version = 1.0
author = Tor
max_sprites = 2048
compile_source = false
compile_source = true
3 changes: 3 additions & 0 deletions assets/Programs/WizRobo/Code/Shapes.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Shapes{
println "NOOOO"
}
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ project(":core") {

implementation "org.lwjgl.lwjgl:lwjgl:2.9.3"

implementation "org.apache.commons:commons-lang3:3.9"
implementation "org.codehaus.groovy:groovy-all:3.0.0-beta-3"
}
}
Expand Down
68 changes: 41 additions & 27 deletions core/src/main/java/leikr/loaders/EngineLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import leikr.customProperties.CustomProgramProperties;
import leikr.Engine;
import leikr.GameRuntime;
import leikr.screens.MenuScreen;
import org.apache.commons.lang3.ArrayUtils;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.tools.Compiler;
import org.mini2Dx.core.Mdx;
import org.mini2Dx.core.files.FileHandle;

/**
*
Expand Down Expand Up @@ -86,34 +87,42 @@ public Engine getEngine() throws CompilationFailedException, IOException, Instan

private Engine getSourceEngine() throws MalformedURLException, CompilationFailedException, IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
gcl.clearCache();
gcl.addURL(new File(rootPath).toURI().toURL());
gcl.addClasspath(rootPath);
return (Engine) gcl.parseClass(new File(Mdx.files.local(rootPath + MenuScreen.GAME_NAME + ".groovy").path())).getDeclaredConstructors()[0].newInstance();//loads the game code
}

private Engine getJavaSourceEngine() throws MalformedURLException, CompilationFailedException, IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
gcl.clearCache();
gcl.addURL(new File(rootPath).toURI().toURL());
gcl.addClasspath(rootPath);
return (Engine) gcl.parseClass(new File(Mdx.files.local(rootPath + MenuScreen.GAME_NAME + ".java").path())).getDeclaredConstructors()[0].newInstance();//loads the game code
}

private Engine getCompiledEngine() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
String COMPILED = rootPath + "Compiled/";
gcl.addURL(new File(COMPILED).toURI().toURL());
gcl.addClasspath(COMPILED);
return (Engine) gcl.loadClass(MenuScreen.GAME_NAME).getConstructors()[0].newInstance();
}

private void compileEngine() {
private void compileEngine() throws IOException {
String COMPILED = rootPath + "Compiled/";
CompilerConfiguration cc = new CompilerConfiguration();
cc.setClasspath(rootPath);
if (!(new File(COMPILED).exists())) {
new File(COMPILED).mkdir();
if (!Mdx.files.local(COMPILED).exists()) {
Mdx.files.local(COMPILED).mkdirs();
}

cc.setTargetDirectory(COMPILED);
Compiler compiler = new Compiler(cc);

File[] files = ArrayUtils.removeElement(new File(rootPath).listFiles(), new File(rootPath + "Compiled"));
compiler.compile(files);
FileHandle[] list = Mdx.files.local(rootPath).list(".groovy");
ArrayList<String> files = new ArrayList<>();
for (FileHandle f : list) {
files.add(f.path());
}
String[] out = new String[files.size()];
out = files.toArray(out);

compiler.compile(out);
}

/**
Expand Down Expand Up @@ -142,7 +151,7 @@ public void destroy() {
public Object compile(String path) {
try {
String url = path.substring(0, path.lastIndexOf("/"));
gcl.addURL(new File(GameRuntime.getProgramPath()+"/"+url).toURI().toURL());
gcl.addClasspath(Mdx.files.local(GameRuntime.getProgramPath() + "/" + url).path());
return gcl.parseClass(new File(Mdx.files.local(GameRuntime.getProgramPath() + "/" + path + ".groovy").path())).getDeclaredConstructors()[0].newInstance();
} catch (CompilationFailedException | IOException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(EngineLoader.class.getName()).log(Level.SEVERE, null, ex);
Expand All @@ -160,23 +169,33 @@ public Object compile(String path) {
public void compile(String path, String out) {
String output = GameRuntime.getProgramPath() + "/" + out;
String COMPILED = Mdx.files.local(output).toString();
try {
gcl.addURL(new File(COMPILED).toURI().toURL());
} catch (MalformedURLException ex) {
Logger.getLogger(EngineLoader.class.getName()).log(Level.SEVERE, null, ex);
}
gcl.addClasspath(Mdx.files.local(COMPILED).path());

String codePath = GameRuntime.getProgramPath() + "/" + path;

CompilerConfiguration cc = new CompilerConfiguration();
cc.setClasspath(codePath);
if (!(new File(COMPILED).exists())) {
new File(COMPILED).mkdir();
try {
if (!Mdx.files.local(COMPILED).exists()) {
Mdx.files.local(COMPILED).mkdirs();
}

cc.setTargetDirectory(COMPILED);
Compiler compiler = new Compiler(cc);

FileHandle[] list = Mdx.files.local(rootPath).list(".groovy");
ArrayList<String> files = new ArrayList<>();
for (FileHandle f : list) {
files.add(f.path());
}
String[] fileNames = new String[files.size()];
fileNames = files.toArray(fileNames);

compiler.compile(fileNames);
} catch (IOException ex) {
Logger.getLogger(EngineLoader.class.getName()).log(Level.SEVERE, null, ex);
}
cc.setTargetDirectory(COMPILED);
Compiler compiler = new Compiler(cc);
File[] files = ArrayUtils.removeElement(new File(codePath).listFiles(), new File(codePath + out));
compiler.compile(files);

}

/**
Expand All @@ -187,12 +206,7 @@ public void compile(String path, String out) {
*/
public void loadLib(String path) {
String COMPILED = GameRuntime.getProgramPath() + "/" + path + "/";
try {
gcl.addURL(new File(COMPILED).toURI().toURL());
} catch (MalformedURLException ex) {
Logger.getLogger(EngineLoader.class.getName()).log(Level.SEVERE, null, ex);
}

gcl.addClasspath(Mdx.files.local(COMPILED).path());
}

/**
Expand Down

0 comments on commit 07dca26

Please sign in to comment.