THE UPDATE

Merging months of dev work into master. The update is playable, but
untested.
This commit is contained in:
StevenRS11
2013-11-05 18:15:23 -05:00
parent be89913263
commit a04a266c17
154 changed files with 8826 additions and 8272 deletions

View File

@@ -0,0 +1,269 @@
package StevenDimDoors.mod_pocketDim.saving;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.minecraftforge.common.DimensionManager;
import StevenDimDoors.mod_pocketDim.Point3D;
import StevenDimDoors.mod_pocketDim.core.DimLink;
import StevenDimDoors.mod_pocketDim.core.NewDimData;
import StevenDimDoors.mod_pocketDim.core.PocketManager;
import StevenDimDoors.mod_pocketDim.dungeon.DungeonData;
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack;
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType;
import StevenDimDoors.mod_pocketDim.helpers.DungeonHelper;
import StevenDimDoors.mod_pocketDim.util.FileFilters;
import StevenDimDoors.mod_pocketDim.util.Point4D;
import com.google.common.io.Files;
public class DDSaveHandler
{
public static boolean loadAll()
{
// SenseiKiwi: Loading up our save data is not as simple as just reading files.
// To properly restore dimensions, we need to make sure we always load
// a dimension's parent and root before trying to load it. We'll use
// topological sorting to determine the order in which to recreate the
// dimension objects such that we respect those dependencies.
// Links must be loaded after instantiating all the dimensions and must
// be checked against our dimension blacklist.
// Don't surround this code with try-catch. Our mod should crash if an error
// occurs at this level, since it could lead to some nasty problems.
String basePath = DimensionManager.getCurrentSaveRootDirectory() + "/DimensionalDoors/data/";
File dataDirectory = new File(basePath);
// Check if the folder exists. If it doesn't, just return.
if (!dataDirectory.exists())
{
return true;
}
// Load the dimension blacklist
// --insert code here--
// List any dimension data files and read each dimension
DimDataProcessor reader = new DimDataProcessor();
List<PackedDimData> packedDims = new ArrayList<PackedDimData>();
FileFilter dataFileFilter = new FileFilters.RegexFileFilter("dim_-?\\d+\\.txt");
File[] dataFiles = dataDirectory.listFiles(dataFileFilter);
for (File dataFile : dataFiles)
{
PackedDimData packedDim = readDimension(dataFile, reader);
packedDims.add(packedDim);
}
List<PackedLinkData> linksToUnpack = new ArrayList<PackedLinkData>();
//get the grand list of all links to unpack
for(PackedDimData packedDim : packedDims)
{
linksToUnpack.addAll(packedDim.Links);
}
return unpackDimData(packedDims)&&unpackLinkData(linksToUnpack);
}
/**
* Takes a list of packedDimData and rebuilds the DimData for it
* @param packedDims
* @return
*/
public static boolean unpackDimData(List<PackedDimData> packedDims)
{
List<PackedDimData> unpackedDims = new ArrayList<PackedDimData>();
//Load roots
for(PackedDimData packedDim : packedDims)
{
if(packedDim.ParentID==packedDim.ID)
{
PocketManager.registerPackedDimData(packedDim);
unpackedDims.add(packedDim);
}
}
packedDims.removeAll(unpackedDims);
unpackedDims.clear();
//Load the pockets
while(!packedDims.isEmpty())
{
for(PackedDimData packedDim : packedDims)
{
if(PocketManager.isRegisteredInternally(packedDim.ParentID))
{
PocketManager.registerPackedDimData(packedDim);
unpackedDims.add(packedDim);
}
else
{
//break here gracefully
}
}
packedDims.removeAll(unpackedDims);
unpackedDims.clear();
}
return true;
}
public static boolean unpackLinkData(List<PackedLinkData> linksToUnpack)
{
Point3D fakePoint = new Point3D(-1,-1,-1);
List<PackedLinkData> unpackedLinks = new ArrayList<PackedLinkData>();
/**
* sort through the list, unpacking links that do not have parents.
*/
//TODO- what we have a loop of links?
for(PackedLinkData packedLink : linksToUnpack)
{
if(packedLink.parent.equals(fakePoint))
{
NewDimData data = PocketManager.getDimensionData(packedLink.source.getDimension());
DimLink link = data.createLink(packedLink.source, packedLink.tail.linkType, packedLink.orientation);
Point4D destination = packedLink.tail.destination;
if(destination!=null)
{
PocketManager.getDimensionData(destination.getDimension()).setDestination(link, destination.getX(),destination.getY(),destination.getZ());
}
unpackedLinks.add(packedLink);
}
}
linksToUnpack.removeAll(unpackedLinks);
//unpack remaining children
while(!linksToUnpack.isEmpty())
{
for(PackedLinkData packedLink : linksToUnpack)
{
NewDimData data = PocketManager.getDimensionData(packedLink.source.getDimension());
if(data.getLink(packedLink.parent)!=null)
{
data.createChildLink(packedLink.source.getX(), packedLink.source.getY(), packedLink.source.getZ(), data.getLink(packedLink.parent));
}
unpackedLinks.add(packedLink);
}
linksToUnpack.removeAll(unpackedLinks);
}
return true;
}
private static PackedDimData readDimension(File dataFile, DimDataProcessor reader)
{
try
{
return reader.readFromFile(dataFile);
}
catch (Exception e)
{
System.err.println("Could not read dimension data from: " + dataFile.getAbsolutePath());
System.err.println("The following error occurred:");
printException(e, false);
return null;
}
}
public static boolean saveAll(Iterable<? extends IPackable<PackedDimData>> dimensions) throws IOException
{
// Create the data directory for our dimensions
// Don't catch exceptions here. If we can't create this folder,
// the mod should crash to let the user know early on.
String basePath = DimensionManager.getCurrentSaveRootDirectory() + "/DimensionalDoors/data/";
File basePathFile = new File(basePath);
Files.createParentDirs(basePathFile);
basePathFile.mkdir();
FileFilter dataFileFilter = new FileFilters.RegexFileFilter("dim_-?\\d+\\.txt");
//TODO Deal with temp files correctly
File[] dataFiles = basePathFile.listFiles(dataFileFilter);
for (File dataFile : dataFiles)
{
dataFile.delete();
}
basePathFile = null;
basePath += "dim_";
boolean succeeded = true;
DimDataProcessor writer = new DimDataProcessor();
for (IPackable<PackedDimData> dimension : dimensions)
{
succeeded &= writeDimension(dimension, writer, basePath);
}
return succeeded;
}
private static boolean writeDimension(IPackable<PackedDimData> dimension, DimDataProcessor writer, String basePath)
{
try
{
File tempFile = new File(basePath + (dimension.name() + ".tmp"));
File saveFile = new File(basePath + (dimension.name() + ".txt"));
writer.writeToFile(tempFile, dimension.pack());
saveFile.delete();
tempFile.renameTo(saveFile);
return true;
}
catch (Exception e)
{
System.err.println("Could not save data for dimension #" + dimension.name() + ". The following error occurred:");
printException(e, true);
return false;
}
}
private static void printException(Exception e, boolean verbose)
{
if (e.getCause() == null)
{
if (verbose)
{
e.printStackTrace();
}
else
{
System.err.println(e.getMessage());
}
}
else
{
System.out.println(e.getMessage());
System.err.println("Caused by an underlying error:");
if (verbose)
{
e.getCause().printStackTrace();
}
else
{
System.err.println(e.getCause().getMessage());
}
}
}
//TODO - make this more robust
public static DungeonData unpackDungeonData(PackedDungeonData packedDungeon)
{
DungeonPack pack;
DungeonType type;
for(DungeonData data : DungeonHelper.instance().getRegisteredDungeons())
{
if(data.schematicName().equals(packedDungeon.SchematicName))
{
//return data;
}
}
return null;
}
}

View File

@@ -0,0 +1,307 @@
package StevenDimDoors.mod_pocketDim.saving;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import scala.Char;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import StevenDimDoors.mod_pocketDim.Point3D;
import StevenDimDoors.mod_pocketDim.util.BaseConfigurationProcessor;
import StevenDimDoors.mod_pocketDim.util.ConfigurationProcessingException;
import StevenDimDoors.mod_pocketDim.util.Point4D;
public class DimDataProcessor extends BaseConfigurationProcessor<PackedDimData>
{
@Override
public PackedDimData readFromStream(InputStream inputStream)
throws ConfigurationProcessingException
{
try
{
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
PackedDimData data = this.createDImDataFromJson(reader);
return data;
}
catch (IOException e)
{
e.printStackTrace();
throw new ConfigurationProcessingException("Could not read packedDimData");
}
}
@Override
public void writeToStream(OutputStream outputStream, PackedDimData data)
throws ConfigurationProcessingException
{
/** Print out dimData using the GSON built in serializer. I dont feel bad doing this because
* 1- We can read it
* 2- We are manually reading the data in.
* 3- The error messages tell us exactly where its failing, so its easy to fix
*/
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setPrettyPrinting().create();
try
{
outputStream.write(gson.toJson(data).getBytes("UTF-8"));
outputStream.close();
}
catch (IOException e)
{
// not sure if this is kosher, we need it to explode, but not by throwing the IO exception.
throw new ConfigurationProcessingException("Incorrectly formatted save data");
}
// TODO Auto-generated method stub
}
/**
* Nightmare method that takes a JsonReader pointed at a serialized instance of PackedDimData
* @param reader
* @return
* @throws IOException
*/
public PackedDimData createDImDataFromJson(JsonReader reader) throws IOException
{
int ID;
boolean IsDungeon;
boolean IsFilled;
int Depth;
int PackDepth;
int ParentID;
int RootID;
PackedDungeonData Dungeon = null;
Point3D Origin;
int Orientation;
List<Integer> ChildIDs;
List<PackedLinkData> Links;
List<PackedLinkTail> Tails = new ArrayList<PackedLinkTail>();
reader.beginObject();
reader.nextName();
if(reader.nextLong()!=PackedDimData.SAVE_DATA_VERSION_ID)
{
throw new IOException("Save data version mismatch");
}
reader.nextName();
ID = reader.nextInt();
reader.nextName();
IsDungeon = reader.nextBoolean();
reader.nextName();
IsFilled = reader.nextBoolean();
reader.nextName();
Depth = reader.nextInt();
reader.nextName();
PackDepth = reader.nextInt();
reader.nextName();
ParentID=reader.nextInt();
reader.nextName();
RootID= reader.nextInt();
if(reader.nextName().equals("DungeonData"))
{
Dungeon = createDungeonDataFromJson(reader);
reader.nextName();
}
Origin = createPointFromJson(reader);
reader.nextName();
Orientation = reader.nextInt();
reader.nextName();
ChildIDs = this.createIntListFromJson(reader);
reader.nextName();
Links = this.createLinksListFromJson(reader);
return new PackedDimData(ID, Depth, PackDepth, ParentID, RootID, Orientation, IsDungeon, IsFilled, Dungeon, Origin, ChildIDs, Links, Tails);
}
private Point3D createPointFromJson(JsonReader reader) throws IOException
{
reader.beginObject();
reader.nextName();
int x = reader.nextInt();
reader.nextName();
int y = reader.nextInt();
reader.nextName();
int z = reader.nextInt();
reader.endObject();
return new Point3D(x,y,z);
}
private Point4D createPoint4DFromJson(JsonReader reader) throws IOException
{
reader.beginObject();
reader.nextName();
int x = reader.nextInt();
reader.nextName();
int y = reader.nextInt();
reader.nextName();
int z = reader.nextInt();
reader.nextName();
int dimension = reader.nextInt();
reader.endObject();
return new Point4D(x,y,z,dimension);
}
private List<Integer> createIntListFromJson(JsonReader reader) throws IOException
{
List<Integer> list = new ArrayList<Integer>();
reader.beginArray();
while(reader.peek()!= JsonToken.END_ARRAY)
{
list.add(reader.nextInt());
}
reader.endArray();
return list;
}
private List<PackedLinkData> createLinksListFromJson(JsonReader reader) throws IOException
{
List<PackedLinkData> list = new ArrayList<PackedLinkData>();
reader.beginArray();
while(reader.peek()!= JsonToken.END_ARRAY)
{
list.add(createLinkDataFromJson(reader));
}
reader.endArray();
return list;
}
private PackedLinkData createLinkDataFromJson(JsonReader reader) throws IOException
{
Point4D source;
Point3D parent;
PackedLinkTail tail;
int orientation;
List<Point3D> children = new ArrayList<Point3D>();
reader.beginObject();
reader.nextName();
source = this.createPoint4DFromJson(reader);
reader.nextName();
parent = this.createPointFromJson(reader);
reader.nextName();
tail = this.createLinkTailFromJson(reader);
reader.nextName();
orientation = reader.nextInt();
reader.nextName();
reader.beginArray();
while(reader.peek() != JsonToken.END_ARRAY)
{
children.add(this.createPointFromJson(reader));
}
reader.endArray();
reader.endObject();
return new PackedLinkData(source, parent, tail, orientation, children);
}
private PackedDungeonData createDungeonDataFromJson(JsonReader reader) throws IOException
{
int Weight;
boolean IsOpen;
boolean IsInternal;
String SchematicPath;
String SchematicName;
String DungeonTypeName;
String DungeonPackName;
reader.beginObject();
JsonToken test = reader.peek();
if(reader.peek() == JsonToken.END_OBJECT)
{
return null;
}
reader.nextName();
Weight=reader.nextInt();
reader.nextName();
IsOpen=reader.nextBoolean();
reader.nextName();
IsInternal=reader.nextBoolean();
reader.nextName();
SchematicPath=reader.nextString();
reader.nextName();
SchematicName=reader.nextString();
reader.nextName();
DungeonTypeName=reader.nextString();
reader.nextName();
DungeonPackName=reader.nextString();
reader.endObject();
return new PackedDungeonData(Weight, IsOpen, IsInternal, SchematicPath, SchematicName, DungeonTypeName, DungeonPackName);
}
private PackedLinkTail createLinkTailFromJson(JsonReader reader) throws IOException
{
Point4D destination=null;
int linkType;
reader.beginObject();
reader.nextName();
JsonToken test =reader.peek();
if(reader.peek()==JsonToken.BEGIN_OBJECT)
{
destination = this.createPoint4DFromJson(reader);
reader.nextName();
}
linkType = reader.nextInt();
reader.endObject();
return new PackedLinkTail(destination, linkType);
}
}

View File

@@ -0,0 +1,7 @@
package StevenDimDoors.mod_pocketDim.saving;
public interface IPackable<T>
{
public String name();
public T pack();
}

View File

@@ -0,0 +1,73 @@
package StevenDimDoors.mod_pocketDim.saving;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import StevenDimDoors.mod_pocketDim.DimData;
import StevenDimDoors.mod_pocketDim.LinkData;
import StevenDimDoors.mod_pocketDim.Point3D;
import StevenDimDoors.mod_pocketDim.ObjectSaveInputStream;
import StevenDimDoors.mod_pocketDim.util.Point4D;
public class OldSaveImporter
{
public static void importOldSave(File file) throws IOException, ClassNotFoundException
{
FileInputStream saveFile = new FileInputStream(file);
ObjectSaveInputStream save = new ObjectSaveInputStream(saveFile);
HashMap comboSave =((HashMap) save.readObject());
save.close();
List<PackedLinkData> allPackedLinks = new ArrayList<PackedLinkData>();
List<PackedDimData> newPackedDimData = new ArrayList<PackedDimData>();
HashMap<Integer, DimData> dimMap;
try
{
dimMap = (HashMap<Integer, DimData>) comboSave.get("dimList");
}
catch(Exception e)
{
System.out.println("Could not import old save data");
return;
}
for(DimData data : dimMap.values())
{
List<PackedLinkData> newPackedLinkData = new ArrayList<PackedLinkData>();
List<Integer> childDims = new ArrayList<Integer>();
for(LinkData link : data.getLinksInDim())
{
Point4D source = new Point4D(link.locXCoord,link.locYCoord,link.locZCoord,link.locDimID);
Point4D destintion = new Point4D(link.destXCoord,link.destYCoord,link.destZCoord,link.destDimID);
PackedLinkTail tail = new PackedLinkTail(destintion, link.linkOrientation);
List<Point3D> children = new ArrayList<Point3D>();
PackedLinkData newPackedLink = new PackedLinkData(source, new Point3D(-1,-1,-1), tail, link.linkOrientation,children);
newPackedLinkData.add(newPackedLink);
allPackedLinks.add(newPackedLink);
}
PackedDimData dim = new PackedDimData(data.dimID, data.depth, data.depth, data.exitDimLink.locDimID, data.exitDimLink.locDimID, 0, data.dungeonGenerator!=null, data.hasBeenFilled, null, new Point3D(0,64,0), childDims, newPackedLinkData, null);
newPackedDimData.add(dim);
DDSaveHandler.unpackDimData(newPackedDimData);
DDSaveHandler.unpackLinkData(allPackedLinks);
}
}
}

View File

@@ -0,0 +1,46 @@
package StevenDimDoors.mod_pocketDim.saving;
import java.util.List;
import StevenDimDoors.mod_pocketDim.Point3D;
public class PackedDimData
{
// These fields will be public since this is a simple data container
public final static long SAVE_DATA_VERSION_ID = 982405775L;
public final long SAVE_DATA_VERSION_ID_INSTANCE = SAVE_DATA_VERSION_ID;
public final int ID;
public final boolean IsDungeon;
public final boolean IsFilled;
public final int Depth;
public final int PackDepth;
public final int ParentID;
public final int RootID;
public final PackedDungeonData DungeonData;
public final Point3D Origin;
public final int Orientation;
public final List<Integer> ChildIDs;
public final List<PackedLinkData> Links;
public final List<PackedLinkTail> Tails;
// FIXME Missing dungeon data, not sure how to include it
public PackedDimData(int id, int depth, int packDepth, int parentID, int rootID, int orientation,
boolean isDungeon, boolean isFilled,PackedDungeonData dungeonData, Point3D origin, List<Integer> childIDs, List<PackedLinkData> links,
List<PackedLinkTail> tails)
{
ID = id;
Depth = depth;
PackDepth = packDepth;
ParentID = parentID;
RootID = rootID;
Orientation = orientation;
IsDungeon = isDungeon;
IsFilled = isFilled;
DungeonData = dungeonData;
Origin = origin;
ChildIDs = childIDs;
Links = links;
Tails = tails;
}
}

View File

@@ -0,0 +1,29 @@
package StevenDimDoors.mod_pocketDim.saving;
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType;
public class PackedDungeonData
{
public final int Weight;
public final boolean IsOpen;
public final boolean IsInternal;
public final String SchematicPath;
public final String SchematicName;
public final String DungeonTypeName;
public final String DungeonPackName;
public PackedDungeonData(int weight, boolean isOpen, boolean isInternal, String schematicPath, String schematicName, String dungeonTypeName, String dungeonPackName)
{
this.Weight= weight;
this.IsOpen=isOpen;
this.IsInternal=isInternal;
this.SchematicName=schematicName;
this.SchematicPath=schematicPath;
this.DungeonTypeName=dungeonTypeName;
this.DungeonPackName=dungeonPackName;
}
}

View File

@@ -0,0 +1,25 @@
package StevenDimDoors.mod_pocketDim.saving;
import java.util.List;
import StevenDimDoors.mod_pocketDim.Point3D;
import StevenDimDoors.mod_pocketDim.core.DimLink;
import StevenDimDoors.mod_pocketDim.util.Point4D;
public class PackedLinkData
{
public final Point4D source;
public final Point3D parent;
public final PackedLinkTail tail;
public final int orientation;
public final List<Point3D> children;
public PackedLinkData(Point4D source, Point3D parent, PackedLinkTail tail, int orientation, List<Point3D> children)
{
this.source=source;
this.parent=parent;
this.tail=tail;
this.orientation=orientation;
this.children=children;
}
}

View File

@@ -0,0 +1,16 @@
package StevenDimDoors.mod_pocketDim.saving;
import StevenDimDoors.mod_pocketDim.util.Point4D;
public class PackedLinkTail
{
public final Point4D destination;
public final int linkType;
public PackedLinkTail(Point4D destination, int linkType)
{
this.destination=destination;
this.linkType=linkType;
}
}