Save resource packs to replay file and use those during replay

This commit is contained in:
johni0702
2015-05-29 12:10:06 +02:00
parent 95e2c932f0
commit 37d31408bd
7 changed files with 323 additions and 115 deletions

View File

@@ -2,6 +2,8 @@ package eu.crushedpixel.replaymod.utils;
import com.google.common.base.Supplier;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import eu.crushedpixel.replaymod.holders.KeyframeSet;
import eu.crushedpixel.replaymod.holders.PlayerVisibility;
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
@@ -11,6 +13,8 @@ import org.apache.commons.compress.archivers.zip.ZipFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class ReplayFile extends ZipFile {
@@ -21,6 +25,8 @@ public class ReplayFile extends ZipFile {
public static final String ENTRY_METADATA = "metaData" + JSON_FILE_EXTENSION;
public static final String ENTRY_PATHS = "paths";
public static final String ENTRY_THUMB = "thumb";
public static final String ENTRY_RESOURCE_PACK = "resourcepack/%s.zip";
public static final String ENTRY_RESOURCE_PACK_INDEX = "resourcepack/index.json";
public static final String ENTRY_VISIBILITY = "visibility";
private final File file;
@@ -138,4 +144,52 @@ public class ReplayFile extends ZipFile {
}
};
}
public ZipArchiveEntry resourcePackIndexEntry() {
return getEntry(ENTRY_RESOURCE_PACK_INDEX);
}
public Supplier<Map<Integer, String>> resourcePackIndex() {
return new Supplier<Map<Integer, String>>() {
@Override
@SuppressWarnings("unchecked")
public Map<Integer, String> get() {
try {
ZipArchiveEntry entry = resourcePackIndexEntry();
if (entry == null) {
return null;
}
Map<Integer, String> index = new HashMap<Integer, String>();
JsonObject json = new Gson().fromJson(new InputStreamReader(getInputStream(entry)), JsonObject.class);
for (Map.Entry<String, JsonElement> e : json.entrySet()) {
index.put(Integer.parseInt(e.getKey()), e.getValue().getAsString());
}
return index;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
public ZipArchiveEntry resourcePackEntry(String hash) {
return getEntry(String.format(ENTRY_RESOURCE_PACK, hash));
}
public Supplier<InputStream> resourcePack(final String hash) {
return new Supplier<InputStream>() {
@Override
public InputStream get() {
try {
ZipArchiveEntry entry = resourcePackEntry(hash);
if (entry == null) {
return null;
}
return getInputStream(entry);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
}

View File

@@ -16,12 +16,14 @@ import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.S01PacketJoinGame;
import net.minecraft.network.play.server.S08PacketPlayerPosLook;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
@@ -58,7 +60,8 @@ public class ReplayFileIO {
return files;
}
public static void writeReplayFile(File replayFile, File tempFile, ReplayMetaData metaData) throws IOException {
public static void writeReplayFile(File replayFile, File tempFile, ReplayMetaData metaData,
Map<String, File> resourcePacks, Map<Integer, String> resourcePackRequests) throws IOException {
byte[] buffer = new byte[1024];
if(!replayFile.exists()) {
@@ -86,6 +89,20 @@ public class ReplayFileIO {
fis.close();
zos.closeEntry();
if (!resourcePackRequests.isEmpty()) {
zos.putNextEntry(new ZipEntry(ReplayFile.ENTRY_RESOURCE_PACK_INDEX));
pw = new PrintWriter(zos);
pw.write(new Gson().toJson(resourcePackRequests));
pw.flush();
zos.closeEntry();
for (Map.Entry<String, File> entry : resourcePacks.entrySet()) {
zos.putNextEntry(new ZipEntry(String.format(ReplayFile.ENTRY_RESOURCE_PACK, entry.getKey())));
IOUtils.copy(new FileInputStream(entry.getValue()), zos);
zos.closeEntry();
}
}
zos.close();
}