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

@ -50,8 +50,10 @@ public class Region {
// 1 MiB
private static final int MAX_CHUNK_SIZE = 1024 * 1024;
private static final int SECTOR_SIZE = MAX_CHUNK_SIZE / 256;
private static final int DEFINITION_SIZE = Integer.BYTES + 1;
private static final int HEADER_SIZE = Integer.BYTES * REGION_DIAMETER * REGION_DIAMETER * REGION_DIAMETER;
private static final int HEADER_SIZE = DEFINITION_SIZE * REGION_DIAMETER * REGION_DIAMETER * REGION_DIAMETER;
private final RandomAccessFile file;
@ -151,7 +153,7 @@ public class Region {
public void save(DefaultChunkData chunk, Server server) throws IOException {
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)) {
allocateChunk(definitionOffset, pos);
@ -182,7 +184,7 @@ public class Region {
file.seek(HEADER_SIZE + SECTOR_SIZE * dataOffset);
file.write(buffer);
file.seek(definitionOffset + Integer.BYTES - 1);
file.seek(definitionOffset + Integer.BYTES);
int sectors = buffer.length / SECTOR_SIZE + 1;
file.write(sectors);

View File

@ -55,7 +55,7 @@ public class TestWorldDiskIO implements WorldContainer {
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 ChunkMap<Region> regions = ChunkMaps.newHashMap();
@ -74,6 +74,7 @@ public class TestWorldDiskIO implements WorldContainer {
Region region = getRegion(chunkPos, false);
if (region == null) {
debug("Could not load chunk {} {} {}: region did not load", chunkPos);
return null;
}
@ -81,12 +82,7 @@ public class TestWorldDiskIO implements WorldContainer {
return result;
} catch (IOException | DecodingException e) {
LOG.warn(
"Failed to load chunk {} {} {}",
chunkPos.x,
chunkPos.y,
chunkPos.z
);
warn("Failed to load chunk {} {} {}", chunkPos);
e.printStackTrace();
return null;
}
@ -99,22 +95,11 @@ public class TestWorldDiskIO implements WorldContainer {
}
try {
LOG.debug(
"Saving {} {} {}",
chunk.getPosition().x,
chunk.getPosition().y,
chunk.getPosition().z
);
debug("Saving chunk {} {} {}", chunk.getPosition());
Region region = getRegion(chunk.getPosition(), true);
region.save(chunk, server);
} catch (IOException e) {
LOG.warn(
"Failed to save chunk {} {} {}",
chunk.getPosition().x,
chunk.getPosition().y,
chunk.getPosition().z
);
warn("Failed to save chunk {} {} {}", chunk.getPosition());
e.printStackTrace();
}
}
@ -128,21 +113,17 @@ public class TestWorldDiskIO implements WorldContainer {
Region region = regions.get(regionCoords);
if (region == null) {
debug("Region {} {} {} is not loaded, loading", regionCoords);
Path path = getPath().resolve(
String.format(
FILE_NAME_FORMAT,
regionCoords.x,
regionCoords.y,
regionCoords.z
)
);
Path path = getRegionPath(regionCoords);
if (!Files.exists(path) && !createIfMissing) {
if (!createIfMissing && !Files.exists(path)) {
debug("Region {} {} {} does not exist on disk, aborting load", regionCoords);
return null;
}
region = openRegion(path, regionCoords);
debug("Region {} {} {} loaded", regionCoords);
}
return region;
@ -170,6 +151,17 @@ public class TestWorldDiskIO implements WorldContainer {
public Path getPath() {
return path;
}
private Path getRegionPath(Vec3i regionPos) {
return getPath().resolve(
String.format(
FILE_NAME_FORMAT,
regionPos.x,
regionPos.y,
regionPos.z
)
);
}
@Override
public void close() {
@ -181,5 +173,15 @@ public class TestWorldDiskIO implements WorldContainer {
CrashReports.report(e, "Could not close region files");
}
}
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 0" level="DEBUG" />
-->
<!-- <Logger name="TestWorldDiskIO" level="DEBUG" /> -->
<!-- Uncomment to enable Region file logger debugging
<Logger name="TestWorldDiskIO" level="DEBUG" />
-->
<Root level="info">
<AppenderRef ref="FileLog" />