Add preloading for non-anonymous classes.

This commit is contained in:
Jan Philipp Timme 2014-11-09 22:12:31 +01:00
parent 189c526b67
commit d59fbafd8c
1 changed files with 21 additions and 0 deletions

View File

@ -83,6 +83,11 @@ public class Loader {
loadingScreen.initialize(0, elements.length);
for(int i=0; i<elements.length; i++) {
String e = elements[i];
if(e.endsWith(".class")) {
if(GameConfig.DEBUG)
System.out.println("Loading Class for: " + e);
Loader.preloadClassByFilename(e);
}
if(e.endsWith(".png")) {
if(GameConfig.DEBUG)
System.out.println("Creating BufferedImage for: " + e);
@ -97,6 +102,22 @@ public class Loader {
}
}
/**
* Preload a given class by its filename using the ClassLoader.
* This way, we avoid reading out of a jar later.
*/
private static void preloadClassByFilename(String classFilename) {
if(classFilename.contains("$")) return; //skip anonymous classes
String className = classFilename.replace(".class", "").replace(File.separator, ".");
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
System.err.println("Error preloading class: " + classFilename);
System.err.println("> Resulting class name: " + className);
e.printStackTrace();
}
}
/**
* Load a BufferedImage by relative filename.
*/