Merge branch 'master' of https://github.com/teamteamteam/SpaceScooter
This commit is contained in:
commit
9bba32307e
@ -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
|
* This helper is used by the Loader to get a list of files of the whole Code
|
||||||
* and determine specific runtime configurations.
|
* and determine specific runtime configurations. Since there are some
|
||||||
* Since there are some differences between OS and jar/non-jar environments,
|
* differences between OS and jar/non-jar environments, this helper makes sure,
|
||||||
* this helper makes sure, everything still works.
|
* everything still works.
|
||||||
*/
|
*/
|
||||||
public class CodeEnvironment {
|
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() {
|
private CodeEnvironment() {
|
||||||
return (CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation().toString().endsWith("jar"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a String[] containing relative paths to files within the code.
|
* Returns a String[] containing relative paths to files within the code.
|
||||||
*/
|
*/
|
||||||
public static String[] getFileList() {
|
public static String[] getFileList() {
|
||||||
URL codeLocation = CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation();
|
URL codeLocation = CodeEnvironment.class.getProtectionDomain()
|
||||||
if(CodeEnvironment.isJar()) {
|
.getCodeSource().getLocation();
|
||||||
|
if (CodeEnvironment.isJar) {
|
||||||
return CodeEnvironment.getFileListFromJar(codeLocation);
|
return CodeEnvironment.getFileListFromJar(codeLocation);
|
||||||
} else {
|
} else {
|
||||||
File codeFolder = null;
|
File codeFolder = null;
|
||||||
try {
|
try {
|
||||||
codeFolder = new File(codeLocation.toURI());
|
codeFolder = new File(codeLocation.toURI());
|
||||||
} catch (URISyntaxException e) {
|
} 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();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return CodeEnvironment.getFileListFromFolder(codeFolder);
|
return CodeEnvironment.getFileListFromFolder(codeFolder);
|
||||||
@ -55,8 +57,9 @@ public class CodeEnvironment {
|
|||||||
private static String[] getFileListFromFolder(File folder) {
|
private static String[] getFileListFromFolder(File folder) {
|
||||||
ArrayList<String> fileList = new ArrayList<String>();
|
ArrayList<String> fileList = new ArrayList<String>();
|
||||||
String rootPath = folder.getAbsolutePath() + File.separator;
|
String rootPath = folder.getAbsolutePath() + File.separator;
|
||||||
String[] folderContents = CodeEnvironment.getAbsoluteFileListFromFolder(folder);
|
String[] folderContents = CodeEnvironment
|
||||||
for(String element : folderContents) {
|
.getAbsoluteFileListFromFolder(folder);
|
||||||
|
for (String element : folderContents) {
|
||||||
fileList.add(element.replace(rootPath, ""));
|
fileList.add(element.replace(rootPath, ""));
|
||||||
}
|
}
|
||||||
return fileList.toArray(new String[fileList.size()]);
|
return fileList.toArray(new String[fileList.size()]);
|
||||||
@ -68,10 +71,11 @@ public class CodeEnvironment {
|
|||||||
private static String[] getAbsoluteFileListFromFolder(File folder) {
|
private static String[] getAbsoluteFileListFromFolder(File folder) {
|
||||||
ArrayList<String> fileList = new ArrayList<String>();
|
ArrayList<String> fileList = new ArrayList<String>();
|
||||||
File[] folderContents = folder.listFiles();
|
File[] folderContents = folder.listFiles();
|
||||||
for(File f : folderContents) {
|
for (File f : folderContents) {
|
||||||
if(f.isDirectory()) {
|
if (f.isDirectory()) {
|
||||||
String[] filesInDirectory = CodeEnvironment.getAbsoluteFileListFromFolder(f);
|
String[] filesInDirectory = CodeEnvironment
|
||||||
for(String entry : filesInDirectory) {
|
.getAbsoluteFileListFromFolder(f);
|
||||||
|
for (String entry : filesInDirectory) {
|
||||||
fileList.add(entry);
|
fileList.add(entry);
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
private static String[] getFileListFromJar(URL jar) {
|
||||||
List<String> list = new ArrayList<String>();
|
List<String> list = new ArrayList<String>();
|
||||||
|
@ -67,7 +67,7 @@ public class Loader {
|
|||||||
*/
|
*/
|
||||||
public static BufferedImage getBufferedImageByFilename(String filename) {
|
public static BufferedImage getBufferedImageByFilename(String filename) {
|
||||||
BufferedImage image = null;
|
BufferedImage image = null;
|
||||||
if(CodeEnvironment.isJar()) {
|
if(CodeEnvironment.isJar) {
|
||||||
image = Loader.images.get(filename);
|
image = Loader.images.get(filename);
|
||||||
} else {
|
} else {
|
||||||
image = Loader.images.get(filename.replace("/", File.separator));
|
image = Loader.images.get(filename.replace("/", File.separator));
|
||||||
@ -83,7 +83,7 @@ public class Loader {
|
|||||||
* Return the loaded sound URL by its relative filename.
|
* Return the loaded sound URL by its relative filename.
|
||||||
*/
|
*/
|
||||||
public static URL getSoundURLByFilename(String filename) {
|
public static URL getSoundURLByFilename(String filename) {
|
||||||
if(CodeEnvironment.isJar()) {
|
if(CodeEnvironment.isJar) {
|
||||||
return Loader.sounds.get(filename);
|
return Loader.sounds.get(filename);
|
||||||
} else {
|
} else {
|
||||||
return Loader.sounds.get(filename.replace("/", File.separator));
|
return Loader.sounds.get(filename.replace("/", File.separator));
|
||||||
@ -94,7 +94,7 @@ public class Loader {
|
|||||||
* Return the LevelConfig by its relative filename.
|
* Return the LevelConfig by its relative filename.
|
||||||
*/
|
*/
|
||||||
public static LevelConfig getLevelConfigByFilename(String filename) {
|
public static LevelConfig getLevelConfigByFilename(String filename) {
|
||||||
if(CodeEnvironment.isJar()) {
|
if(CodeEnvironment.isJar) {
|
||||||
return Loader.levelConfigs.get(filename);
|
return Loader.levelConfigs.get(filename);
|
||||||
} else {
|
} else {
|
||||||
return Loader.levelConfigs.get(filename.replace("/", File.separator));
|
return Loader.levelConfigs.get(filename.replace("/", File.separator));
|
||||||
|
Loading…
Reference in New Issue
Block a user