Skip to content

Commit

Permalink
Handle missing item images
Browse files Browse the repository at this point in the history
Made StiLoader use VFSAccessor instead of byte streams
Added toString to StiLoader
Added JavaFx image cache to StiLoader
Added error messages for missing item images
Added two more test examples to intro tab
Refactored and reformatted a few files
  • Loading branch information
starcatter committed Mar 27, 2019
1 parent 85627cd commit 3c994a1
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 192 deletions.
104 changes: 54 additions & 50 deletions assetLoader/src/main/java/thebob/assetloader/AssetLoader.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License
*
* Copyright 2017 the_bob.
Expand All @@ -23,103 +23,107 @@
*/
package thebob.assetloader;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import javafx.scene.control.TreeItem;
import thebob.assetloader.slf.SlfLoader;
import thebob.assetloader.sti.StiLoader;
import thebob.assetloader.vfs.VFSConfig;
import thebob.assetloader.vfs.VirtualFileSystem;
import thebob.assetloader.vfs.accessors.SLFAccessor;
import thebob.assetloader.vfs.accessors.VFSAccessor;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;

/**
*
* @author the_bob
*/
public class AssetLoader {

public SlfLoader loadLibrary(String fileName) {
SlfLoader libLoader = new SlfLoader();
libLoader.loadFile(fileName);
return libLoader;
SlfLoader libLoader = new SlfLoader();
libLoader.loadFile(fileName);
return libLoader;
}

protected static boolean testAssets() {
SlfLoader libLoader = new SlfLoader();
libLoader.loadFile("Tilesets.slf");
SlfLoader libLoader = new SlfLoader();
libLoader.loadFile("Tilesets.slf");

StiLoader stiLoader = new StiLoader();

for (String fileName : libLoader.getFiles()) {
String storedName = "@LOCAL\\" + fileName.toUpperCase();

VFSAccessor accessor = new SLFAccessor(libLoader, fileName, storedName);

StiLoader stiLoader = new StiLoader();
stiLoader.loadAsset(accessor);
}

for (int i = 0; i < libLoader.getAssetCount(); i++) {
ByteBuffer buffer = libLoader.getAsset(i);
stiLoader.loadAsset(buffer);
}
return true;
return true;
}

protected static void printVFSFileVariants(VirtualFileSystem vfs, String filePath) {
for (String configName : vfs.getConfigNames()) {
VFSConfig config = vfs.getConfig(configName);

LinkedList<VFSAccessor> variants = config.getFileVariants(filePath);
System.out.println("\nthebob.assetloader.AssetLoader.testVFS() config " + configName);
if (variants == null) {
System.out.println("\t-> no file (" + filePath + ")");
} else {
for (VFSAccessor variant : variants) {
System.out.println("\t-> " + variant);
}
}
}
for (String configName : vfs.getConfigNames()) {
VFSConfig config = vfs.getConfig(configName);

LinkedList<VFSAccessor> variants = config.getFileVariants(filePath);
System.out.println("\nthebob.assetloader.AssetLoader.testVFS() config " + configName);
if (variants == null) {
System.out.println("\t-> no file (" + filePath + ")");
} else {
for (VFSAccessor variant : variants) {
System.out.println("\t-> " + variant);
}
}
}
}

protected static void dumpVFSMapList(VirtualFileSystem vfs) {
for (String configName : vfs.getConfigNames()) {
dumpVFSMapList(vfs, configName);
}
for (String configName : vfs.getConfigNames()) {
dumpVFSMapList(vfs, configName);
}
}

protected static void dumpVFSMapList(VirtualFileSystem vfs, String configName) {
VFSConfig config = vfs.getConfig(configName);
System.out.println(configName);
VFSConfig config = vfs.getConfig(configName);
System.out.println(configName);

for (String profile : config.getProfiles().keySet()) {
System.out.println("\t" + profile);
for (String profile : config.getProfiles().keySet()) {
System.out.println("\t" + profile);

ArrayList<VFSAccessor> files = config.getProfiles().get(profile);
ArrayList<VFSAccessor> files = config.getProfiles().get(profile);

System.out.println("\t\t" + files.stream().filter(f -> f.getVFSPath().startsWith("\\MAPS\\")).count() + " maps");
System.out.println("\t\t" + files.stream().filter(f -> f.getVFSPath().startsWith("\\MAPS\\")).count() + " maps");

/*
files.stream().filter(f -> f.startsWith("\\MAPS\\")).sorted().forEach(mapName -> {
System.out.println("\t\t" + (mapName.replace("\\MAPS\\", "")));
});
*/
}
}
}

protected static boolean testVFS() {
try {
VirtualFileSystem vfs = new VirtualFileSystem("../../JA113.data/gameData");
//dumpVFSMapList(vfs, "vfs_config.JA2113AIMNAS.ini");
dumpVFSMapList(vfs, "vfs_config.JA2113-Metavira.ini");
try {
VirtualFileSystem vfs = new VirtualFileSystem("../../JA113.data/gameData");
//dumpVFSMapList(vfs, "vfs_config.JA2113AIMNAS.ini");
dumpVFSMapList(vfs, "vfs_config.JA2113-Metavira.ini");

// printVFSFileVariants(vfs, "\\MAPS\\A9.DAT");
// printVFSFileVariants(vfs, "\\TABLEDATA\\ITEMS\\ITEMS.XML");
// printVFSFileVariants(vfs, "\\Ja2Set.dat.xml");
// printVFSFileVariants(vfs, "\\Ja2_Options.INI");
// printVFSFileVariants(vfs, "\\BinaryData\\JA2set.dat");
// printVFSFileVariants(vfs, "\\TILESETS\\0\\BUILD_01.STI");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

public static void main(String[] args) {
testVFS();
testVFS();
}

}
93 changes: 66 additions & 27 deletions assetLoader/src/main/java/thebob/assetloader/sti/StiLoader.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License
*
* Copyright 2017 the_bob.
Expand All @@ -23,29 +23,29 @@
*/
package thebob.assetloader.sti;

import javolution.io.Union;
import thebob.assetloader.common.AutoLoadingStruct;
import thebob.assetloader.common.ImageAdapter;
import thebob.assetloader.slf.SlfLoader;
import thebob.assetloader.vfs.accessors.VFSAccessor;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javolution.io.Struct;
import javolution.io.Union;
import thebob.assetloader.common.AutoLoadingStruct;
import thebob.assetloader.slf.SlfLoader;

import static thebob.assetloader.sti.HImageConstants.iCOMPRESS_RUN_MASK;
import static thebob.assetloader.sti.HImageConstants.iCOMPRESS_TRANSPARENT;
import static thebob.assetloader.sti.StiConstants.*;
import static thebob.assetloader.sti.HImageConstants.*;

/**
*
*
* BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
*
*
* @author the_bob
*/
class ETRLEObject extends AutoLoadingStruct {
Expand Down Expand Up @@ -113,7 +113,7 @@ class STCIHeader extends AutoLoadingStruct {
Unsigned16 usHeight = new Unsigned16();
Unsigned16 usWidth = new Unsigned16();
STCIHeaderFormatInfo format = inner(new STCIHeaderFormatInfo());
Unsigned8 ubDepth = new Unsigned8(); // size in bits of one pixel as stored in the file
Unsigned8 ubDepth = new Unsigned8(); // size in bits of one pixel as stored in the file
Unsigned32 uiAppDataSize = new Unsigned32();
Unsigned8[] cUnused = array(new Unsigned8[11]); // was [15] in JA2, shortened to match required 64B size

Expand Down Expand Up @@ -204,7 +204,7 @@ class HImageConstants {
public static final int iCOMPRESS_TRANSPARENT = 0x80;
public static final int iCOMPRESS_RUN_MASK = 0x7F;

// Defines for type of file readers
// Defines for type of file readers
public static final int PCX_FILE_READER = 0x1;
public static final int TGA_FILE_READER = 0x2;
public static final int STCI_FILE_READER = 0x4;
Expand All @@ -213,11 +213,11 @@ class HImageConstants {
public static final int JPC_FILE_READER = 0x20;
public static final int UNKNOWN_FILE_READER = 0x200;

// Defines for buffer bit depth
// Defines for buffer bit depth
public static final int BUFFER_8BPP = 0x1;
public static final int BUFFER_16BPP = 0x2;

// Defines for image charactoristics
// Defines for image charactoristics
public static final int IMAGE_COMPRESSED = 0x0001;
public static final int IMAGE_TRLECOMPRESSED = 0x0002;
public static final int IMAGE_PALETTE = 0x0004;
Expand Down Expand Up @@ -248,7 +248,7 @@ class StiConstants {
public static final int STCI_ALPHA = 0x0002;
public static final int STCI_TRANSPARENT = 0x0001;

// ETRLE defines
// ETRLE defines
public static final int COMPRESS_TRANSPARENT = 0x80;
public static final int COMPRESS_NON_TRANSPARENT = 0x00;
public static final int COMPRESS_RUN_LIMIT = 0x7F;
Expand All @@ -262,6 +262,8 @@ class StiConstants {

public class StiLoader {

private String file;

private long fileLength;
ByteBuffer buffer;

Expand All @@ -272,26 +274,28 @@ public class StiLoader {
private STCIPaletteElement[] palette = null;
private ETRLEObject[] objects = null;

public int getTransparentValue(){
private javafx.scene.image.Image[] jfxImageCache;

public int getTransparentValue() {
return (int) header.uiTransparentValue.get();
}
public int getImageWidth(int index){

public int getImageWidth(int index) {
return objects == null ? header.usWidth.get() : objects[index].usWidth.get();
}
public int getImageHeight(int index){

public int getImageHeight(int index) {
return objects == null ? header.usHeight.get() : objects[index].usHeight.get();
}
public int getImageOffsetX(int index){

public int getImageOffsetX(int index) {
return objects == null ? 0 : objects[index].sOffsetX.get();
}
public int getImageOffsetY(int index){

public int getImageOffsetY(int index) {
return objects == null ? 0 : objects[index].sOffsetY.get();
}

public int getImageCount() {
if (objects == null) {
return 1;
Expand All @@ -300,6 +304,24 @@ public int getImageCount() {
}
}


public javafx.scene.image.Image getJFXImage(int index) {
if (jfxImageCache == null) {
jfxImageCache = new javafx.scene.image.Image[getImageCount()];
}

if (index >= getImageCount()) {
// System.err.println("Requested image index out of bounds: " + index + "/"+(getImageCount()-1));
return null;
}

if (jfxImageCache[index] == null) {
jfxImageCache[index] = ImageAdapter.convertStiImage(getImageWidth(index), getImageHeight(index), getImage(index), getPalette());
}

return jfxImageCache[index];
}

public byte[] getImage(int index) {
if (objects == null) {
return imageData;
Expand All @@ -320,7 +342,10 @@ public byte[][] getPalette() {
return paletteArray;
}

// ---

public boolean loadFile(String fileName) {
file = fileName;
try (final RandomAccessFile file = new RandomAccessFile(fileName, "r")) {
fileLength = file.length();

Expand All @@ -337,7 +362,12 @@ public boolean loadFile(String fileName) {
return true;
}

public boolean loadAsset(ByteBuffer inputBuffer) {
public boolean loadAsset(VFSAccessor accessor) {
file = accessor.toString();
return loadAsset(accessor.getBytes());
}

private boolean loadAsset(ByteBuffer inputBuffer) {
buffer = inputBuffer;

if (buffer.limit() < STCI_HEADER_SIZE) {
Expand Down Expand Up @@ -368,7 +398,7 @@ public boolean loadAsset(ByteBuffer inputBuffer) {
if (!STCILoadIndexed()) {
return (false);
}
} else { // unsupported type of data, or the right flags weren't set!
} else { // unsupported type of data, or the right flags weren't set!
throw new UnsupportedOperationException("Unsupported file type."); // throw here cause we should've caught the bogus data earlier!
}

Expand All @@ -381,6 +411,8 @@ public boolean loadAsset(ByteBuffer inputBuffer) {
return true;
}

// ---

private boolean STCILoadRGB() {

// Allocate memory for the image data and read it in
Expand Down Expand Up @@ -503,4 +535,11 @@ private byte[] decodeIndex(int index) {
return decodeImage(objects[index]);
}

@Override
public String toString() {
return "StiLoader{" +
"file='" + file + '\'' +
", imageCount=" + getImageCount() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ private void loadImage() {
height = loader.getImageHeight(index);
offsetX = loader.getImageOffsetX(index);
offsetY = loader.getImageOffsetY(index);

// TODO: do this once per file, in stiloader or tileloader
image = ImageAdapter.convertStiImage(width, height, loader.getImage(index), loader.getPalette());

image = loader.getJFXImage(index);
}

public int getWidth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TileArray {
public TileArray(VFSAccessor file, int tileType) {
this.file=file;

loader.loadAsset(file.getBytes());
loader.loadAsset(file);
int tileCount = loader.getImageCount();

tileArray = new Tile[tileCount];
Expand Down
Loading

0 comments on commit 3c994a1

Please sign in to comment.