2014-08-25 22:00:51 +02:00
|
|
|
package com.pahimar.ee3.util;
|
|
|
|
|
2015-05-02 20:07:01 +02:00
|
|
|
import com.google.common.io.Files;
|
2014-09-12 22:11:18 +02:00
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
2015-05-07 19:45:06 +02:00
|
|
|
import com.pahimar.ee3.api.exchange.EnergyValue;
|
2015-05-01 20:21:59 +02:00
|
|
|
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
2014-09-12 22:11:18 +02:00
|
|
|
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
|
|
|
|
import com.pahimar.ee3.exchange.WrappedStack;
|
2015-02-01 05:47:45 +01:00
|
|
|
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
|
2015-01-27 05:17:32 +01:00
|
|
|
import com.pahimar.ee3.reference.Reference;
|
2014-08-29 22:25:31 +02:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
2014-08-25 22:00:51 +02:00
|
|
|
import cpw.mods.fml.common.Loader;
|
|
|
|
import cpw.mods.fml.common.ModContainer;
|
2014-08-29 22:25:31 +02:00
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-08-25 22:00:51 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
2014-08-25 22:00:51 +02:00
|
|
|
|
|
|
|
public class SerializationHelper
|
|
|
|
{
|
2015-06-10 05:25:40 +02:00
|
|
|
private static File instanceDataDirectory;
|
|
|
|
private static File instancePlayerDataDirectory;
|
2015-01-27 05:17:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a File reference to the mod specific directory in the data directory
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2015-06-10 05:25:40 +02:00
|
|
|
public static File getInstanceDataDirectory()
|
2015-01-27 05:17:32 +01:00
|
|
|
{
|
2015-06-10 05:25:40 +02:00
|
|
|
return instanceDataDirectory;
|
2015-01-27 05:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a File reference to the mod specific directory in the playerdata directory
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2015-06-10 05:25:40 +02:00
|
|
|
public static File getInstancePlayerDataDirectory()
|
2015-01-27 05:17:32 +01:00
|
|
|
{
|
2015-06-10 05:25:40 +02:00
|
|
|
return instancePlayerDataDirectory;
|
2015-01-27 05:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates (if one does not exist already) and initializes a mod specific File reference inside of the current world's playerdata directory
|
|
|
|
*/
|
|
|
|
public static void initModDataDirectories()
|
|
|
|
{
|
2015-06-10 05:25:40 +02:00
|
|
|
instanceDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID);
|
|
|
|
instanceDataDirectory.mkdirs();
|
2015-01-27 05:17:32 +01:00
|
|
|
|
2015-06-10 05:25:40 +02:00
|
|
|
instancePlayerDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "playerdata" + File.separator + Reference.LOWERCASE_MOD_ID);
|
|
|
|
instancePlayerDataDirectory.mkdirs();
|
2015-01-27 05:17:32 +01:00
|
|
|
}
|
2014-09-12 22:11:18 +02:00
|
|
|
|
2014-08-25 22:00:51 +02:00
|
|
|
public static String getModListMD5()
|
|
|
|
{
|
|
|
|
List<String> modList = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (ModContainer modContainer : Loader.instance().getModList())
|
|
|
|
{
|
|
|
|
modList.add("[" + modContainer.getModId() + "-" + modContainer.getName() + "-" + modContainer.getVersion() + "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(modList);
|
|
|
|
|
|
|
|
StringBuilder modListString = new StringBuilder();
|
|
|
|
for (String modEntry : modList)
|
|
|
|
{
|
|
|
|
modListString.append(modEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return DigestUtils.md5Hex(modListString.toString());
|
|
|
|
}
|
2014-08-29 22:25:31 +02:00
|
|
|
|
2015-01-27 05:17:32 +01:00
|
|
|
public static NBTTagCompound readNBTFromFile(File nbtEncodedFile)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2015-01-27 05:17:32 +01:00
|
|
|
if (nbtEncodedFile.exists() && nbtEncodedFile.isFile())
|
2014-09-04 21:15:16 +02:00
|
|
|
{
|
2015-01-27 05:17:32 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return CompressedStreamTools.readCompressed(new FileInputStream(nbtEncodedFile));
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-08-29 22:25:31 +02:00
|
|
|
}
|
|
|
|
|
2015-01-27 05:17:32 +01:00
|
|
|
return null;
|
2014-08-29 22:25:31 +02:00
|
|
|
}
|
|
|
|
|
2015-01-27 05:17:32 +01:00
|
|
|
public static void writeNBTToFile(File directory, String fileName, INBTTaggable nbtTaggable)
|
2015-01-29 16:50:38 +01:00
|
|
|
{
|
|
|
|
writeNBTToFile(directory, fileName, nbtTaggable, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writeNBTToFile(File directory, String fileName, INBTTaggable nbtTaggable, boolean verboseLogging)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2015-01-27 05:17:32 +01:00
|
|
|
if (directory != null && fileName != null && nbtTaggable != null)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2015-01-27 05:17:32 +01:00
|
|
|
if (!directory.exists())
|
2014-09-04 21:15:16 +02:00
|
|
|
{
|
2015-01-27 05:17:32 +01:00
|
|
|
directory.mkdirs();
|
2014-09-04 21:15:16 +02:00
|
|
|
}
|
2014-08-29 22:25:31 +02:00
|
|
|
|
2015-01-27 05:17:32 +01:00
|
|
|
NBTTagCompound nbtTagCompound = new NBTTagCompound();
|
|
|
|
nbtTaggable.writeToNBT(nbtTagCompound);
|
2014-08-29 22:25:31 +02:00
|
|
|
|
2014-09-04 21:15:16 +02:00
|
|
|
try
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2015-01-27 05:17:32 +01:00
|
|
|
File file1 = new File(directory, fileName + ".tmp");
|
|
|
|
File file2 = new File(directory, fileName);
|
|
|
|
CompressedStreamTools.writeCompressed(nbtTagCompound, new FileOutputStream(file1));
|
2014-08-29 22:25:31 +02:00
|
|
|
|
2014-09-04 21:15:16 +02:00
|
|
|
if (file2.exists())
|
|
|
|
{
|
|
|
|
file2.delete();
|
|
|
|
}
|
2014-09-04 20:55:06 +02:00
|
|
|
|
2014-09-04 21:15:16 +02:00
|
|
|
file1.renameTo(file2);
|
|
|
|
|
2015-01-29 16:50:38 +01:00
|
|
|
if (verboseLogging)
|
|
|
|
{
|
2015-11-19 21:24:40 +01:00
|
|
|
LogHelper.info("Successfully saved {} to file: {}", nbtTaggable.getTagLabel(), file2.getAbsolutePath());
|
2015-01-29 16:50:38 +01:00
|
|
|
}
|
2015-02-05 05:48:07 +01:00
|
|
|
}
|
|
|
|
catch (Exception exception)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2015-11-19 21:24:40 +01:00
|
|
|
LogHelper.warn("Failed to save {} to file: {}{}{}", nbtTaggable.getTagLabel(), directory.getAbsolutePath(), File.separator, fileName);
|
2015-02-16 03:13:35 +01:00
|
|
|
exception.printStackTrace();
|
2015-02-01 05:47:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:48:07 +01:00
|
|
|
public static TransmutationKnowledge readTransmutationKnowledgeFromFile(File directory, String fileName)
|
|
|
|
{
|
|
|
|
if (!directory.exists())
|
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
directory.mkdirs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return TransmutationKnowledge.readFromFile(new File(directory, fileName));
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:48:07 +01:00
|
|
|
public static void writeTransmutationKnowledgeToFile(File directory, String fileName, TransmutationKnowledge transmutationKnowledge)
|
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
writeTransmutationKnowledgeToFile(directory, fileName, transmutationKnowledge, false);
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:48:07 +01:00
|
|
|
public static void writeTransmutationKnowledgeToFile(File directory, String fileName, TransmutationKnowledge transmutationKnowledge, boolean verboseLogging)
|
|
|
|
{
|
|
|
|
if (directory != null && fileName != null)
|
|
|
|
{
|
|
|
|
if (!directory.exists())
|
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
directory.mkdirs();
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:48:07 +01:00
|
|
|
if (transmutationKnowledge == null)
|
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
transmutationKnowledge = new TransmutationKnowledge();
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:48:07 +01:00
|
|
|
try
|
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
File file1 = new File(directory, fileName + ".tmp");
|
|
|
|
File file2 = new File(directory, fileName);
|
2015-02-10 01:08:18 +01:00
|
|
|
TransmutationKnowledge.writeToFile(file1, transmutationKnowledge);
|
2015-02-01 05:47:45 +01:00
|
|
|
|
2015-02-05 05:48:07 +01:00
|
|
|
if (file2.exists())
|
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
file2.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
file1.renameTo(file2);
|
|
|
|
|
2015-01-29 16:50:38 +01:00
|
|
|
if (verboseLogging)
|
|
|
|
{
|
2015-11-19 21:24:40 +01:00
|
|
|
LogHelper.info("Successfully saved TransmutationKnowledge to file: {}", file2.getAbsolutePath());
|
2015-01-29 16:50:38 +01:00
|
|
|
}
|
2015-02-05 05:48:07 +01:00
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
2015-11-19 21:24:40 +01:00
|
|
|
LogHelper.error("Failed to save TransmutationKnowledge to file: {}{}", directory.getAbsolutePath(), fileName);
|
2014-08-29 22:25:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 22:11:18 +02:00
|
|
|
|
|
|
|
public static Map<WrappedStack, EnergyValue> readEnergyValueStackMapFromJsonFile(String fileName)
|
|
|
|
{
|
2015-02-13 06:18:01 +01:00
|
|
|
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
|
|
|
|
return readEnergyValueStackMapFromJsonFile(new File(energyValuesDataDirectory, fileName));
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Map<WrappedStack, EnergyValue> readEnergyValueStackMapFromJsonFile(File jsonFile)
|
|
|
|
{
|
|
|
|
Map<WrappedStack, EnergyValue> energyValueStackMap = new TreeMap<WrappedStack, EnergyValue>();
|
|
|
|
JsonReader jsonReader;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
jsonReader = new JsonReader(new FileReader(jsonFile));
|
|
|
|
jsonReader.beginArray();
|
|
|
|
while (jsonReader.hasNext())
|
|
|
|
{
|
2015-01-27 05:17:32 +01:00
|
|
|
EnergyValueStackMapping energyValueStackMapping = EnergyValueStackMapping.jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
|
2015-04-09 18:40:19 +02:00
|
|
|
if (energyValueStackMapping != null)
|
|
|
|
{
|
|
|
|
energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue);
|
|
|
|
}
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
jsonReader.endArray();
|
|
|
|
jsonReader.close();
|
2015-02-05 05:48:07 +01:00
|
|
|
}
|
|
|
|
catch (FileNotFoundException ignored)
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return energyValueStackMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writeEnergyValueStackMapToJsonFile(String fileName, Map<WrappedStack, EnergyValue> energyValueMap)
|
|
|
|
{
|
2015-02-13 06:18:01 +01:00
|
|
|
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
|
|
|
|
writeEnergyValueStackMapToJsonFile(new File(energyValuesDataDirectory, fileName), energyValueMap);
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void writeEnergyValueStackMapToJsonFile(File jsonFile, Map<WrappedStack, EnergyValue> energyValueMap)
|
|
|
|
{
|
|
|
|
JsonWriter jsonWriter;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
jsonWriter = new JsonWriter(new FileWriter(jsonFile));
|
|
|
|
jsonWriter.setIndent(" ");
|
|
|
|
jsonWriter.beginArray();
|
|
|
|
for (WrappedStack wrappedStack : energyValueMap.keySet())
|
|
|
|
{
|
2015-04-21 03:16:48 +02:00
|
|
|
if (wrappedStack != null && wrappedStack.getWrappedObject() != null)
|
2015-04-17 00:18:49 +02:00
|
|
|
{
|
|
|
|
EnergyValueStackMapping.jsonSerializer.toJson(new EnergyValueStackMapping(wrappedStack, energyValueMap.get(wrappedStack)), EnergyValueStackMapping.class, jsonWriter);
|
|
|
|
}
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonWriter.endArray();
|
|
|
|
jsonWriter.close();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 20:21:59 +02:00
|
|
|
|
|
|
|
public static void compressEnergyValueStackMapToFile(String fileName, Map<WrappedStack, EnergyValue> energyValueMap)
|
|
|
|
{
|
|
|
|
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
|
|
|
|
compressEnergyValueStackMapToFile(new File(energyValuesDataDirectory, fileName), energyValueMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void compressEnergyValueStackMapToFile(File file, Map<WrappedStack, EnergyValue> energyValueMap)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
byte[] energyValueRegistryArray = CompressionHelper.compressStringToByteArray(EnergyValueRegistry.getInstance().toJson());
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
fos.write(energyValueRegistryArray);
|
|
|
|
fos.close();
|
|
|
|
}
|
|
|
|
catch (UnsupportedEncodingException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-05-02 20:07:01 +02:00
|
|
|
|
|
|
|
public static Map<WrappedStack, EnergyValue> decompressEnergyValueStackMapFromFile(String fileName)
|
|
|
|
{
|
|
|
|
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
|
|
|
|
return decompressEnergyValueStackMapFromFile(new File(energyValuesDataDirectory, fileName));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Map<WrappedStack, EnergyValue> decompressEnergyValueStackMapFromFile(File file)
|
|
|
|
{
|
|
|
|
Map<WrappedStack, EnergyValue> energyValueStackMap = new TreeMap<WrappedStack, EnergyValue>();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String jsonEnergyValueStackMap = CompressionHelper.decompressStringFromByteArray(Files.toByteArray(file));
|
|
|
|
JsonReader jsonReader = new JsonReader(new StringReader(jsonEnergyValueStackMap));
|
|
|
|
jsonReader.beginArray();
|
|
|
|
while (jsonReader.hasNext())
|
|
|
|
{
|
|
|
|
EnergyValueStackMapping energyValueStackMapping = EnergyValueStackMapping.jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
|
|
|
|
if (energyValueStackMapping != null)
|
|
|
|
{
|
|
|
|
energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jsonReader.endArray();
|
|
|
|
jsonReader.close();
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return energyValueStackMap;
|
|
|
|
}
|
2014-08-25 22:00:51 +02:00
|
|
|
}
|