Move MapFileDecoder to the cache module.

This commit is contained in:
Major-
2015-05-27 14:55:58 +01:00
parent e1bb561f95
commit 4665d7617d
4 changed files with 264 additions and 265 deletions
@@ -1,4 +1,4 @@
package org.apollo.game.fs.decoder; package org.apollo.cache.decoder;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@@ -8,7 +8,6 @@ import java.util.Map;
import org.apollo.cache.IndexedFileSystem; import org.apollo.cache.IndexedFileSystem;
import org.apollo.cache.archive.Archive; import org.apollo.cache.archive.Archive;
import org.apollo.cache.archive.ArchiveEntry; import org.apollo.cache.archive.ArchiveEntry;
import org.apollo.game.model.area.Region;
/** /**
* Decodes {@link MapDefinition}s from the {@link IndexedFileSystem}. * Decodes {@link MapDefinition}s from the {@link IndexedFileSystem}.
@@ -21,7 +20,7 @@ public final class MapFileDecoder {
/** /**
* The width (and length) of a map file, in tiles. * The width (and length) of a map file, in tiles.
*/ */
public static final int MAP_FILE_WIDTH = Region.SIZE * Region.SIZE; public static final int MAP_FILE_WIDTH = 64;
/** /**
* The file id of the versions archive. * The file id of the versions archive.
@@ -35,7 +34,7 @@ public final class MapFileDecoder {
* @return A {@link Map} of packed coordinates to their MapDefinitions. * @return A {@link Map} of packed coordinates to their MapDefinitions.
* @throws IOException If there is an error reading or decoding the Archive. * @throws IOException If there is an error reading or decoding the Archive.
*/ */
protected static Map<Integer, MapDefinition> decode(IndexedFileSystem fs) throws IOException { public static Map<Integer, MapDefinition> decode(IndexedFileSystem fs) throws IOException {
Archive archive = Archive.decode(fs.getFile(0, VERSIONS_ARCHIVE_FILE_ID)); Archive archive = Archive.decode(fs.getFile(0, VERSIONS_ARCHIVE_FILE_ID));
ArchiveEntry entry = archive.getEntry("map_index"); ArchiveEntry entry = archive.getEntry("map_index");
Map<Integer, MapDefinition> definitions = new HashMap<>(); Map<Integer, MapDefinition> definitions = new HashMap<>();
@@ -9,8 +9,9 @@ import java.util.Map.Entry;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.apollo.cache.IndexedFileSystem; import org.apollo.cache.IndexedFileSystem;
import org.apollo.cache.decoder.MapFileDecoder;
import org.apollo.cache.decoder.MapFileDecoder.MapDefinition;
import org.apollo.cache.def.ObjectDefinition; import org.apollo.cache.def.ObjectDefinition;
import org.apollo.game.fs.decoder.MapFileDecoder.MapDefinition;
import org.apollo.game.model.Position; import org.apollo.game.model.Position;
import org.apollo.game.model.World; import org.apollo.game.model.World;
import org.apollo.game.model.area.Region; import org.apollo.game.model.area.Region;
@@ -17,7 +17,6 @@ import org.apollo.game.model.area.collision.CollisionMatrix;
import org.apollo.game.model.area.update.UpdateOperation; import org.apollo.game.model.area.update.UpdateOperation;
import org.apollo.game.model.entity.Entity; import org.apollo.game.model.entity.Entity;
import org.apollo.game.model.entity.EntityType; import org.apollo.game.model.entity.EntityType;
import org.apollo.game.model.entity.obj.GameObject;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@@ -53,8 +52,6 @@ public final class Region {
*/ */
public static final int SIZE = 8; public static final int SIZE = 8;
static final long start = System.currentTimeMillis();
/** /**
* The default size of newly-created sets, to reduce memory usage. * The default size of newly-created sets, to reduce memory usage.
*/ */
@@ -130,10 +127,6 @@ public final class Region {
Set<Entity> local = entities.computeIfAbsent(position, key -> new HashSet<>(DEFAULT_SET_SIZE)); Set<Entity> local = entities.computeIfAbsent(position, key -> new HashSet<>(DEFAULT_SET_SIZE));
local.add(entity); local.add(entity);
if ((System.currentTimeMillis() - start) / 1000 > 10 && (entity instanceof GameObject)) {
System.out.println("Adding entity " + entity + " to " + entity.getPosition());
}
if (notify) { if (notify) {
notifyListeners(entity, EntityUpdateType.ADD); notifyListeners(entity, EntityUpdateType.ADD);
} }
@@ -206,7 +199,8 @@ public final class Region {
Set<EntityType> set = new HashSet<>(Arrays.asList(types)); Set<EntityType> set = new HashSet<>(Arrays.asList(types));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Set<T> filtered = (Set<T>) local.stream().filter(entity -> set.contains(entity.getEntityType())).collect(Collectors.toSet()); Set<T> filtered = (Set<T>) local.stream().filter(entity -> set.contains(entity.getEntityType()))
.collect(Collectors.toSet());
return ImmutableSet.copyOf(filtered); return ImmutableSet.copyOf(filtered);
} }
@@ -217,7 +211,8 @@ public final class Region {
* @return The CollisionMatrix. * @return The CollisionMatrix.
*/ */
public CollisionMatrix getMatrix(int height) { public CollisionMatrix getMatrix(int height) {
Preconditions.checkElementIndex(height, matrices.length, "Matrix height level must be [0, " + matrices.length + ")."); Preconditions.checkElementIndex(height, matrices.length, "Matrix height level must be [0, " + matrices.length
+ ").");
return matrices[height]; return matrices[height];
} }
@@ -272,7 +267,8 @@ public final class Region {
Set<Entity> local = entities.get(position); Set<Entity> local = entities.get(position);
if (local == null || !local.remove(entity)) { if (local == null || !local.remove(entity)) {
throw new IllegalArgumentException("Entity belongs in this Region (" + this + ") but does not exist."); throw new IllegalArgumentException("Entity (" + entity + ") belongs in this Region (" + this
+ ") but does not exist.");
} }
notifyListeners(entity, EntityUpdateType.REMOVE); notifyListeners(entity, EntityUpdateType.REMOVE);
@@ -306,7 +302,8 @@ public final class Region {
* @throws IllegalArgumentException If the specified position is not included in this Region. * @throws IllegalArgumentException If the specified position is not included in this Region.
*/ */
private void checkPosition(Position position) { private void checkPosition(Position position) {
Preconditions.checkArgument(coordinates.equals(RegionCoordinates.fromPosition(position)), "Position is not included in this Region."); Preconditions.checkArgument(coordinates.equals(RegionCoordinates.fromPosition(position)),
"Position is not included in this Region.");
} }
/** /**
@@ -323,8 +320,8 @@ public final class Region {
updates.get(height).add(message); updates.get(height).add(message);
snapshots.get(height).remove(entity); snapshots.get(height).remove(entity);
if ((entity.getEntityType() == EntityType.STATIC_OBJECT && type == EntityUpdateType.REMOVE) || if ((entity.getEntityType() == EntityType.STATIC_OBJECT && type == EntityUpdateType.REMOVE)
(entity.getEntityType() != EntityType.STATIC_OBJECT && type == EntityUpdateType.ADD)) { || (entity.getEntityType() != EntityType.STATIC_OBJECT && type == EntityUpdateType.ADD)) {
snapshots.get(height).put(entity, message); snapshots.get(height).put(entity, message);
} }
} }
@@ -39,12 +39,14 @@ public final class GroupedRegionUpdateMessageEncoder extends MessageEncoder<Grou
builder.put(DataType.BYTE, region.getLocalY(lastKnownRegion)); builder.put(DataType.BYTE, region.getLocalY(lastKnownRegion));
builder.put(DataType.BYTE, DataTransformation.NEGATE, region.getLocalX(lastKnownRegion)); builder.put(DataType.BYTE, DataTransformation.NEGATE, region.getLocalX(lastKnownRegion));
System.out.println("Grum: local x: " + lastKnownRegion.getLocalX(region) + ", local y: " + lastKnownRegion.getLocalY(region)); System.out.println("Grum: local x: " + lastKnownRegion.getLocalX(region) + ", local y: "
+ lastKnownRegion.getLocalY(region));
for (RegionUpdateMessage update : message.getMessages()) { for (RegionUpdateMessage update : message.getMessages()) {
System.out.println("==== Sending " + update + " as part of grum"); System.out.println("==== Sending " + update + " as part of grum");
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
MessageEncoder<RegionUpdateMessage> encoder = (MessageEncoder<RegionUpdateMessage>) release.getMessageEncoder(update.getClass()); MessageEncoder<RegionUpdateMessage> encoder = (MessageEncoder<RegionUpdateMessage>) release
.getMessageEncoder(update.getClass());
GamePacket packet = encoder.encode(update); GamePacket packet = encoder.encode(update);
builder.put(DataType.BYTE, packet.getOpcode()); builder.put(DataType.BYTE, packet.getOpcode());