Hi there developers!
I'm trying to develop my own online multiplayer game, using the DarkGDK. But I don't want everyone to have access to the media of the game, so I'm working on my own cache archive format.
This is how it works:
.idx0 = textures
.idx1 = sprites
.idx2 = sounds
.idx3 = models
.idx4 = maps
The .idx# files contain 32bit entries. Each entry is 2 integers long. First int of each entry is an offset into the .dat file (the file where all the textures, sprites, sounds, etc, etc, is actually stored). So after you retrieved the offset into the .dat file you need to know how many bytes you have to read to get the requested file. - This is the next integer of the entry. My problem is that I don't know how to unpack the cache again. I use a RandomAccessFile to compress each file, using it's writeInt(int) method. So naturally I used RandomAccessFiles readInt() method. But when I do that the file I try to unpack has an invalid size. How come?
Here's my "compress()" method:
public void compress(int id) throws Exception{
path = src.getPath();
dst = findIdxFile(path);
byte[] buffer = new byte[(int)src.length()];
RandomAccessFile outputStr = new RandomAccessFile(dst, "rw");
RandomAccessFile inputStr = new RandomAccessFile(src, "rw");
int blockId = id * 2;
int blockLen = buffer.length;
String state;
inputStr.readFully(buffer);
inputStr.close();
outputStr.seek(blockId);
outputStr.writeInt(blockId);
outputStr.writeInt(blockLen);
outputStr.close();
File dataFile = new File("./cache/main_file_cache.dat");
dataFile.delete();
dataFile.createNewFile();
RandomAccessFile mainOutStr = new RandomAccessFile(dataFile, "rw");
mainOutStr.seek(blockId);
mainOutStr.write(buffer);
mainOutStr.close();
System.out.println("src: " + src.getName() + " dst: " + dst.getName() + " off: " + blockId + " len: " + blockLen);
}
Best regards,
Benjamin :).