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; package ru.windcorp.progressia;
import ru.windcorp.progressia.client.ClientProxy;
import ru.windcorp.progressia.client.graphics.GUI; import ru.windcorp.progressia.client.graphics.GUI;
import ru.windcorp.progressia.common.util.crash.CrashReports; import ru.windcorp.progressia.common.util.crash.CrashReports;
import ru.windcorp.progressia.common.util.crash.analyzers.OutOfMemoryAnalyzer; import ru.windcorp.progressia.common.util.crash.analyzers.OutOfMemoryAnalyzer;
@ -28,17 +27,21 @@ import ru.windcorp.progressia.test.LayerTitle;
public class ProgressiaLauncher { public class ProgressiaLauncher {
public static String[] arguments; 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(); arguments = args.clone();
setupCrashReports(); setupCrashReports();
inProxy.initialize(); proxy.initialize();
proxy = inProxy; ProgressiaLauncher.proxy = proxy;
GUI.addTopLayer(new LayerTitle("Title")); GUI.addTopLayer(new LayerTitle("Title"));
} }
public static Proxy getProxy() {
return proxy;
}
private static void setupCrashReports() { private static void setupCrashReports() {
// Context providers // Context providers
CrashReports.registerProvider(new OSContextProvider()); CrashReports.registerProvider(new OSContextProvider());

View File

@ -88,13 +88,11 @@ public class Region {
offsets.put(pos, offset); offsets.put(pos, offset);
} }
public AtomicBoolean isClosed() public AtomicBoolean isClosed() {
{
return isClosed; return isClosed;
} }
public AtomicBoolean isUsing() public AtomicBoolean isUsing() {
{
return isUsing; return isUsing;
} }
@ -129,8 +127,6 @@ public class Region {
return arrayStream.toByteArray(); return arrayStream.toByteArray();
} }
public DefaultChunkData load(Vec3i chunkPos, DefaultWorldData world, Server server) public DefaultChunkData load(Vec3i chunkPos, DefaultWorldData world, Server server)
throws IOException, throws IOException,
DecodingException { DecodingException {

View File

@ -13,30 +13,28 @@ import org.apache.logging.log4j.LogManager;
import glm.vec._3.i.Vec3i; import glm.vec._3.i.Vec3i;
import ru.windcorp.progressia.common.world.generic.ChunkMap; 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 * Use similarly to a file object
*
*/ */
public class RegionFile { public class RegionFile {
// 4 MiB // 4 MiB
private static final int MAX_CHUNK_SIZE = 4 * 1024 * 1024; private static final int MAX_CHUNK_SIZE = 4 * 1024 * 1024;
private static final int SECTORS_BYTES = Short.BYTES; 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; private static final int SECTOR_HEADER_SIZE = 1;
final byte endBytes[] = new byte[SECTOR_SIZE]; final byte endBytes[] = new byte[SECTOR_SIZE];
public static enum SectorType public static enum SectorType {
{ Ending(0), // Just an empty block
Ending (0), // Just an empty block Data(1), // has a byte counting up in position 1, and then
Data (1), // has a byte counting up in position 1, and then PartitionLink(2),
PartitionLink (2), BulkSaved(3); // TODO implement this
BulkSaved (3); // TODO implement this
private final byte data; private final byte data;
SectorType(int i) SectorType(int i) {
{
this.data = (byte) i; this.data = (byte) i;
} }
@ -48,8 +46,7 @@ public class RegionFile {
private final RandomAccessFile file; private final RandomAccessFile file;
public RegionFile(RandomAccessFile inFile) public RegionFile(RandomAccessFile inFile) {
{
file = inFile; file = inFile;
} }
@ -64,7 +61,7 @@ public class RegionFile {
} }
for (int i = 0; i < chunksPerRegion; i++) { for (int i = 0; i < chunksPerRegion; i++) {
file.seek(i*DEFINITION_SIZE); file.seek(i * DEFINITION_SIZE);
int offset = file.readInt(); int offset = file.readInt();
if (offset == 0) { if (offset == 0) {
@ -82,10 +79,8 @@ public class RegionFile {
boolean shouldEnd = false; boolean shouldEnd = false;
byte counter = 0; byte counter = 0;
while (!shouldEnd) while (!shouldEnd) {
{ if (offset > maxUsed) {
if (offset > maxUsed)
{
maxUsed = offset; maxUsed = offset;
} }
@ -93,55 +88,47 @@ public class RegionFile {
throw new IOException("A sector is used twice"); 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(); byte type = file.readByte();
if (type == SectorType.Data.data) if (type == SectorType.Data.data) {
{
byte fileCounter = file.readByte(); byte fileCounter = file.readByte();
if (fileCounter != counter) if (fileCounter != counter) {
{
throw new IOException("An unexpected block was found"); throw new IOException("An unexpected block was found");
} }
counter++; counter++;
offset++; offset++;
} } else if (type == SectorType.Ending.data) {
else if (type == SectorType.Ending.data) {
shouldEnd = true; shouldEnd = true;
} } else if (type == SectorType.PartitionLink.data) {
else if (type == SectorType.PartitionLink.data)
{
offset = file.readInt(); 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); file.seek(0);
for (int i=0;i<HEADER_SIZE;i++) for (int i = 0; i < HEADER_SIZE; i++) {
{
file.write(0); file.write(0);
} }
} }
public void writeBuffer(byte[] buffer, int dataOffset, Vec3i pos) throws IOException { public void writeBuffer(byte[] buffer, int dataOffset, Vec3i pos) throws IOException {
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset); file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
int loc=0; int loc = 0;
byte tempBuffer[] = new byte[SECTOR_SIZE]; byte tempBuffer[] = new byte[SECTOR_SIZE];
byte counter = 0; byte counter = 0;
boolean isDone = false; boolean isDone = false;
while (!isDone) while (!isDone) {
{
if (file.length() > HEADER_SIZE + SECTOR_SIZE * (dataOffset + 1)) { if (file.length() > HEADER_SIZE + SECTOR_SIZE * (dataOffset + 1)) {
file.seek(HEADER_SIZE + SECTOR_SIZE * (dataOffset + 1)); file.seek(HEADER_SIZE + SECTOR_SIZE * (dataOffset + 1));
byte header = file.readByte(); byte header = file.readByte();
if (header == SectorType.Data.data) { if (header == SectorType.Data.data) {
byte fileCounter = file.readByte(); byte fileCounter = file.readByte();
if (fileCounter != counter+1) // This makes the actual if (fileCounter != counter + 1) // This makes the actual
// partition place // partition place
{ {
int newOffset = allocateEmptySector(); int newOffset = allocateEmptySector();
@ -155,21 +142,18 @@ public class RegionFile {
tempBuffer[0] = 1; tempBuffer[0] = 1;
tempBuffer[1] = counter; tempBuffer[1] = counter;
counter++; counter++;
for (int i=0;i<(SECTOR_SIZE-SECTOR_HEADER_SIZE-1);i++) for (int i = 0; i < (SECTOR_SIZE - SECTOR_HEADER_SIZE - 1); i++) {
{ if (loc * (SECTOR_SIZE - SECTOR_HEADER_SIZE - 1) + i < buffer.length) {
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 {
tempBuffer[i+SECTOR_HEADER_SIZE+1] = buffer[loc*(SECTOR_SIZE-SECTOR_HEADER_SIZE-1) + i];
}
else
{
isDone = true; isDone = true;
break; break;
} }
} }
loc++; loc++;
if (file.getFilePointer()<256) if (file.getFilePointer() < 256)
LogManager.getLogger("Region").debug("at {}, ({},{},{}), {}", file.getFilePointer(),pos.x,pos.y,pos.z, dataOffset); LogManager.getLogger("Region")
.debug("at {}, ({},{},{}), {}", file.getFilePointer(), pos.x, pos.y, pos.z, dataOffset);
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset); file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
dataOffset++; dataOffset++;
file.write(tempBuffer); file.write(tempBuffer);
@ -178,7 +162,7 @@ public class RegionFile {
file.write(endBytes); 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 definitionOffset = DEFINITION_SIZE * (pos.z + REGION_DIAMETER * (pos.y + REGION_DIAMETER * pos.x));
int outputLen = (int) file.length(); int outputLen = (int) file.length();
@ -186,14 +170,13 @@ public class RegionFile {
int dataOffset = (int) Math.ceil((double) (outputLen - HEADER_SIZE) / SECTOR_SIZE); int dataOffset = (int) Math.ceil((double) (outputLen - HEADER_SIZE) / SECTOR_SIZE);
file.seek(definitionOffset); file.seek(definitionOffset);
file.writeInt(dataOffset+1); file.writeInt(dataOffset + 1);
file.setLength(HEADER_SIZE + dataOffset * SECTOR_SIZE); file.setLength(HEADER_SIZE + dataOffset * SECTOR_SIZE);
return dataOffset; return dataOffset;
} }
private int allocateEmptySector() throws IOException private int allocateEmptySector() throws IOException {
{
int outputLen = (int) file.length(); int outputLen = (int) file.length();
int dataOffset = (int) Math.ceil((double) (outputLen - HEADER_SIZE) / SECTOR_SIZE); 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); file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
int bufferPos = 0; int bufferPos = 0;
byte buffer[] = new byte[SECTOR_SIZE*16]; byte buffer[] = new byte[SECTOR_SIZE * 16];
byte tempBuffer[] = new byte[SECTOR_SIZE]; byte tempBuffer[] = new byte[SECTOR_SIZE];
boolean reachedEnd = false; boolean reachedEnd = false;
byte counter = 0; byte counter = 0;
while (!reachedEnd) while (!reachedEnd) {
{
int bytesRead = file.read(tempBuffer, 0, SECTOR_SIZE); int bytesRead = file.read(tempBuffer, 0, SECTOR_SIZE);
if (bytesRead<0) if (bytesRead < 0) {
{
reachedEnd = true; reachedEnd = true;
continue; continue;
} }
if (tempBuffer[0] == SectorType.Data.data) if (tempBuffer[0] == SectorType.Data.data) {
{ if (tempBuffer[1] != counter) {
if (tempBuffer[1] != counter) throw new IOException(
{ "Sectors were read out of order\nExpected chunk number " + Byte.toString(counter)
throw new IOException("Sectors were read out of order\nExpected chunk number "+Byte.toString(counter)+" but encountered number " + Byte.toString(tempBuffer[1])); + " but encountered number " + Byte.toString(tempBuffer[1])
);
} }
counter++; counter++;
if (buffer.length - bufferPos < SECTOR_SIZE-SECTOR_HEADER_SIZE-1) if (buffer.length - bufferPos < SECTOR_SIZE - SECTOR_HEADER_SIZE - 1) {
{ byte newBuffer[] = new byte[buffer.length + SECTOR_SIZE * 16];
byte newBuffer[] = new byte[buffer.length + SECTOR_SIZE*16]; for (int i = 0; i < buffer.length; i++) // TODO dedicated
for (int i=0;i<buffer.length;i++) // TODO dedicated copy, java-y at least // copy, java-y at
// least
{ {
newBuffer[i] = buffer[i]; newBuffer[i] = buffer[i];
} }
buffer = newBuffer; buffer = newBuffer;
} }
for (int i=0;i<SECTOR_SIZE-SECTOR_HEADER_SIZE-1;i++) for (int i = 0; i < SECTOR_SIZE - SECTOR_HEADER_SIZE - 1; i++) {
{ buffer[bufferPos + i] = tempBuffer[i + 2];
buffer[bufferPos+i] = tempBuffer[i+2];
} }
bufferPos += SECTOR_SIZE-SECTOR_HEADER_SIZE-1; bufferPos += SECTOR_SIZE - SECTOR_HEADER_SIZE - 1;
} } else if (tempBuffer[0] == SectorType.Ending.data) {
else if (tempBuffer[0] == SectorType.Ending.data)
{
reachedEnd = true; reachedEnd = true;
} } else if (tempBuffer[0] == SectorType.PartitionLink.data) {
else if (tempBuffer[0] == SectorType.PartitionLink.data)
{
ByteBuffer intBuffer = ByteBuffer.wrap(tempBuffer); ByteBuffer intBuffer = ByteBuffer.wrap(tempBuffer);
int newOffset = intBuffer.getInt(1); int newOffset = intBuffer.getInt(1);
file.seek(HEADER_SIZE + SECTOR_SIZE * newOffset); file.seek(HEADER_SIZE + SECTOR_SIZE * newOffset);
} } else {
else
{
throw new IOException("Invalid sector ID."); throw new IOException("Invalid sector ID.");
} }
} }

View File

@ -232,23 +232,18 @@ public class TestWorldDiskIO implements WorldContainer {
ChunkMap<AtomicBoolean> isCloseds = ChunkMaps.newHashMap(); ChunkMap<AtomicBoolean> isCloseds = ChunkMaps.newHashMap();
ChunkMap<AtomicBoolean> isUsings = ChunkMaps.newHashMap(); ChunkMap<AtomicBoolean> isUsings = ChunkMaps.newHashMap();
for (Vec3i region : regions.keys()) for (Vec3i region : regions.keys()) {
{
isCloseds.put(region, regions.get(region).isClosed()); isCloseds.put(region, regions.get(region).isClosed());
isUsings.put(region, regions.get(region).isUsing()); isUsings.put(region, regions.get(region).isUsing());
} }
boolean stillOpen = true; boolean stillOpen = true;
while (stillOpen) while (stillOpen) {
{
stillOpen = false; stillOpen = false;
for (Vec3i region : regions.keys()) { 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(); regions.get(region).close();
} } else if (isUsings.get(region).get()) {
else if (isUsings.get(region).get())
{
stillOpen = false; stillOpen = false;
} }
} }