Progress on the New Save Format and Minor Fixes #88

Merged
SenseiKiwi merged 11 commits from rewrite into DevBranch 2013-09-12 03:14:50 +00:00
12 changed files with 397 additions and 93 deletions
Showing only changes of commit c2fa4964f8 - Show all commits

View File

@@ -89,7 +89,7 @@ public class EventHookContainer
} }
@ForgeSubscribe @ForgeSubscribe
public void onWorldsave(WorldEvent.Save event) public void onWorldSave(WorldEvent.Save event)
{ {
if (event.world.provider.dimensionId == 0) if (event.world.provider.dimensionId == 0)
{ {

View File

@@ -14,6 +14,12 @@ public abstract class DimLink
protected DimLink(Point4D source, DimLink parent) protected DimLink(Point4D source, DimLink parent)
{ {
if (parent.source.getDimension() != source.getDimension())
{
// Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails
throw new IllegalArgumentException("source and parent.source must have the same dimension.");
}
this.parent = parent; this.parent = parent;
this.source = source; this.source = source;
this.tail = parent.tail; this.tail = parent.tail;

View File

@@ -58,12 +58,16 @@ public abstract class NewDimData
{ {
throw new IllegalArgumentException("nextParent cannot be null."); throw new IllegalArgumentException("nextParent cannot be null.");
} }
if (this == nextParent) if (this == nextParent)
{ {
//Ignore this request silently //Ignore this request silently
return false; return false;
} }
if (nextParent.source.getDimension() != source.getDimension())
{
// Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails
throw new IllegalArgumentException("source and parent.source must have the same dimension.");
}
//Release children //Release children
for (DimLink child : children) for (DimLink child : children)
@@ -106,22 +110,22 @@ public abstract class NewDimData
} }
} }
private static Random random = new Random(); protected static Random random = new Random();
private final int id; protected int id;
private final Map<Point4D, InnerDimLink> linkMapping; protected Map<Point4D, InnerDimLink> linkMapping;
private final List<InnerDimLink> linkList; protected List<InnerDimLink> linkList;
private final boolean isDungeon; protected boolean isDungeon;
private boolean isFilled; protected boolean isFilled;
private final int depth; protected int depth;
private int packDepth; protected int packDepth;
private final NewDimData parent; protected NewDimData parent;
private final NewDimData root; protected NewDimData root;
private final List<NewDimData> children; protected List<NewDimData> children;
private Point4D origin; protected Point4D origin;
private int orientation; protected int orientation;
private DungeonData dungeon; protected DungeonData dungeon;
private final IUpdateWatcher<Point4D> linkWatcher; protected IUpdateWatcher<Point4D> linkWatcher;
protected NewDimData(int id, NewDimData parent, boolean isPocket, boolean isDungeon, protected NewDimData(int id, NewDimData parent, boolean isPocket, boolean isDungeon,
IUpdateWatcher<Point4D> linkWatcher) IUpdateWatcher<Point4D> linkWatcher)

View File

@@ -13,6 +13,9 @@ import net.minecraftforge.common.DimensionManager;
import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.DDProperties;
import StevenDimDoors.mod_pocketDim.helpers.Compactor; import StevenDimDoors.mod_pocketDim.helpers.Compactor;
import StevenDimDoors.mod_pocketDim.helpers.DeleteFolder; import StevenDimDoors.mod_pocketDim.helpers.DeleteFolder;
import StevenDimDoors.mod_pocketDim.saving.DDSaveHandler;
import StevenDimDoors.mod_pocketDim.saving.IPackable;
import StevenDimDoors.mod_pocketDim.saving.PackedDimData;
import StevenDimDoors.mod_pocketDim.util.Point4D; import StevenDimDoors.mod_pocketDim.util.Point4D;
import StevenDimDoors.mod_pocketDim.watcher.ClientDimData; import StevenDimDoors.mod_pocketDim.watcher.ClientDimData;
import StevenDimDoors.mod_pocketDim.watcher.IUpdateSource; import StevenDimDoors.mod_pocketDim.watcher.IUpdateSource;
@@ -25,7 +28,7 @@ import StevenDimDoors.mod_pocketDim.watcher.UpdateWatcherProxy;
*/ */
public class PocketManager public class PocketManager
{ {
private static class InnerDimData extends NewDimData private static class InnerDimData extends NewDimData implements IPackable<PackedDimData>
{ {
// This class allows us to instantiate NewDimData indirectly without exposing // This class allows us to instantiate NewDimData indirectly without exposing
// a public constructor from NewDimData. It's meant to stop us from constructing // a public constructor from NewDimData. It's meant to stop us from constructing
@@ -43,6 +46,49 @@ public class PocketManager
// This constructor is meant for client-side code only // This constructor is meant for client-side code only
super(id, root); super(id, root);
} }
public void clear()
{
// If this dimension has a parent, remove it from its parent's list of children
if (parent != null)
{
parent.children.remove(this);
}
// Remove this dimension as the parent of its children
for (NewDimData child : children)
{
child.parent = null;
}
// Clear all fields
id = Integer.MIN_VALUE;
linkMapping.clear();
linkMapping = null;
linkList.clear();
linkList = null;
children.clear();
children = null;
isDungeon = false;
isFilled = false;
depth = Integer.MIN_VALUE;
packDepth = Integer.MIN_VALUE;
origin = null;
orientation = Integer.MIN_VALUE;
dungeon = null;
linkWatcher = null;
}
@Override
public String name()
{
return String.valueOf(id);
}
@Override
public PackedDimData pack()
{
// FIXME: IMPLEMENTATION PLZTHX
return null;
}
} }
private static class ClientLinkWatcher implements IUpdateWatcher<Point4D> private static class ClientLinkWatcher implements IUpdateWatcher<Point4D>
@@ -140,45 +186,45 @@ public class PocketManager
isLoading = false; isLoading = false;
} }
public boolean clearPocket(NewDimData dimension) public boolean resetDungeon(NewDimData target)
{ {
if (!dimension.isPocketDimension() || DimensionManager.getWorld(dimension.id()) != null) // We can't reset the dimension if it's currently loaded or if it's not a dungeon.
// We cast to InnerDimData so that if anyone tries to be a smartass and create their
// own version of NewDimData, this will throw an exception.
InnerDimData dimension = (InnerDimData) target;
if (dimension.isDungeon() && DimensionManager.getWorld(dimension.id()) == null)
{ {
File saveDirectory = new File(DimensionManager.getCurrentSaveRootDirectory() + "/DimensionalDoors/pocketDimID" + dimension.id());
if (DeleteFolder.deleteFolder(saveDirectory))
{
dimension.setFilled(false);
return true;
}
}
return false; return false;
} }
File save = new File(DimensionManager.getCurrentSaveRootDirectory() + "/DimensionalDoors/pocketDimID" + dimension.id()); public static boolean deletePocket(NewDimData target, boolean deleteFolder)
DeleteFolder.deleteFolder(save);
dimension.setFilled(false);
//FIXME: Reset door information?
return true;
}
public static boolean deletePocket(NewDimData dimension, boolean deleteFolder)
{ {
//FIXME: Shouldn't the links in and out of this dimension be altered somehow? Otherwise we have links pointing // We can't delete the dimension if it's currently loaded or if it's not actually a pocket.
//into a deleted dimension! // We cast to InnerDimData so that if anyone tries to be a smartass and create their
// own version of NewDimData, this will throw an exception.
//Checks to see if the pocket is loaded or isn't actually a pocket. InnerDimData dimension = (InnerDimData) target;
if (dimension.isPocketDimension() && DimensionManager.getWorld(dimension.id()) == null) if (dimension.isPocketDimension() && DimensionManager.getWorld(dimension.id()) == null)
{ {
dimensionData.remove(dimension.id());
DimensionManager.unregisterDimension(dimension.id());
if (deleteFolder) if (deleteFolder)
{ {
File save = new File(DimensionManager.getCurrentSaveRootDirectory() + "/DimensionalDoors/pocketDimID" + dimension.id()); File saveDirectory = new File(DimensionManager.getCurrentSaveRootDirectory() + "/DimensionalDoors/pocketDimID" + dimension.id());
DeleteFolder.deleteFolder(save); DeleteFolder.deleteFolder(saveDirectory);
} }
dimensionData.remove(dimension.id());
// Raise the dim deleted event // Raise the dim deleted event
dimWatcher.onDeleted(new ClientDimData(dimension)); dimWatcher.onDeleted(new ClientDimData(dimension));
//dimension.implode()??? -- more like delete, but yeah dimension.clear();
return true; return true;
} }
else
{
return false; return false;
} }
}
private static void registerPockets(DDProperties properties) private static void registerPockets(DDProperties properties)
{ {
@@ -223,31 +269,28 @@ public class PocketManager
*/ */
private static void loadInternal() private static void loadInternal()
{ {
// SenseiKiwi: This is a temporary function for testing purposes.
// We'll move on to using a text-based format in the future.
if (!DimensionManager.getWorld(OVERWORLD_DIMENSION_ID).isRemote && if (!DimensionManager.getWorld(OVERWORLD_DIMENSION_ID).isRemote &&
DimensionManager.getCurrentSaveRootDirectory() != null) DimensionManager.getCurrentSaveRootDirectory() != null)
{ {
System.out.println("Loading Dimensional Doors save data..."); // Load and register blacklisted dimension IDs
/*File saveFile = new File(DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.dat");
setState(saveData);*/ // Load save data
System.out.println("Loading Dimensional Doors save data...");
if (DDSaveHandler.loadAll())
{
System.out.println("Loaded successfully!"); System.out.println("Loaded successfully!");
} }
} }
}
public static void save() public static void save()
{ {
// SenseiKiwi: This is a temporary function for testing purposes.
// We'll move on to using a text-based format in the future.
if (!isLoaded) if (!isLoaded)
{ {
return; return;
} }
World world = DimensionManager.getWorld(OVERWORLD_DIMENSION_ID); World world = DimensionManager.getWorld(OVERWORLD_DIMENSION_ID);
if (world == null || world.isRemote || DimensionManager.getCurrentSaveRootDirectory() != null) if (world == null || world.isRemote || DimensionManager.getCurrentSaveRootDirectory() == null)
{ {
return; return;
} }
@@ -256,30 +299,23 @@ public class PocketManager
{ {
return; return;
} }
isSaving = true; isSaving = true;
try try
{ {
System.out.println("Writing Dimensional Doors save data..."); System.out.println("Writing Dimensional Doors save data...");
/*String tempPath = DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.tmp"; if ( DDSaveHandler.saveAll(dimensionData.values()) )
String savePath = DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.dat"; {
File tempFile = new File(tempPath);
File saveFile = new File(savePath);
DataOutputStream writer = new DataOutputStream(new FileOutputStream(tempFile));
getState().writeToStream(writer);
writer.close();
saveFile.delete();
tempFile.renameTo(saveFile);*/
System.out.println("Saved successfully!"); System.out.println("Saved successfully!");
} }
/*catch (FileNotFoundException e)
{
e.printStackTrace();
} }
catch (IOException e) catch (Exception e)
{ {
e.printStackTrace(); // Wrap the exception in a RuntimeException so functions that call
}*/ // PocketManager.save() don't need to catch it. We want MC to
// crash if something really bad happens rather than ignoring it!
throw new RuntimeException(e);
}
finally finally
{ {
isSaving = false; isSaving = false;

View File

@@ -33,29 +33,11 @@ import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPackConfigReader;
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType; import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType;
import StevenDimDoors.mod_pocketDim.items.ItemDimensionalDoor; import StevenDimDoors.mod_pocketDim.items.ItemDimensionalDoor;
import StevenDimDoors.mod_pocketDim.util.ConfigurationProcessingException; import StevenDimDoors.mod_pocketDim.util.ConfigurationProcessingException;
import StevenDimDoors.mod_pocketDim.util.FileFilters;
import StevenDimDoors.mod_pocketDim.util.WeightedContainer; import StevenDimDoors.mod_pocketDim.util.WeightedContainer;
public class DungeonHelper public class DungeonHelper
{ {
//TODO: File-handling functionality should be spun off to a helper class later
private static class DirectoryFilter implements FileFilter
{
@Override
public boolean accept(File file)
{
return file.isDirectory();
}
}
private static class SchematicFileFilter implements FileFilter
{
@Override
public boolean accept(File file)
{
return file.isFile() && file.getName().endsWith(SCHEMATIC_FILE_EXTENSION);
}
}
private static DungeonHelper instance = null; private static DungeonHelper instance = null;
private static DDProperties properties = null; private static DDProperties properties = null;
@@ -387,7 +369,7 @@ public class DungeonHelper
File[] packFiles; File[] packFiles;
ArrayList<String> packFilePaths; ArrayList<String> packFilePaths;
File directory = new File(path); File directory = new File(path);
SchematicFileFilter schematicFileFilter = new SchematicFileFilter(); FileFilter schematicFileFilter = new FileFilters.FileExtensionFilter(SCHEMATIC_FILE_EXTENSION);
//Check that the Ruins pack has been loaded //Check that the Ruins pack has been loaded
if (RuinsPack == null) if (RuinsPack == null)
@@ -411,7 +393,7 @@ public class DungeonHelper
schematics = null; //Release memory schematics = null; //Release memory
//Load the custom dungeon packs //Load the custom dungeon packs
packDirectories = directory.listFiles(new DirectoryFilter()); packDirectories = directory.listFiles(new FileFilters.DirectoryFilter());
if (packDirectories != null) if (packDirectories != null)
{ {
//Loop through each directory, which is assumed to be a dungeon pack //Loop through each directory, which is assumed to be a dungeon pack

View File

@@ -0,0 +1,136 @@
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.util.FileFilters;
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);
}
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 = 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, false);
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());
}
}
}
}

View File

@@ -0,0 +1,28 @@
package StevenDimDoors.mod_pocketDim.saving;
import java.io.InputStream;
import java.io.OutputStream;
import StevenDimDoors.mod_pocketDim.util.BaseConfigurationProcessor;
import StevenDimDoors.mod_pocketDim.util.ConfigurationProcessingException;
public class DimDataProcessor extends BaseConfigurationProcessor<PackedDimData>
{
@Override
public PackedDimData readFromStream(InputStream inputStream)
throws ConfigurationProcessingException
{
// TODO Auto-generated method stub
return null;
}
@Override
public void writeToStream(OutputStream outputStream, PackedDimData data)
throws ConfigurationProcessingException
{
// TODO Auto-generated method stub
}
}

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,42 @@
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 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 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, 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;
Origin = origin;
ChildIDs = childIDs;
Links = links;
Tails = tails;
}
}

View File

@@ -0,0 +1,6 @@
package StevenDimDoors.mod_pocketDim.saving;
public class PackedLinkData
{
}

View File

@@ -0,0 +1,6 @@
package StevenDimDoors.mod_pocketDim.saving;
public class PackedLinkTail
{
}

View File

@@ -0,0 +1,51 @@
package StevenDimDoors.mod_pocketDim.util;
import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;
public class FileFilters
{
private FileFilters() { }
public static class DirectoryFilter implements FileFilter
{
@Override
public boolean accept(File file)
{
return file.isDirectory();
}
}
public static class FileExtensionFilter implements FileFilter
{
private final String extension;
public FileExtensionFilter(String extension)
{
this.extension = extension;
}
@Override
public boolean accept(File file)
{
return file.isFile() && file.getName().endsWith(extension);
}
}
public static class RegexFileFilter implements FileFilter
{
private final Pattern pattern;
public RegexFileFilter(String expression)
{
this.pattern = Pattern.compile(expression);
}
@Override
public boolean accept(File file)
{
return file.isFile() && pattern.matcher(file.getName()).matches();
}
}
}