Removed warnings and formatted code
This commit is contained in:
parent
e0f6a08740
commit
e2308b825d
@ -18,7 +18,6 @@
|
||||
|
||||
package ru.windcorp.progressia;
|
||||
|
||||
import ru.windcorp.progressia.client.ClientProxy;
|
||||
import ru.windcorp.progressia.client.graphics.GUI;
|
||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||
import ru.windcorp.progressia.common.util.crash.analyzers.OutOfMemoryAnalyzer;
|
||||
@ -28,17 +27,21 @@ import ru.windcorp.progressia.test.LayerTitle;
|
||||
public class ProgressiaLauncher {
|
||||
|
||||
public static String[] arguments;
|
||||
private static ClientProxy proxy;
|
||||
private static Proxy proxy;
|
||||
|
||||
public static void launch(String[] args, ClientProxy inProxy) {
|
||||
public static void launch(String[] args, Proxy proxy) {
|
||||
arguments = args.clone();
|
||||
setupCrashReports();
|
||||
|
||||
inProxy.initialize();
|
||||
proxy = inProxy;
|
||||
proxy.initialize();
|
||||
ProgressiaLauncher.proxy = proxy;
|
||||
GUI.addTopLayer(new LayerTitle("Title"));
|
||||
}
|
||||
|
||||
public static Proxy getProxy() {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
private static void setupCrashReports() {
|
||||
// Context providers
|
||||
CrashReports.registerProvider(new OSContextProvider());
|
||||
|
@ -88,13 +88,11 @@ public class Region {
|
||||
offsets.put(pos, offset);
|
||||
}
|
||||
|
||||
public AtomicBoolean isClosed()
|
||||
{
|
||||
public AtomicBoolean isClosed() {
|
||||
return isClosed;
|
||||
}
|
||||
|
||||
public AtomicBoolean isUsing()
|
||||
{
|
||||
public AtomicBoolean isUsing() {
|
||||
return isUsing;
|
||||
}
|
||||
|
||||
@ -129,8 +127,6 @@ public class Region {
|
||||
return arrayStream.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public DefaultChunkData load(Vec3i chunkPos, DefaultWorldData world, Server server)
|
||||
throws IOException,
|
||||
DecodingException {
|
||||
|
@ -13,30 +13,28 @@ import org.apache.logging.log4j.LogManager;
|
||||
import glm.vec._3.i.Vec3i;
|
||||
import ru.windcorp.progressia.common.world.generic.ChunkMap;
|
||||
|
||||
/**Backend for the .progressia_region file.
|
||||
/**
|
||||
* Backend for the .progressia_region file.
|
||||
* Use similarly to a file object
|
||||
*
|
||||
*/
|
||||
public class RegionFile {
|
||||
// 4 MiB
|
||||
private static final int MAX_CHUNK_SIZE = 4 * 1024 * 1024;
|
||||
private static final int SECTORS_BYTES = Short.BYTES;
|
||||
private static final int SECTOR_SIZE = MAX_CHUNK_SIZE >> (SECTORS_BYTES*8);
|
||||
private static final int SECTOR_SIZE = MAX_CHUNK_SIZE >> (SECTORS_BYTES * 8);
|
||||
private static final int SECTOR_HEADER_SIZE = 1;
|
||||
|
||||
final byte endBytes[] = new byte[SECTOR_SIZE];
|
||||
|
||||
public static enum SectorType
|
||||
{
|
||||
Ending (0), // Just an empty block
|
||||
Data (1), // has a byte counting up in position 1, and then
|
||||
PartitionLink (2),
|
||||
BulkSaved (3); // TODO implement this
|
||||
public static enum SectorType {
|
||||
Ending(0), // Just an empty block
|
||||
Data(1), // has a byte counting up in position 1, and then
|
||||
PartitionLink(2),
|
||||
BulkSaved(3); // TODO implement this
|
||||
|
||||
private final byte data;
|
||||
|
||||
SectorType(int i)
|
||||
{
|
||||
SectorType(int i) {
|
||||
this.data = (byte) i;
|
||||
}
|
||||
|
||||
@ -48,8 +46,7 @@ public class RegionFile {
|
||||
|
||||
private final RandomAccessFile file;
|
||||
|
||||
public RegionFile(RandomAccessFile inFile)
|
||||
{
|
||||
public RegionFile(RandomAccessFile inFile) {
|
||||
file = inFile;
|
||||
}
|
||||
|
||||
@ -64,7 +61,7 @@ public class RegionFile {
|
||||
}
|
||||
|
||||
for (int i = 0; i < chunksPerRegion; i++) {
|
||||
file.seek(i*DEFINITION_SIZE);
|
||||
file.seek(i * DEFINITION_SIZE);
|
||||
int offset = file.readInt();
|
||||
|
||||
if (offset == 0) {
|
||||
@ -82,10 +79,8 @@ public class RegionFile {
|
||||
|
||||
boolean shouldEnd = false;
|
||||
byte counter = 0;
|
||||
while (!shouldEnd)
|
||||
{
|
||||
if (offset > maxUsed)
|
||||
{
|
||||
while (!shouldEnd) {
|
||||
if (offset > maxUsed) {
|
||||
maxUsed = offset;
|
||||
}
|
||||
|
||||
@ -93,56 +88,48 @@ public class RegionFile {
|
||||
throw new IOException("A sector is used twice");
|
||||
}
|
||||
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE*offset);
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE * offset);
|
||||
byte type = file.readByte();
|
||||
|
||||
if (type == SectorType.Data.data)
|
||||
{
|
||||
if (type == SectorType.Data.data) {
|
||||
byte fileCounter = file.readByte();
|
||||
if (fileCounter != counter)
|
||||
{
|
||||
if (fileCounter != counter) {
|
||||
throw new IOException("An unexpected block was found");
|
||||
}
|
||||
counter++;
|
||||
offset++;
|
||||
}
|
||||
else if (type == SectorType.Ending.data) {
|
||||
} else if (type == SectorType.Ending.data) {
|
||||
shouldEnd = true;
|
||||
}
|
||||
else if (type == SectorType.PartitionLink.data)
|
||||
{
|
||||
} else if (type == SectorType.PartitionLink.data) {
|
||||
offset = file.readInt();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
LogManager.getLogger("Region").debug("Efficiency of {}", (double) used.size()/maxUsed);
|
||||
LogManager.getLogger("Region").debug("Efficiency of {}", (double) used.size() / maxUsed);
|
||||
}
|
||||
|
||||
public void makeHeader() throws IOException
|
||||
{
|
||||
public void makeHeader() throws IOException {
|
||||
file.seek(0);
|
||||
for (int i=0;i<HEADER_SIZE;i++)
|
||||
{
|
||||
for (int i = 0; i < HEADER_SIZE; i++) {
|
||||
file.write(0);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeBuffer(byte[] buffer, int dataOffset, Vec3i pos) throws IOException {
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
|
||||
int loc=0;
|
||||
int loc = 0;
|
||||
byte tempBuffer[] = new byte[SECTOR_SIZE];
|
||||
byte counter = 0;
|
||||
boolean isDone = false;
|
||||
while (!isDone)
|
||||
{
|
||||
while (!isDone) {
|
||||
if (file.length() > HEADER_SIZE + SECTOR_SIZE * (dataOffset + 1)) {
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE * (dataOffset + 1));
|
||||
byte header = file.readByte();
|
||||
if (header == SectorType.Data.data) {
|
||||
byte fileCounter = file.readByte();
|
||||
if (fileCounter != counter+1) // This makes the actual
|
||||
// partition place
|
||||
if (fileCounter != counter + 1) // This makes the actual
|
||||
// partition place
|
||||
{
|
||||
int newOffset = allocateEmptySector();
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
|
||||
@ -155,21 +142,18 @@ public class RegionFile {
|
||||
tempBuffer[0] = 1;
|
||||
tempBuffer[1] = counter;
|
||||
counter++;
|
||||
for (int i=0;i<(SECTOR_SIZE-SECTOR_HEADER_SIZE-1);i++)
|
||||
{
|
||||
if (loc*(SECTOR_SIZE-SECTOR_HEADER_SIZE-1) + i<buffer.length)
|
||||
{
|
||||
tempBuffer[i+SECTOR_HEADER_SIZE+1] = buffer[loc*(SECTOR_SIZE-SECTOR_HEADER_SIZE-1) + i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < (SECTOR_SIZE - SECTOR_HEADER_SIZE - 1); i++) {
|
||||
if (loc * (SECTOR_SIZE - SECTOR_HEADER_SIZE - 1) + i < buffer.length) {
|
||||
tempBuffer[i + SECTOR_HEADER_SIZE + 1] = buffer[loc * (SECTOR_SIZE - SECTOR_HEADER_SIZE - 1) + i];
|
||||
} else {
|
||||
isDone = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
loc++;
|
||||
if (file.getFilePointer()<256)
|
||||
LogManager.getLogger("Region").debug("at {}, ({},{},{}), {}", file.getFilePointer(),pos.x,pos.y,pos.z, dataOffset);
|
||||
if (file.getFilePointer() < 256)
|
||||
LogManager.getLogger("Region")
|
||||
.debug("at {}, ({},{},{}), {}", file.getFilePointer(), pos.x, pos.y, pos.z, dataOffset);
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
|
||||
dataOffset++;
|
||||
file.write(tempBuffer);
|
||||
@ -178,7 +162,7 @@ public class RegionFile {
|
||||
file.write(endBytes);
|
||||
}
|
||||
|
||||
public int allocateChunk( Vec3i pos) throws IOException {
|
||||
public int allocateChunk(Vec3i pos) throws IOException {
|
||||
int definitionOffset = DEFINITION_SIZE * (pos.z + REGION_DIAMETER * (pos.y + REGION_DIAMETER * pos.x));
|
||||
|
||||
int outputLen = (int) file.length();
|
||||
@ -186,14 +170,13 @@ public class RegionFile {
|
||||
int dataOffset = (int) Math.ceil((double) (outputLen - HEADER_SIZE) / SECTOR_SIZE);
|
||||
|
||||
file.seek(definitionOffset);
|
||||
file.writeInt(dataOffset+1);
|
||||
file.writeInt(dataOffset + 1);
|
||||
|
||||
file.setLength(HEADER_SIZE + dataOffset * SECTOR_SIZE);
|
||||
return dataOffset;
|
||||
}
|
||||
|
||||
private int allocateEmptySector() throws IOException
|
||||
{
|
||||
private int allocateEmptySector() throws IOException {
|
||||
int outputLen = (int) file.length();
|
||||
|
||||
int dataOffset = (int) Math.ceil((double) (outputLen - HEADER_SIZE) / SECTOR_SIZE);
|
||||
@ -207,53 +190,46 @@ public class RegionFile {
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
|
||||
|
||||
int bufferPos = 0;
|
||||
byte buffer[] = new byte[SECTOR_SIZE*16];
|
||||
byte buffer[] = new byte[SECTOR_SIZE * 16];
|
||||
byte tempBuffer[] = new byte[SECTOR_SIZE];
|
||||
|
||||
boolean reachedEnd = false;
|
||||
byte counter = 0;
|
||||
while (!reachedEnd)
|
||||
{
|
||||
while (!reachedEnd) {
|
||||
int bytesRead = file.read(tempBuffer, 0, SECTOR_SIZE);
|
||||
if (bytesRead<0)
|
||||
{
|
||||
if (bytesRead < 0) {
|
||||
reachedEnd = true;
|
||||
continue;
|
||||
}
|
||||
if (tempBuffer[0] == SectorType.Data.data)
|
||||
{
|
||||
if (tempBuffer[1] != counter)
|
||||
{
|
||||
throw new IOException("Sectors were read out of order\nExpected chunk number "+Byte.toString(counter)+" but encountered number " + Byte.toString(tempBuffer[1]));
|
||||
if (tempBuffer[0] == SectorType.Data.data) {
|
||||
if (tempBuffer[1] != counter) {
|
||||
throw new IOException(
|
||||
"Sectors were read out of order\nExpected chunk number " + Byte.toString(counter)
|
||||
+ " but encountered number " + Byte.toString(tempBuffer[1])
|
||||
);
|
||||
}
|
||||
counter++;
|
||||
if (buffer.length - bufferPos < SECTOR_SIZE-SECTOR_HEADER_SIZE-1)
|
||||
{
|
||||
byte newBuffer[] = new byte[buffer.length + SECTOR_SIZE*16];
|
||||
for (int i=0;i<buffer.length;i++) // TODO dedicated copy, java-y at least
|
||||
if (buffer.length - bufferPos < SECTOR_SIZE - SECTOR_HEADER_SIZE - 1) {
|
||||
byte newBuffer[] = new byte[buffer.length + SECTOR_SIZE * 16];
|
||||
for (int i = 0; i < buffer.length; i++) // TODO dedicated
|
||||
// copy, java-y at
|
||||
// least
|
||||
{
|
||||
newBuffer[i] = buffer[i];
|
||||
}
|
||||
buffer = newBuffer;
|
||||
}
|
||||
for (int i=0;i<SECTOR_SIZE-SECTOR_HEADER_SIZE-1;i++)
|
||||
{
|
||||
buffer[bufferPos+i] = tempBuffer[i+2];
|
||||
for (int i = 0; i < SECTOR_SIZE - SECTOR_HEADER_SIZE - 1; i++) {
|
||||
buffer[bufferPos + i] = tempBuffer[i + 2];
|
||||
}
|
||||
bufferPos += SECTOR_SIZE-SECTOR_HEADER_SIZE-1;
|
||||
}
|
||||
else if (tempBuffer[0] == SectorType.Ending.data)
|
||||
{
|
||||
bufferPos += SECTOR_SIZE - SECTOR_HEADER_SIZE - 1;
|
||||
} else if (tempBuffer[0] == SectorType.Ending.data) {
|
||||
reachedEnd = true;
|
||||
}
|
||||
else if (tempBuffer[0] == SectorType.PartitionLink.data)
|
||||
{
|
||||
} else if (tempBuffer[0] == SectorType.PartitionLink.data) {
|
||||
ByteBuffer intBuffer = ByteBuffer.wrap(tempBuffer);
|
||||
int newOffset = intBuffer.getInt(1);
|
||||
file.seek(HEADER_SIZE + SECTOR_SIZE * newOffset);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
throw new IOException("Invalid sector ID.");
|
||||
}
|
||||
}
|
||||
|
@ -232,23 +232,18 @@ public class TestWorldDiskIO implements WorldContainer {
|
||||
ChunkMap<AtomicBoolean> isCloseds = ChunkMaps.newHashMap();
|
||||
ChunkMap<AtomicBoolean> isUsings = ChunkMaps.newHashMap();
|
||||
|
||||
for (Vec3i region : regions.keys())
|
||||
{
|
||||
for (Vec3i region : regions.keys()) {
|
||||
isCloseds.put(region, regions.get(region).isClosed());
|
||||
isUsings.put(region, regions.get(region).isUsing());
|
||||
}
|
||||
|
||||
boolean stillOpen = true;
|
||||
while (stillOpen)
|
||||
{
|
||||
while (stillOpen) {
|
||||
stillOpen = false;
|
||||
for (Vec3i region : regions.keys()) {
|
||||
if (!isCloseds.get(region).get() && !isUsings.get(region).get())
|
||||
{
|
||||
if (!isCloseds.get(region).get() && !isUsings.get(region).get()) {
|
||||
regions.get(region).close();
|
||||
}
|
||||
else if (isUsings.get(region).get())
|
||||
{
|
||||
} else if (isUsings.get(region).get()) {
|
||||
stillOpen = false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user