Skip to content

Commit 29aae45

Browse files
Fix world creation writing allowCheats and defaultGamemode incorrectly and adds a migration path for existing worlds that migrates the incorrectly written field.
fixes #2457
1 parent 6ce7e00 commit 29aae45

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/server/world.zig

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ pub fn tryCreateWorld(worldName: []const u8, worldSettings: Settings) !void {
9494
{
9595
const settings = main.ZonElement.initObject(main.stackAllocator);
9696

97-
settings.put("default_gamemode", @tagName(worldSettings.defaultGamemode));
98-
settings.put("cheats", worldSettings.allowCheats);
97+
settings.put("defaultGamemode", @tagName(worldSettings.defaultGamemode));
98+
settings.put("allowCheats", worldSettings.allowCheats);
9999
settings.put("testingMode", worldSettings.testingMode);
100100
settings.put("seed", worldSettings.seed);
101101

@@ -402,7 +402,7 @@ pub const ChunkManager = struct { // MARK: ChunkManager
402402
}
403403
};
404404

405-
pub const worldDataVersion: u32 = 3;
405+
pub const worldDataVersion: u32 = 4;
406406

407407
pub const ServerWorld = struct { // MARK: ServerWorld
408408
pub const dayCycle: u31 = 12000; // Length of one in-game day in units of 100ms. Midnight is at DAY_CYCLE/2. Sunrise and sunset each take about 1/16 of the day. Currently set to 20 minutes
@@ -532,7 +532,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
532532
}
533533

534534
pub fn loadWorldConfig(self: *ServerWorld, arena: NeverFailingAllocator, dir: main.files.Dir, worldData: ZonElement) !void { // MARK: loadWorldConfig
535-
if(worldData.get(u32, "version", 0) == 2) {
535+
if(worldData.get(u32, "version", 0) == 2) { // TODO: #2458
536536
std.log.info("Migrating old world with world version 2 to version 3", .{});
537537

538538
const gamerules = try dir.readToZon(arena, "gamerules.zig.zon");
@@ -555,6 +555,20 @@ pub const ServerWorld = struct { // MARK: ServerWorld
555555
try dir.deleteFile("generatorSettings.zig.zon");
556556
}
557557

558+
if(worldData.get(u32, "version", 0) == 3) { // TODO: #2458
559+
// In version 0.1.0 these values were written incorrectly
560+
const settings = worldData.getChild("settings");
561+
if(settings.removeChild("default_gamemode")) |gamemode| {
562+
settings.put("defaultGamemode", gamemode);
563+
}
564+
if(settings.removeChild("cheats")) |allowCheats| {
565+
settings.put("allowCheats", allowCheats);
566+
}
567+
568+
worldData.put("version", 4);
569+
try dir.writeZon("world.zig.zon", worldData);
570+
}
571+
558572
if(worldData.get(u32, "version", 0) != worldDataVersion) {
559573
std.log.err("Cannot read world file version {}. Expected version {}.", .{worldData.get(u32, "version", 0), worldDataVersion});
560574
return error.OldWorld;

src/zon.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ pub const ZonElement = union(enum) { // MARK: Zon
7272
return null;
7373
}
7474

75+
pub fn removeChild(self: *const ZonElement, key: []const u8) ?ZonElement {
76+
if(self.* != .object) return null;
77+
return (self.object.fetchRemove(key) orelse return null).value;
78+
}
79+
7580
pub fn clone(self: *const ZonElement, allocator: NeverFailingAllocator) ZonElement {
7681
return switch(self.*) {
7782
.int, .float, .string, .bool, .null => self.*,

0 commit comments

Comments
 (0)