Removed warnings and formatted code

This commit is contained in:
OLEGSHA 2021-09-10 23:31:14 +03:00
parent e0f6a08740
commit e2308b825d
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
4 changed files with 103 additions and 133 deletions

View File

@ -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,16 +27,20 @@ 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

View File

@ -40,14 +40,14 @@ import ru.windcorp.progressia.common.world.io.ChunkIO;
import ru.windcorp.progressia.server.Server;
public class Region {
private static final boolean RESET_CORRUPTED = true;
public int loadedChunks;
private AtomicBoolean isUsing = new AtomicBoolean(false);
private AtomicBoolean isClosed = new AtomicBoolean(false);
private final RegionFile file;
private final ChunkMap<Integer> offsets = ChunkMaps.newHashMap();
@ -87,28 +87,26 @@ public class Region {
public void putOffset(Vec3i pos, int offset) {
offsets.put(pos, offset);
}
public AtomicBoolean isClosed()
{
public AtomicBoolean isClosed() {
return isClosed;
}
public AtomicBoolean isUsing()
{
public AtomicBoolean isUsing() {
return isUsing;
}
public void save(DefaultChunkData chunk, Server server) throws IOException {
isUsing.set(true);
Vec3i pos = TestWorldDiskIO.getInRegionCoords(chunk.getPosition());
if (!hasOffset(pos)) {
putOffset(pos, file.allocateChunk(pos));
}
int dataOffset = getOffset(pos);
byte[] buffer = saveToBuffer(chunk, server);
file.writeBuffer(buffer, dataOffset, pos);
isUsing.set(false);
}
@ -129,8 +127,6 @@ public class Region {
return arrayStream.toByteArray();
}
public DefaultChunkData load(Vec3i chunkPos, DefaultWorldData world, Server server)
throws IOException,
DecodingException {

View File

@ -13,46 +13,43 @@ 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;
}
}
private static final int DEFINITION_SIZE = Integer.BYTES;
private static final int HEADER_SIZE = DEFINITION_SIZE * REGION_DIAMETER * REGION_DIAMETER * REGION_DIAMETER;
private final RandomAccessFile file;
public RegionFile(RandomAccessFile inFile)
{
public RegionFile(RandomAccessFile inFile) {
file = inFile;
}
public void confirmHeaderHealth(ChunkMap<Integer> offsets) throws IOException {
Set<Integer> used = new HashSet<Integer>();
@ -63,14 +60,14 @@ public class RegionFile {
throw new IOException("File is too short to contain a header");
}
for (int i = 0; i < chunksPerRegion; i++) {
file.seek(i*DEFINITION_SIZE);
for (int i = 0; i < chunksPerRegion; i++) {
file.seek(i * DEFINITION_SIZE);
int offset = file.readInt();
if (offset == 0) {
continue;
}
offset--;
Vec3i pos = new Vec3i();
@ -79,70 +76,60 @@ public class RegionFile {
pos.z = i % REGION_DIAMETER;
offsets.put(pos, offset);
boolean shouldEnd = false;
byte counter = 0;
while (!shouldEnd)
{
if (offset > maxUsed)
{
while (!shouldEnd) {
if (offset > maxUsed) {
maxUsed = offset;
}
if (!used.add(offset)) {
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,51 +142,47 @@ 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);
}
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();
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);
file.setLength(HEADER_SIZE + dataOffset * SECTOR_SIZE);
return dataOffset;
}
@ -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.");
}
}
@ -263,5 +239,5 @@ public class RegionFile {
public void close() throws IOException {
file.close();
}
}

View File

@ -231,24 +231,19 @@ public class TestWorldDiskIO implements WorldContainer {
try {
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;
}
}