Fixed the bug that opfromthestart found, reloading now works

This commit is contained in:
OLEGSHA 2021-08-29 12:04:02 +03:00
parent f4300558d5
commit d2ffe1fe0e
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
3 changed files with 40 additions and 33 deletions

View File

@ -51,7 +51,9 @@ public class Region {
private static final int MAX_CHUNK_SIZE = 1024 * 1024; private static final int MAX_CHUNK_SIZE = 1024 * 1024;
private static final int SECTOR_SIZE = MAX_CHUNK_SIZE / 256; private static final int SECTOR_SIZE = MAX_CHUNK_SIZE / 256;
private static final int HEADER_SIZE = Integer.BYTES * REGION_DIAMETER * REGION_DIAMETER * REGION_DIAMETER; private static final int DEFINITION_SIZE = Integer.BYTES + 1;
private static final int HEADER_SIZE = DEFINITION_SIZE * REGION_DIAMETER * REGION_DIAMETER * REGION_DIAMETER;
private final RandomAccessFile file; private final RandomAccessFile file;
@ -151,7 +153,7 @@ public class Region {
public void save(DefaultChunkData chunk, Server server) throws IOException { public void save(DefaultChunkData chunk, Server server) throws IOException {
Vec3i pos = TestWorldDiskIO.getInRegionCoords(chunk.getPosition()); Vec3i pos = TestWorldDiskIO.getInRegionCoords(chunk.getPosition());
int definitionOffset = Integer.BYTES * (pos.z + REGION_DIAMETER * (pos.y + REGION_DIAMETER * pos.x)); int definitionOffset = DEFINITION_SIZE * (pos.z + REGION_DIAMETER * (pos.y + REGION_DIAMETER * pos.x));
if (!hasOffset(pos)) { if (!hasOffset(pos)) {
allocateChunk(definitionOffset, pos); allocateChunk(definitionOffset, pos);
@ -182,7 +184,7 @@ public class Region {
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset); file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
file.write(buffer); file.write(buffer);
file.seek(definitionOffset + Integer.BYTES - 1); file.seek(definitionOffset + Integer.BYTES);
int sectors = buffer.length / SECTOR_SIZE + 1; int sectors = buffer.length / SECTOR_SIZE + 1;
file.write(sectors); file.write(sectors);

View File

@ -55,7 +55,7 @@ public class TestWorldDiskIO implements WorldContainer {
return Coordinates.convertGlobalToInCell(BITS_IN_CHUNK_COORDS, chunkCoords, null); return Coordinates.convertGlobalToInCell(BITS_IN_CHUNK_COORDS, chunkCoords, null);
} }
static final Logger LOG = LogManager.getLogger(); static final Logger LOG = LogManager.getLogger("TestWorldDiskIO");
private final Path path; private final Path path;
private final ChunkMap<Region> regions = ChunkMaps.newHashMap(); private final ChunkMap<Region> regions = ChunkMaps.newHashMap();
@ -74,6 +74,7 @@ public class TestWorldDiskIO implements WorldContainer {
Region region = getRegion(chunkPos, false); Region region = getRegion(chunkPos, false);
if (region == null) { if (region == null) {
debug("Could not load chunk {} {} {}: region did not load", chunkPos);
return null; return null;
} }
@ -81,12 +82,7 @@ public class TestWorldDiskIO implements WorldContainer {
return result; return result;
} catch (IOException | DecodingException e) { } catch (IOException | DecodingException e) {
LOG.warn( warn("Failed to load chunk {} {} {}", chunkPos);
"Failed to load chunk {} {} {}",
chunkPos.x,
chunkPos.y,
chunkPos.z
);
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
@ -99,22 +95,11 @@ public class TestWorldDiskIO implements WorldContainer {
} }
try { try {
LOG.debug( debug("Saving chunk {} {} {}", chunk.getPosition());
"Saving {} {} {}",
chunk.getPosition().x,
chunk.getPosition().y,
chunk.getPosition().z
);
Region region = getRegion(chunk.getPosition(), true); Region region = getRegion(chunk.getPosition(), true);
region.save(chunk, server); region.save(chunk, server);
} catch (IOException e) { } catch (IOException e) {
LOG.warn( warn("Failed to save chunk {} {} {}", chunk.getPosition());
"Failed to save chunk {} {} {}",
chunk.getPosition().x,
chunk.getPosition().y,
chunk.getPosition().z
);
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -128,21 +113,17 @@ public class TestWorldDiskIO implements WorldContainer {
Region region = regions.get(regionCoords); Region region = regions.get(regionCoords);
if (region == null) { if (region == null) {
debug("Region {} {} {} is not loaded, loading", regionCoords);
Path path = getPath().resolve( Path path = getRegionPath(regionCoords);
String.format(
FILE_NAME_FORMAT,
regionCoords.x,
regionCoords.y,
regionCoords.z
)
);
if (!Files.exists(path) && !createIfMissing) { if (!createIfMissing && !Files.exists(path)) {
debug("Region {} {} {} does not exist on disk, aborting load", regionCoords);
return null; return null;
} }
region = openRegion(path, regionCoords); region = openRegion(path, regionCoords);
debug("Region {} {} {} loaded", regionCoords);
} }
return region; return region;
@ -171,6 +152,17 @@ public class TestWorldDiskIO implements WorldContainer {
return path; return path;
} }
private Path getRegionPath(Vec3i regionPos) {
return getPath().resolve(
String.format(
FILE_NAME_FORMAT,
regionPos.x,
regionPos.y,
regionPos.z
)
);
}
@Override @Override
public void close() { public void close() {
try { try {
@ -182,4 +174,14 @@ public class TestWorldDiskIO implements WorldContainer {
} }
} }
private static void debug(String message, Vec3i vector) {
if (LOG.isDebugEnabled()) {
LOG.debug(message, vector.x, vector.y, vector.z);
}
}
private static void warn(String message, Vec3i vector) {
LOG.warn(message, vector.x, vector.y, vector.z);
}
} }

View File

@ -26,7 +26,10 @@
<Logger name="Ticker Coordinator" level="DEBUG" /> <Logger name="Ticker Coordinator" level="DEBUG" />
<Logger name="Ticker 0" level="DEBUG" /> <Logger name="Ticker 0" level="DEBUG" />
--> -->
<!-- <Logger name="TestWorldDiskIO" level="DEBUG" /> -->
<!-- Uncomment to enable Region file logger debugging
<Logger name="TestWorldDiskIO" level="DEBUG" />
-->
<Root level="info"> <Root level="info">
<AppenderRef ref="FileLog" /> <AppenderRef ref="FileLog" />