some work on the blueprint database, new unique id concept

This commit is contained in:
Player 2013-11-18 16:06:38 +01:00
parent 7594b70a2e
commit a035a15632
8 changed files with 432 additions and 207 deletions

View file

@ -173,7 +173,8 @@ public class BuildCraftCore {
BCLog.initLog();
BlueprintDatabase.configFolder = evt.getModConfigurationDirectory();
BlueprintDatabase.init(evt.getModConfigurationDirectory());
mainConfiguration = new BuildCraftConfiguration(new File(evt.getModConfigurationDirectory(), "buildcraft/main.conf"));
try {
mainConfiguration.load();

View file

@ -13,12 +13,14 @@ import buildcraft.core.CreativeTabBuildCraft;
import buildcraft.core.ItemBuildCraft;
import buildcraft.core.utils.NBTUtils;
import buildcraft.core.utils.StringUtils;
import java.util.List;
import java.util.UUID;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.builders.blueprints.BlueprintId;
public abstract class ItemBlueprint extends ItemBuildCraft {
public ItemBlueprint(int i) {
@ -39,11 +41,14 @@ public abstract class ItemBlueprint extends ItemBuildCraft {
}
public static Blueprint getBlueprint(ItemStack stack) {
if (stack != null && stack.getItem() instanceof ItemBlueprint) {
NBTTagCompound nbt = NBTUtils.getItemData(stack);
UUID uuid = NBTUtils.readUUID(nbt, "blueprint");
return BlueprintDatabase.getBlueprint(uuid);
}
byte[] idRaw = nbt.getByteArray("blueprint");
BlueprintId id = BlueprintId.fromRawId(idRaw);
if (id == null) {
return null;
} else {
return BlueprintDatabase.get(id);
}
}
}

View file

@ -93,14 +93,14 @@ public class TileArchitect extends TileBuildCraft implements IInventory {
blueprint.anchorOrientation = ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord));
BlueprintDatabase.addBlueprint(blueprint);
BlueprintDatabase.add(blueprint);
setInventorySlotContents(1, blueprint.getBlueprintItem());
setInventorySlotContents(0, null);
}
private Blueprint createMaskBlueprint(Box box) {
Blueprint blueprint = new Blueprint(box.sizeX(), box.sizeY(), box.sizeZ());
Blueprint blueprint = Blueprint.create(box.sizeX(), box.sizeY(), box.sizeZ());
for (int x = box.xMin; x <= box.xMax; ++x) {
for (int y = box.yMin; y <= box.yMax; ++y) {
@ -120,7 +120,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory {
}
private Blueprint createStandardBlueprint(Box box) {
Blueprint blueprint = new Blueprint(box.sizeX(), box.sizeY(), box.sizeZ());
Blueprint blueprint = Blueprint.create(box.sizeX(), box.sizeY(), box.sizeZ());
for (int x = box.xMin; x <= box.xMax; ++x) {
for (int y = box.yMin; y <= box.yMax; ++y) {

View file

@ -13,16 +13,18 @@ import buildcraft.core.inventory.StackHelper;
import buildcraft.core.utils.BCLog;
import buildcraft.core.utils.NBTUtils;
import buildcraft.factory.TileQuarry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
/**
@ -30,42 +32,82 @@ import net.minecraftforge.common.ForgeDirection;
* world.
*
* @author CovertJaguar <http://www.railcraft.info/>
* @author Player
*/
public class Blueprint {
private final String version = "Blueprint-2.0";
private final UUID uuid;
private String name;
private String creator;
private BlueprintMeta meta;
private final Schematic[][][] schematics;
public final int sizeX, sizeY, sizeZ;
public int anchorX, anchorY, anchorZ;
public ForgeDirection anchorOrientation = ForgeDirection.NORTH;
private List<ItemStack> costs;
public Blueprint(int sizeX, int sizeY, int sizeZ) {
this(sizeX, sizeY, sizeZ, UUID.randomUUID());
public static Blueprint create(int sizeX, int sizeY, int sizeZ) {
return new Blueprint(new BlueprintMeta(), sizeX, sizeY, sizeZ);
}
private Blueprint(int sizeX, int sizeY, int sizeZ, UUID uuid) {
this.uuid = uuid;
private Blueprint(BlueprintMeta meta, int sizeX, int sizeY, int sizeZ) {
this.meta = meta;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.sizeZ = sizeZ;
schematics = new Schematic[sizeX][sizeY][sizeZ];
}
protected Blueprint(BlueprintMeta meta, NBTTagCompound nbt) {
this(meta, nbt.getInteger("sizeX"),
nbt.getInteger("sizeY"),
nbt.getInteger("sizeZ"));
anchorX = nbt.getInteger("anchorX");
anchorY = nbt.getInteger("anchorY");
anchorZ = nbt.getInteger("anchorZ");
anchorOrientation = ForgeDirection.getOrientation(nbt.getByte("anchorOrientation"));
NBTTagList blockList = nbt.getTagList("blocks");
for (int i = 0; i < blockList.tagCount(); i++) {
NBTTagCompound blockNBT = (NBTTagCompound) blockList.tagAt(i);
Schematic schematic = Schematic.createSchematicFromNBT(blockNBT);
schematics[schematic.x][schematic.y][schematic.z] = schematic;
}
}
public BlueprintId getId() {
return meta.getId();
}
protected void setId(BlueprintId id) {
meta.setId(id);
}
public String getName() {
return name;
return meta.getName();
}
public void setName(String name) {
this.name = name;
meta.setName(name);
}
/**
* @return the creator
*/
public String getCreator() {
return meta.getCreator();
}
/**
* @param creator the creator to set
*/
public void setCreator(String creator) {
meta.setCreator(creator);
}
private void setSchematic(int x, int y, int z, Schematic schematic) {
if (schematic == null)
return;
if (getId() != null) throw new IllegalStateException("modifying finalized blueprint");
schematic.x = x;
schematic.y = y;
schematic.z = z;
@ -124,10 +166,6 @@ public class Blueprint {
return schematics[x][y][z];
}
public UUID getUUID() {
return uuid;
}
/**
* Returns a list of all blocks in the Blueprint in the order they should be
* built.
@ -174,7 +212,10 @@ public class Blueprint {
}
public void writeToNBT(NBTTagCompound nbt) {
meta.writeToNBT(nbt);
NBTTagList blockList = new NBTTagList();
for (int y = 0; y < sizeY; y++) {
for (int x = 0; x < sizeX; x++) {
for (int z = 0; z < sizeZ; z++) {
@ -186,12 +227,8 @@ public class Blueprint {
}
}
}
nbt.setTag("blocks", blockList);
nbt.setLong("uuidMost", uuid.getMostSignificantBits());
nbt.setLong("uuidLeast", uuid.getLeastSignificantBits());
nbt.setString("name", name);
nbt.setString("version", version);
nbt.setString("creator", creator);
nbt.setInteger("sizeX", sizeX);
nbt.setInteger("sizeY", sizeY);
nbt.setInteger("sizeZ", sizeZ);
@ -201,46 +238,7 @@ public class Blueprint {
nbt.setByte("anchorOrientation", (byte) anchorOrientation.ordinal());
}
public static Blueprint readFromNBT(NBTTagCompound nbt) {
long most = nbt.getLong("uuidMost");
long least = nbt.getLong("uuidLeast");
int sizeX = nbt.getInteger("sizeX");
int sizeY = nbt.getInteger("sizeY");
int sizeZ = nbt.getInteger("sizeZ");
Blueprint blueprint = new Blueprint(sizeX, sizeY, sizeZ, new UUID(most, least));
blueprint.name = nbt.getString("name");
blueprint.creator = nbt.getString("creator");
blueprint.anchorX = nbt.getInteger("anchorX");
blueprint.anchorY = nbt.getInteger("anchorY");
blueprint.anchorZ = nbt.getInteger("anchorZ");
blueprint.anchorOrientation = ForgeDirection.getOrientation(nbt.getByte("anchorOrientation"));
NBTTagList blockList = nbt.getTagList("blocks");
for (int i = 0; i < blockList.tagCount(); i++) {
NBTTagCompound blockNBT = (NBTTagCompound) blockList.tagAt(i);
Schematic schematic = Schematic.createSchematicFromNBT(blockNBT);
blueprint.schematics[schematic.x][schematic.y][schematic.z] = schematic;
}
return blueprint;
}
/**
* @return the creator
*/
public String getCreator() {
return creator;
}
/**
* @param creator the creator to set
*/
public void setCreator(String creator) {
this.creator = creator;
}
public void rotateLeft() {
anchorOrientation = anchorOrientation.getRotation(ForgeDirection.DOWN);
@ -249,7 +247,7 @@ public class Blueprint {
public ItemStack getBlueprintItem() {
ItemStack blueprint = new ItemStack(BuildCraftBuilders.blueprintItem, 1, 1);
NBTTagCompound nbt = NBTUtils.getItemData(blueprint);
NBTUtils.writeUUID(nbt, "blueprint", uuid);
nbt.setByteArray("blueprint", getId().toRawId());
return blueprint;
}
}

View file

@ -8,127 +8,225 @@
*/
package buildcraft.builders.blueprints;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
/**
*
* @author Player
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class BlueprintDatabase {
public static void init(File configDir) {
blueprintFolder = new File(new File(configDir, "buildcraft"), "blueprints");
public static File configFolder;
private static Map<UUID, Blueprint> blueprints = new HashMap<UUID, Blueprint>();
if (!blueprintFolder.exists()) blueprintFolder.mkdirs();
public static Blueprint getBlueprint(UUID uuid) {
if(uuid == null)
return null;
Blueprint blueprint = blueprints.get(uuid);
if (blueprint == null) {
blueprint = loadBlueprint(uuid);
addBlueprint(blueprint);
}
return blueprint;
loadIndex();
}
public static void addBlueprint(Blueprint blueprint) {
if (blueprint == null)
return;
blueprints.put(blueprint.getUUID(), blueprint);
public static Blueprint get(BlueprintId id) {
Blueprint ret = blueprints.get(id);
if (ret == null) {
BlueprintMeta meta = blueprintMetas.get(id);
if (meta == null) return null; // no meta -> no bpt as well
ret = load(meta);
}
private static File getBlueprintFolder() {
File blueprintFolder = new File(configFolder, "buildcraft/blueprints/");
if (!blueprintFolder.exists()) {
blueprintFolder.mkdirs();
}
return blueprintFolder;
return ret;
}
private static String uuidToString(UUID uuid) {
return String.format(Locale.ENGLISH, "%x%x", uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
public static BlueprintId add(Blueprint blueprint) {
BlueprintId id = save(blueprint);
blueprint.setId(id);
blueprints.put(id, blueprint);
return id;
}
public static void saveBlueprint(Blueprint blueprint) {
private static BlueprintId save(Blueprint blueprint) {
NBTTagCompound nbt = new NBTTagCompound();
blueprint.writeToNBT(nbt);
File blueprintFile = new File(getBlueprintFolder(), String.format(Locale.ENGLISH, "%x%x-%s.nbt", uuidToString(blueprint.getUUID()), blueprint.getName()));
if (blueprintFile.exists())
return;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bos);
try {
NBTBase.writeNamedTag(nbt, os);
} catch (IOException e) {
throw new RuntimeException(e);
}
byte[] data = bos.toByteArray();
BlueprintId id = BlueprintId.generate(data);
File blueprintFile = new File(blueprintFolder, String.format(Locale.ENGLISH, "%s-%s.nbt", id.toString(), blueprint.getName()));
if (!blueprintFile.exists()) {
OutputStream gzOs = null;
try {
gzOs = new GZIPOutputStream(new FileOutputStream(blueprintFile));
gzOs.write(data);
CompressedStreamTools.write(nbt, blueprintFile);
} catch (IOException ex) {
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to save Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
} finally {
try {
if (gzOs != null) gzOs.close();
} catch (IOException e) { }
}
}
public static void saveBlueprints() {
for (Blueprint blueprint : blueprints.values()) {
saveBlueprint(blueprint);
}
return id;
}
private static Blueprint loadBlueprint(final UUID uuid) {
private static void loadIndex() {
FilenameFilter filter = new FilenameFilter() {
private String uuidString = uuidToString(uuid);
@Override
public boolean accept(File dir, String name) {
return name.startsWith(uuidString);
return name.endsWith(fileExt);
}
};
NBTTagCompound nbt = null;
File blueprintFolder = getBlueprintFolder();
for (File blueprintFile : blueprintFolder.listFiles(filter)) {
RawBlueprint rawBlueprint = load(blueprintFile);
if (rawBlueprint == null) {
// TODO: delete?
continue;
}
BlueprintMeta meta;
try {
nbt = CompressedStreamTools.read(blueprintFile);
break;
} catch (IOException ex) {
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to load Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
meta = new BlueprintMeta(rawBlueprint.id, rawBlueprint.nbt);
} catch (Exception e) {
// TODO: delete?
continue;
}
// TODO: check if the filename is matching id+name
BlueprintMeta prevValue = blueprintMetas.put(meta.getId(), meta);
if (prevValue != null) {
// TODO: duplicate entry, handle
}
}
}
if (nbt == null) {
private static Blueprint load(final BlueprintMeta meta) {
FilenameFilter filter = new FilenameFilter() {
String prefix = meta.getId().toString();
@Override
public boolean accept(File dir, String name) {
return name.endsWith(fileExt) && name.startsWith(prefix);
}
};
for (File blueprintFile : blueprintFolder.listFiles(filter)) {
RawBlueprint rawBlueprint = load(blueprintFile);
if (rawBlueprint == null) {
continue;
}
Blueprint blueprint;
try {
blueprint = new Blueprint(meta, rawBlueprint.nbt);
} catch (Exception e) {
// TODO: delete?
continue;
}
blueprints.put(blueprint.getId(), blueprint);
return blueprint;
}
return null;
}
return Blueprint.readFromNBT(nbt);
}
public static void loadBlueprints() {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".nbt");
}
};
File blueprintFolder = getBlueprintFolder();
for (File blueprintFile : blueprintFolder.listFiles(filter)) {
private static RawBlueprint load(File file) {
InputStream fileIs = null;
ByteArrayOutputStream decompressedStream;
try {
NBTTagCompound nbt = CompressedStreamTools.read(blueprintFile);
addBlueprint(Blueprint.readFromNBT(nbt));
} catch (IOException ex) {
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to load Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
}
fileIs = new GZIPInputStream(new FileInputStream(file), bufferSize);
decompressedStream = new ByteArrayOutputStream(bufferSize * 4);
byte buffer[] = new byte[bufferSize];
int len;
while ((len = fileIs.read(buffer)) != -1) {
decompressedStream.write(buffer, 0, len);
}
} catch (IOException e) {
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to load Blueprint file: %s %s", file.getName(), e.getMessage()));
return null;
} finally {
try {
fileIs.close();
} catch (IOException e) {}
}
@SideOnly(Side.CLIENT)
public static void sendBlueprintsToServer() {
// TODO
byte[] data = decompressedStream.toByteArray();
BlueprintId id = BlueprintId.generate(data);
DataInputStream dataIs = new DataInputStream(new ByteArrayInputStream(data));
NBTTagCompound nbt;
try {
nbt = CompressedStreamTools.read(dataIs);
} catch (IOException e) {
return null;
}
return new RawBlueprint(id, nbt);
}
private static class RawBlueprint {
RawBlueprint(BlueprintId id, NBTTagCompound nbt) {
this.id = id;
this.nbt = nbt;
}
final BlueprintId id;
final NBTTagCompound nbt;
}
private static final int bufferSize = 8192;
private static final String fileExt = ".bpt";
private static File blueprintFolder;
private static Map<BlueprintId, BlueprintMeta> blueprintMetas = new HashMap<BlueprintId, BlueprintMeta>();
private static Map<BlueprintId, Blueprint> blueprints = new WeakHashMap<BlueprintId, Blueprint>();
}

View file

@ -0,0 +1,64 @@
package buildcraft.builders.blueprints;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public final class BlueprintId {
public static BlueprintId generate(byte[] data) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] id = digest.digest(data);
return new BlueprintId(id);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static BlueprintId fromRawId(byte[] id) {
if (id.length != 32) return null;
return new BlueprintId(id);
}
private BlueprintId(byte[] id) {
this.id = id;
}
public byte[] toRawId() {
return id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BlueprintId) {
return Arrays.equals(id, ((BlueprintId) obj).id);
} else {
return false;
}
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
@Override
public String toString() {
char[] ret = new char[id.length * 2];
for (int i = 0; i < id.length; i++) {
ret[i * 2] = toHex(id[i] >>> 4);
ret[i * 2 + 1] = toHex(id[i] & 0xf);
}
return new String(ret);
}
private char toHex(int i) {
return (char) (i < 10 ? '0' + i : 'a' - 10 + i);
}
private final byte[] id;
}

View file

@ -0,0 +1,60 @@
package buildcraft.builders.blueprints;
import net.minecraft.nbt.NBTTagCompound;
public class BlueprintMeta {
private final String version = "Blueprint-2.0";
private BlueprintId id;
private String name;
private String creator;
protected BlueprintMeta() {
}
protected BlueprintMeta(BlueprintId id, NBTTagCompound nbt) {
this.id = id;
name = nbt.getString("name");
creator = nbt.getString("creator");
}
protected BlueprintId getId() {
return id;
}
protected void setId(BlueprintId id) {
this.id = id;
}
protected String getName() {
return name;
}
protected void setName(String name) {
if (getId() != null) throw new IllegalStateException("modifying finalized blueprint");
this.name = name;
}
/**
* @return the creator
*/
protected String getCreator() {
return creator;
}
/**
* @param creator the creator to set
*/
protected void setCreator(String creator) {
if (getId() != null) throw new IllegalStateException("modifying finalized blueprint");
this.creator = creator;
}
protected void writeToNBT(NBTTagCompound nbt) {
nbt.setString("name", name);
nbt.setString("version", version);
nbt.setString("creator", creator);
}
}

View file

@ -51,7 +51,6 @@ import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.ForgeChunkManager.Ticket;
import net.minecraftforge.common.ForgeChunkManager.Type;
import net.minecraftforge.common.ForgeDirection;
import static net.minecraftforge.common.ForgeDirection.*;
public class TileQuarry extends TileBuildCraft implements IMachine, IPowerReceptor, IBuilderInventory {
@ -619,7 +618,7 @@ public class TileQuarry extends TileBuildCraft implements IMachine, IPowerRecept
}
private void initializeBlueprintBuilder() {
Blueprint blueprint = new Blueprint(box.sizeX(), box.sizeY(), box.sizeZ());
Blueprint blueprint = Blueprint.create(box.sizeX(), box.sizeY(), box.sizeZ());
for (int it = 0; it < 2; it++) {
for (int i = 0; i < blueprint.sizeX; ++i) {