mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 19:28:49 -04:00
move toBase64 into world.zig
This commit is contained in:
parent
a8a2c14f8d
commit
e204a27fe2
@ -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 {
|
fn toBytes(self: Inventory, writer: *BinaryWriter) void {
|
||||||
writer.writeVarInt(u32, @intCast(self._items.len));
|
writer.writeVarInt(u32, @intCast(self._items.len));
|
||||||
for(self._items) |stack| {
|
for(self._items) |stack| {
|
||||||
|
@ -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"});
|
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;
|
const old = playerData.object.fetchPut(key, .{.stringOwned = base64Data}) catch unreachable orelse unreachable;
|
||||||
old.value.deinit(main.stackAllocator);
|
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);
|
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 {
|
pub fn savePlayer(self: *ServerWorld, user: *User) !void {
|
||||||
const dest: []u8 = main.stackAllocator.alloc(u8, std.base64.url_safe.Encoder.calcSize(user.name.len));
|
const dest: []u8 = main.stackAllocator.alloc(u8, std.base64.url_safe.Encoder.calcSize(user.name.len));
|
||||||
defer main.stackAllocator.free(dest);
|
defer main.stackAllocator.free(dest);
|
||||||
@ -990,11 +1000,11 @@ pub const ServerWorld = struct { // MARK: ServerWorld
|
|||||||
main.items.Inventory.Sync.ServerSide.mutex.lock();
|
main.items.Inventory.Sync.ServerSide.mutex.lock();
|
||||||
defer main.items.Inventory.Sync.ServerSide.mutex.unlock();
|
defer main.items.Inventory.Sync.ServerSide.mutex.unlock();
|
||||||
if(main.items.Inventory.Sync.ServerSide.getInventoryFromSource(.{.playerInventory = user.id})) |inv| {
|
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.");
|
} else @panic("The player inventory wasn't found. Cannot save player data.");
|
||||||
|
|
||||||
if(main.items.Inventory.Sync.ServerSide.getInventoryFromSource(.{.hand = user.id})) |inv| {
|
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.");
|
} else @panic("The player hand inventory wasn't found. Cannot save player data.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user