Added Option to save Player Visibility Settings in Replay

This commit is contained in:
CrushedPixel
2015-05-20 23:56:34 +02:00
parent 4754ecc6bf
commit fa187ae0e5
9 changed files with 168 additions and 19 deletions

View File

@@ -3,6 +3,7 @@ package eu.crushedpixel.replaymod.utils;
import com.google.common.base.Supplier;
import com.google.gson.Gson;
import eu.crushedpixel.replaymod.holders.KeyframeSet;
import eu.crushedpixel.replaymod.holders.PlayerVisibility;
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
@@ -20,6 +21,7 @@ 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_VISIBILITY = "visibility";
private final File file;
@@ -71,6 +73,10 @@ public class ReplayFile extends ZipFile {
return getEntry(ENTRY_PATHS);
}
public ZipArchiveEntry visibilityEntry() {
return getEntry(ENTRY_VISIBILITY);
}
public Supplier<KeyframeSet[]> paths() {
return new Supplier<KeyframeSet[]>() {
@Override
@@ -89,6 +95,24 @@ public class ReplayFile extends ZipFile {
};
}
public Supplier<PlayerVisibility> visibility() {
return new Supplier<PlayerVisibility>() {
@Override
public PlayerVisibility get() {
try {
ZipArchiveEntry entry = visibilityEntry();
if (entry == null) {
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(getInputStream(entry)));
return new Gson().fromJson(reader, PlayerVisibility.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
public ZipArchiveEntry thumbEntry() {
return getEntry(ENTRY_THUMB);
}

View File

@@ -4,6 +4,7 @@ import com.google.gson.Gson;
import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.holders.KeyframeSet;
import eu.crushedpixel.replaymod.holders.PacketData;
import eu.crushedpixel.replaymod.holders.PlayerVisibility;
import eu.crushedpixel.replaymod.recording.PacketSerializer;
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
import io.netty.buffer.ByteBuf;
@@ -291,11 +292,20 @@ public class ReplayFileIO {
Files.write(file.toPath(), json.getBytes());
}
public static KeyframeSet[] getKeyframeRegistryFromFile(File file) throws Exception {
return gson.fromJson(Files.readAllLines(file.toPath()).get(0), KeyframeSet[].class);
public static void writePlayerVisibilityToFile(PlayerVisibility visibility, File file) throws IOException {
file.mkdirs();
file.createNewFile();
String json = gson.toJson(visibility);
Files.write(file.toPath(), json.getBytes());
}
public static void addFileToZip(File zipFile, File toAdd, String fileName) throws IOException {
if(toAdd == null) {
removeFileFromZip(zipFile, fileName);
return;
}
// get a temp file
File tempFile = File.createTempFile(zipFile.getName(), null, zipFile.getParentFile());
// delete it, otherwise you cannot rename your existing zip to it.
@@ -347,4 +357,39 @@ public class ReplayFileIO {
toAdd.delete();
}
public static void removeFileFromZip(File zipFile, String toRemove) throws IOException {
// get a temp file
File tempFile = File.createTempFile(zipFile.getName(), null, zipFile.getParentFile());
// delete it, otherwise you cannot rename your existing zip to it.
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tempFile));
ZipEntry entry = zin.getNextEntry();
while(entry != null) {
String name = entry.getName();
boolean isFile = name.equalsIgnoreCase(toRemove);
if(!isFile) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(name));
// Transfer bytes from the ZIP file to the output file
int len;
while((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
// Close the streams
zin.close();
// Complete the ZIP file
out.close();
zipFile.delete();
tempFile.renameTo(zipFile);
}
}