Now that you've clicked on this thread since the name looked interesting, here's why I'm posting this:
I've been looking around all over the forums for and in-depth explanation of how RuneScape's Cache works, I was unable to find anything remotely close. So, after reading a number of different Cache readers/writers I decided to make a post and explain it. Note: None of this terminology is official and perfect, I'm simply going off what seemed most common throughout all of the different implementations I've read to keep confusion to a minimum.
The Cache is like a Text Book
An easy way I find to think of the RuneScape Cache is like a Text Book, below is definitions and comparisons between the different parts of make up the RuneScape Cache and a Text Book.
Index Files – Table of Contents
File Name: main_file_cache.idx0 . . . main_file_cache.idx255
Index files can be thought of as large Tables of Contents like you would see in a book. They contain many entries, Indexes, which tell where individual files are within the main Data File so that they can be retrieved and used. Note: there are multiple Index Files, each Index File only holds the Indexes for 1 type of file, you can tell which file type a Index File holds by the number at the end.
Data Files – All of the Information
File Name: main_file_cache.dat and main_file_cache.dat2
Data Files can be thought of as the book from chapter 1 to the last chapter, they contain many different files all bonded together to form a single file. It is impossible to quickly find the correct file within the Cache without using the Indexes provided by Index Files.
Indexes – Table of Contents Entries
Indexes can be thought of as the individual entries of the Table of Contents, they are used to find where files start and where they end so that Sectors from the data file can be combined together form their original file.
Information |
Type |
Description |
File Size |
Medium (3 Bytes) |
The size of the file that this Index points to. |
Sector ID |
Medium (3 Bytes) |
The first Sector of the file that this Index points to. |
Example Code
Code:
public static Index read(ByteBuffer buffer) {
// Read values
int fileSize = ByteBufferUtil.getMedium(buffer);
int sectorId = ByteBufferUtil.getMedium(buffer);
// Create and Return
return new Index(fileSize, sectorId);
}
public ByteBuffer toByteBuffer() {
// Allocate buffer
ByteBuffer buf = ByteBuffer.allocate(SIZE);
// Write values
ByteBufferUtil.putMedium(fileSize, buf);
ByteBufferUtil.putMedium(sectorId, buf);
// Flip and Return
return (ByteBuffer) buf.flip();
}
Sectors – Pages
Sectors can be thought of as pages within the book. Each page is exactly the same size but can be more or less filled with information, if there’s empty space that space is left blank rather than cutting it out. Multiple pages form chapters much like how multiple Sectors form files.
Information |
Type |
Description |
File ID |
Unsigned Short |
The file that this Sector belongs to. |
Chunk ID |
Unsigned Short |
Which chunk of the file the data of the Sector is. |
Sector ID |
Medium (3 Bytes) |
Which Sector of the data file this is. |
Type ID |
Unsigned Byte |
The type of file this Sector belongs to. |
Data |
512 Bytes |
The raw data that this Section contains. |
Example Code
Code:
public static Sector read(ByteBuffer buf) {
// Read header
int file = (buf.getShort() & 0xffff);
int chunk = (buf.getShort() & 0xffff);
int id = ByteBufferUtil.getMedium(buf);
int type = (buf.get() & 0xff);
// Read data into temporary buffer
byte[] temp = new byte[DATA_SIZE];
buf.get(temp);
// Create and Return
return new Sector(id, file, type, chunk, ByteBuffer.wrap(temp));
}
public ByteBuffer toByteBuffer() {
// Allocate buffer
ByteBuffer buf = ByteBuffer.allocate(SIZE);
// Write header
buf.putShort((short) file);
buf.putShort((short) chunk);
ByteBufferUtil.putMedium(id, buf);
buf.put((byte) type);
// Write data
buf.put(data);
// Flip and Return
return (ByteBuffer) buf.flip();
}
Still Working on this! Feel free to ask questions or fix mistakes while I finish this!