Checking in some progress on a more unified way of serializing data to disk. Should really help in the future.

This commit is contained in:
Pahimar 2015-01-26 23:17:32 -05:00
parent dc01ec4a91
commit 8671984507
10 changed files with 182 additions and 101 deletions

View file

@ -55,6 +55,7 @@ public class EquivalentExchange3
event.registerServerCommand(new CommandSetValue());
event.registerServerCommand(new CommandSetCurrentItemValue());
event.registerServerCommand(new CommandSyncValues());
SerializationHelper.initModDataDirectories();
}
@EventHandler
@ -122,7 +123,8 @@ public class EquivalentExchange3
}
else
{
SerializationHelper.writeEnergyValueRegistryToFile(SerializationHelper.getModListMD5() + "." + Reference.MOD_ID.toLowerCase());
// SerializationHelper.writeEnergyValueRegistryToFile(SerializationHelper.getModListMD5() + "." + Reference.MOD_ID.toLowerCase());
SerializationHelper.writeNBTToFile(SerializationHelper.getDataDirectory(), SerializationHelper.getModListMD5() + "." + Reference.MOD_ID.toLowerCase(), getEnergyValueRegistry());
}
WorldEventHandler.hasInitilialized = false;

View file

@ -5,9 +5,12 @@ import com.pahimar.ee3.api.EnergyValue;
import com.pahimar.ee3.api.IEnergyValueProvider;
import com.pahimar.ee3.recipe.RecipeRegistry;
import com.pahimar.ee3.reference.Files;
import com.pahimar.ee3.reference.Reference;
import com.pahimar.ee3.util.EnergyValueHelper;
import com.pahimar.ee3.util.INBTTaggable;
import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.util.SerializationHelper;
import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@ -15,6 +18,7 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.OreDictionary;
import java.io.File;
import java.util.*;
public class EnergyValueRegistry implements INBTTaggable
@ -346,18 +350,20 @@ public class EnergyValueRegistry implements INBTTaggable
protected final void init()
{
String fileName = SerializationHelper.getModListMD5() + ".ee3";
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.MOD_ID.toLowerCase());
File energyValueRegistryFile = new File(dataDirectory, SerializationHelper.getModListMD5() + ".ee3");
if (!SerializationHelper.dataFileExist(fileName))
if (!energyValueRegistryFile.exists())
{
runDynamicEnergyValueResolution();
}
else
{
NBTTagCompound nbtEnergyValueRegistry = SerializationHelper.readEnergyValueRegistryFromFile(fileName);
NBTTagCompound nbtEnergyValueRegistry = SerializationHelper.readNBTFromFile(energyValueRegistryFile);
if (nbtEnergyValueRegistry != null)
{
energyValueRegistry.readFromNBT(nbtEnergyValueRegistry);
LogHelper.info("Successfully loaded EnergyValueRegistry from file: " + energyValueRegistryFile.getAbsolutePath());
}
else
{
@ -495,7 +501,8 @@ public class EnergyValueRegistry implements INBTTaggable
valueMappings = ImmutableSortedMap.copyOf(tempValueMappings);
// Serialize values to disk
SerializationHelper.writeEnergyValueRegistryToFile(SerializationHelper.getModListMD5() + ".ee3");
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.MOD_ID.toLowerCase());
SerializationHelper.writeNBTToFile(dataDirectory, SerializationHelper.getModListMD5() + ".ee3", this);
}
private Map<WrappedStack, EnergyValue> computeStackMappings()
@ -663,6 +670,12 @@ public class EnergyValueRegistry implements INBTTaggable
nbtTagCompound.setTag("stackMappingList", stackMappingTagList);
}
@Override
public String getTagLabel()
{
return "EnergyValueRegistry";
}
public void setEnergyValue(WrappedStack wrappedStack, EnergyValue energyValue)
{
if (wrappedStack != null && energyValue != null && Float.compare(energyValue.getEnergyValue(), 0f) > 0)

View file

@ -7,7 +7,7 @@ import java.lang.reflect.Type;
public class EnergyValueStackMapping implements JsonSerializer<EnergyValueStackMapping>, JsonDeserializer<EnergyValueStackMapping>
{
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(EnergyValueStackMapping.class, new EnergyValueStackMapping()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
public static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(EnergyValueStackMapping.class, new EnergyValueStackMapping()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
public final WrappedStack wrappedStack;
public final EnergyValue energyValue;

View file

@ -3,7 +3,6 @@ package com.pahimar.ee3.handler;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.network.PacketHandler;
import com.pahimar.ee3.network.message.MessageSyncEnergyValues;
import com.pahimar.ee3.reference.Reference;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.event.entity.player.PlayerEvent;
@ -12,35 +11,20 @@ import java.io.File;
public class PlayerEventHandler
{
public static File playerDataDirectory;
public static File knowledgeDirectory;
@SubscribeEvent
public void onPlayerLoadFromFileEvent(PlayerEvent.LoadFromFile event)
{
if (!event.entityPlayer.worldObj.isRemote)
{
// Grab the correct directory to be reading/writing player knowledge data to
if (TransmutationKnowledgeHandler.playerDataDirectory == null || !TransmutationKnowledgeHandler.playerDataDirectory.getAbsolutePath().equalsIgnoreCase(event.playerDirectory.getAbsolutePath()))
{
TransmutationKnowledgeHandler.playerDataDirectory = new File(event.playerDirectory, Reference.MOD_ID.toLowerCase());
if (!TransmutationKnowledgeHandler.playerDataDirectory.exists())
{
TransmutationKnowledgeHandler.playerDataDirectory.mkdir();
}
TransmutationKnowledgeHandler.transmutationKnowledgeDirectory = new File(TransmutationKnowledgeHandler.playerDataDirectory, "knowledge" + File.pathSeparator + "transmutation");
if (!TransmutationKnowledgeHandler.transmutationKnowledgeDirectory.exists())
{
TransmutationKnowledgeHandler.transmutationKnowledgeDirectory.mkdir();
}
}
// If player knowledge data doesn't exist, initialize a file for the player
File playerTransmutationKnowledgeFile = new File(TransmutationKnowledgeHandler.transmutationKnowledgeDirectory, event.entityPlayer.getUniqueID() + TransmutationKnowledgeHandler.KNOWLEDGE_FILE_EXTENSION);
if (!playerTransmutationKnowledgeFile.exists())
{
TransmutationKnowledgeHandler.savePlayerKnowledge(event.entityPlayer);
}
// File playerTransmutationKnowledgeFile = new File(TransmutationKnowledgeHandler.transmutationKnowledgeDirectory, event.entityPlayer.getUniqueID() + TransmutationKnowledgeHandler.KNOWLEDGE_FILE_EXTENSION);
// if (!playerTransmutationKnowledgeFile.exists())
// {
// TransmutationKnowledgeHandler.savePlayerKnowledge(event.entityPlayer);
// }
}
}

View file

@ -1,11 +1,13 @@
package com.pahimar.ee3.handler;
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
import com.pahimar.ee3.reference.Reference;
import com.pahimar.ee3.reference.Settings;
import com.pahimar.ee3.util.LogHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.event.entity.player.PlayerEvent;
import java.io.File;
import java.io.FileInputStream;
@ -21,6 +23,27 @@ public class TransmutationKnowledgeHandler
private static TransmutationKnowledge templateTransmutationKnowledge;
// TODO Look into caching TransmutationKnowledge for currently logged in players, rather than going to disk constantly
public static void lazyInitKnowledgeSystem(PlayerEvent.LoadFromFile event)
{
// Grab the correct directory to be reading/writing player knowledge data to
if (playerDataDirectory == null || !playerDataDirectory.getAbsolutePath().equalsIgnoreCase(event.playerDirectory.getAbsolutePath()))
{
playerDataDirectory = new File(event.playerDirectory, Reference.MOD_ID.toLowerCase());
if (!playerDataDirectory.exists())
{
playerDataDirectory.mkdir();
}
transmutationKnowledgeDirectory = new File(playerDataDirectory, "knowledge");
if (!transmutationKnowledgeDirectory.exists())
{
transmutationKnowledgeDirectory.mkdir();
}
}
}
public static NBTTagCompound getPlayerKnowledge(EntityPlayer entityPlayer)
{
if (playerDataDirectory != null && playerDataDirectory.isDirectory())
@ -232,7 +255,7 @@ public class TransmutationKnowledgeHandler
}
public static final String KNOWLEDGE_FILE_EXTENSION = ".knowledge";
public static final String KNOWLEDGE_FILE_EXTENSION = ".transmutation";
private static final String TEMPLATE_FILENAME = "template" + KNOWLEDGE_FILE_EXTENSION;
private static final String ALLOWED_KNOWLEDGE_FILENAME = "allowedKnowledge" + KNOWLEDGE_FILE_EXTENSION;
private static final String ALLOWED_KNOWLEDGE_FILENAME = "allowed" + KNOWLEDGE_FILE_EXTENSION;
}

View file

@ -248,6 +248,12 @@ public class InventoryAlchemicalBag implements IInventory, INBTTaggable
nbtTagCompound.setTag(Names.NBT.ITEMS, tagList);
}
@Override
public String getTagLabel()
{
return "InventoryAlchemicalBag";
}
public boolean hasCustomName()
{
return customName != null && customName.length() > 0;

View file

@ -0,0 +1,41 @@
package com.pahimar.ee3.knowledge;
import com.pahimar.ee3.util.SerializationHelper;
import java.io.File;
import java.util.HashMap;
import java.util.UUID;
public class KnowledgeRegistry
{
private static KnowledgeRegistry knowledgeRegistry = null;
private static File knowledgeDirectory;
private static TransmutationKnowledge templateKnowledge;
private static HashMap<UUID, TransmutationKnowledge> playerKnowledgeMap;
private KnowledgeRegistry()
{
knowledgeDirectory = new File(SerializationHelper.getPlayerDataDirectory(), "knowledge");
knowledgeDirectory.mkdirs();
}
public static KnowledgeRegistry getInstance()
{
if (knowledgeRegistry == null)
{
knowledgeRegistry = new KnowledgeRegistry();
}
return knowledgeRegistry;
}
public TransmutationKnowledge getTemplateKnowledge()
{
if (templateKnowledge == null)
{
}
return templateKnowledge;
}
}

View file

@ -141,6 +141,12 @@ public class TransmutationKnowledge implements INBTTaggable
nbtTagCompound.setTag(Names.NBT.ITEM_TRANSMUTATION_KNOWLEDGE, tagList);
}
@Override
public String getTagLabel()
{
return "TransmutationKnowledge";
}
public static TransmutationKnowledge readTransmutationKnowledgeFromNBT(NBTTagCompound nbtTagCompound)
{
TransmutationKnowledge transmutationKnowledge = new TransmutationKnowledge();

View file

@ -7,4 +7,6 @@ public interface INBTTaggable
void readFromNBT(NBTTagCompound nbtTagCompound);
void writeToNBT(NBTTagCompound nbtTagCompound);
String getTagLabel();
}

View file

@ -1,13 +1,11 @@
package com.pahimar.ee3.util;
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;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
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;
@ -20,7 +18,40 @@ import java.util.*;
public class SerializationHelper
{
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(EnergyValueStackMapping.class, new EnergyValueStackMapping()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
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()
{
@ -42,73 +73,13 @@ public class SerializationHelper
return DigestUtils.md5Hex(modListString.toString());
}
public static boolean dataFileExist(String fileName)
public static NBTTagCompound readNBTFromFile(File nbtEncodedFile)
{
if (FMLCommonHandler.instance().getMinecraftServerInstance() == null)
if (nbtEncodedFile.exists() && nbtEncodedFile.isFile())
{
return false;
}
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;
}
File file = new File(dataDirectory, fileName);
return file.exists() && file.isFile();
}
public static void writeEnergyValueRegistryToFile(String fileName)
{
if (FMLCommonHandler.instance().getMinecraftServerInstance() != null)
{
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + "ee3");
if (!dataDirectory.exists())
{
dataDirectory.mkdir();
}
NBTTagCompound energyValueRegistryNBT = new NBTTagCompound();
EnergyValueRegistry.getInstance().writeToNBT(energyValueRegistryNBT);
try
{
File file1 = new File(dataDirectory, fileName + ".tmp");
File file2 = new File(dataDirectory, fileName);
CompressedStreamTools.writeCompressed(energyValueRegistryNBT, new FileOutputStream(file1));
if (file2.exists())
{
file2.delete();
}
file1.renameTo(file2);
LogHelper.info("Successfully saved EnergyValueRegistry to file: " + file2.getPath());
}
catch (Exception exception)
{
LogHelper.warn("Failed to save EnergyValueRegistry to file " + dataDirectory.getPath() + SerializationHelper.getModListMD5() + ".ee3");
}
}
}
public static NBTTagCompound readEnergyValueRegistryFromFile(String fileName)
{
if (dataFileExist(fileName))
{
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + "ee3");
File energyValueRegistryFile = new File(dataDirectory, fileName);
try
{
return CompressedStreamTools.readCompressed(new FileInputStream(energyValueRegistryFile));
return CompressedStreamTools.readCompressed(new FileInputStream(nbtEncodedFile));
}
catch (IOException e)
{
@ -119,6 +90,39 @@ public class SerializationHelper
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));
@ -135,7 +139,7 @@ public class SerializationHelper
jsonReader.beginArray();
while (jsonReader.hasNext())
{
EnergyValueStackMapping energyValueStackMapping = jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
EnergyValueStackMapping energyValueStackMapping = EnergyValueStackMapping.jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue);
}
jsonReader.endArray();
@ -167,7 +171,7 @@ public class SerializationHelper
jsonWriter.beginArray();
for (WrappedStack wrappedStack : energyValueMap.keySet())
{
jsonSerializer.toJson(new EnergyValueStackMapping(wrappedStack, energyValueMap.get(wrappedStack)), EnergyValueStackMapping.class, jsonWriter);
EnergyValueStackMapping.jsonSerializer.toJson(new EnergyValueStackMapping(wrappedStack, energyValueMap.get(wrappedStack)), EnergyValueStackMapping.class, jsonWriter);
}
jsonWriter.endArray();