More serialization work, looking a lot cleaner than before but definitely still some improvements possible
This commit is contained in:
parent
3c28db961c
commit
21e1db1468
|
@ -1,10 +1,10 @@
|
|||
package com.pahimar.ee3;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.pahimar.ee3.array.AlchemyArrayRegistry;
|
||||
import com.pahimar.ee3.command.CommandEE;
|
||||
import com.pahimar.ee3.exchange.*;
|
||||
import com.pahimar.ee3.exchange.CachedOreDictionary;
|
||||
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.NewEnergyValueRegistry;
|
||||
import com.pahimar.ee3.handler.*;
|
||||
import com.pahimar.ee3.init.*;
|
||||
import com.pahimar.ee3.knowledge.AbilityRegistry;
|
||||
|
@ -22,10 +22,6 @@ import com.pahimar.ee3.util.FluidHelper;
|
|||
import com.pahimar.ee3.util.LogHelper;
|
||||
import com.pahimar.ee3.util.SerializationHelper;
|
||||
import com.pahimar.ee3.util.TileEntityDataHelper;
|
||||
import com.pahimar.ee3.util.serialize.FluidStackSerializer;
|
||||
import com.pahimar.ee3.util.serialize.ItemStackSerializer;
|
||||
import com.pahimar.ee3.util.serialize.OreStackSerializer;
|
||||
import com.pahimar.ee3.util.serialize.WrappedStackSerializer;
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.Mod.EventHandler;
|
||||
import cpw.mods.fml.common.Mod.Instance;
|
||||
|
@ -33,8 +29,6 @@ import cpw.mods.fml.common.SidedProxy;
|
|||
import cpw.mods.fml.common.event.*;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
@ -48,13 +42,6 @@ public class EquivalentExchange3
|
|||
@SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS)
|
||||
public static IProxy proxy;
|
||||
|
||||
public static final Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(ItemStack.class, new ItemStackSerializer())
|
||||
.registerTypeAdapter(OreStack.class, new OreStackSerializer())
|
||||
.registerTypeAdapter(FluidStack.class, new FluidStackSerializer())
|
||||
.registerTypeAdapter(WrappedStack.class, new WrappedStackSerializer())
|
||||
.create();
|
||||
|
||||
@EventHandler
|
||||
public void invalidFingerprint(FMLFingerprintViolationEvent event)
|
||||
{
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
package com.pahimar.ee3.exchange;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.*;
|
||||
import com.pahimar.ee3.EquivalentExchange3;
|
||||
import com.pahimar.ee3.api.exchange.EnergyValue;
|
||||
import com.pahimar.ee3.util.LogHelper;
|
||||
import com.pahimar.ee3.util.SerializationHelper;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
|
@ -18,14 +17,14 @@ import java.lang.reflect.Type;
|
|||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegistry>, JsonDeserializer<NewEnergyValueRegistry> {
|
||||
public class NewEnergyValueRegistry {
|
||||
|
||||
public static final Marker ENERGY_VALUE_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE", LogHelper.MOD_MARKER);
|
||||
public static final NewEnergyValueRegistry INSTANCE = new NewEnergyValueRegistry();
|
||||
public static final Type ENERGY_VALUE_MAP_TYPE = new TypeToken<Map<WrappedStack, EnergyValue>>(){}.getType();
|
||||
|
||||
private final Map<WrappedStack, EnergyValue> preCalculationMappings;
|
||||
private final Map<WrappedStack, EnergyValue> postCalculationMappings;
|
||||
public final Map<WrappedStack, EnergyValue> preCalculationMappings;
|
||||
public final Map<WrappedStack, EnergyValue> postCalculationMappings;
|
||||
|
||||
public static File energyValuesDataDirectory;
|
||||
public static File energyValuesDataFile;
|
||||
|
@ -37,12 +36,15 @@ public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegi
|
|||
preCalculationMappings.put(WrappedStack.wrap(Items.arrow), new EnergyValue(2));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.baked_potato), new EnergyValue(3));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.bed), new EnergyValue(4));
|
||||
preCalculationMappings.put(WrappedStack.wrap(new OreStack("oreIron")), new EnergyValue(5));
|
||||
|
||||
|
||||
postCalculationMappings = new TreeMap<>();
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return EquivalentExchange3.GSON.toJson(this);
|
||||
LogHelper.info(SerializationHelper.GSON.toJson(this));
|
||||
return SerializationHelper.GSON.toJson(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -76,29 +78,4 @@ public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegi
|
|||
public void load() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public NewEnergyValueRegistry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
|
||||
if (json.isJsonObject()) {
|
||||
JsonObject jsonRegistry = new JsonObject();
|
||||
|
||||
if (jsonRegistry.getAsJsonObject("pre_calculation_value_mappings") instanceof JsonObject) { // TODO String constant for property name
|
||||
Map<WrappedStack, EnergyValue> tempMap = context.deserialize(jsonRegistry.getAsJsonObject("pre_calculation_value_mappings"), ENERGY_VALUE_MAP_TYPE);
|
||||
this.preCalculationMappings.clear();
|
||||
this.preCalculationMappings.putAll(tempMap);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(NewEnergyValueRegistry src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
||||
JsonObject jsonRegistry = new JsonObject();
|
||||
jsonRegistry.add("pre_calculation_value_mappings", context.serialize(src.preCalculationMappings)); // TODO String constant for property name
|
||||
jsonRegistry.add("post_calculation_value_mappings", context.serialize(src.postCalculationMappings)); // TODO String constant for property name
|
||||
return jsonRegistry;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,42 @@
|
|||
package com.pahimar.ee3.util;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
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.exchange.EnergyValue;
|
||||
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
|
||||
import com.pahimar.ee3.exchange.OreStack;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
|
||||
import com.pahimar.ee3.reference.Reference;
|
||||
import com.pahimar.ee3.util.serialize.FluidStackSerializer;
|
||||
import com.pahimar.ee3.util.serialize.ItemStackSerializer;
|
||||
import com.pahimar.ee3.util.serialize.OreStackSerializer;
|
||||
import com.pahimar.ee3.util.serialize.WrappedStackSerializer;
|
||||
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 net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class SerializationHelper
|
||||
{
|
||||
public class SerializationHelper {
|
||||
|
||||
public static final Gson GSON = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.enableComplexMapKeySerialization()
|
||||
.registerTypeAdapter(ItemStack.class, new ItemStackSerializer())
|
||||
.registerTypeAdapter(OreStack.class, new OreStackSerializer())
|
||||
.registerTypeAdapter(FluidStack.class, new FluidStackSerializer())
|
||||
.registerTypeAdapter(WrappedStack.class, new WrappedStackSerializer())
|
||||
.create();
|
||||
|
||||
private static File instanceDataDirectory;
|
||||
private static File instancePlayerDataDirectory;
|
||||
|
||||
|
@ -76,66 +92,6 @@ public class SerializationHelper
|
|||
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)
|
||||
{
|
||||
writeNBTToFile(directory, fileName, nbtTaggable, false);
|
||||
}
|
||||
|
||||
public static void writeNBTToFile(File directory, String fileName, INBTTaggable nbtTaggable, boolean verboseLogging)
|
||||
{
|
||||
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);
|
||||
|
||||
if (verboseLogging)
|
||||
{
|
||||
LogHelper.info("Successfully saved {} to file: {}", nbtTaggable.getTagLabel(), file2.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
LogHelper.warn("Failed to save {} to file: {}{}{}", nbtTaggable.getTagLabel(), directory.getAbsolutePath(), File.separator, fileName);
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static TransmutationKnowledge readTransmutationKnowledgeFromFile(File directory, String fileName)
|
||||
{
|
||||
if (!directory.exists())
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.pahimar.ee3.util.serialize;
|
|||
import com.google.gson.*;
|
||||
import com.pahimar.ee3.exchange.OreStack;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import com.pahimar.ee3.util.SerializationHelper;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
|
@ -17,12 +18,11 @@ public class WrappedStackSerializer implements JsonSerializer<WrappedStack>, Jso
|
|||
|
||||
if (json.isJsonObject()) {
|
||||
|
||||
JsonObject jsonObject = (JsonObject) json;
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
|
||||
String type = null;
|
||||
int stackSize = Integer.MIN_VALUE;
|
||||
String data = null;
|
||||
|
||||
JsonObject data = null;
|
||||
|
||||
try {
|
||||
if (jsonObject.get("type").getAsJsonPrimitive().isString()) {
|
||||
|
@ -41,8 +41,8 @@ public class WrappedStackSerializer implements JsonSerializer<WrappedStack>, Jso
|
|||
}
|
||||
|
||||
try {
|
||||
if (jsonObject.get("data").getAsJsonPrimitive().isString()) {
|
||||
data = jsonObject.get("data").getAsString();
|
||||
if (jsonObject.get("data").isJsonObject()) {
|
||||
data = jsonObject.getAsJsonObject("data");
|
||||
}
|
||||
}
|
||||
catch (IllegalStateException exception) {
|
||||
|
@ -50,8 +50,28 @@ public class WrappedStackSerializer implements JsonSerializer<WrappedStack>, Jso
|
|||
|
||||
if ("itemstack".equalsIgnoreCase(type) || "orestack".equalsIgnoreCase(type) || "fluidstack".equalsIgnoreCase(type)) {
|
||||
if (stackSize >= 1) {
|
||||
// TODO PICK UP HERE
|
||||
// TODO ALSO LOOK INTO MOVING THESE ALL INTO SerializationHelper
|
||||
|
||||
if ("itemstack".equalsIgnoreCase(type)) {
|
||||
ItemStack itemStack = SerializationHelper.GSON.fromJson(data, ItemStack.class);
|
||||
|
||||
if (itemStack != null) {
|
||||
return WrappedStack.wrap(itemStack, stackSize);
|
||||
}
|
||||
}
|
||||
else if ("orestack".equalsIgnoreCase(type)) {
|
||||
OreStack oreStack = SerializationHelper.GSON.fromJson(data, OreStack.class);
|
||||
|
||||
if (oreStack != null) {
|
||||
return WrappedStack.wrap(oreStack, stackSize);
|
||||
}
|
||||
}
|
||||
else if ("fluidstack".equalsIgnoreCase(type)) {
|
||||
FluidStack fluidStack = SerializationHelper.GSON.fromJson(data, FluidStack.class);
|
||||
|
||||
if (fluidStack != null) {
|
||||
return WrappedStack.wrap(fluidStack, stackSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue