fix performances issues when saving a blueprint from the architect, #1477
This commit is contained in:
parent
8948339b15
commit
c8ebad37c3
3 changed files with 77 additions and 37 deletions
|
@ -68,7 +68,26 @@ public class TileArchitect extends TileBuildCraft implements IInventory, IBoxPro
|
||||||
|
|
||||||
computingTime = (int) ((1 - (float) blockScanner.blocksLeft()
|
computingTime = (int) ((1 - (float) blockScanner.blocksLeft()
|
||||||
/ (float) blockScanner.totalBlocks()) * 100);
|
/ (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();
|
createBpt();
|
||||||
|
|
||||||
computingTime = 0;
|
computingTime = 0;
|
||||||
|
@ -94,22 +113,6 @@ public class TileArchitect extends TileBuildCraft implements IInventory, IBoxPro
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createBpt() {
|
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);
|
BuildCraftBuilders.serverDB.add(writingBlueprint);
|
||||||
|
|
||||||
setInventorySlotContents(1, ItemBlueprint.getBlueprintItem(writingBlueprint));
|
setInventorySlotContents(1, ItemBlueprint.getBlueprintItem(writingBlueprint));
|
||||||
|
|
|
@ -8,9 +8,6 @@
|
||||||
*/
|
*/
|
||||||
package buildcraft.builders.blueprints;
|
package buildcraft.builders.blueprints;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
@ -31,7 +28,6 @@ import net.minecraft.nbt.CompressedStreamTools;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import buildcraft.BuildCraftBuilders;
|
import buildcraft.BuildCraftBuilders;
|
||||||
import buildcraft.core.blueprints.BlueprintBase;
|
import buildcraft.core.blueprints.BlueprintBase;
|
||||||
import buildcraft.core.utils.Utils;
|
|
||||||
|
|
||||||
public class BlueprintDatabase {
|
public class BlueprintDatabase {
|
||||||
private final int bufferSize = 8192;
|
private final int bufferSize = 8192;
|
||||||
|
@ -99,17 +95,7 @@ public class BlueprintDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private BlueprintId save(BlueprintBase blueprint) {
|
private BlueprintId save(BlueprintBase blueprint) {
|
||||||
NBTTagCompound nbt = new NBTTagCompound();
|
blueprint.id.generateUniqueId(blueprint.getData());
|
||||||
blueprint.writeToNBT(nbt);
|
|
||||||
|
|
||||||
ByteBuf buf = Unpooled.buffer();
|
|
||||||
|
|
||||||
Utils.writeNBT(buf, nbt);
|
|
||||||
|
|
||||||
byte[] data = new byte [buf.readableBytes()];
|
|
||||||
buf.readBytes(data);
|
|
||||||
|
|
||||||
blueprint.id.generateUniqueId(data);
|
|
||||||
|
|
||||||
BlueprintId id = blueprint.id;
|
BlueprintId id = blueprint.id;
|
||||||
|
|
||||||
|
@ -118,12 +104,16 @@ public class BlueprintDatabase {
|
||||||
if (!blueprintFile.exists()) {
|
if (!blueprintFile.exists()) {
|
||||||
OutputStream gzOs = null;
|
OutputStream gzOs = null;
|
||||||
try {
|
try {
|
||||||
CompressedStreamTools.writeCompressed(nbt, new FileOutputStream(blueprintFile));
|
FileOutputStream f = new FileOutputStream(blueprintFile);
|
||||||
|
f.write(blueprint.getData());
|
||||||
|
f.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to save Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
|
Logger.getLogger("Buildcraft").log(Level.SEVERE, String.format("Failed to save Blueprint file: %s %s", blueprintFile.getName(), ex.getMessage()));
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (gzOs != null) gzOs.close();
|
if (gzOs != null) {
|
||||||
|
gzOs.close();
|
||||||
|
}
|
||||||
} catch (IOException e) { }
|
} catch (IOException e) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,8 +155,12 @@ public class BlueprintDatabase {
|
||||||
|
|
||||||
if (blueprintFile.exists()) {
|
if (blueprintFile.exists()) {
|
||||||
try {
|
try {
|
||||||
NBTTagCompound nbt = CompressedStreamTools
|
FileInputStream f = new FileInputStream(blueprintFile);
|
||||||
.readCompressed(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);
|
BlueprintBase blueprint = BlueprintBase.loadBluePrint(nbt);
|
||||||
blueprint.id = id;
|
blueprint.id = id;
|
||||||
|
|
|
@ -8,12 +8,15 @@
|
||||||
*/
|
*/
|
||||||
package buildcraft.core.blueprints;
|
package buildcraft.core.blueprints;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.nbt.CompressedStreamTools;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import buildcraft.api.blueprints.BlueprintManager;
|
import buildcraft.api.blueprints.BlueprintManager;
|
||||||
import buildcraft.api.blueprints.Schematic;
|
|
||||||
import buildcraft.api.blueprints.MappingRegistry;
|
import buildcraft.api.blueprints.MappingRegistry;
|
||||||
|
import buildcraft.api.blueprints.Schematic;
|
||||||
import buildcraft.builders.blueprints.BlueprintId;
|
import buildcraft.builders.blueprints.BlueprintId;
|
||||||
import buildcraft.core.Box;
|
import buildcraft.core.Box;
|
||||||
import buildcraft.core.Version;
|
import buildcraft.core.Version;
|
||||||
|
@ -29,6 +32,8 @@ public abstract class BlueprintBase {
|
||||||
private String version = "";
|
private String version = "";
|
||||||
protected MappingRegistry mapping = new MappingRegistry();
|
protected MappingRegistry mapping = new MappingRegistry();
|
||||||
|
|
||||||
|
private byte [] data;
|
||||||
|
|
||||||
public BlueprintBase() {
|
public BlueprintBase() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,4 +269,42 @@ public abstract class BlueprintBase {
|
||||||
public abstract void loadContents(NBTTagCompound nbt) throws BptError;
|
public abstract void loadContents(NBTTagCompound nbt) throws BptError;
|
||||||
|
|
||||||
public abstract void saveContents(NBTTagCompound nbt);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue