fix performances issues when saving a blueprint from the architect, #1477

This commit is contained in:
SpaceToad 2014-03-08 17:13:58 +01:00
parent 8948339b15
commit c8ebad37c3
3 changed files with 77 additions and 37 deletions

View file

@ -68,7 +68,26 @@ public class TileArchitect extends TileBuildCraft implements IInventory, IBoxPro
computingTime = (int) ((1 - (float) blockScanner.blocksLeft()
/ (float) blockScanner.totalBlocks()) * 100);
} else {
if (blockScanner.blocksLeft() == 0) {
ForgeDirection o = ForgeDirection.values()[worldObj.getBlockMetadata(
xCoord, yCoord, zCoord)].getOpposite();
if (o == ForgeDirection.EAST) {
// Do nothing
} else if (o == ForgeDirection.SOUTH) {
writingBlueprint.rotateLeft(writingContext);
writingBlueprint.rotateLeft(writingContext);
writingBlueprint.rotateLeft(writingContext);
} else if (o == ForgeDirection.WEST) {
writingBlueprint.rotateLeft(writingContext);
writingBlueprint.rotateLeft(writingContext);
} else if (o == ForgeDirection.NORTH) {
writingBlueprint.rotateLeft(writingContext);
}
}
} else if (writingBlueprint.getData() != null) {
createBpt();
computingTime = 0;
@ -94,22 +113,6 @@ public class TileArchitect extends TileBuildCraft implements IInventory, IBoxPro
}
public void createBpt() {
ForgeDirection o = ForgeDirection.values()[worldObj.getBlockMetadata(
xCoord, yCoord, zCoord)].getOpposite();
if (o == ForgeDirection.EAST) {
// Do nothing
} else if (o == ForgeDirection.SOUTH) {
writingBlueprint.rotateLeft(writingContext);
writingBlueprint.rotateLeft(writingContext);
writingBlueprint.rotateLeft(writingContext);
} else if (o == ForgeDirection.WEST) {
writingBlueprint.rotateLeft(writingContext);
writingBlueprint.rotateLeft(writingContext);
} else if (o == ForgeDirection.NORTH) {
writingBlueprint.rotateLeft(writingContext);
}
BuildCraftBuilders.serverDB.add(writingBlueprint);
setInventorySlotContents(1, ItemBlueprint.getBlueprintItem(writingBlueprint));

View file

@ -8,9 +8,6 @@
*/
package buildcraft.builders.blueprints;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -31,7 +28,6 @@ import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.BuildCraftBuilders;
import buildcraft.core.blueprints.BlueprintBase;
import buildcraft.core.utils.Utils;
public class BlueprintDatabase {
private final int bufferSize = 8192;
@ -99,17 +95,7 @@ public class BlueprintDatabase {
}
private BlueprintId save(BlueprintBase blueprint) {
NBTTagCompound nbt = new NBTTagCompound();
blueprint.writeToNBT(nbt);
ByteBuf buf = Unpooled.buffer();
Utils.writeNBT(buf, nbt);
byte[] data = new byte [buf.readableBytes()];
buf.readBytes(data);
blueprint.id.generateUniqueId(data);
blueprint.id.generateUniqueId(blueprint.getData());
BlueprintId id = blueprint.id;
@ -118,12 +104,16 @@ public class BlueprintDatabase {
if (!blueprintFile.exists()) {
OutputStream gzOs = null;
try {
CompressedStreamTools.writeCompressed(nbt, new FileOutputStream(blueprintFile));
FileOutputStream f = new FileOutputStream(blueprintFile);
f.write(blueprint.getData());
f.close();
} 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();
if (gzOs != null) {
gzOs.close();
}
} catch (IOException e) { }
}
}
@ -165,8 +155,12 @@ public class BlueprintDatabase {
if (blueprintFile.exists()) {
try {
NBTTagCompound nbt = CompressedStreamTools
.readCompressed(new FileInputStream(blueprintFile));
FileInputStream f = new FileInputStream(blueprintFile);
byte [] data = new byte [(int) blueprintFile.length()];
f.read (data);
f.close();
NBTTagCompound nbt = CompressedStreamTools.decompress(data);
BlueprintBase blueprint = BlueprintBase.loadBluePrint(nbt);
blueprint.id = id;

View file

@ -8,12 +8,15 @@
*/
package buildcraft.core.blueprints;
import java.io.IOException;
import net.minecraft.block.Block;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import buildcraft.api.blueprints.BlueprintManager;
import buildcraft.api.blueprints.Schematic;
import buildcraft.api.blueprints.MappingRegistry;
import buildcraft.api.blueprints.Schematic;
import buildcraft.builders.blueprints.BlueprintId;
import buildcraft.core.Box;
import buildcraft.core.Version;
@ -29,6 +32,8 @@ public abstract class BlueprintBase {
private String version = "";
protected MappingRegistry mapping = new MappingRegistry();
private byte [] data;
public BlueprintBase() {
}
@ -264,4 +269,42 @@ public abstract class BlueprintBase {
public abstract void loadContents(NBTTagCompound nbt) throws BptError;
public abstract void saveContents(NBTTagCompound nbt);
class ComputeDataThread extends Thread {
public NBTTagCompound nbt;
@Override
public void run () {
try {
BlueprintBase.this.setData(CompressedStreamTools.compress(nbt));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ComputeDataThread computeData;
/**
* This function will return the binary data associated to this blueprint.
* This data is computed asynchronously. If the data is not yet available,
* null will be returned.
*/
public synchronized byte [] getData () {
if (data != null) {
return data;
} else if (computeData == null) {
computeData = new ComputeDataThread();
computeData.nbt = new NBTTagCompound();
writeToNBT(computeData.nbt);
computeData.start();
}
return null;
}
public synchronized void setData (byte [] b) {
data = b;
}
}