From 07dca26528ef846c095e27230f93567a61e2dae0 Mon Sep 17 00:00:00 2001 From: torbuntu Date: Mon, 26 Aug 2019 20:02:11 -0500 Subject: [PATCH] -- Upgrades to EngineLoader. Speed improvement on smaller programs. --- assets/Programs/Shapes/program.properties | 2 +- assets/Programs/WizRobo/Code/Shapes.groovy | 3 + build.gradle | 1 - .../main/java/leikr/loaders/EngineLoader.java | 68 +++++++++++-------- 4 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 assets/Programs/WizRobo/Code/Shapes.groovy diff --git a/assets/Programs/Shapes/program.properties b/assets/Programs/Shapes/program.properties index 24385ece..81513db1 100644 --- a/assets/Programs/Shapes/program.properties +++ b/assets/Programs/Shapes/program.properties @@ -4,4 +4,4 @@ type = Demo version = 1.0 author = Tor max_sprites = 2048 -compile_source = false +compile_source = true diff --git a/assets/Programs/WizRobo/Code/Shapes.groovy b/assets/Programs/WizRobo/Code/Shapes.groovy new file mode 100644 index 00000000..8b706fa6 --- /dev/null +++ b/assets/Programs/WizRobo/Code/Shapes.groovy @@ -0,0 +1,3 @@ +class Shapes{ + println "NOOOO" +} diff --git a/build.gradle b/build.gradle index b4a5b894..bd4ed75f 100644 --- a/build.gradle +++ b/build.gradle @@ -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" } } diff --git a/core/src/main/java/leikr/loaders/EngineLoader.java b/core/src/main/java/leikr/loaders/EngineLoader.java index 8c2008f5..327eaf3c 100644 --- a/core/src/main/java/leikr/loaders/EngineLoader.java +++ b/core/src/main/java/leikr/loaders/EngineLoader.java @@ -9,6 +9,7 @@ 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; @@ -16,11 +17,11 @@ 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; /** * @@ -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 files = new ArrayList<>(); + for (FileHandle f : list) { + files.add(f.path()); + } + String[] out = new String[files.size()]; + out = files.toArray(out); + + compiler.compile(out); } /** @@ -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); @@ -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 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); + } /** @@ -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()); } /**