capture adventure SaveFileData exception to sentry

This commit is contained in:
Anthony Calosa
2025-08-25 05:33:45 +08:00
parent 9ff03cbd41
commit 99d191901a

View File

@@ -5,30 +5,27 @@ import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import forge.Forge; import forge.Forge;
import io.sentry.Hint;
import io.sentry.Sentry;
import java.io.*; 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 void store(String key, SaveFileData subData) {
public void store(String key,SaveFileData subData)
{
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
objStream.writeObject(subData); objStream.writeObject(subData);
objStream.flush(); objStream.flush();
put(key, stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); captureException(e, key, subData);
} }
} }
public void store(String key,float subData) public void store(String key, float subData) {
{
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -39,8 +36,8 @@ public class SaveFileData extends HashMap<String,byte[]>
e.printStackTrace(); e.printStackTrace();
} }
} }
public void store(String key,double subData)
{ public void store(String key, double subData) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -51,8 +48,8 @@ public class SaveFileData extends HashMap<String,byte[]>
e.printStackTrace(); e.printStackTrace();
} }
} }
public void store(String key,int subData)
{ public void store(String key, int subData) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -63,8 +60,8 @@ public class SaveFileData extends HashMap<String,byte[]>
e.printStackTrace(); e.printStackTrace();
} }
} }
public void store(String key,long subData)
{ public void store(String key, long subData) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -75,8 +72,8 @@ public class SaveFileData extends HashMap<String,byte[]>
e.printStackTrace(); e.printStackTrace();
} }
} }
public void store(String key,boolean subData)
{ public void store(String key, boolean subData) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -87,8 +84,8 @@ public class SaveFileData extends HashMap<String,byte[]>
e.printStackTrace(); e.printStackTrace();
} }
} }
public void store(String key, Pixmap pixmap)
{ public void store(String key, Pixmap pixmap) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
@@ -101,8 +98,8 @@ public class SaveFileData extends HashMap<String,byte[]>
e.printStackTrace(); e.printStackTrace();
} }
} }
public void storeObject(String key,Object subData)
{ public void storeObject(String key, Object subData) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -110,11 +107,11 @@ 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) {
e.printStackTrace(); captureException(e, key, subData);
} }
} }
public void store(String key,String subData)
{ public void store(String key, String subData) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -122,12 +119,11 @@ 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) {
e.printStackTrace(); captureException(e, key, subData);
} }
} }
public void store(String key, Vector2 vector) { public void store(String key, Vector2 vector) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -139,8 +135,8 @@ public class SaveFileData extends HashMap<String,byte[]>
e.printStackTrace(); e.printStackTrace();
} }
} }
public void store(String key, Rectangle rectangle) {
public void store(String key, Rectangle rectangle) {
try { try {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(stream); ObjectOutputStream objStream = new ObjectOutputStream(stream);
@@ -155,31 +151,26 @@ public class SaveFileData extends HashMap<String,byte[]>
} }
} }
public SaveFileData readSubData(String key) public SaveFileData readSubData(String key) {
{
if (!containsKey(key)) if (!containsKey(key))
return null; return null;
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return (SaveFileData) objStream.readObject(); return (SaveFileData) objStream.readObject();
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
public Object readObject(String key)
{ public Object readObject(String key) {
if (!containsKey(key)) if (!containsKey(key))
return null; return null;
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return objStream.readObject(); return objStream.readObject();
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
Forge.delayedSwitchBack(); Forge.delayedSwitchBack();
@@ -188,12 +179,11 @@ public class SaveFileData extends HashMap<String,byte[]>
} }
return null; return null;
} }
public String readString(String key)
{ public String readString(String key) {
if (!containsKey(key)) if (!containsKey(key))
return null; return null;
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return objStream.readUTF(); return objStream.readUTF();
@@ -208,7 +198,6 @@ public class SaveFileData extends HashMap<String,byte[]>
if (!containsKey(key)) if (!containsKey(key))
return 0; return 0;
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return objStream.readLong(); return objStream.readLong();
@@ -218,12 +207,11 @@ public class SaveFileData extends HashMap<String,byte[]>
} }
return 0; return 0;
} }
public float readFloat(String key)
{ public float readFloat(String key) {
if (!containsKey(key)) if (!containsKey(key))
return 0.0f; return 0.0f;
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return objStream.readFloat(); return objStream.readFloat();
@@ -234,12 +222,10 @@ public class SaveFileData extends HashMap<String,byte[]>
return 0.0f; return 0.0f;
} }
public double readDouble(String key) public double readDouble(String key) {
{
if (!containsKey(key)) if (!containsKey(key))
return 0.0; return 0.0;
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return objStream.readDouble(); return objStream.readDouble();
@@ -249,25 +235,23 @@ public class SaveFileData extends HashMap<String,byte[]>
} }
return 0.0; return 0.0;
} }
public Vector2 readVector2(String key)
{ public Vector2 readVector2(String key) {
if (!containsKey(key)) if (!containsKey(key))
return new Vector2(); return new Vector2();
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
float x = objStream.readFloat(); float x = objStream.readFloat();
float y = objStream.readFloat(); float y = objStream.readFloat();
return new Vector2(x, y); return new Vector2(x, y);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return new Vector2(); return new Vector2();
} }
public Rectangle readRectangle(String key)
{ public Rectangle readRectangle(String key) {
if (!containsKey(key)) if (!containsKey(key))
return new Rectangle(); return new Rectangle();
try { try {
@@ -279,7 +263,6 @@ public class SaveFileData extends HashMap<String,byte[]>
float width = objStream.readFloat(); float width = objStream.readFloat();
float height = objStream.readFloat(); float height = objStream.readFloat();
return new Rectangle(x, y, width, height); return new Rectangle(x, y, width, height);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -287,19 +270,16 @@ public class SaveFileData extends HashMap<String,byte[]>
} }
public Pixmap readPixmap(String key) {
public Pixmap readPixmap(String key)
{
if (!containsKey(key)) if (!containsKey(key))
return null; return null;
return new Pixmap(get(key), 0, get(key).length); return new Pixmap(get(key), 0, get(key).length);
} }
public int readInt(String key)
{ public int readInt(String key) {
if (!containsKey(key)) if (!containsKey(key))
return 0; return 0;
try { try {
ByteArrayInputStream stream = new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream = new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return objStream.readInt(); return objStream.readInt();
@@ -309,8 +289,8 @@ public class SaveFileData extends HashMap<String,byte[]>
} }
return 0; return 0;
} }
public boolean readBool(String key)
{ public boolean readBool(String key) {
if (!containsKey(key)) if (!containsKey(key))
return false; return false;
try { try {
@@ -325,8 +305,12 @@ public class SaveFileData extends HashMap<String,byte[]>
return false; return false;
} }
private void captureException(Exception e, String key, Object object) {
Hint hint = new Hint();
hint.set(key, object);
Sentry.captureException(e, hint);
e.printStackTrace();
}
static class DecompressibleInputStream extends ObjectInputStream { static class DecompressibleInputStream extends ObjectInputStream {