From e204a27fe28f0c38930c2e97b37e95e9685758b9 Mon Sep 17 00:00:00 2001 From: OneAvargeCoder193 Date: Fri, 25 Jul 2025 16:20:38 -0400 Subject: [PATCH] move toBase64 into world.zig --- src/Inventory.zig | 10 ---------- src/server/world.zig | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Inventory.zig b/src/Inventory.zig index 43721060..908e6ba7 100644 --- a/src/Inventory.zig +++ b/src/Inventory.zig @@ -2011,16 +2011,6 @@ pub fn loadFromZon(self: Inventory, zon: ZonElement) void { } } -pub fn toBase64(self: Inventory, allocator: NeverFailingAllocator) []const u8 { - var writer = BinaryWriter.init(main.stackAllocator); - defer writer.deinit(); - - self.toBytes(&writer); - - const destination: []u8 = allocator.alloc(u8, std.base64.url_safe.Encoder.calcSize(writer.data.items.len)); - return std.base64.url_safe.Encoder.encode(destination, writer.data.items); -} - fn toBytes(self: Inventory, writer: *BinaryWriter) void { writer.writeVarInt(u32, @intCast(self._items.len)); for(self._items) |stack| { diff --git a/src/server/world.zig b/src/server/world.zig index d61f17b0..4ae5a096 100644 --- a/src/server/world.zig +++ b/src/server/world.zig @@ -594,7 +594,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld std.log.debug("Item #{}: {} x {s}", .{i, stack.amount, if(stack.item) |item| item.id() else "null"}); } - const base64Data = temp.toBase64(main.stackAllocator); + const base64Data = savePlayerInventory(temp, main.stackAllocator); const old = playerData.object.fetchPut(key, .{.stringOwned = base64Data}) catch unreachable orelse unreachable; old.value.deinit(main.stackAllocator); }, @@ -965,6 +965,16 @@ pub const ServerWorld = struct { // MARK: ServerWorld return main.items.Inventory.Sync.ServerSide.createExternallyManagedInventory(size, .normal, source, &reader); } + fn savePlayerInventory(inv: main.items.Inventory, allocator: NeverFailingAllocator) []const u8 { + var writer = main.utils.BinaryWriter.init(main.stackAllocator); + defer writer.deinit(); + + inv.toBytes(&writer); + + const destination: []u8 = allocator.alloc(u8, std.base64.url_safe.Encoder.calcSize(writer.data.items.len)); + return std.base64.url_safe.Encoder.encode(destination, writer.data.items); + } + pub fn savePlayer(self: *ServerWorld, user: *User) !void { const dest: []u8 = main.stackAllocator.alloc(u8, std.base64.url_safe.Encoder.calcSize(user.name.len)); defer main.stackAllocator.free(dest); @@ -990,11 +1000,11 @@ pub const ServerWorld = struct { // MARK: ServerWorld main.items.Inventory.Sync.ServerSide.mutex.lock(); defer main.items.Inventory.Sync.ServerSide.mutex.unlock(); if(main.items.Inventory.Sync.ServerSide.getInventoryFromSource(.{.playerInventory = user.id})) |inv| { - playerZon.put("playerInventory", ZonElement{.stringOwned = inv.toBase64(main.stackAllocator)}); + playerZon.put("playerInventory", ZonElement{.stringOwned = savePlayerInventory(inv, main.stackAllocator)}); } else @panic("The player inventory wasn't found. Cannot save player data."); if(main.items.Inventory.Sync.ServerSide.getInventoryFromSource(.{.hand = user.id})) |inv| { - playerZon.put("hand", ZonElement{.stringOwned = inv.toBase64(main.stackAllocator)}); + playerZon.put("hand", ZonElement{.stringOwned = savePlayerInventory(inv, main.stackAllocator)}); } else @panic("The player hand inventory wasn't found. Cannot save player data."); }