Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hopefully save the hand and also when the player leaves or server closes #943

Merged
merged 17 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/Inventory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ pub const Sync = struct { // MARK: Sync
}
}
},
.playerInventory, .other => {},
.playerInventory, .hand, .other => {},
.alreadyFreed => unreachable,
}
const inventory = ServerInventory.init(len, typ, source);

switch (source) {
.sharedTestingInventory => {},
.playerInventory => {
.playerInventory, .hand => {
const dest: []u8 = main.stackAllocator.alloc(u8, std.base64.url_safe.Encoder.calcSize(user.name.len));
defer main.stackAllocator.free(dest);
const hashedName = std.base64.url_safe.Encoder.encode(dest, user.name);
Expand All @@ -328,7 +328,7 @@ pub const Sync = struct { // MARK: Sync
const playerData = main.files.readToZon(main.stackAllocator, path) catch .null;
defer playerData.deinit(main.stackAllocator);

const inventoryZon = playerData.getChild("inventory");
const inventoryZon = playerData.getChild(@tagName(source));

inventory.inv.loadFromZon(inventoryZon);
},
OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -352,10 +352,10 @@ pub const Sync = struct { // MARK: Sync
return inventories.items[serverId].inv;
}

pub fn getInventoryFromSource(source: SourceType) ?Inventory {
pub fn getInventoryFromSource(source: Source) ?Inventory {
main.utils.assertLocked(&mutex);
for(inventories.items) |inv| {
if (inv.source == source) {
if (std.meta.eql(inv.source, source)) {
return inv.inv;
}
}
Expand Down Expand Up @@ -1059,8 +1059,14 @@ pub const Command = struct { // MARK: Command
std.mem.writeInt(usize, data.addMany(8)[0..8], self.inv._items.len, .big);
data.append(@intFromEnum(self.inv.type));
data.append(@intFromEnum(self.source));
switch (self.source) {
.playerInventory, .hand => |val| {
std.mem.writeInt(u32, data.addMany(4)[0..4], val, .big);
},
else => {}
}
switch(self.source) {
.playerInventory, .sharedTestingInventory, .other => {},
.playerInventory, .sharedTestingInventory, .hand, .other => {},
.alreadyFreed => unreachable,
}
}
Expand All @@ -1073,8 +1079,9 @@ pub const Command = struct { // MARK: Command
const typ: Inventory.Type = @enumFromInt(data[12]);
const sourceType: SourceType = @enumFromInt(data[13]);
const source: Source = switch(sourceType) {
.playerInventory => .{.playerInventory = {}},
.playerInventory => .{.playerInventory = std.mem.readInt(u32, data[14..18], .big)},
.sharedTestingInventory => .{.sharedTestingInventory = {}},
.hand => .{.hand = std.mem.readInt(u32, data[14..18], .big)},
.other => .{.other = {}},
.alreadyFreed => unreachable,
};
Expand Down Expand Up @@ -1647,12 +1654,14 @@ const SourceType = enum(u8) {
alreadyFreed = 0,
playerInventory = 1,
sharedTestingInventory = 2,
hand = 3,
other = 0xff, // TODO: List every type separately here.
};
const Source = union(SourceType) {
alreadyFreed: void,
playerInventory: void,
playerInventory: u32,
sharedTestingInventory: void,
hand: u32,
other: void,
};

Expand Down Expand Up @@ -1691,7 +1700,13 @@ fn _init(allocator: NeverFailingAllocator, _size: usize, _type: Type, side: Side
}

pub fn deinit(self: Inventory, allocator: NeverFailingAllocator) void {
Sync.ClientSide.executeCommand(.{.close = .{.inv = self, .allocator = allocator}});
if (main.game.world.?.isConnected()) {
Sync.ClientSide.executeCommand(.{.close = .{.inv = self, .allocator = allocator}});
} else {
Sync.ClientSide.mutex.lock();
defer Sync.ClientSide.mutex.unlock();
self._deinit(allocator, .client);
}
}

fn _deinit(self: Inventory, allocator: NeverFailingAllocator, side: Side) void {
Expand Down
15 changes: 12 additions & 3 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ pub const World = struct { // MARK: World
milliTime: i64,
gameTime: Atomic(i64) = .init(0),
spawn: Vec3f = undefined,
connected: Atomic(bool) = .init(false),
OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
blockPalette: *assets.Palette = undefined,
biomePalette: *assets.Palette = undefined,
itemDrops: ClientItemDropManager = undefined,
Expand All @@ -533,6 +534,8 @@ pub const World = struct { // MARK: World
.name = "client",
.milliTime = std.time.milliTimestamp(),
};
self.connected.store(true, .monotonic);

self.itemDrops.init(main.globalAllocator, self);
network.Protocols.handShake.clientSide(self.conn, settings.playerName);

Expand All @@ -545,15 +548,17 @@ pub const World = struct { // MARK: World
}

pub fn deinit(self: *World) void {
self.conn.deinit();

self.connected.store(false, .monotonic);

// TODO: Close all world related guis.
main.gui.inventory.deinit();
main.gui.deinit();
main.gui.init();

Player.inventory.deinit(main.globalAllocator);

main.threadPool.clear();
self.conn.deinit();
self.itemDrops.deinit();
self.blockPalette.deinit();
self.biomePalette.deinit();
Expand All @@ -580,7 +585,11 @@ pub const World = struct { // MARK: World
try assets.loadWorldAssets("serverAssets", self.blockPalette, self.biomePalette);
Player.loadFrom(zon.getChild("player"));
Player.id = zon.get(u32, "player_id", std.math.maxInt(u32));
Player.inventory = Inventory.init(main.globalAllocator, 32, .normal, .playerInventory);
Player.inventory = Inventory.init(main.globalAllocator, 32, .normal, .{.playerInventory = Player.id});
}

pub fn isConnected(self: *World) bool {
return self.connected.load(.monotonic);
}

pub fn update(self: *World) void {
Expand Down
2 changes: 1 addition & 1 deletion src/gui/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ pub const inventory = struct { // MARK: inventory
var initialized: bool = false;

pub fn init() void {
carried = Inventory.init(main.globalAllocator, 1, .normal, .other);
carried = Inventory.init(main.globalAllocator, 1, .normal, .{.hand = main.game.Player.id});
leftClickSlots = .init(main.globalAllocator);
rightClickSlots = .init(main.globalAllocator);
carriedItemSlot = ItemSlot.init(.{0, 0}, carried, 0, .default, .normal);
Expand Down
6 changes: 6 additions & 0 deletions src/server/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ pub const User = struct { // MARK: User

pub fn deinit(self: *User) void {
std.debug.assert(self.refCount.load(.monotonic) == 0);

world.?.savePlayer(self) catch |err| {
OneAvargeCoder193 marked this conversation as resolved.
Show resolved Hide resolved
std.log.err("Failed to save player: {s}", .{@errorName(err)});
return;
};

main.items.Inventory.Sync.ServerSide.disconnectUser(self);
std.debug.assert(self.inventoryClientToServerIdMap.count() == 0); // leak
self.inventoryClientToServerIdMap.deinit();
Expand Down
8 changes: 6 additions & 2 deletions src/server/world.zig
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,12 @@ 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)) |inv| {
playerZon.put("inventory", inv.save(main.stackAllocator));
if (main.items.Inventory.Sync.ServerSide.getInventoryFromSource(.{.playerInventory = user.id})) |inv| {
playerZon.put("playerInventory", inv.save(main.stackAllocator));
}

if (main.items.Inventory.Sync.ServerSide.getInventoryFromSource(.{.hand = user.id})) |inv| {
playerZon.put("hand", inv.save(main.stackAllocator));
}
}

Expand Down
Loading