Skip to content

Commit b093454

Browse files
committed
Fixed RC2's crash on load if using certain mods (RAT)
1 parent 7310837 commit b093454

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

jars/MagicLib-Kotlin.jar

0 Bytes
Binary file not shown.

jars/MagicLib.jar

3.39 KB
Binary file not shown.

magiclib.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
{
99
"major": "1",
1010
"minor": "4",
11-
"patch": "6"
11+
"patch": "7"
1212
}
1313
}

src/org/magiclib/Magic_modPlugin.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
* Master ModPlugin for MagicLib. Handles all the loading of data and scripts.
2525
*/
2626
public class Magic_modPlugin extends BaseModPlugin {
27-
/**
28-
* DO NOT USE. Meant to be used internally as a hacky way to compare arbitrary objects by serializing them to XML.
29-
*/
30-
public static XStream magiclibXStream;
3127

3228
////////////////////////////////////////
3329
// //
@@ -191,7 +187,6 @@ public void afterGameSave() {
191187
@Override
192188
public void configureXStream(XStream x) {
193189
super.configureXStream(x);
194-
magiclibXStream = x;
195190
data.scripts.Magic_modPlugin.configureXStream(x);
196191

197192
x.alias("MagicBountyBarEvent", MagicBountyBarEvent.class);

src/org/magiclib/achievements/MagicAchievement.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@
1010
import com.fs.starfarer.api.ui.TooltipMakerAPI;
1111
import com.fs.starfarer.api.util.IntervalUtil;
1212
import com.fs.starfarer.api.util.Misc;
13+
import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
14+
import com.thoughtworks.xstream.XStream;
15+
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
16+
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
17+
import com.thoughtworks.xstream.io.StreamException;
18+
import com.thoughtworks.xstream.io.xml.StaxDriver;
19+
import com.thoughtworks.xstream.io.xml.StaxWriter;
20+
import com.thoughtworks.xstream.mapper.MapperWrapper;
1321
import org.apache.log4j.Logger;
1422
import org.jetbrains.annotations.NotNull;
1523
import org.jetbrains.annotations.Nullable;
1624
import org.json.JSONObject;
1725

26+
import javax.xml.stream.XMLStreamException;
27+
import javax.xml.stream.XMLStreamWriter;
1828
import java.awt.*;
29+
import java.io.*;
1930
import java.util.List;
2031
import java.util.*;
2132

@@ -64,6 +75,55 @@ public class MagicAchievement {
6475
* Put your initialization code in {@link #onApplicationLoaded(boolean)} or {@link #onSaveGameLoaded(boolean)} instead.
6576
*/
6677
public MagicAchievement() {
78+
if (xStream == null) {
79+
try {
80+
xStream = buildXStream();
81+
} catch (Exception e) {
82+
logger.error(e);
83+
}
84+
}
85+
}
86+
87+
private static XStream buildXStream() {
88+
// From obf vanilla code
89+
XStream xStream = new XStream(new StaxDriver() {
90+
public HierarchicalStreamWriter createWriter(OutputStream outputStream) {
91+
System.gc();
92+
long l2 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
93+
try {
94+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
95+
IndentingXMLStreamWriter indentingXMLStreamWriter = new IndentingXMLStreamWriter(this.getOutputFactory().createXMLStreamWriter(outputStreamWriter));
96+
indentingXMLStreamWriter.setIndentStep("");
97+
return new StaxWriter(this.getQnameMap(), (XMLStreamWriter) indentingXMLStreamWriter, true, this.isRepairingNamespace());
98+
} catch (XMLStreamException xMLStreamException) {
99+
throw new StreamException(xMLStreamException);
100+
} catch (UnsupportedEncodingException unsupportedEncodingException) {
101+
throw new StreamException(unsupportedEncodingException);
102+
}
103+
}
104+
105+
public HierarchicalStreamReader createReader(InputStream inputStream) {
106+
try {
107+
return super.createReader(new InputStreamReader(inputStream, "UTF-8"));
108+
} catch (UnsupportedEncodingException unsupportedEncodingException) {
109+
throw new RuntimeException(unsupportedEncodingException);
110+
}
111+
}
112+
}) {
113+
protected MapperWrapper wrapMapper(MapperWrapper mapperWrapper) {
114+
return new MapperWrapper(mapperWrapper) {
115+
116+
public boolean shouldSerializeMember(Class clazz, String string2) {
117+
if (clazz == Object.class) {
118+
return false;
119+
}
120+
return super.shouldSerializeMember(clazz, string2);
121+
}
122+
};
123+
}
124+
};
125+
126+
return xStream;
67127
}
68128

69129
/**
@@ -598,9 +658,14 @@ public void setCompletedByUserName(@Nullable String completedByUserName) {
598658
return memory;
599659
}
600660

661+
private static XStream xStream;
601662

602663
private int toHashcode(Object obj) {
603-
return org.magiclib.Magic_modPlugin.magiclibXStream.toXML(obj).hashCode();
664+
if (xStream != null) {
665+
return xStream.toXML(obj).hashCode();
666+
}
667+
668+
return obj.hashCode(); // Not correct but better than nothing
604669
}
605670

606671
/**

0 commit comments

Comments
 (0)