Optimise definition decoding for faster start-up.

This commit is contained in:
Major-
2015-08-29 19:57:57 +01:00
parent b134f6fdf5
commit 38af001083
15 changed files with 726 additions and 934 deletions
@@ -1,6 +1,7 @@
package org.apollo.cache.decoder;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import org.apollo.cache.IndexedFileSystem;
@@ -13,47 +14,46 @@ import org.apollo.util.BufferUtil;
*
* @author Graham
*/
public final class ItemDefinitionDecoder {
public final class ItemDefinitionDecoder implements Runnable {
/**
* The {@link IndexedFileSystem}.
* The IndexedFileSystem.
*/
private final IndexedFileSystem fs;
/**
* Creates the item definition decoder.
* Creates the ItemDefinitionDecoder.
*
* @param fs The indexed file system.
* @param fs The {@link IndexedFileSystem}.
*/
public ItemDefinitionDecoder(IndexedFileSystem fs) {
this.fs = fs;
}
/**
* Decodes the item definitions.
*
* @return The item definitions.
* @throws IOException If an I/O error occurs.
*/
public ItemDefinition[] decode() throws IOException {
Archive config = fs.getArchive(0, 2);
ByteBuffer data = config.getEntry("obj.dat").getBuffer();
ByteBuffer idx = config.getEntry("obj.idx").getBuffer();
@Override
public void run() {
try {
Archive config = fs.getArchive(0, 2);
ByteBuffer data = config.getEntry("obj.dat").getBuffer();
ByteBuffer idx = config.getEntry("obj.idx").getBuffer();
int count = idx.getShort(), index = 2;
int[] indices = new int[count];
for (int i = 0; i < count; i++) {
indices[i] = index;
index += idx.getShort();
int count = idx.getShort(), index = 2;
int[] indices = new int[count];
for (int i = 0; i < count; i++) {
indices[i] = index;
index += idx.getShort();
}
ItemDefinition[] definitions = new ItemDefinition[count];
for (int i = 0; i < count; i++) {
data.position(indices[i]);
definitions[i] = decode(i, data);
}
ItemDefinition.init(definitions);
} catch (IOException e) {
throw new UncheckedIOException("Error decoding ItemDefinitions.", e);
}
ItemDefinition[] defs = new ItemDefinition[count];
for (int i = 0; i < count; i++) {
data.position(indices[i]);
defs[i] = decode(i, data);
}
return defs;
}
/**
@@ -121,12 +121,12 @@ public final class MapFileDecoder {
int count = buffer.capacity() / (3 * Short.BYTES + Byte.BYTES);
for (int times = 0; times < count; times++) {
int packed = buffer.getShort() & 0xFFFF;
int id = buffer.getShort() & 0xFFFF;
int terrain = buffer.getShort() & 0xFFFF;
int objects = buffer.getShort() & 0xFFFF;
boolean members = buffer.get() == 1;
definitions.put(packed, new MapDefinition(packed, terrain, objects, members));
definitions.put(id, new MapDefinition(id, terrain, objects, members));
}
return definitions;
@@ -1,6 +1,7 @@
package org.apollo.cache.decoder;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
@@ -14,47 +15,47 @@ import org.apollo.util.BufferUtil;
*
* @author Major
*/
public final class NpcDefinitionDecoder {
public final class NpcDefinitionDecoder implements Runnable {
/**
* The {@link IndexedFileSystem}.
* The IndexedFileSystem.
*/
private final IndexedFileSystem fs;
/**
* Creates the npc definition decoder.
* Creates the NpcDefinitionDecoder.
*
* @param fs The indexed file system.
* @param fs The {@link IndexedFileSystem}.
*/
public NpcDefinitionDecoder(IndexedFileSystem fs) {
this.fs = fs;
}
/**
* Decodes the npc definitions.
*
* @return An array of all parsed npc definitions.
* @throws IOException If an I/O error occurs.
*/
public NpcDefinition[] decode() throws IOException {
Archive config = fs.getArchive(0, 2);
ByteBuffer data = config.getEntry("npc.dat").getBuffer();
ByteBuffer idx = config.getEntry("npc.idx").getBuffer();
@Override
public void run() {
try {
Archive config = fs.getArchive(0, 2);
ByteBuffer data = config.getEntry("npc.dat").getBuffer();
ByteBuffer idx = config.getEntry("npc.idx").getBuffer();
int count = idx.getShort(), index = 2;
int[] indices = new int[count];
for (int i = 0; i < count; i++) {
indices[i] = index;
index += idx.getShort();
int count = idx.getShort(), index = 2;
int[] indices = new int[count];
for (int i = 0; i < count; i++) {
indices[i] = index;
index += idx.getShort();
}
NpcDefinition[] definitions = new NpcDefinition[count];
for (int i = 0; i < count; i++) {
data.position(indices[i]);
definitions[i] = decode(i, data);
}
NpcDefinition.init(definitions);
} catch (IOException e) {
throw new UncheckedIOException("Error decoding NpcDefinitions.", e);
}
NpcDefinition[] defs = new NpcDefinition[count];
for (int i = 0; i < count; i++) {
data.position(indices[i]);
defs[i] = decode(i, data);
}
return defs;
}
/**
@@ -75,8 +76,8 @@ public final class NpcDefinitionDecoder {
} else if (opcode == 1) {
int length = buffer.get() & 0xFF;
int[] models = new int[length];
for (int i = 0; i < length; i++) {
models[i] = buffer.getShort();
for (int index = 0; index < length; index++) {
models[index] = buffer.getShort();
}
} else if (opcode == 2) {
definition.setName(BufferUtil.readString(buffer));
@@ -92,24 +93,27 @@ public final class NpcDefinitionDecoder {
definition
.setWalkAnimations(buffer.getShort(), buffer.getShort(), buffer.getShort(), buffer.getShort());
} else if (opcode >= 30 && opcode < 40) {
String str = BufferUtil.readString(buffer);
if (str.equals("hidden")) {
str = null;
String action = BufferUtil.readString(buffer);
if (action.equals("hidden")) {
action = null;
}
definition.setInteraction(opcode - 30, str);
definition.setInteraction(opcode - 30, action);
} else if (opcode == 40) {
int length = buffer.get() & 0xFF;
int[] originalColours = new int[length];
int[] replacementColours = new int[length];
for (int i = 0; i < length; i++) {
originalColours[i] = buffer.getShort();
replacementColours[i] = buffer.getShort();
for (int index = 0; index < length; index++) {
originalColours[index] = buffer.getShort();
replacementColours[index] = buffer.getShort();
}
} else if (opcode == 60) {
int length = buffer.get() & 0xFF;
int[] additionalModels = new int[length];
for (int i = 0; i < length; i++) {
additionalModels[i] = buffer.getShort();
for (int index = 0; index < length; index++) {
additionalModels[index] = buffer.getShort();
}
} else if (opcode >= 90 && opcode <= 92) {
buffer.getShort(); // Dummy
@@ -1,10 +1,12 @@
package org.apollo.cache.decoder;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import org.apollo.cache.IndexedFileSystem;
import org.apollo.cache.archive.Archive;
import org.apollo.cache.def.ItemDefinition;
import org.apollo.cache.def.ObjectDefinition;
import org.apollo.util.BufferUtil;
@@ -13,15 +15,15 @@ import org.apollo.util.BufferUtil;
*
* @author Major
*/
public final class ObjectDefinitionDecoder {
public final class ObjectDefinitionDecoder implements Runnable {
/**
* The {@link IndexedFileSystem}.
* The IndexedFileSystem.
*/
private final IndexedFileSystem fs;
/**
* Creates the decoder.
* Creates the ObjectDefinitionDecoder.
*
* @param fs The {@link IndexedFileSystem}.
*/
@@ -29,6 +31,32 @@ public final class ObjectDefinitionDecoder {
this.fs = fs;
}
@Override
public void run() {
try {
Archive config = fs.getArchive(0, 2);
ByteBuffer data = config.getEntry("loc.dat").getBuffer();
ByteBuffer idx = config.getEntry("loc.idx").getBuffer();
int count = idx.getShort(), index = 2;
int[] indices = new int[count];
for (int i = 0; i < count; i++) {
indices[i] = index;
index += idx.getShort();
}
ObjectDefinition[] definitions = new ObjectDefinition[count];
for (int i = 0; i < count; i++) {
data.position(indices[i]);
definitions[i] = decode(i, data);
}
ObjectDefinition.init(definitions);
} catch (IOException e) {
throw new UncheckedIOException("Error decoding ObjectDefinitions.", e);
}
}
/**
* Decodes data from the cache into an {@link ObjectDefinition}.
*
@@ -36,7 +64,7 @@ public final class ObjectDefinitionDecoder {
* @param data The {@link ByteBuffer} containing the data.
* @return The object definition.
*/
public ObjectDefinition decode(int id, ByteBuffer data) {
private ObjectDefinition decode(int id, ByteBuffer data) {
ObjectDefinition definition = new ObjectDefinition(id);
while (true) {
int opcode = data.get() & 0xFF;
@@ -111,30 +139,4 @@ public final class ObjectDefinitionDecoder {
}
}
/**
* Decodes all of the data into {@link ObjectDefinition}s.
*
* @return The definitions.
* @throws IOException If an error occurs when decoding the archive or finding an entry.
*/
public ObjectDefinition[] decode() throws IOException {
Archive config = fs.getArchive(0, 2);
ByteBuffer data = config.getEntry("loc.dat").getBuffer();
ByteBuffer idx = config.getEntry("loc.idx").getBuffer();
int count = idx.getShort(), index = 2;
int[] indices = new int[count];
for (int i = 0; i < count; i++) {
indices[i] = index;
index += idx.getShort();
}
ObjectDefinition[] defs = new ObjectDefinition[count];
for (int i = 0; i < count; i++) {
data.position(indices[i]);
defs[i] = decode(i, data);
}
return defs;
}
}
+2 -2
View File
@@ -36,14 +36,14 @@ public final class NpcDefinition {
* Initialises the class with the specified set of definitions.
*
* @param definitions The definitions.
* @throws RuntimeException If there is an id mismatch.
* @throws IllegalStateException If there is an id mismatch.
*/
public static void init(NpcDefinition[] definitions) {
NpcDefinition.definitions = definitions;
for (int id = 0; id < definitions.length; id++) {
NpcDefinition def = definitions[id];
if (def.getId() != id) {
throw new RuntimeException("Npc definition id mismatch.");
throw new IllegalStateException("Npc definition id mismatch.");
}
}
}
File diff suppressed because it is too large Load Diff