Refactor: Remove unsued 'flags' param when writing savegames

* Remove unsued 'flags' parameter in methods for storing savegames.
* Refactor static constructors when reading objects from savegames so that the ctors are named "newFromParcel", to differentiate them from "readFromParcel".
This commit is contained in:
Oskar Wiksten
2013-12-08 09:36:02 +01:00
parent 9e799962e9
commit 8d4adab240
17 changed files with 60 additions and 69 deletions

View File

@@ -163,7 +163,7 @@ public final class GameStatistics {
this.spentGold = src.readInt();
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeInt(deaths);
Set<Entry<String, Integer> > set = killedMonsters.entrySet();
dest.writeInt(set.size());

View File

@@ -1,6 +1,5 @@
package com.gpl.rpg.AndorsTrail.model;
import com.gpl.rpg.AndorsTrail.context.WorldContext;
import com.gpl.rpg.AndorsTrail.model.actor.Monster;
import com.gpl.rpg.AndorsTrail.util.Coord;
@@ -22,7 +21,7 @@ public final class InterfaceData {
// ====== PARCELABLE ===================================================================
public InterfaceData(DataInputStream src, WorldContext world, int fileversion) throws IOException {
public InterfaceData(DataInputStream src, int fileversion) throws IOException {
this.isMainActivityVisible = src.readBoolean();
this.isInCombat = src.readBoolean();
final boolean hasSelectedPosition = src.readBoolean();
@@ -34,12 +33,12 @@ public final class InterfaceData {
this.selectedTabHeroInfo = src.readUTF();
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeBoolean(isMainActivityVisible);
dest.writeBoolean(isInCombat);
if (selectedPosition != null) {
dest.writeBoolean(true);
selectedPosition.writeToParcel(dest, flags);
selectedPosition.writeToParcel(dest);
} else {
dest.writeBoolean(false);
}

View File

@@ -30,9 +30,9 @@ public final class ModelContainer {
// ====== PARCELABLE ===================================================================
public ModelContainer(DataInputStream src, WorldContext world, ControllerContext controllers, int fileversion) throws IOException {
this.player = Player.readFromParcel(src, world, controllers, fileversion);
this.player = Player.newFromParcel(src, world, controllers, fileversion);
this.currentMap = world.maps.findPredefinedMap(src.readUTF());
this.uiSelections = new InterfaceData(src, world, fileversion);
this.uiSelections = new InterfaceData(src, fileversion);
if (uiSelections.selectedPosition != null) {
this.uiSelections.selectedMonster = currentMap.getMonsterAt(uiSelections.selectedPosition);
}
@@ -45,11 +45,11 @@ public final class ModelContainer {
}
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
player.writeToParcel(dest, flags);
public void writeToParcel(DataOutputStream dest) throws IOException {
player.writeToParcel(dest);
dest.writeUTF(currentMap.name);
uiSelections.writeToParcel(dest, flags);
statistics.writeToParcel(dest, flags);
worldData.writeToParcel(dest, flags);
uiSelections.writeToParcel(dest);
statistics.writeToParcel(dest);
worldData.writeToParcel(dest);
}
}

View File

@@ -48,7 +48,7 @@ public final class WorldData {
}
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeLong(worldTime);
dest.writeInt(timers.size());
for(Map.Entry<String, Long> e : timers.entrySet()) {

View File

@@ -39,7 +39,7 @@ public final class ActorCondition {
this.duration = src.readInt();
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeUTF(conditionType.conditionTypeID);
dest.writeInt(magnitude);
dest.writeInt(duration);

View File

@@ -93,14 +93,14 @@ public final class Monster extends Actor {
// ====== PARCELABLE ===================================================================
public static Monster readFromParcel(DataInputStream src, WorldContext world, int fileversion) throws IOException {
public static Monster newFromParcel(DataInputStream src, WorldContext world, int fileversion) throws IOException {
String monsterTypeId = src.readUTF();
if (fileversion < 20) {
monsterTypeId = monsterTypeId.replace(' ', '_').replace("\\'", "").toLowerCase();
}
MonsterType monsterType = world.monsterTypes.getMonsterType(monsterTypeId);
if (fileversion < 25) return LegacySavegameFormatReaderForMonster.readFromParcel_pre_v25(src, fileversion, monsterType);
if (fileversion < 25) return LegacySavegameFormatReaderForMonster.newFromParcel_pre_v25(src, fileversion, monsterType);
return new Monster(src, world, fileversion, monsterType);
}
@@ -141,12 +141,12 @@ public final class Monster extends Actor {
this.forceAggressive = src.readBoolean();
if (fileversion >= 31) {
if (src.readBoolean()) {
this.shopItems = new ItemContainer(src, world, fileversion);
this.shopItems = ItemContainer.newFromParcel(src, world, fileversion);
}
}
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeUTF(getMonsterTypeID());
if (attackCost == monsterType.attackCost
&& attackChance == monsterType.attackChance
@@ -163,23 +163,23 @@ public final class Monster extends Actor {
dest.writeInt(attackChance);
dest.writeInt(criticalSkill);
dest.writeFloat(criticalMultiplier);
damagePotential.writeToParcel(dest, flags);
damagePotential.writeToParcel(dest);
dest.writeInt(blockChance);
dest.writeInt(damageResistance);
}
ap.writeToParcel(dest, flags);
health.writeToParcel(dest, flags);
position.writeToParcel(dest, flags);
ap.writeToParcel(dest);
health.writeToParcel(dest);
position.writeToParcel(dest);
dest.writeInt(conditions.size());
for (ActorCondition c : conditions) {
c.writeToParcel(dest, flags);
c.writeToParcel(dest);
}
dest.writeInt(moveCost);
dest.writeBoolean(forceAggressive);
if (shopItems != null) {
dest.writeBoolean(true);
shopItems.writeToParcel(dest, flags);
shopItems.writeToParcel(dest);
} else {
dest.writeBoolean(false);
}

View File

@@ -260,7 +260,7 @@ public final class Player extends Actor {
// ====== PARCELABLE ===================================================================
public static Player readFromParcel(DataInputStream src, WorldContext world, ControllerContext controllers, int fileversion) throws IOException {
public static Player newFromParcel(DataInputStream src, WorldContext world, ControllerContext controllers, int fileversion) throws IOException {
Player player = new Player(src, world, fileversion);
LegacySavegameFormatReaderForPlayer.upgradeSavegame(player, world, controllers, fileversion);
return player;
@@ -356,7 +356,7 @@ public final class Player extends Actor {
}
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeInt(baseTraits.iconID);
dest.writeInt(baseTraits.maxAP);
dest.writeInt(baseTraits.maxHP);
@@ -366,23 +366,23 @@ public final class Player extends Actor {
dest.writeInt(baseTraits.attackChance);
dest.writeInt(baseTraits.criticalSkill);
dest.writeFloat(baseTraits.criticalMultiplier);
baseTraits.damagePotential.writeToParcel(dest, flags);
baseTraits.damagePotential.writeToParcel(dest);
dest.writeInt(baseTraits.blockChance);
dest.writeInt(baseTraits.damageResistance);
dest.writeInt(baseTraits.moveCost);
ap.writeToParcel(dest, flags);
health.writeToParcel(dest, flags);
position.writeToParcel(dest, flags);
ap.writeToParcel(dest);
health.writeToParcel(dest);
position.writeToParcel(dest);
dest.writeInt(conditions.size());
for (ActorCondition c : conditions) {
c.writeToParcel(dest, flags);
c.writeToParcel(dest);
}
lastPosition.writeToParcel(dest, flags);
nextPosition.writeToParcel(dest, flags);
lastPosition.writeToParcel(dest);
nextPosition.writeToParcel(dest);
dest.writeInt(level);
dest.writeInt(totalExperience);
inventory.writeToParcel(dest, flags);
inventory.writeToParcel(dest);
dest.writeInt(baseTraits.useItemCost);
dest.writeInt(baseTraits.reequipCost);
dest.writeInt(skillLevels.size());

View File

@@ -123,8 +123,8 @@ public final class Inventory extends ItemContainer {
}
@Override
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
super.writeToParcel(dest, flags);
public void writeToParcel(DataOutputStream dest) throws IOException {
super.writeToParcel(dest);
dest.writeInt(gold);
dest.writeInt(NUM_WORN_SLOTS);
for(int i = 0; i < NUM_WORN_SLOTS; ++i) {

View File

@@ -35,7 +35,7 @@ public class ItemContainer {
this.quantity = src.readInt();
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeUTF(itemType.id);
dest.writeInt(quantity);
}
@@ -119,11 +119,13 @@ public class ItemContainer {
// ====== PARCELABLE ===================================================================
public ItemContainer(DataInputStream src, WorldContext world, int fileversion) throws IOException {
readFromParcel(src, world, fileversion);
public static ItemContainer newFromParcel(DataInputStream src, WorldContext world, int fileversion) throws IOException {
ItemContainer result = new ItemContainer();
result.readFromParcel(src, world, fileversion);
return result;
}
public void readFromParcel(DataInputStream src, WorldContext world, int fileversion) throws IOException {
protected void readFromParcel(DataInputStream src, WorldContext world, int fileversion) throws IOException {
items.clear();
final int size = src.readInt();
for(int i = 0; i < size; ++i) {
@@ -132,10 +134,10 @@ public class ItemContainer {
}
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeInt(items.size());
for (ItemEntry e : items) {
e.writeToParcel(dest, flags);
e.writeToParcel(dest);
}
}
}

View File

@@ -7,7 +7,6 @@ import com.gpl.rpg.AndorsTrail.util.Coord;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collection;
public final class Loot {
public int exp = 0;
@@ -58,14 +57,6 @@ public final class Loot {
}
return result;
}
public static boolean hasItems(Collection<Loot> lootBags) {
if (lootBags == null) return false;
if (lootBags.isEmpty()) return false;
for (Loot loot : lootBags) {
if (loot.hasItems()) return true;
}
return false;
}
public void clear() {
exp = 0;
@@ -79,7 +70,7 @@ public final class Loot {
public Loot(DataInputStream src, WorldContext world, int fileversion) throws IOException {
this.exp = src.readInt();
this.gold = src.readInt();
this.items = new ItemContainer(src, world, fileversion);
this.items = ItemContainer.newFromParcel(src, world, fileversion);
if (fileversion < 23) LegacySavegameFormatReaderForItemContainer.refundUpgradedItems(this);
this.position = new Coord(src, fileversion);
@@ -90,11 +81,11 @@ public final class Loot {
this.isVisible = src.readBoolean();
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeInt(exp);
dest.writeInt(gold);
items.writeToParcel(dest, flags);
position.writeToParcel(dest, flags);
items.writeToParcel(dest);
position.writeToParcel(dest);
dest.writeBoolean(isVisible);
}
}

View File

@@ -88,7 +88,7 @@ public final class MapCollection {
return false;
}
public void writeToParcel(DataOutputStream dest, WorldContext world, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest, WorldContext world) throws IOException {
List<PredefinedMap> mapsToExport = new ArrayList<PredefinedMap>();
for(PredefinedMap map : getAllMaps()) {
if (shouldSaveMap(world, map)) mapsToExport.add(map);
@@ -96,7 +96,7 @@ public final class MapCollection {
dest.writeInt(mapsToExport.size());
for(PredefinedMap map : mapsToExport) {
dest.writeUTF(map.name);
map.writeToParcel(dest, world, flags);
map.writeToParcel(dest, world);
}
}
}

View File

@@ -125,15 +125,15 @@ public final class MonsterSpawnArea {
if (fileversion >= 41) isSpawning = src.readBoolean();
quantity.current = src.readInt();
for(int i = 0; i < quantity.current; ++i) {
monsters.add(Monster.readFromParcel(src, world, fileversion));
monsters.add(Monster.newFromParcel(src, world, fileversion));
}
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeBoolean(isSpawning);
dest.writeInt(monsters.size());
for (Monster m : monsters) {
m.writeToParcel(dest, flags);
m.writeToParcel(dest);
}
}
}

View File

@@ -269,16 +269,16 @@ public final class PredefinedMap {
return false;
}
public void writeToParcel(DataOutputStream dest, WorldContext world, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest, WorldContext world) throws IOException {
if (shouldSaveMapData(world)) {
dest.writeBoolean(true);
dest.writeInt(spawnAreas.length);
for(MonsterSpawnArea a : spawnAreas) {
a.writeToParcel(dest, flags);
a.writeToParcel(dest);
}
dest.writeInt(groundBags.size());
for(Loot l : groundBags) {
l.writeToParcel(dest, flags);
l.writeToParcel(dest);
}
dest.writeLong(lastVisitTime);
} else {

View File

@@ -8,7 +8,7 @@ import java.io.DataInputStream;
import java.io.IOException;
public final class LegacySavegameFormatReaderForMonster {
public static Monster readFromParcel_pre_v25(DataInputStream src, int fileversion, MonsterType monsterType) throws IOException {
public static Monster newFromParcel_pre_v25(DataInputStream src, int fileversion, MonsterType monsterType) throws IOException {
Monster m = new Monster(monsterType);
m.position.set(new Coord(src, fileversion));
m.ap.current = src.readInt();

View File

@@ -94,10 +94,9 @@ public final class Savegames {
public static void saveWorld(WorldContext world, OutputStream outStream, String displayInfo) throws IOException {
DataOutputStream dest = new DataOutputStream(outStream);
final int flags = 0;
FileHeader.writeToParcel(dest, world.model.player.getName(), displayInfo);
world.maps.writeToParcel(dest, world, flags);
world.model.writeToParcel(dest, flags);
world.maps.writeToParcel(dest, world);
world.model.writeToParcel(dest);
dest.close();
}

View File

@@ -43,7 +43,7 @@ public final class Coord {
this.y = src.readInt();
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeInt(x);
dest.writeInt(y);
}

View File

@@ -95,7 +95,7 @@ public final class Range {
this.current = src.readInt();
}
public void writeToParcel(DataOutputStream dest, int flags) throws IOException {
public void writeToParcel(DataOutputStream dest) throws IOException {
dest.writeInt(max);
dest.writeInt(current);
}