2014-08-25 22:00:51 +02:00
|
|
|
package com.pahimar.ee3.util;
|
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
|
|
|
import com.pahimar.ee3.api.EnergyValue;
|
2014-08-29 22:25:31 +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;
|
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
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(EnergyValueStackMapping.class, new EnergyValueStackMapping()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
|
|
|
|
|
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
|
|
|
|
2014-09-16 18:08:16 +02:00
|
|
|
public static boolean dataFileExist(String fileName)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2014-09-04 21:15:16 +02:00
|
|
|
if (FMLCommonHandler.instance().getMinecraftServerInstance() == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-29 22:25:31 +02:00
|
|
|
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + "ee3");
|
|
|
|
if (!dataDirectory.exists())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (dataDirectory.isFile())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-16 18:08:16 +02:00
|
|
|
File file = new File(dataDirectory, fileName);
|
2014-08-29 22:25:31 +02:00
|
|
|
|
|
|
|
return file.exists() && file.isFile();
|
|
|
|
}
|
|
|
|
|
2014-09-16 18:08:16 +02:00
|
|
|
public static void writeEnergyValueRegistryToFile(String fileName)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2014-09-04 21:15:16 +02:00
|
|
|
if (FMLCommonHandler.instance().getMinecraftServerInstance() != null)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2014-09-04 21:15:16 +02:00
|
|
|
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + "ee3");
|
|
|
|
if (!dataDirectory.exists())
|
|
|
|
{
|
|
|
|
dataDirectory.mkdir();
|
|
|
|
}
|
2014-08-29 22:25:31 +02:00
|
|
|
|
2014-09-04 21:15:16 +02:00
|
|
|
NBTTagCompound energyValueRegistryNBT = new NBTTagCompound();
|
|
|
|
EnergyValueRegistry.getInstance().writeToNBT(energyValueRegistryNBT);
|
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
|
|
|
{
|
2014-09-16 18:08:16 +02:00
|
|
|
File file1 = new File(dataDirectory, fileName + ".tmp");
|
|
|
|
File file2 = new File(dataDirectory, fileName);
|
2014-09-04 21:15:16 +02:00
|
|
|
CompressedStreamTools.writeCompressed(energyValueRegistryNBT, 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);
|
|
|
|
|
2014-09-16 18:08:16 +02:00
|
|
|
LogHelper.info("Successfully saved EnergyValueRegistry to file: " + file2.getPath());
|
2014-09-04 21:15:16 +02:00
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
LogHelper.warn("Failed to save EnergyValueRegistry to file " + dataDirectory.getPath() + SerializationHelper.getModListMD5() + ".ee3");
|
|
|
|
}
|
2014-08-29 22:25:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 18:08:16 +02:00
|
|
|
public static NBTTagCompound readEnergyValueRegistryFromFile(String fileName)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2014-09-16 18:08:16 +02:00
|
|
|
if (dataFileExist(fileName))
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
|
|
|
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + "ee3");
|
2014-09-16 18:08:16 +02:00
|
|
|
File energyValueRegistryFile = new File(dataDirectory, fileName);
|
2014-08-29 22:25:31 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-09-16 18:08:16 +02:00
|
|
|
return CompressedStreamTools.readCompressed(new FileInputStream(energyValueRegistryFile));
|
2014-08-29 22:25:31 +02:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2014-09-16 18:08:16 +02:00
|
|
|
|
|
|
|
return null;
|
2014-08-29 22:25:31 +02:00
|
|
|
}
|
2014-09-12 22:11:18 +02:00
|
|
|
|
|
|
|
public static Map<WrappedStack, EnergyValue> readEnergyValueStackMapFromJsonFile(String fileName)
|
|
|
|
{
|
|
|
|
return readEnergyValueStackMapFromJsonFile(getFileInDataDirectory(fileName));
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
EnergyValueStackMapping energyValueStackMapping = jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
|
|
|
|
energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue);
|
|
|
|
}
|
|
|
|
jsonReader.endArray();
|
|
|
|
jsonReader.close();
|
2014-11-15 04:37:30 +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)
|
|
|
|
{
|
|
|
|
writeEnergyValueStackMapToJsonFile(getFileInDataDirectory(fileName), energyValueMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
jsonSerializer.toJson(new EnergyValueStackMapping(wrappedStack, energyValueMap.get(wrappedStack)), EnergyValueStackMapping.class, jsonWriter);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonWriter.endArray();
|
|
|
|
jsonWriter.close();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static File getFileInDataDirectory(String fileName)
|
|
|
|
{
|
|
|
|
if (FMLCommonHandler.instance().getMinecraftServerInstance() != null && FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld() != null)
|
|
|
|
{
|
|
|
|
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + "ee3");
|
2014-09-15 22:06:20 +02:00
|
|
|
return new File(dataDirectory, fileName);
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-25 22:00:51 +02:00
|
|
|
}
|