Image loader fixes.
Image locations are now searched separately and in order, instead of just relying on ResourceLoader. Fixes issues with some images in the skins directory not being loaded at all, as well as image arrays being loaded from mixed locations. Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -25,6 +25,7 @@ import java.util.List;
|
|||||||
import org.newdawn.slick.Animation;
|
import org.newdawn.slick.Animation;
|
||||||
import org.newdawn.slick.Image;
|
import org.newdawn.slick.Image;
|
||||||
import org.newdawn.slick.SlickException;
|
import org.newdawn.slick.SlickException;
|
||||||
|
import org.newdawn.slick.util.ResourceLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Game images.
|
* Game images.
|
||||||
@@ -537,16 +538,41 @@ public enum GameImage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of possible filenames (with extensions).
|
* Returns the image file name, with extension, by first looking through
|
||||||
* @return filename list
|
* the given directory and then the default resource locations (unless
|
||||||
|
* dirOnly is true).
|
||||||
|
* @param filename the base file name
|
||||||
|
* @param dir the directory to search first (if non-null)
|
||||||
|
* @param type the file type bitmask (IMG_*)
|
||||||
|
* @param dirOnly if true and dir is non-null, will not search default resource locations
|
||||||
|
* @return the full file name, or null if no file found
|
||||||
*/
|
*/
|
||||||
private static List<String> getFileNames(String filename, byte type) {
|
private static String getImageFileName(String filename, File dir, byte type, boolean dirOnly) {
|
||||||
List<String> list = new ArrayList<String>(2);
|
ArrayList<String> names = new ArrayList<String>(2);
|
||||||
if ((type & IMG_PNG) != 0)
|
if ((type & IMG_PNG) != 0)
|
||||||
list.add(String.format("%s.png", filename));
|
names.add(String.format("%s.png", filename));
|
||||||
if ((type & IMG_JPG) != 0)
|
if ((type & IMG_JPG) != 0)
|
||||||
list.add(String.format("%s.jpg", filename));
|
names.add(String.format("%s.jpg", filename));
|
||||||
return list;
|
int size = names.size();
|
||||||
|
|
||||||
|
// look through directory
|
||||||
|
if (dir != null) {
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
File file = new File(dir, names.get(i));
|
||||||
|
if (file.isFile())
|
||||||
|
return file.getAbsolutePath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// look through default resource path
|
||||||
|
if (!dirOnly || dir == null) {
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
if (ResourceLoader.resourceExists(names.get(i)))
|
||||||
|
return names.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -661,25 +687,28 @@ public enum GameImage {
|
|||||||
// load image array
|
// load image array
|
||||||
if (filenameFormat != null) {
|
if (filenameFormat != null) {
|
||||||
List<Image> list = new ArrayList<Image>();
|
List<Image> list = new ArrayList<Image>();
|
||||||
|
File dir = Options.getSkinDir();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
boolean loaded;
|
while (true) {
|
||||||
do {
|
// look for next image
|
||||||
loaded = false;
|
String filenameFormatted = String.format(filenameFormat, i++);
|
||||||
for (String name : getFileNames(String.format(filenameFormat, i), type)) {
|
String name = getImageFileName(filenameFormatted, dir, type, true);
|
||||||
try {
|
if (i == 1 && name == null) { // first image: check other location
|
||||||
// try loading the image
|
dir = null;
|
||||||
Image img = new Image(name);
|
name = getImageFileName(filenameFormatted, dir, type, true);
|
||||||
|
|
||||||
// image successfully loaded
|
|
||||||
list.add(img);
|
|
||||||
loaded = true;
|
|
||||||
break;
|
|
||||||
} catch (SlickException | RuntimeException e) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
i++;
|
if (name == null)
|
||||||
} while (loaded);
|
break;
|
||||||
|
|
||||||
|
// add image to list
|
||||||
|
try {
|
||||||
|
Image img = new Image(name);
|
||||||
|
list.add(img);
|
||||||
|
} catch (SlickException e) {
|
||||||
|
ErrorHandler.error(String.format("Failed to set default image '%s'.", name), null, false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!list.isEmpty()) {
|
if (!list.isEmpty()) {
|
||||||
this.defaultImages = list.toArray(new Image[list.size()]);
|
this.defaultImages = list.toArray(new Image[list.size()]);
|
||||||
process();
|
process();
|
||||||
@@ -688,20 +717,18 @@ public enum GameImage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load single image
|
// load single image
|
||||||
for (String name : getFileNames(filename, type)) {
|
String name = getImageFileName(filename, Options.getSkinDir(), type, false);
|
||||||
try {
|
if (name == null) {
|
||||||
// try loading the image
|
ErrorHandler.error(String.format("Could not find image '%s'.", filename), null, false);
|
||||||
Image img = new Image(name);
|
return;
|
||||||
|
}
|
||||||
// image successfully loaded
|
try {
|
||||||
this.defaultImage = img;
|
Image img = new Image(name);
|
||||||
process();
|
this.defaultImage = img;
|
||||||
return;
|
process();
|
||||||
} catch (SlickException | RuntimeException e) {
|
} catch (SlickException e) {
|
||||||
continue;
|
ErrorHandler.error(String.format("Failed to set default image '%s'.", filename), null, false);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ErrorHandler.error(String.format("Failed to set default image '%s'.", filename), null, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -724,27 +751,22 @@ public enum GameImage {
|
|||||||
if (filenameFormat != null) {
|
if (filenameFormat != null) {
|
||||||
List<Image> list = new ArrayList<Image>();
|
List<Image> list = new ArrayList<Image>();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
boolean loaded;
|
while (true) {
|
||||||
do {
|
// look for next image
|
||||||
loaded = false;
|
String filenameFormatted = String.format(filenameFormat, i++);
|
||||||
for (String name : getFileNames(String.format(filenameFormat, i), type)) {
|
String name = getImageFileName(filenameFormatted, dir, type, true);
|
||||||
File file = new File(dir, name);
|
if (name == null)
|
||||||
if (!file.isFile())
|
break;
|
||||||
continue;
|
|
||||||
try {
|
|
||||||
// try loading the image
|
|
||||||
Image img = new Image(file.getAbsolutePath());
|
|
||||||
|
|
||||||
// image successfully loaded
|
// add image to list
|
||||||
list.add(img);
|
try {
|
||||||
loaded = true;
|
Image img = new Image(name);
|
||||||
break;
|
list.add(img);
|
||||||
} catch (SlickException | RuntimeException e) {
|
} catch (SlickException e) {
|
||||||
continue;
|
ErrorHandler.error(String.format("Failed to set skin image '%s'.", name), null, false);
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
i++;
|
}
|
||||||
} while (loaded);
|
|
||||||
if (!list.isEmpty()) {
|
if (!list.isEmpty()) {
|
||||||
this.skinImages = list.toArray(new Image[list.size()]);
|
this.skinImages = list.toArray(new Image[list.size()]);
|
||||||
process();
|
process();
|
||||||
@@ -753,27 +775,18 @@ public enum GameImage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// look for a skin image
|
// look for a skin image
|
||||||
String errorFile = null;
|
String name = getImageFileName(filename, dir, type, true);
|
||||||
for (String name : getFileNames(filename, type)) {
|
if (name == null)
|
||||||
File file = new File(dir, name);
|
return false;
|
||||||
if (!file.isFile())
|
try {
|
||||||
continue;
|
Image img = new Image(name);
|
||||||
try {
|
this.skinImage = img;
|
||||||
// try loading the image
|
process();
|
||||||
Image img = new Image(file.getAbsolutePath());
|
return true;
|
||||||
|
} catch (SlickException e) {
|
||||||
// image successfully loaded
|
skinImage = null;
|
||||||
this.skinImage = img;
|
ErrorHandler.error(String.format("Failed to set skin image '%s'.", name), null, false);
|
||||||
process();
|
|
||||||
return true;
|
|
||||||
} catch (SlickException | RuntimeException e) {
|
|
||||||
errorFile = file.getAbsolutePath();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
skinImage = null;
|
|
||||||
if (errorFile != null)
|
|
||||||
ErrorHandler.error(String.format("Failed to set skin image '%s'.", errorFile), null, false);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ public class SoundController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the sound file name, with extension, by first looking through
|
* Returns the sound file name, with extension, by first looking through
|
||||||
* the skins directory and then the default images.
|
* the skins directory and then the default resource locations.
|
||||||
* @param filename the base file name
|
* @param filename the base file name
|
||||||
* @return the full file name, or null if no file found
|
* @return the full file name, or null if no file found
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user