Cache isJar value in CodeEnvironment instead of looking it up everytime somebody asks.
This commit is contained in:
parent
d1caeb341a
commit
343c26ecc8
@ -11,38 +11,40 @@ import java.util.zip.ZipInputStream;
|
||||
|
||||
/**
|
||||
* This helper is used by the Loader to get a list of files of the whole Code
|
||||
* and determine specific runtime configurations.
|
||||
* Since there are some differences between OS and jar/non-jar environments,
|
||||
* this helper makes sure, everything still works.
|
||||
* and determine specific runtime configurations. Since there are some
|
||||
* differences between OS and jar/non-jar environments, this helper makes sure,
|
||||
* everything still works.
|
||||
*/
|
||||
public class CodeEnvironment {
|
||||
|
||||
/**
|
||||
* Private constructor, this class will never be instantiated.
|
||||
* Constant telling whether the Code is run from a jar or not.
|
||||
*/
|
||||
private CodeEnvironment() {}
|
||||
|
||||
public static final boolean isJar = CodeEnvironment.class
|
||||
.getProtectionDomain().getCodeSource().getLocation().toString()
|
||||
.endsWith("jar");
|
||||
|
||||
/**
|
||||
* Returns true if the program is run out of a jar.
|
||||
* Private constructor, this class will never be instantiated.
|
||||
*/
|
||||
public static boolean isJar() {
|
||||
return (CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation().toString().endsWith("jar"));
|
||||
private CodeEnvironment() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String[] containing relative paths to files within the code.
|
||||
*/
|
||||
public static String[] getFileList() {
|
||||
URL codeLocation = CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation();
|
||||
if(CodeEnvironment.isJar()) {
|
||||
URL codeLocation = CodeEnvironment.class.getProtectionDomain()
|
||||
.getCodeSource().getLocation();
|
||||
if (CodeEnvironment.isJar) {
|
||||
return CodeEnvironment.getFileListFromJar(codeLocation);
|
||||
} else {
|
||||
File codeFolder = null;
|
||||
try {
|
||||
codeFolder = new File(codeLocation.toURI());
|
||||
} catch (URISyntaxException e) {
|
||||
System.err.println("Could not convert codeLocation URL to File!");
|
||||
System.err
|
||||
.println("Could not convert codeLocation URL to File!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return CodeEnvironment.getFileListFromFolder(codeFolder);
|
||||
@ -55,8 +57,9 @@ public class CodeEnvironment {
|
||||
private static String[] getFileListFromFolder(File folder) {
|
||||
ArrayList<String> fileList = new ArrayList<String>();
|
||||
String rootPath = folder.getAbsolutePath() + File.separator;
|
||||
String[] folderContents = CodeEnvironment.getAbsoluteFileListFromFolder(folder);
|
||||
for(String element : folderContents) {
|
||||
String[] folderContents = CodeEnvironment
|
||||
.getAbsoluteFileListFromFolder(folder);
|
||||
for (String element : folderContents) {
|
||||
fileList.add(element.replace(rootPath, ""));
|
||||
}
|
||||
return fileList.toArray(new String[fileList.size()]);
|
||||
@ -68,10 +71,11 @@ public class CodeEnvironment {
|
||||
private static String[] getAbsoluteFileListFromFolder(File folder) {
|
||||
ArrayList<String> fileList = new ArrayList<String>();
|
||||
File[] folderContents = folder.listFiles();
|
||||
for(File f : folderContents) {
|
||||
if(f.isDirectory()) {
|
||||
String[] filesInDirectory = CodeEnvironment.getAbsoluteFileListFromFolder(f);
|
||||
for(String entry : filesInDirectory) {
|
||||
for (File f : folderContents) {
|
||||
if (f.isDirectory()) {
|
||||
String[] filesInDirectory = CodeEnvironment
|
||||
.getAbsoluteFileListFromFolder(f);
|
||||
for (String entry : filesInDirectory) {
|
||||
fileList.add(entry);
|
||||
}
|
||||
} else {
|
||||
@ -82,7 +86,8 @@ public class CodeEnvironment {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a relative path list of files that are contained within the current jar.
|
||||
* Returns a relative path list of files that are contained within the
|
||||
* current jar.
|
||||
*/
|
||||
private static String[] getFileListFromJar(URL jar) {
|
||||
List<String> list = new ArrayList<String>();
|
||||
|
@ -67,7 +67,7 @@ public class Loader {
|
||||
*/
|
||||
public static BufferedImage getBufferedImageByFilename(String filename) {
|
||||
BufferedImage image = null;
|
||||
if(CodeEnvironment.isJar()) {
|
||||
if(CodeEnvironment.isJar) {
|
||||
image = Loader.images.get(filename);
|
||||
} else {
|
||||
image = Loader.images.get(filename.replace("/", File.separator));
|
||||
@ -83,7 +83,7 @@ public class Loader {
|
||||
* Return the loaded sound URL by its relative filename.
|
||||
*/
|
||||
public static URL getSoundURLByFilename(String filename) {
|
||||
if(CodeEnvironment.isJar()) {
|
||||
if(CodeEnvironment.isJar) {
|
||||
return Loader.sounds.get(filename);
|
||||
} else {
|
||||
return Loader.sounds.get(filename.replace("/", File.separator));
|
||||
@ -94,7 +94,7 @@ public class Loader {
|
||||
* Return the LevelConfig by its relative filename.
|
||||
*/
|
||||
public static LevelConfig getLevelConfigByFilename(String filename) {
|
||||
if(CodeEnvironment.isJar()) {
|
||||
if(CodeEnvironment.isJar) {
|
||||
return Loader.levelConfigs.get(filename);
|
||||
} else {
|
||||
return Loader.levelConfigs.get(filename.replace("/", File.separator));
|
||||
|
Loading…
Reference in New Issue
Block a user