Created LegacyKeyframeSetAdapter to properly deserialize old, non-generic PositionKeyframes and TimeKeyframes for Path Presets
This commit is contained in:
@@ -4,10 +4,12 @@ import eu.crushedpixel.replaymod.interpolation.KeyframeValue;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Keyframe<T extends KeyframeValue> {
|
||||
|
||||
private int realTimestamp;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package eu.crushedpixel.replaymod.holders;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.builder.HashCodeBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@NoArgsConstructor
|
||||
public class KeyframeSet implements GuiEntryListEntry {
|
||||
private String name;
|
||||
private Keyframe<AdvancedPosition>[] positionKeyframes;
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package eu.crushedpixel.replaymod.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import eu.crushedpixel.replaymod.holders.AdvancedPosition;
|
||||
import eu.crushedpixel.replaymod.holders.Keyframe;
|
||||
import eu.crushedpixel.replaymod.holders.KeyframeSet;
|
||||
import eu.crushedpixel.replaymod.holders.TimestampValue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LegacyKeyframeSetAdapter extends TypeAdapter<KeyframeSet[]> {
|
||||
|
||||
public LegacyKeyframeSetAdapter() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyframeSet[] read(JsonReader in) throws IOException {
|
||||
List<KeyframeSet> sets = new ArrayList<KeyframeSet>();
|
||||
|
||||
in.beginArray();
|
||||
while(in.hasNext()) { //iterate over all array entries
|
||||
|
||||
KeyframeSet set = new KeyframeSet();
|
||||
List<Keyframe> keyframes = new ArrayList<Keyframe>();
|
||||
|
||||
in.beginObject();
|
||||
while(in.hasNext()) { //iterate over all object entries
|
||||
String jsonTag = in.nextName();
|
||||
|
||||
if("name".equals(jsonTag)) {
|
||||
set.setName(in.nextString());
|
||||
|
||||
} else if("positionKeyframes".equals(jsonTag)) {
|
||||
in.beginArray();
|
||||
while(in.hasNext()) {
|
||||
Keyframe<AdvancedPosition> newKeyframe = new Keyframe<AdvancedPosition>();
|
||||
|
||||
in.beginObject();
|
||||
while(in.hasNext()) {
|
||||
String jsonKeyframeTag = in.nextName();
|
||||
if("value".equals(jsonKeyframeTag) || "position".equals(jsonKeyframeTag)) {
|
||||
AdvancedPosition position = new Gson().fromJson(in, AdvancedPosition.class);
|
||||
newKeyframe.setValue(position);
|
||||
} else if("realTimestamp".equals(jsonKeyframeTag)) {
|
||||
newKeyframe.setRealTimestamp(in.nextInt());
|
||||
}
|
||||
}
|
||||
in.endObject();
|
||||
|
||||
keyframes.add(newKeyframe);
|
||||
}
|
||||
in.endArray();
|
||||
|
||||
} else if("timeKeyframes".equals(jsonTag)) {
|
||||
in.beginArray();
|
||||
while(in.hasNext()) {
|
||||
Keyframe<TimestampValue> newKeyframe = new Keyframe<TimestampValue>();
|
||||
|
||||
in.beginObject();
|
||||
while(in.hasNext()) {
|
||||
String jsonKeyframeTag = in.nextName();
|
||||
if("timestamp".equals(jsonKeyframeTag)) {
|
||||
TimestampValue timestampValue = new TimestampValue(in.nextInt());
|
||||
newKeyframe.setValue(timestampValue);
|
||||
} else if("value".equals(jsonKeyframeTag)) {
|
||||
TimestampValue timestampValue = new Gson().fromJson(in, TimestampValue.class);
|
||||
newKeyframe.setValue(timestampValue);
|
||||
} else if("realTimestamp".equals(jsonKeyframeTag)) {
|
||||
newKeyframe.setRealTimestamp(in.nextInt());
|
||||
}
|
||||
}
|
||||
in.endObject();
|
||||
|
||||
keyframes.add(newKeyframe);
|
||||
}
|
||||
in.endArray();
|
||||
}
|
||||
}
|
||||
in.endObject();
|
||||
|
||||
set.setKeyframes(keyframes.toArray(new Keyframe[keyframes.size()]));
|
||||
sets.add(set);
|
||||
}
|
||||
in.endArray();
|
||||
|
||||
return sets.toArray(new KeyframeSet[sets.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, KeyframeSet[] value) throws IOException {}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package eu.crushedpixel.replaymod.utils;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import eu.crushedpixel.replaymod.assets.AssetRepository;
|
||||
@@ -100,7 +101,11 @@ public class ReplayFile extends ZipFile {
|
||||
return null;
|
||||
}
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(getInputStream(entry)));
|
||||
return new Gson().fromJson(reader, KeyframeSet[].class);
|
||||
|
||||
GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(KeyframeSet[].class, new LegacyKeyframeSetAdapter());
|
||||
Gson gson = gsonBuilder.create();
|
||||
|
||||
return gson.fromJson(reader, KeyframeSet[].class);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user