Emagherd work on the 1.7.10 version!

This commit is contained in:
Pahimar 2016-05-11 15:59:52 -04:00
parent f6102b21da
commit 3a29be6d9a
9 changed files with 206 additions and 44 deletions

View file

@ -4,6 +4,7 @@ import com.pahimar.ee3.array.AlchemyArrayRegistry;
import com.pahimar.ee3.command.CommandEE; import com.pahimar.ee3.command.CommandEE;
import com.pahimar.ee3.exchange.CachedOreDictionary; import com.pahimar.ee3.exchange.CachedOreDictionary;
import com.pahimar.ee3.exchange.EnergyValueRegistry; import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.NewEnergyValueRegistry;
import com.pahimar.ee3.handler.*; import com.pahimar.ee3.handler.*;
import com.pahimar.ee3.init.*; import com.pahimar.ee3.init.*;
import com.pahimar.ee3.knowledge.AbilityRegistry; import com.pahimar.ee3.knowledge.AbilityRegistry;
@ -30,6 +31,7 @@ import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.GameRegistry;
import java.io.File; import java.io.File;
import java.io.IOException;
@Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, certificateFingerprint = Reference.FINGERPRINT, version = Reference.MOD_VERSION, dependencies = Reference.DEPENDENCIES, guiFactory = Reference.GUI_FACTORY_CLASS) @Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, certificateFingerprint = Reference.FINGERPRINT, version = Reference.MOD_VERSION, dependencies = Reference.DEPENDENCIES, guiFactory = Reference.GUI_FACTORY_CLASS)
public class EquivalentExchange3 public class EquivalentExchange3
@ -117,6 +119,12 @@ public class EquivalentExchange3
{ {
CachedOreDictionary.getInstance(); CachedOreDictionary.getInstance();
Abilities.initNotLearnables(); Abilities.initNotLearnables();
try {
NewEnergyValueRegistry.INSTANCE.save();
} catch (IOException e) {
e.printStackTrace();
}
} }
@EventHandler @EventHandler

View file

@ -52,6 +52,7 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
energyValueRegistry = new EnergyValueRegistry(); energyValueRegistry = new EnergyValueRegistry();
} }
return energyValueRegistry; return energyValueRegistry;
} }
@ -692,7 +693,7 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
energyValuesDataDirectory.mkdirs(); energyValuesDataDirectory.mkdirs();
if (shouldRegenNextRestart) { if (shouldRegenNextRestart) {
File staticEnergyValuesJsonFile = new File(energyValuesDataDirectory, Files.STATIC_ENERGY_VALUES_JSON); File staticEnergyValuesJsonFile = new File(energyValuesDataDirectory, Files.ENERGY_VALUES_JSON);
File md5EnergyValuesJsonFile = new File(energyValuesDataDirectory, SerializationHelper.getModListMD5() + ".json"); File md5EnergyValuesJsonFile = new File(energyValuesDataDirectory, SerializationHelper.getModListMD5() + ".json");
// JSON // JSON
@ -705,21 +706,31 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
shouldRegenNextRestart = false; shouldRegenNextRestart = false;
} else { } else {
SerializationHelper.compressEnergyValueStackMapToFile(new File(energyValuesDataDirectory, Files.STATIC_ENERGY_VALUES_JSON), energyValueRegistry.stackMappings); SerializationHelper.compressEnergyValueStackMapToFile(new File(energyValuesDataDirectory, Files.ENERGY_VALUES_JSON), energyValueRegistry.stackMappings);
SerializationHelper.compressEnergyValueStackMapToFile(new File(energyValuesDataDirectory, SerializationHelper.getModListMD5() + ".json.gz"), energyValueRegistry.stackMappings); SerializationHelper.compressEnergyValueStackMapToFile(new File(energyValuesDataDirectory, SerializationHelper.getModListMD5() + ".json.gz"), energyValueRegistry.stackMappings);
} }
} }
public boolean loadFromFile(File energyValueFile) {
if (energyValueFile != null) {
LogHelper.info(ENERGY_VALUE_MARKER, "Attempting to load energy values from file: {}", energyValueFile.getAbsolutePath());
}
return false;
}
public boolean loadEnergyValueRegistryFromFile() { public boolean loadEnergyValueRegistryFromFile() {
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues"); File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
energyValuesDataDirectory.mkdirs(); energyValuesDataDirectory.mkdirs();
File staticEnergyValuesFile = new File(energyValuesDataDirectory, Files.STATIC_ENERGY_VALUES_JSON); File staticEnergyValuesFile = new File(energyValuesDataDirectory, Files.ENERGY_VALUES_JSON);
File md5EnergyValuesFile = new File(energyValuesDataDirectory, SerializationHelper.getModListMD5() + ".json.gz"); File md5EnergyValuesFile = new File(energyValuesDataDirectory, SerializationHelper.getModListMD5() + ".json.gz");
Map<WrappedStack, EnergyValue> stackValueMap = null; Map<WrappedStack, EnergyValue> stackValueMap = null;
loadFromFile(new File(Files.Global.dataDirectory, Files.ENERGY_VALUES_JSON));
if (!Settings.DynamicEnergyValueGeneration.regenerateEnergyValuesWhen.equalsIgnoreCase("Always")) { if (!Settings.DynamicEnergyValueGeneration.regenerateEnergyValuesWhen.equalsIgnoreCase("Always")) {
if (Settings.DynamicEnergyValueGeneration.regenerateEnergyValuesWhen.equalsIgnoreCase("When Mods Change")) { if (Settings.DynamicEnergyValueGeneration.regenerateEnergyValuesWhen.equalsIgnoreCase("When Mods Change")) {
if (md5EnergyValuesFile.exists()) { if (md5EnergyValuesFile.exists()) {

View file

@ -0,0 +1,103 @@
package com.pahimar.ee3.exchange;
import com.google.common.reflect.TypeToken;
import com.google.gson.*;
import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.reference.Files;
import com.pahimar.ee3.util.LogHelper;
import net.minecraft.init.Items;
import net.minecraft.nbt.NBTTagCompound;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.TreeMap;
public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegistry>, JsonDeserializer<NewEnergyValueRegistry> {
public static final Marker ENERGY_VALUE_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE", LogHelper.MOD_MARKER);
public static final NewEnergyValueRegistry INSTANCE = new NewEnergyValueRegistry();
private 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;
private NewEnergyValueRegistry() {
preCalculationMappings = new TreeMap<>();
// Loading up some dummy values for testing serialization
preCalculationMappings.put(WrappedStack.wrap(Items.apple), new EnergyValue(1));
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));
postCalculationMappings = new TreeMap<>();
}
public String toJson() {
// FIXME This shouldn't be a new object all the time - we should be using an instance of a single serializer
return new GsonBuilder().setPrettyPrinting().registerTypeAdapter(NewEnergyValueRegistry.class, new NewEnergyValueRegistry()).create().toJson(this);
}
/**
* @see net.minecraft.nbt.CompressedStreamTools#safeWrite(NBTTagCompound, File)
*/
public void save() throws IOException {
// TODO We should probably keep the references to the energy value data dir and file in this class and not in Files. You know, anti-patterns and stuff.
File file = Files.energyValuesDataFile;
File tempFile = new File(file.getAbsolutePath() + "_tmp");
if (tempFile.exists()) {
tempFile.delete();
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile));
bufferedWriter.write(this.toJson());
bufferedWriter.close();
if (file.exists()) {
file.delete();
}
if (file.exists()) {
throw new IOException("Failed to delete " + file);
}
else {
tempFile.renameTo(file);
}
}
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, ENERGY_VALUE_MAP_TYPE)); // TODO String constant for property name
jsonRegistry.add("post_calculation_value_mappings", context.serialize(src.postCalculationMappings, ENERGY_VALUE_MAP_TYPE)); // TODO String constant for property name
return jsonRegistry;
}
}

View file

@ -11,6 +11,8 @@ import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext; import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.io.IOException;
public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageSetEnergyValue, IMessage> public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageSetEnergyValue, IMessage>
{ {
public EnergyValueStackMapping energyValueStackMapping; public EnergyValueStackMapping energyValueStackMapping;
@ -37,7 +39,12 @@ public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageS
if (compressedEnergyValueStackMapping != null) if (compressedEnergyValueStackMapping != null)
{ {
String decompressedEnergyValueStackMapping = CompressionHelper.decompressStringFromByteArray(compressedEnergyValueStackMapping); String decompressedEnergyValueStackMapping = null;
try {
decompressedEnergyValueStackMapping = CompressionHelper.decompress(compressedEnergyValueStackMapping);
} catch (IOException e) {
e.printStackTrace();
}
this.energyValueStackMapping = EnergyValueStackMapping.createFromJson(decompressedEnergyValueStackMapping); this.energyValueStackMapping = EnergyValueStackMapping.createFromJson(decompressedEnergyValueStackMapping);
} }
} }
@ -51,7 +58,11 @@ public class MessageSetEnergyValue implements IMessage, IMessageHandler<MessageS
if (jsonEnergyValueStackMapping != null) if (jsonEnergyValueStackMapping != null)
{ {
compressedBytes = CompressionHelper.compressStringToByteArray(jsonEnergyValueStackMapping); try {
compressedBytes = CompressionHelper.compress(jsonEnergyValueStackMapping);
} catch (IOException e) {
e.printStackTrace();
}
} }
if (compressedBytes != null) if (compressedBytes != null)

View file

@ -48,7 +48,11 @@ public class MessageSyncEnergyValues implements IMessage, IMessageHandler<Messag
if (compressedBytes != null) if (compressedBytes != null)
{ {
this.jsonEnergyValueRegistry = CompressionHelper.decompressStringFromByteArray(compressedBytes); try {
this.jsonEnergyValueRegistry = CompressionHelper.decompress(compressedBytes);
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
@ -64,7 +68,11 @@ public class MessageSyncEnergyValues implements IMessage, IMessageHandler<Messag
if (jsonEnergyValueRegistry != null) if (jsonEnergyValueRegistry != null)
{ {
compressedBytes = CompressionHelper.compressStringToByteArray(jsonEnergyValueRegistry); try {
compressedBytes = CompressionHelper.compress(jsonEnergyValueRegistry);
} catch (IOException e) {
e.printStackTrace();
}
} }
if (compressedBytes != null) if (compressedBytes != null)

View file

@ -12,6 +12,7 @@ import io.netty.buffer.ByteBuf;
import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHandler<MessageTransmutationKnowledgeUpdate, IMessage> public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHandler<MessageTransmutationKnowledgeUpdate, IMessage>
@ -82,7 +83,12 @@ public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHa
if (compressedString != null) if (compressedString != null)
{ {
String uncompressedString = CompressionHelper.decompressStringFromByteArray(compressedString); String uncompressedString = null;
try {
uncompressedString = CompressionHelper.decompress(compressedString);
} catch (IOException e) {
e.printStackTrace();
}
this.transmutationKnowledge = TransmutationKnowledge.createFromJson(uncompressedString); this.transmutationKnowledge = TransmutationKnowledge.createFromJson(uncompressedString);
} }
} }
@ -98,7 +104,11 @@ public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHa
if (transmutationKnowledge != null) if (transmutationKnowledge != null)
{ {
compressedString = CompressionHelper.compressStringToByteArray(transmutationKnowledge.toJson()); try {
compressedString = CompressionHelper.compress(transmutationKnowledge.toJson());
} catch (IOException e) {
e.printStackTrace();
}
} }
if (compressedString != null) if (compressedString != null)

View file

@ -4,13 +4,38 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import java.io.File; import java.io.File;
public class Files public class Files {
{
public static final String PRE_CALCULATION_ENERGY_VALUES = "pre-calculation-energy-values.json"; public static final String PRE_CALCULATION_ENERGY_VALUES = "pre-calculation-energy-values.json";
public static final String POST_CALCULATION_ENERGY_VALUES = "post-calculation-energy-values.json"; public static final String POST_CALCULATION_ENERGY_VALUES = "post-calculation-energy-values.json";
public static final String TEMPLATE_JSON_FILE = "template.json"; public static final String TEMPLATE_JSON_FILE = "template.json";
public static final String ABILITIES_JSON_FILE = "abilities.json"; public static final String ABILITIES_JSON_FILE = "abilities.json";
public static final String STATIC_ENERGY_VALUES_JSON = "energy-values.json.gz"; public static final String ENERGY_VALUES_JSON = "energy-values.json";
public static File dataDirectory;
public static File energyValuesDataDirectory;
public static File abilitiesDataDirectory;
public static File knowledgeDataDirectory;
public static File energyValuesDataFile;
public static File abilitiesDataFile;
public static File knowledgeDataFile;
public static void init(FMLPreInitializationEvent event) {
dataDirectory = new File(event.getModConfigurationDirectory().getParentFile(), "data" + File.separator + Reference.LOWERCASE_MOD_ID);
energyValuesDataDirectory = new File(dataDirectory, "energy-values");
energyValuesDataDirectory.mkdirs();
energyValuesDataFile = new File(energyValuesDataDirectory, ENERGY_VALUES_JSON);
abilitiesDataDirectory = new File(dataDirectory, "abilities");
abilitiesDataDirectory.mkdirs();
abilitiesDataFile = new File(abilitiesDataDirectory, ABILITIES_JSON_FILE);
knowledgeDataDirectory = new File(dataDirectory, "knowledge");
knowledgeDataDirectory.mkdirs();
knowledgeDataFile = new File(knowledgeDataDirectory, TEMPLATE_JSON_FILE);
}
public static class Global public static class Global
{ {
@ -28,7 +53,7 @@ public class Files
dataDirectory = new File(event.getModConfigurationDirectory().getParentFile(), "data" + File.separator + Reference.LOWERCASE_MOD_ID); dataDirectory = new File(event.getModConfigurationDirectory().getParentFile(), "data" + File.separator + Reference.LOWERCASE_MOD_ID);
dataDirectory.mkdirs(); dataDirectory.mkdirs();
File energyValueDataDirectory = new File(dataDirectory, "energyvalues"); File energyValueDataDirectory = new File(dataDirectory, "energy-values");
energyValueDataDirectory.mkdirs(); energyValueDataDirectory.mkdirs();
preCalcluationEnergyValueFile = new File(energyValueDataDirectory, PRE_CALCULATION_ENERGY_VALUES); preCalcluationEnergyValueFile = new File(energyValueDataDirectory, PRE_CALCULATION_ENERGY_VALUES);
postCalcluationEnergyValueFile = new File(energyValueDataDirectory, POST_CALCULATION_ENERGY_VALUES); postCalcluationEnergyValueFile = new File(energyValueDataDirectory, POST_CALCULATION_ENERGY_VALUES);

View file

@ -1,47 +1,33 @@
package com.pahimar.ee3.util; package com.pahimar.ee3.util;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
public class CompressionHelper public class CompressionHelper
{ {
public static byte[] compressStringToByteArray(String uncompressedString) public static byte[] compress(String uncompressedString) throws IOException {
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream; GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
try gzipOutputStream.write(uncompressedString.getBytes(StandardCharsets.UTF_8));
{ gzipOutputStream.close();
gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
gzipOutputStream.write(uncompressedString.getBytes("UTF-8"));
gzipOutputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return byteArrayOutputStream.toByteArray(); return byteArrayOutputStream.toByteArray();
} }
public static String decompressStringFromByteArray(byte[] compressedString) public static String decompress(byte[] compressedString) throws IOException {
{
StringBuilder stringBuilder = new StringBuilder();
try
{
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedString));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gzipInputStream, "UTF-8"));
String line; GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedString));
while ((line = bufferedReader.readLine()) != null) BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8));
{
stringBuilder.append(line); StringBuilder stringBuilder = new StringBuilder();
} String line;
} while ((line = bufferedReader.readLine()) != null) {
catch (IOException e) stringBuilder.append(line);
{
e.printStackTrace();
} }
return stringBuilder.toString(); return stringBuilder.toString();
} }
} }

View file

@ -270,7 +270,7 @@ public class SerializationHelper
{ {
try try
{ {
byte[] energyValueRegistryArray = CompressionHelper.compressStringToByteArray(EnergyValueRegistry.getInstance().toJson()); byte[] energyValueRegistryArray = CompressionHelper.compress(EnergyValueRegistry.getInstance().toJson());
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file);
fos.write(energyValueRegistryArray); fos.write(energyValueRegistryArray);
fos.close(); fos.close();
@ -301,7 +301,7 @@ public class SerializationHelper
try try
{ {
String jsonEnergyValueStackMap = CompressionHelper.decompressStringFromByteArray(Files.toByteArray(file)); String jsonEnergyValueStackMap = CompressionHelper.decompress(Files.toByteArray(file));
JsonReader jsonReader = new JsonReader(new StringReader(jsonEnergyValueStackMap)); JsonReader jsonReader = new JsonReader(new StringReader(jsonEnergyValueStackMap));
jsonReader.beginArray(); jsonReader.beginArray();
while (jsonReader.hasNext()) while (jsonReader.hasNext())