|
10 | 10 | import com.fs.starfarer.api.ui.TooltipMakerAPI; |
11 | 11 | import com.fs.starfarer.api.util.IntervalUtil; |
12 | 12 | 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; |
13 | 21 | import org.apache.log4j.Logger; |
14 | 22 | import org.jetbrains.annotations.NotNull; |
15 | 23 | import org.jetbrains.annotations.Nullable; |
16 | 24 | import org.json.JSONObject; |
17 | 25 |
|
| 26 | +import javax.xml.stream.XMLStreamException; |
| 27 | +import javax.xml.stream.XMLStreamWriter; |
18 | 28 | import java.awt.*; |
| 29 | +import java.io.*; |
19 | 30 | import java.util.List; |
20 | 31 | import java.util.*; |
21 | 32 |
|
@@ -64,6 +75,55 @@ public class MagicAchievement { |
64 | 75 | * Put your initialization code in {@link #onApplicationLoaded(boolean)} or {@link #onSaveGameLoaded(boolean)} instead. |
65 | 76 | */ |
66 | 77 | 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; |
67 | 127 | } |
68 | 128 |
|
69 | 129 | /** |
@@ -598,9 +658,14 @@ public void setCompletedByUserName(@Nullable String completedByUserName) { |
598 | 658 | return memory; |
599 | 659 | } |
600 | 660 |
|
| 661 | + private static XStream xStream; |
601 | 662 |
|
602 | 663 | 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 |
604 | 669 | } |
605 | 670 |
|
606 | 671 | /** |
|
0 commit comments