Only register changes in ReplayFileAppender if something changed - this applies to GuiKeyframeRepository, GuiObjectManager, GuiPlayerOverview, GuiAssetManager

Added equals method to ReplayImageAsset which is based on the BufferedImage's hash code (which is calculated only once)
This commit is contained in:
CrushedPixel
2015-07-16 13:26:12 +02:00
parent e6fcf4f094
commit d45fcb8a34
9 changed files with 69 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ package eu.crushedpixel.replaymod.assets;
import eu.crushedpixel.replaymod.ReplayMod; import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.replay.ReplayHandler; import eu.crushedpixel.replaymod.replay.ReplayHandler;
import eu.crushedpixel.replaymod.utils.ReplayFile; import eu.crushedpixel.replaymod.utils.ReplayFile;
import lombok.EqualsAndHashCode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
@@ -13,14 +14,25 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
@EqualsAndHashCode
public class AssetRepository { public class AssetRepository {
private Map<UUID, ReplayAsset> replayAssets; private final Map<UUID, ReplayAsset> replayAssets;
public AssetRepository() { public AssetRepository() {
replayAssets = new HashMap<UUID, ReplayAsset>(); replayAssets = new HashMap<UUID, ReplayAsset>();
} }
public AssetRepository(AssetRepository toCopy) {
HashMap<UUID, ReplayAsset> newAssetList = new HashMap<UUID, ReplayAsset>();
for(Map.Entry<UUID, ReplayAsset> e : toCopy.replayAssets.entrySet()) {
newAssetList.put(e.getKey(), e.getValue().copy());
}
this.replayAssets = newAssetList;
}
public ReplayAsset addAsset(String assetFileName, InputStream inputStream) throws IOException { public ReplayAsset addAsset(String assetFileName, InputStream inputStream) throws IOException {
return addAsset(assetFileName, inputStream, null); return addAsset(assetFileName, inputStream, null);
} }

View File

@@ -4,6 +4,7 @@ import eu.crushedpixel.replaymod.holders.GuiEntryListEntry;
import eu.crushedpixel.replaymod.holders.Transformations; import eu.crushedpixel.replaymod.holders.Transformations;
import eu.crushedpixel.replaymod.registry.ResourceHelper; import eu.crushedpixel.replaymod.registry.ResourceHelper;
import eu.crushedpixel.replaymod.replay.ReplayHandler; import eu.crushedpixel.replaymod.replay.ReplayHandler;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@@ -14,6 +15,7 @@ import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;
@EqualsAndHashCode
public class CustomImageObject implements GuiEntryListEntry { public class CustomImageObject implements GuiEntryListEntry {
public CustomImageObject(String name, UUID assetUUID) throws IOException { public CustomImageObject(String name, UUID assetUUID) throws IOException {

View File

@@ -17,6 +17,8 @@ public interface ReplayAsset<T> extends GuiEntryListEntry {
void setAssetName(String name); void setAssetName(String name);
ReplayAsset<T> copy();
T getObject(); T getObject();
} }

View File

@@ -3,6 +3,8 @@ package eu.crushedpixel.replaymod.assets;
import eu.crushedpixel.replaymod.registry.ResourceHelper; import eu.crushedpixel.replaymod.registry.ResourceHelper;
import eu.crushedpixel.replaymod.replay.ReplayHandler; import eu.crushedpixel.replaymod.replay.ReplayHandler;
import eu.crushedpixel.replaymod.utils.BoundingUtils; import eu.crushedpixel.replaymod.utils.BoundingUtils;
import eu.crushedpixel.replaymod.utils.BufferedImageUtils;
import lombok.EqualsAndHashCode;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.texture.DynamicTexture; import net.minecraft.client.renderer.texture.DynamicTexture;
@@ -16,14 +18,24 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.UUID; import java.util.UUID;
@EqualsAndHashCode(of={"name", "bufferedImageHashCode"})
public class ReplayImageAsset implements ReplayAsset<BufferedImage> { public class ReplayImageAsset implements ReplayAsset<BufferedImage> {
private final Minecraft mc = Minecraft.getMinecraft(); private final Minecraft mc = Minecraft.getMinecraft();
private BufferedImage object; private BufferedImage object;
private int bufferedImageHashCode;
private String name; private String name;
public ReplayImageAsset copy() {
ReplayImageAsset newReplay = new ReplayImageAsset(name);
newReplay.object = object;
newReplay.bufferedImageHashCode = bufferedImageHashCode;
return newReplay;
}
public ReplayImageAsset(String name) { public ReplayImageAsset(String name) {
this.name = name; this.name = name;
} }
@@ -46,6 +58,7 @@ public class ReplayImageAsset implements ReplayAsset<BufferedImage> {
@Override @Override
public void loadFromStream(InputStream inputStream) throws IOException { public void loadFromStream(InputStream inputStream) throws IOException {
this.object = ImageIO.read(inputStream); this.object = ImageIO.read(inputStream);
this.bufferedImageHashCode = BufferedImageUtils.hashCode(object);
ResourceHelper.freeResource(previewResource); ResourceHelper.freeResource(previewResource);
for(CustomImageObject object : ReplayHandler.getCustomImageObjects()) { for(CustomImageObject object : ReplayHandler.getCustomImageObjects()) {

View File

@@ -34,10 +34,13 @@ public class GuiAssetManager extends GuiScreen {
private AssetRepository assetRepository; private AssetRepository assetRepository;
private final AssetRepository initialRepository;
private ReplayAsset currentAsset; private ReplayAsset currentAsset;
public GuiAssetManager() { public GuiAssetManager() {
this.assetRepository = ReplayHandler.getAssetRepository(); this.initialRepository = ReplayHandler.getAssetRepository();
this.assetRepository = new AssetRepository(ReplayHandler.getAssetRepository());
} }
@Override @Override
@@ -190,6 +193,8 @@ public class GuiAssetManager extends GuiScreen {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
if(assetRepository.equals(initialRepository)) return;
ReplayHandler.setAssetRepository(assetRepository);
assetRepository.saveAssets(); assetRepository.saveAssets();
} }
}, "replaymod-asset-saver").start(); }, "replaymod-asset-saver").start();

View File

@@ -15,6 +15,7 @@ import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.resources.I18n; import net.minecraft.client.resources.I18n;
import org.apache.commons.lang3.time.DurationFormatUtils; import org.apache.commons.lang3.time.DurationFormatUtils;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import scala.actors.threadpool.Arrays;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
@@ -40,8 +41,11 @@ public class GuiKeyframeRepository extends GuiScreen implements GuiReplayOverlay
private int currentSetTimeKeyframeCount, currentSetPositionKeyframeCount, currentSetDuration; private int currentSetTimeKeyframeCount, currentSetPositionKeyframeCount, currentSetDuration;
private final List<KeyframeSet> initialKeyframeSets;
public GuiKeyframeRepository(KeyframeSet[] keyframeRepository) { public GuiKeyframeRepository(KeyframeSet[] keyframeRepository) {
this.keyframeRepository = keyframeRepository; this.keyframeRepository = keyframeRepository;
this.initialKeyframeSets = new ArrayList<KeyframeSet>(Arrays.asList(keyframeRepository));
ReplayMod.replaySender.setReplaySpeed(0); ReplayMod.replaySender.setReplaySpeed(0);
} }
@@ -230,6 +234,8 @@ public class GuiKeyframeRepository extends GuiScreen implements GuiReplayOverlay
private void saveOnQuit() { private void saveOnQuit() {
ArrayList<KeyframeSet> copy = new ArrayList<KeyframeSet>(keyframeSetList.getCopyOfElements()); ArrayList<KeyframeSet> copy = new ArrayList<KeyframeSet>(keyframeSetList.getCopyOfElements());
if(initialKeyframeSets.equals(copy)) return;
this.keyframeRepository = copy.toArray(new KeyframeSet[copy.size()]); this.keyframeRepository = copy.toArray(new KeyframeSet[copy.size()]);
ReplayHandler.setKeyframeRepository(keyframeRepository, true); ReplayHandler.setKeyframeRepository(keyframeRepository, true);
} }

View File

@@ -42,6 +42,8 @@ public class GuiObjectManager extends GuiScreen {
private GuiString dropdownLabel; private GuiString dropdownLabel;
private GuiDropdown<GuiEntryListValueEntry<UUID>> assetDropdown; private GuiDropdown<GuiEntryListValueEntry<UUID>> assetDropdown;
private final List<CustomImageObject> initialObjects = ReplayHandler.getCustomImageObjects();
private GuiDraggingNumberInput anchorXInput, anchorYInput, anchorZInput; private GuiDraggingNumberInput anchorXInput, anchorYInput, anchorZInput;
private GuiDraggingNumberInput positionXInput, positionYInput, positionZInput; private GuiDraggingNumberInput positionXInput, positionYInput, positionZInput;
private GuiDraggingNumberInput orientationXInput, orientationYInput, orientationZInput; private GuiDraggingNumberInput orientationXInput, orientationYInput, orientationZInput;
@@ -173,7 +175,7 @@ public class GuiObjectManager extends GuiScreen {
nameInput = new GuiAdvancedTextField(fontRendererObj, 0, 0, 0, 20); nameInput = new GuiAdvancedTextField(fontRendererObj, 0, 0, 0, 20);
nameInput.hint = I18n.format("replaymod.gui.objects.properties.name"); nameInput.hint = I18n.format("replaymod.gui.objects.properties.name");
for(CustomImageObject customImageObject : ReplayHandler.getCustomImageObjects()) { for(CustomImageObject customImageObject : initialObjects) {
objectList.addElement(customImageObject); objectList.addElement(customImageObject);
} }
@@ -366,6 +368,11 @@ public class GuiObjectManager extends GuiScreen {
private void saveOnQuit() { private void saveOnQuit() {
List<CustomImageObject> objects = objectList.getCopyOfElements(); List<CustomImageObject> objects = objectList.getCopyOfElements();
if(objects.equals(initialObjects)) {
return;
}
ReplayHandler.setCustomImageObjects(objects); ReplayHandler.setCustomImageObjects(objects);
if(objects.size() > 0) { if(objects.size() > 0) {

View File

@@ -54,7 +54,11 @@ public class GuiPlayerOverview extends GuiScreen implements GuiReplayOverlay.NoO
private final String screenTitle = I18n.format("replaymod.input.playeroverview"); private final String screenTitle = I18n.format("replaymod.input.playeroverview");
private final Set<UUID> initialHiddenPlayers;
public GuiPlayerOverview(List<EntityPlayer> players) { public GuiPlayerOverview(List<EntityPlayer> players) {
initialHiddenPlayers = new HashSet<UUID>(PlayerHandler.getHiddenPlayers());
Collections.sort(players, new PlayerComparator()); Collections.sort(players, new PlayerComparator());
this.players = new ArrayList<Pair<EntityPlayer, ResourceLocation>>(); this.players = new ArrayList<Pair<EntityPlayer, ResourceLocation>>();
@@ -304,6 +308,7 @@ public class GuiPlayerOverview extends GuiScreen implements GuiReplayOverlay.NoO
private void saveOnQuit() { private void saveOnQuit() {
if(rememberHidden.isChecked()) { if(rememberHidden.isChecked()) {
if(initialHiddenPlayers.equals(PlayerHandler.getHiddenPlayers())) return;
try { try {
File f = File.createTempFile(ReplayFile.ENTRY_VISIBILITY, "json"); File f = File.createTempFile(ReplayFile.ENTRY_VISIBILITY, "json");
ReplayFileIO.write(getVisibilityInstance(), f); ReplayFileIO.write(getVisibilityInstance(), f);
@@ -312,6 +317,7 @@ public class GuiPlayerOverview extends GuiScreen implements GuiReplayOverlay.NoO
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
if(initialHiddenPlayers.isEmpty()) return;
ReplayMod.replayFileAppender.registerModifiedFile(null, ReplayFile.ENTRY_VISIBILITY, ReplayHandler.getReplayFile()); ReplayMod.replayFileAppender.registerModifiedFile(null, ReplayFile.ENTRY_VISIBILITY, ReplayHandler.getReplayFile());
} }
} }

View File

@@ -0,0 +1,13 @@
package eu.crushedpixel.replaymod.utils;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.Arrays;
public class BufferedImageUtils {
public static int hashCode(BufferedImage bi) {
byte[] data = ((DataBufferByte) bi.getData().getDataBuffer()).getData();
return Arrays.hashCode(data);
}
}