(Re)move time keyframes when trimming replay
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
package eu.crushedpixel.replaymod.studio;
|
package eu.crushedpixel.replaymod.studio;
|
||||||
|
|
||||||
|
import com.google.common.base.Optional;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import de.johni0702.replaystudio.PacketData;
|
import de.johni0702.replaystudio.PacketData;
|
||||||
import de.johni0702.replaystudio.filter.ChangeTimestampFilter;
|
import de.johni0702.replaystudio.filter.ChangeTimestampFilter;
|
||||||
@@ -13,11 +16,15 @@ import de.johni0702.replaystudio.replay.ZipReplayFile;
|
|||||||
import de.johni0702.replaystudio.stream.PacketStream;
|
import de.johni0702.replaystudio.stream.PacketStream;
|
||||||
import de.johni0702.replaystudio.studio.ReplayStudio;
|
import de.johni0702.replaystudio.studio.ReplayStudio;
|
||||||
import eu.crushedpixel.replaymod.gui.elements.listeners.ProgressUpdateListener;
|
import eu.crushedpixel.replaymod.gui.elements.listeners.ProgressUpdateListener;
|
||||||
|
import eu.crushedpixel.replaymod.holders.Keyframe;
|
||||||
|
import eu.crushedpixel.replaymod.holders.KeyframeSet;
|
||||||
|
import eu.crushedpixel.replaymod.holders.TimestampValue;
|
||||||
|
import eu.crushedpixel.replaymod.utils.LegacyKeyframeSetAdapter;
|
||||||
import net.minecraft.client.resources.I18n;
|
import net.minecraft.client.resources.I18n;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -64,6 +71,8 @@ public class StudioImplementation {
|
|||||||
metaData.setDuration(ending - beginning);
|
metaData.setDuration(ending - beginning);
|
||||||
replayFile.writeMetaData(metaData);
|
replayFile.writeMetaData(metaData);
|
||||||
|
|
||||||
|
shiftPaths(replayFile, beginning, ending);
|
||||||
|
|
||||||
updateListener.onProgressChanged(9/10f, I18n.format("replaymod.gui.editor.progress.status.writing.final"));
|
updateListener.onProgressChanged(9/10f, I18n.format("replaymod.gui.editor.progress.status.writing.final"));
|
||||||
|
|
||||||
replayFile.saveTo(outputFile);
|
replayFile.saveTo(outputFile);
|
||||||
@@ -71,6 +80,49 @@ public class StudioImplementation {
|
|||||||
updateListener.onProgressChanged(1f, I18n.format("replaymod.gui.editor.progress.status.finished"));
|
updateListener.onProgressChanged(1f, I18n.format("replaymod.gui.editor.progress.status.finished"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void shiftPaths(ReplayFile replayFile, int beginning, int ending) throws IOException {
|
||||||
|
Optional<InputStream> in = replayFile.get(eu.crushedpixel.replaymod.utils.ReplayFile.ENTRY_PATHS);
|
||||||
|
if (!in.isPresent()) {
|
||||||
|
in = replayFile.get(eu.crushedpixel.replaymod.utils.ReplayFile.ENTRY_PATHS_OLD);
|
||||||
|
if (!in.isPresent()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KeyframeSet[] keyframeSets = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(KeyframeSet[].class, new LegacyKeyframeSetAdapter())
|
||||||
|
.create().fromJson(new InputStreamReader(in.get()), KeyframeSet[].class);
|
||||||
|
IOUtils.closeQuietly(in.get());
|
||||||
|
List<KeyframeSet> resultSets = new ArrayList<KeyframeSet>();
|
||||||
|
for (KeyframeSet set : keyframeSets) {
|
||||||
|
List<Keyframe<?>> resultKeyframes = new ArrayList<Keyframe<?>>();
|
||||||
|
int timeKeyframes = 0;
|
||||||
|
for (Keyframe<?> keyframe : set.getKeyframes()) {
|
||||||
|
Object value = keyframe.getValue();
|
||||||
|
if (value instanceof TimestampValue) {
|
||||||
|
int time = ((TimestampValue) value).asInt();
|
||||||
|
if (time > beginning && time < ending) {
|
||||||
|
Keyframe<?> copy = keyframe.copy();
|
||||||
|
((TimestampValue) copy.getValue()).value = time - beginning;
|
||||||
|
resultKeyframes.add(copy);
|
||||||
|
timeKeyframes++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resultKeyframes.add(keyframe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (timeKeyframes >= 2) {
|
||||||
|
Keyframe[] keyframes = resultKeyframes.toArray(new Keyframe[resultKeyframes.size()]);
|
||||||
|
resultSets.add(new KeyframeSet(set.getName(), keyframes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Writer out = new OutputStreamWriter(replayFile.write(eu.crushedpixel.replaymod.utils.ReplayFile.ENTRY_PATHS));
|
||||||
|
new Gson().toJson(resultSets.toArray(new KeyframeSet[resultSets.size()]), out);
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
public static void connectReplayFiles(List<File> filesToConnect, File outputFile, ProgressUpdateListener updateListener) throws IOException {
|
public static void connectReplayFiles(List<File> filesToConnect, File outputFile, ProgressUpdateListener updateListener) throws IOException {
|
||||||
updateListener.onProgressChanged(0f, I18n.format("replaymod.gui.editor.progress.status.initializing"));
|
updateListener.onProgressChanged(0f, I18n.format("replaymod.gui.editor.progress.status.initializing"));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user