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,181 +5,172 @@ 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);
objStream.writeFloat(subData); objStream.writeFloat(subData);
objStream.flush(); objStream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
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);
objStream.writeDouble(subData); objStream.writeDouble(subData);
objStream.flush(); objStream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
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);
objStream.writeInt(subData); objStream.writeInt(subData);
objStream.flush(); objStream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
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);
objStream.writeLong(subData); objStream.writeLong(subData);
objStream.flush(); objStream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
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);
objStream.writeBoolean(subData); objStream.writeBoolean(subData);
objStream.flush(); objStream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
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();
PixmapIO.PNG png = new PixmapIO.PNG(); PixmapIO.PNG png = new PixmapIO.PNG();
png.setFlipY(false); png.setFlipY(false);
png.write(stream, pixmap); png.write(stream, pixmap);
stream.flush(); stream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
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);
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,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);
objStream.writeUTF(subData); objStream.writeUTF(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, 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);
objStream.writeFloat(vector.x); objStream.writeFloat(vector.x);
objStream.writeFloat(vector.y); objStream.writeFloat(vector.y);
objStream.flush(); objStream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
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);
objStream.writeFloat(rectangle.x); objStream.writeFloat(rectangle.x);
objStream.writeFloat(rectangle.y); objStream.writeFloat(rectangle.y);
objStream.writeFloat(rectangle.width); objStream.writeFloat(rectangle.width);
objStream.writeFloat(rectangle.height); objStream.writeFloat(rectangle.height);
objStream.flush(); objStream.flush();
put(key,stream.toByteArray()); put(key, stream.toByteArray());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
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,14 +179,13 @@ 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();
} catch (IOException e) { } catch (IOException e) {
@@ -205,12 +195,11 @@ public class SaveFileData extends HashMap<String,byte[]>
} }
public long readLong(String key) { public long readLong(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.readLong(); return objStream.readLong();
} catch (IOException e) { } catch (IOException e) {
@@ -218,14 +207,13 @@ 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();
} catch (IOException e) { } catch (IOException e) {
@@ -234,14 +222,12 @@ 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();
} catch (IOException e) { } catch (IOException e) {
@@ -249,37 +235,34 @@ 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 {
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();
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,21 +270,18 @@ 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();
} catch (IOException e) { } catch (IOException e) {
@@ -309,14 +289,14 @@ 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 {
ByteArrayInputStream stream=new ByteArrayInputStream(get(key)); ByteArrayInputStream stream = new ByteArrayInputStream(get(key));
ObjectInputStream objStream= new DecompressibleInputStream(stream); ObjectInputStream objStream = new DecompressibleInputStream(stream);
return objStream.readBoolean(); return objStream.readBoolean();
} catch (IOException e) { } catch (IOException e) {
@@ -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 {
@@ -357,7 +341,7 @@ public class SaveFileData extends HashMap<String,byte[]>
String s = "Overriding serialized class version mismatch: " + "local serialVersionUID = " + localSUID + String s = "Overriding serialized class version mismatch: " + "local serialVersionUID = " + localSUID +
" stream serialVersionUID = " + streamSUID; " stream serialVersionUID = " + streamSUID;
System.err.println("[Invalid Class Exception]\n"+ s); System.err.println("[Invalid Class Exception]\n" + s);
resultClassDescriptor = localClassDescriptor; // Use local class descriptor for deserialization resultClassDescriptor = localClassDescriptor; // Use local class descriptor for deserialization
} }
} }