196 lines
6.6 KiB
Java
196 lines
6.6 KiB
Java
package com.pahimar.ee3.util;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
import com.google.gson.stream.JsonWriter;
|
|
import com.pahimar.ee3.api.EnergyValue;
|
|
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
|
|
import com.pahimar.ee3.exchange.WrappedStack;
|
|
import com.pahimar.ee3.reference.Reference;
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
import cpw.mods.fml.common.Loader;
|
|
import cpw.mods.fml.common.ModContainer;
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
public class SerializationHelper
|
|
{
|
|
private static File dataDirectory;
|
|
private static File playerDataDirectory;
|
|
|
|
/**
|
|
* Returns a File reference to the mod specific directory in the data directory
|
|
*
|
|
* @return
|
|
*/
|
|
public static File getDataDirectory()
|
|
{
|
|
return dataDirectory;
|
|
}
|
|
|
|
/**
|
|
* Returns a File reference to the mod specific directory in the playerdata directory
|
|
*
|
|
* @return
|
|
*/
|
|
public static File getPlayerDataDirectory()
|
|
{
|
|
return playerDataDirectory;
|
|
}
|
|
|
|
/**
|
|
* 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()
|
|
{
|
|
dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.MOD_ID.toLowerCase());
|
|
dataDirectory.mkdirs();
|
|
|
|
playerDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "playerdata" + File.separator + Reference.MOD_ID.toLowerCase());
|
|
playerDataDirectory.mkdirs();
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
public static NBTTagCompound readNBTFromFile(File nbtEncodedFile)
|
|
{
|
|
if (nbtEncodedFile.exists() && nbtEncodedFile.isFile())
|
|
{
|
|
try
|
|
{
|
|
return CompressedStreamTools.readCompressed(new FileInputStream(nbtEncodedFile));
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static void writeNBTToFile(File directory, String fileName, INBTTaggable nbtTaggable)
|
|
{
|
|
if (directory != null && fileName != null && nbtTaggable != null)
|
|
{
|
|
if (!directory.exists())
|
|
{
|
|
directory.mkdirs();
|
|
}
|
|
|
|
NBTTagCompound nbtTagCompound = new NBTTagCompound();
|
|
nbtTaggable.writeToNBT(nbtTagCompound);
|
|
|
|
try
|
|
{
|
|
File file1 = new File(directory, fileName + ".tmp");
|
|
File file2 = new File(directory, fileName);
|
|
CompressedStreamTools.writeCompressed(nbtTagCompound, new FileOutputStream(file1));
|
|
|
|
if (file2.exists())
|
|
{
|
|
file2.delete();
|
|
}
|
|
|
|
file1.renameTo(file2);
|
|
|
|
LogHelper.info(String.format("Successfully saved %s to file: %s", nbtTaggable.getTagLabel(), file2.getAbsolutePath()));
|
|
} catch (Exception exception)
|
|
{
|
|
LogHelper.warn(String.format("Failed to save %s to file: %s%s", nbtTaggable.getTagLabel(), directory.getAbsolutePath(), fileName));
|
|
}
|
|
}
|
|
}
|
|
|
|
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 = EnergyValueStackMapping.jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
|
|
energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue);
|
|
}
|
|
jsonReader.endArray();
|
|
jsonReader.close();
|
|
} catch (FileNotFoundException ignored)
|
|
{
|
|
}
|
|
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())
|
|
{
|
|
EnergyValueStackMapping.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");
|
|
return new File(dataDirectory, fileName);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|