mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 20:58:03 +00:00
announce savefile error
This commit is contained in:
@@ -12,6 +12,7 @@ import java.io.*;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class SaveFileData extends HashMap<String, byte[]> {
|
public class SaveFileData extends HashMap<String, byte[]> {
|
||||||
|
public boolean hasError = false;
|
||||||
public void store(String key, SaveFileData subData) {
|
public void store(String key, SaveFileData subData) {
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
@@ -20,6 +21,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
captureException(e, key, subData);
|
captureException(e, key, subData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,6 +35,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,6 +48,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,6 +61,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,6 +74,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,6 +87,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,6 +102,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
stream.flush();
|
stream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,6 +115,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
captureException(e, key, subData);
|
captureException(e, key, subData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,6 +128,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
captureException(e, key, subData);
|
captureException(e, key, subData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,6 +142,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,6 +158,7 @@ public class SaveFileData extends HashMap<String, byte[]> {
|
|||||||
objStream.flush();
|
objStream.flush();
|
||||||
put(key, stream.toByteArray());
|
put(key, stream.toByteArray());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
hasError = true;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,51 +28,48 @@ import java.util.zip.InflaterInputStream;
|
|||||||
*/
|
*/
|
||||||
public class WorldSave {
|
public class WorldSave {
|
||||||
|
|
||||||
static final public int AUTO_SAVE_SLOT =-1;
|
static final public int AUTO_SAVE_SLOT = -1;
|
||||||
static final public int QUICK_SAVE_SLOT =-2;
|
static final public int QUICK_SAVE_SLOT = -2;
|
||||||
static final public int INVALID_SAVE_SLOT =-3;
|
static final public int INVALID_SAVE_SLOT = -3;
|
||||||
static final WorldSave currentSave=new WorldSave();
|
static final WorldSave currentSave = new WorldSave();
|
||||||
public WorldSaveHeader header = new WorldSaveHeader();
|
public WorldSaveHeader header = new WorldSaveHeader();
|
||||||
private final AdventurePlayer player=new AdventurePlayer();
|
private final AdventurePlayer player = new AdventurePlayer();
|
||||||
private final World world=new World();
|
private final World world = new World();
|
||||||
private final PointOfInterestChanges.Map pointOfInterestChanges= new PointOfInterestChanges.Map();
|
private final PointOfInterestChanges.Map pointOfInterestChanges = new PointOfInterestChanges.Map();
|
||||||
|
|
||||||
|
|
||||||
private final SignalList onLoadList=new SignalList();
|
private final SignalList onLoadList = new SignalList();
|
||||||
|
|
||||||
public final World getWorld()
|
public final World getWorld() {
|
||||||
{
|
|
||||||
return world;
|
return world;
|
||||||
}
|
}
|
||||||
public AdventurePlayer getPlayer()
|
|
||||||
{
|
public AdventurePlayer getPlayer() {
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onLoad(Runnable run)
|
public void onLoad(Runnable run) {
|
||||||
{
|
|
||||||
onLoadList.add(run);
|
onLoadList.add(run);
|
||||||
}
|
}
|
||||||
public PointOfInterestChanges getPointOfInterestChanges(String id)
|
|
||||||
{
|
public PointOfInterestChanges getPointOfInterestChanges(String id) {
|
||||||
if(!pointOfInterestChanges.containsKey(id))
|
if (!pointOfInterestChanges.containsKey(id))
|
||||||
pointOfInterestChanges.put(id,new PointOfInterestChanges());
|
pointOfInterestChanges.put(id, new PointOfInterestChanges());
|
||||||
return pointOfInterestChanges.get(id);
|
return pointOfInterestChanges.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static public boolean load(int currentSlot) {
|
static public boolean load(int currentSlot) {
|
||||||
|
|
||||||
String fileName = WorldSave.getSaveFile(currentSlot);
|
String fileName = WorldSave.getSaveFile(currentSlot);
|
||||||
if(!new File(fileName).exists())
|
if (!new File(fileName).exists())
|
||||||
return false;
|
return false;
|
||||||
new File(getSaveDir()).mkdirs();
|
new File(getSaveDir()).mkdirs();
|
||||||
try {
|
try {
|
||||||
try(FileInputStream fos = new FileInputStream(fileName);
|
try (FileInputStream fos = new FileInputStream(fileName);
|
||||||
InflaterInputStream inf = new InflaterInputStream(fos);
|
InflaterInputStream inf = new InflaterInputStream(fos);
|
||||||
ObjectInputStream oos = new ObjectInputStream(inf))
|
ObjectInputStream oos = new ObjectInputStream(inf)) {
|
||||||
{
|
|
||||||
currentSave.header = (WorldSaveHeader) oos.readObject();
|
currentSave.header = (WorldSaveHeader) oos.readObject();
|
||||||
SaveFileData mainData=(SaveFileData)oos.readObject();
|
SaveFileData mainData = (SaveFileData) oos.readObject();
|
||||||
currentSave.player.load(mainData.readSubData("player"));
|
currentSave.player.load(mainData.readSubData("player"));
|
||||||
GamePlayerUtil.getGuiPlayer().setName(currentSave.player.getName());
|
GamePlayerUtil.getGuiPlayer().setName(currentSave.player.getName());
|
||||||
try {
|
try {
|
||||||
@@ -95,9 +92,11 @@ public class WorldSave {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSafeFile(String name) {
|
public static boolean isSafeFile(String name) {
|
||||||
return filenameToSlot(name)!= INVALID_SAVE_SLOT;
|
return filenameToSlot(name) != INVALID_SAVE_SLOT;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public int filenameToSlot(String name) {
|
static public int filenameToSlot(String name) {
|
||||||
if (name.equals("auto_save.sav"))
|
if (name.equals("auto_save.sav"))
|
||||||
return AUTO_SAVE_SLOT;
|
return AUTO_SAVE_SLOT;
|
||||||
@@ -131,9 +130,9 @@ public class WorldSave {
|
|||||||
public static WorldSave generateNewWorld(String name, boolean male, int race, int avatarIndex, ColorSet startingColorIdentity, DifficultyData diff, AdventureModes mode, int customDeckIndex, CardEdition starterEdition, long seed) {
|
public static WorldSave generateNewWorld(String name, boolean male, int race, int avatarIndex, ColorSet startingColorIdentity, DifficultyData diff, AdventureModes mode, int customDeckIndex, CardEdition starterEdition, long seed) {
|
||||||
currentSave.world.generateNew(seed);
|
currentSave.world.generateNew(seed);
|
||||||
currentSave.pointOfInterestChanges.clear();
|
currentSave.pointOfInterestChanges.clear();
|
||||||
boolean chaos=mode==AdventureModes.Chaos;
|
boolean chaos = mode == AdventureModes.Chaos;
|
||||||
boolean custom=mode==AdventureModes.Custom;
|
boolean custom = mode == AdventureModes.Custom;
|
||||||
Deck starterDeck = Config.instance().starterDeck(startingColorIdentity,diff,mode,customDeckIndex,starterEdition);
|
Deck starterDeck = Config.instance().starterDeck(startingColorIdentity, diff, mode, customDeckIndex, starterEdition);
|
||||||
currentSave.player.create(name, starterDeck, male, race, avatarIndex, chaos, custom, diff);
|
currentSave.player.create(name, starterDeck, male, race, avatarIndex, chaos, custom, diff);
|
||||||
currentSave.player.setWorldPosY((int) (currentSave.world.getData().playerStartPosY * currentSave.world.getData().height * currentSave.world.getTileSize()));
|
currentSave.player.setWorldPosY((int) (currentSave.world.getData().playerStartPosY * currentSave.world.getData().height * currentSave.world.getTileSize()));
|
||||||
currentSave.player.setWorldPosX((int) (currentSave.world.getData().playerStartPosX * currentSave.world.getData().width * currentSave.world.getTileSize()));
|
currentSave.player.setWorldPosX((int) (currentSave.world.getData().playerStartPosX * currentSave.world.getData().width * currentSave.world.getTileSize()));
|
||||||
@@ -142,14 +141,17 @@ public class WorldSave {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean autoSave() {
|
public boolean autoSave() {
|
||||||
return save("auto save"+ SaveLoadScene.instance().getSaveFileSuffix(),AUTO_SAVE_SLOT);
|
return save("auto save" + SaveLoadScene.instance().getSaveFileSuffix(), AUTO_SAVE_SLOT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean quickSave() {
|
public boolean quickSave() {
|
||||||
return save("quick save"+ SaveLoadScene.instance().getSaveFileSuffix(),QUICK_SAVE_SLOT);
|
return save("quick save" + SaveLoadScene.instance().getSaveFileSuffix(), QUICK_SAVE_SLOT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean quickLoad() {
|
public boolean quickLoad() {
|
||||||
return load(QUICK_SAVE_SLOT);
|
return load(QUICK_SAVE_SLOT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean save(String text, int currentSlot) {
|
public boolean save(String text, int currentSlot) {
|
||||||
header.name = text;
|
header.name = text;
|
||||||
|
|
||||||
@@ -157,18 +159,29 @@ public class WorldSave {
|
|||||||
new File(getSaveDir()).mkdirs();
|
new File(getSaveDir()).mkdirs();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
try(FileOutputStream fos = new FileOutputStream(fileName);
|
try (FileOutputStream fos = new FileOutputStream(fileName);
|
||||||
DeflaterOutputStream def= new DeflaterOutputStream(fos);
|
DeflaterOutputStream def = new DeflaterOutputStream(fos);
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(def))
|
ObjectOutputStream oos = new ObjectOutputStream(def)) {
|
||||||
{
|
SaveFileData player = currentSave.player.save();
|
||||||
header.saveDate= new Date();
|
SaveFileData world = currentSave.world.save();
|
||||||
oos.writeObject(header);
|
SaveFileData worldStage = WorldStage.getInstance().save();
|
||||||
SaveFileData mainData=new SaveFileData();
|
SaveFileData poiChanges = currentSave.pointOfInterestChanges.save();
|
||||||
mainData.store("player",currentSave.player.save());
|
|
||||||
mainData.store("world",currentSave.world.save());
|
|
||||||
mainData.store("worldStage", WorldStage.getInstance().save());
|
|
||||||
mainData.store("pointOfInterestChanges",currentSave.pointOfInterestChanges.save());
|
|
||||||
|
|
||||||
|
if (player.hasError || world.hasError || worldStage.hasError || poiChanges.hasError) {
|
||||||
|
announceError();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SaveFileData mainData = new SaveFileData();
|
||||||
|
mainData.store("player", player);
|
||||||
|
mainData.store("world", world);
|
||||||
|
mainData.store("worldStage", worldStage);
|
||||||
|
mainData.store("pointOfInterestChanges", poiChanges);
|
||||||
|
if (mainData.hasError) {
|
||||||
|
announceError();
|
||||||
|
}
|
||||||
|
header.saveDate = new Date();
|
||||||
|
oos.writeObject(header);
|
||||||
oos.writeObject(mainData);
|
oos.writeObject(mainData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,6 +195,10 @@ public class WorldSave {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void announceError() {
|
||||||
|
currentSave.player.getCurrentGameStage().setExtraAnnouncement("Error Saving File!\nPlease check forge.log");
|
||||||
|
}
|
||||||
|
|
||||||
public void clearChanges() {
|
public void clearChanges() {
|
||||||
pointOfInterestChanges.clear();
|
pointOfInterestChanges.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user