mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 19:28:49 -04:00
Rename ZonElement.free to deinit to follow zig convention.
This commit is contained in:
parent
1c5229e480
commit
de2e4aedb4
@ -481,7 +481,7 @@ pub const Command = struct { // MARK: Command
|
||||
};
|
||||
if(data.len > 12) {
|
||||
const zon = ZonElement.parseFromString(main.stackAllocator, data[12..]);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
self.item = try Item.init(zon);
|
||||
}
|
||||
if(self.amount > 0) { // Create
|
||||
@ -513,7 +513,7 @@ pub const Command = struct { // MARK: Command
|
||||
std.mem.writeInt(i32, data.addMany(4)[0..4], self.amount, .big);
|
||||
if(self.item) |item| {
|
||||
const zon = ZonElement.initObject(main.stackAllocator);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
item.insertIntoZon(main.stackAllocator, zon);
|
||||
const string = zon.toStringEfficient(main.stackAllocator, &.{});
|
||||
defer main.stackAllocator.free(string);
|
||||
@ -1104,7 +1104,7 @@ pub const Command = struct { // MARK: Command
|
||||
std.mem.writeInt(u16, data.addMany(2)[0..2], self.amount, .big);
|
||||
if(self.item) |item| {
|
||||
const zon = ZonElement.initObject(main.stackAllocator);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
item.insertIntoZon(main.stackAllocator, zon);
|
||||
const string = zon.toStringEfficient(main.stackAllocator, &.{});
|
||||
defer main.stackAllocator.free(string);
|
||||
@ -1118,7 +1118,7 @@ pub const Command = struct { // MARK: Command
|
||||
var item: ?Item = null;
|
||||
if(data.len > 10) {
|
||||
const zon = ZonElement.parseFromString(main.stackAllocator, data[10..]);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
item = try Item.init(zon);
|
||||
}
|
||||
return .{
|
||||
|
@ -521,7 +521,7 @@ pub const meshes = struct { // MARK: meshes
|
||||
const path = buffer[0.._path.len];
|
||||
const textureInfoPath = extendedPath(path, &buffer, "_textureInfo.zig.zon");
|
||||
const textureInfoZon = main.files.readToZon(main.stackAllocator, textureInfoPath) catch .null;
|
||||
defer textureInfoZon.free(main.stackAllocator);
|
||||
defer textureInfoZon.deinit(main.stackAllocator);
|
||||
const animationFrames = textureInfoZon.get(u32, "frames", 1);
|
||||
const animationTime = textureInfoZon.get(u32, "time", 1);
|
||||
animation.append(.{.startFrame = @intCast(blockTextures.items.len), .frames = animationFrames, .time = animationTime});
|
||||
|
@ -174,7 +174,7 @@ pub fn deinit() void {
|
||||
|
||||
pub fn save() void { // MARK: save()
|
||||
const guiZon = ZonElement.initObject(main.stackAllocator);
|
||||
defer guiZon.free(main.stackAllocator);
|
||||
defer guiZon.deinit(main.stackAllocator);
|
||||
for(windowList.items) |window| {
|
||||
const windowZon = ZonElement.initObject(main.stackAllocator);
|
||||
for(window.relativePosition, 0..) |relPos, i| {
|
||||
@ -219,7 +219,7 @@ fn load() void {
|
||||
}
|
||||
break :blk .null;
|
||||
};
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
|
||||
for(windowList.items) |window| {
|
||||
const windowZon = zon.getChild(window.id);
|
||||
|
@ -43,7 +43,7 @@ fn flawedCreateWorld() !void {
|
||||
const generatorSettingsPath = std.fmt.allocPrint(main.stackAllocator.allocator, "saves/{s}/generatorSettings.zig.zon", .{worldName}) catch unreachable;
|
||||
defer main.stackAllocator.free(generatorSettingsPath);
|
||||
const generatorSettings = main.ZonElement.initObject(main.stackAllocator);
|
||||
defer generatorSettings.free(main.stackAllocator);
|
||||
defer generatorSettings.deinit(main.stackAllocator);
|
||||
const climateGenerator = main.ZonElement.initObject(main.stackAllocator);
|
||||
climateGenerator.put("id", "cubyz:noise_based_voronoi"); // TODO: Make this configurable
|
||||
generatorSettings.put("climateGenerator", climateGenerator);
|
||||
|
@ -91,7 +91,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
|
||||
}
|
||||
}
|
||||
self.list.deinit(self.allocator.allocator);
|
||||
self.lastUpdates.free(self.allocator);
|
||||
self.lastUpdates.deinit(self.allocator);
|
||||
}
|
||||
|
||||
pub fn loadFrom(self: *ItemDropManager, zon: ZonElement) void {
|
||||
@ -206,7 +206,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
|
||||
const i: u16 = @intCast(self.isEmpty.findFirstSet() orelse {
|
||||
self.emptyMutex.unlock();
|
||||
const zon = itemStack.store(main.stackAllocator);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
const string = zon.toString(main.stackAllocator);
|
||||
defer main.stackAllocator.free(string);
|
||||
std.log.err("Item drop capacitiy limit reached. Failed to add itemStack: {s}", .{string});
|
||||
|
@ -645,7 +645,7 @@ pub const Protocols = struct {
|
||||
switch(data[0]) {
|
||||
stepUserData => {
|
||||
const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
const name = zon.get([]const u8, "name", "unnamed");
|
||||
const version = zon.get([]const u8, "version", "unknown");
|
||||
std.log.info("User {s} joined using version {s}.", .{name, version});
|
||||
@ -664,7 +664,7 @@ pub const Protocols = struct {
|
||||
|
||||
conn.user.?.initPlayer(name);
|
||||
const zonObject = ZonElement.initObject(main.stackAllocator);
|
||||
defer zonObject.free(main.stackAllocator);
|
||||
defer zonObject.deinit(main.stackAllocator);
|
||||
zonObject.put("player", conn.user.?.player.save(main.stackAllocator));
|
||||
zonObject.put("spawn", main.server.world.?.spawn);
|
||||
zonObject.put("blockPalette", main.server.world.?.blockPalette.save(main.stackAllocator));
|
||||
@ -689,7 +689,7 @@ pub const Protocols = struct {
|
||||
},
|
||||
stepServerData => {
|
||||
const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
try conn.manager.world.?.finishHandshake(zon);
|
||||
conn.handShakeState.store(stepComplete, .monotonic);
|
||||
conn.handShakeWaiting.broadcast(); // Notify the waiting client thread.
|
||||
@ -712,7 +712,7 @@ pub const Protocols = struct {
|
||||
|
||||
pub fn clientSide(conn: *Connection, name: []const u8) void {
|
||||
const zonObject = ZonElement.initObject(main.stackAllocator);
|
||||
defer zonObject.free(main.stackAllocator);
|
||||
defer zonObject.deinit(main.stackAllocator);
|
||||
zonObject.putOwnedString("version", settings.version);
|
||||
zonObject.putOwnedString("name", name);
|
||||
const prefix = [1]u8 {stepUserData};
|
||||
@ -924,7 +924,7 @@ pub const Protocols = struct {
|
||||
pub const asynchronous = false;
|
||||
fn receive(conn: *Connection, data: []const u8) !void {
|
||||
const zonArray = ZonElement.parseFromString(main.stackAllocator, data);
|
||||
defer zonArray.free(main.stackAllocator);
|
||||
defer zonArray.deinit(main.stackAllocator);
|
||||
var i: u32 = 0;
|
||||
while(i < zonArray.array.items.len) : (i += 1) {
|
||||
const elem = zonArray.array.items[i];
|
||||
@ -996,7 +996,7 @@ pub const Protocols = struct {
|
||||
type_timeAndBiome => {
|
||||
if(conn.manager.world) |world| {
|
||||
const zon = ZonElement.parseFromString(main.stackAllocator, data[1..]);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
const expectedTime = zon.get(i64, "time", 0);
|
||||
var curTime = world.gameTime.load(.monotonic);
|
||||
if(@abs(curTime -% expectedTime) >= 10) {
|
||||
@ -1060,7 +1060,7 @@ pub const Protocols = struct {
|
||||
|
||||
pub fn sendTimeAndBiome(conn: *Connection, world: *const main.server.ServerWorld) void {
|
||||
const zon = ZonElement.initObject(main.stackAllocator);
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
zon.put("time", world.gameTime);
|
||||
const pos = conn.user.?.player.pos;
|
||||
zon.put("biome", (world.getBiome(@intFromFloat(pos[0]), @intFromFloat(pos[1]), @intFromFloat(pos[2]))).id);
|
||||
|
@ -289,7 +289,7 @@ pub fn freeUserListAndDecreaseRefCount(allocator: utils.NeverFailingAllocator, l
|
||||
fn sendEntityUpdates(comptime getInitialList: bool, allocator: utils.NeverFailingAllocator) if(getInitialList) []const u8 else void {
|
||||
// Send the entity updates:
|
||||
const updateList = main.ZonElement.initArray(main.stackAllocator);
|
||||
defer updateList.free(main.stackAllocator);
|
||||
defer updateList.deinit(main.stackAllocator);
|
||||
defer updateList.array.clearAndFree(); // The children are freed in other locations.
|
||||
if(world.?.itemDropManager.lastUpdates.array.items.len != 0) {
|
||||
updateList.array.append(.null);
|
||||
@ -302,18 +302,18 @@ fn sendEntityUpdates(comptime getInitialList: bool, allocator: utils.NeverFailin
|
||||
defer main.stackAllocator.free(updateData);
|
||||
if(world.?.itemDropManager.lastUpdates.array.items.len != 0) {
|
||||
const alloc = world.?.itemDropManager.lastUpdates.array.allocator;
|
||||
world.?.itemDropManager.lastUpdates.free(alloc);
|
||||
world.?.itemDropManager.lastUpdates.deinit(alloc);
|
||||
world.?.itemDropManager.lastUpdates = main.ZonElement.initArray(alloc);
|
||||
}
|
||||
var initialList: []const u8 = undefined;
|
||||
if(getInitialList) {
|
||||
const list = main.ZonElement.initArray(main.stackAllocator);
|
||||
defer list.free(main.stackAllocator);
|
||||
defer list.deinit(main.stackAllocator);
|
||||
list.array.append(.null);
|
||||
const itemDropList = world.?.itemDropManager.getInitialList(main.stackAllocator);
|
||||
list.array.appendSlice(itemDropList.array.items);
|
||||
itemDropList.array.items.len = 0;
|
||||
itemDropList.free(main.stackAllocator);
|
||||
itemDropList.deinit(main.stackAllocator);
|
||||
initialList = list.toStringEfficient(allocator, &.{});
|
||||
}
|
||||
const userList = getUserListAndIncreaseRefCount(main.stackAllocator);
|
||||
@ -423,7 +423,7 @@ pub fn removePlayer(user: *User) void { // MARK: removePlayer()
|
||||
sendMessage(message);
|
||||
// Let the other clients know about that this new one left.
|
||||
const zonArray = main.ZonElement.initArray(main.stackAllocator);
|
||||
defer zonArray.free(main.stackAllocator);
|
||||
defer zonArray.deinit(main.stackAllocator);
|
||||
zonArray.array.append(.{.int = user.id});
|
||||
const data = zonArray.toStringEfficient(main.stackAllocator, &.{});
|
||||
defer main.stackAllocator.free(data);
|
||||
@ -448,7 +448,7 @@ pub fn connectInternal(user: *User) void {
|
||||
// Let the other clients know about this new one.
|
||||
{
|
||||
const zonArray = main.ZonElement.initArray(main.stackAllocator);
|
||||
defer zonArray.free(main.stackAllocator);
|
||||
defer zonArray.deinit(main.stackAllocator);
|
||||
const entityZon = main.ZonElement.initObject(main.stackAllocator);
|
||||
entityZon.put("id", user.id);
|
||||
entityZon.put("name", user.name);
|
||||
@ -461,7 +461,7 @@ pub fn connectInternal(user: *User) void {
|
||||
}
|
||||
{ // Let this client know about the others:
|
||||
const zonArray = main.ZonElement.initArray(main.stackAllocator);
|
||||
defer zonArray.free(main.stackAllocator);
|
||||
defer zonArray.deinit(main.stackAllocator);
|
||||
for(userList) |other| {
|
||||
const entityZon = main.ZonElement.initObject(main.stackAllocator);
|
||||
entityZon.put("id", other.id);
|
||||
|
@ -378,7 +378,7 @@ const WorldIO = struct { // MARK: WorldIO
|
||||
/// Load the seed, which is needed before custom item and ore generation.
|
||||
pub fn loadWorldSeed(self: WorldIO) !u64 {
|
||||
const worldData = try self.dir.readToZon(main.stackAllocator, "world.zig.zon");
|
||||
defer worldData.free(main.stackAllocator);
|
||||
defer worldData.deinit(main.stackAllocator);
|
||||
if(worldData.get(u32, "version", 0) != worldDataVersion) {
|
||||
std.log.err("Cannot read world file version {}. Expected version {}.", .{worldData.get(u32, "version", 0), worldDataVersion});
|
||||
return error.OldWorld;
|
||||
@ -388,7 +388,7 @@ const WorldIO = struct { // MARK: WorldIO
|
||||
|
||||
pub fn loadWorldData(self: WorldIO) !void {
|
||||
const worldData = try self.dir.readToZon(main.stackAllocator, "world.zig.zon");
|
||||
defer worldData.free(main.stackAllocator);
|
||||
defer worldData.deinit(main.stackAllocator);
|
||||
|
||||
self.world.doGameTimeCycle = worldData.get(bool, "doGameTimeCycle", true);
|
||||
self.world.gameTime = worldData.get(i64, "gameTime", 0);
|
||||
@ -398,7 +398,7 @@ const WorldIO = struct { // MARK: WorldIO
|
||||
|
||||
pub fn saveWorldData(self: WorldIO) !void {
|
||||
const worldData = ZonElement.initObject(main.stackAllocator);
|
||||
defer worldData.free(main.stackAllocator);
|
||||
defer worldData.deinit(main.stackAllocator);
|
||||
worldData.put("version", worldDataVersion);
|
||||
worldData.put("seed", self.world.seed);
|
||||
worldData.put("doGameTimeCycle", self.world.doGameTimeCycle);
|
||||
@ -757,7 +757,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
|
||||
try self.wio.saveWorldData();
|
||||
var buf: [32768]u8 = undefined;
|
||||
const zon = files.readToZon(main.stackAllocator, try std.fmt.bufPrint(&buf, "saves/{s}/items.zig.zon", .{self.name})) catch .null;
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
self.itemDropManager.loadFrom(zon);
|
||||
}
|
||||
|
||||
@ -765,7 +765,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
|
||||
pub fn findPlayer(self: *ServerWorld, user: *User) void {
|
||||
var buf: [1024]u8 = undefined;
|
||||
const playerData = files.readToZon(main.stackAllocator, std.fmt.bufPrint(&buf, "saves/{s}/player/{s}.zig.zon", .{self.name, user.name}) catch "") catch .null; // TODO: Utils.escapeFolderName(user.name)
|
||||
defer playerData.free(main.stackAllocator);
|
||||
defer playerData.deinit(main.stackAllocator);
|
||||
const player = &user.player;
|
||||
if(playerData == .null) {
|
||||
// Generate a new player:
|
||||
@ -779,7 +779,7 @@ pub const ServerWorld = struct { // MARK: ServerWorld
|
||||
// TODO: Save chunks and player data
|
||||
try self.wio.saveWorldData();
|
||||
const itemDropZon = self.itemDropManager.store(main.stackAllocator);
|
||||
defer itemDropZon.free(main.stackAllocator);
|
||||
defer itemDropZon.deinit(main.stackAllocator);
|
||||
var buf: [32768]u8 = undefined;
|
||||
try files.writeZon(try std.fmt.bufPrint(&buf, "saves/{s}/items.zig.zon", .{self.name}), itemDropZon);
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ pub fn init() void {
|
||||
}
|
||||
break :blk .null;
|
||||
};
|
||||
defer zon.free(main.stackAllocator);
|
||||
defer zon.deinit(main.stackAllocator);
|
||||
|
||||
inline for(@typeInfo(@This()).@"struct".decls) |decl| {
|
||||
const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).pointer.is_const; // Sadly there is no direct way to check if a declaration is const.
|
||||
@ -124,7 +124,7 @@ pub fn deinit() void {
|
||||
|
||||
pub fn save() void {
|
||||
const zonObject = ZonElement.initObject(main.stackAllocator);
|
||||
defer zonObject.free(main.stackAllocator);
|
||||
defer zonObject.deinit(main.stackAllocator);
|
||||
|
||||
inline for(@typeInfo(@This()).@"struct".decls) |decl| {
|
||||
const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).pointer.is_const; // Sadly there is no direct way to check if a declaration is const.
|
||||
|
22
src/zon.zig
22
src/zon.zig
@ -205,7 +205,7 @@ pub const ZonElement = union(enum) { // MARK: Zon
|
||||
}
|
||||
}
|
||||
|
||||
pub fn free(self: *const ZonElement, allocator: NeverFailingAllocator) void {
|
||||
pub fn deinit(self: *const ZonElement, allocator: NeverFailingAllocator) void {
|
||||
switch(self.*) {
|
||||
.int, .float, .bool, .null, .string => return,
|
||||
.stringOwned => {
|
||||
@ -213,7 +213,7 @@ pub const ZonElement = union(enum) { // MARK: Zon
|
||||
},
|
||||
.array => {
|
||||
for(self.array.items) |*elem| {
|
||||
elem.free(allocator);
|
||||
elem.deinit(allocator);
|
||||
}
|
||||
self.array.clearAndFree();
|
||||
allocator.destroy(self.array);
|
||||
@ -223,7 +223,7 @@ pub const ZonElement = union(enum) { // MARK: Zon
|
||||
while(true) {
|
||||
const elem = iterator.next() orelse break;
|
||||
allocator.free(elem.key_ptr.*);
|
||||
elem.value_ptr.free(allocator);
|
||||
elem.value_ptr.deinit(allocator);
|
||||
}
|
||||
self.object.clearAndFree();
|
||||
allocator.destroy(self.object);
|
||||
@ -563,7 +563,7 @@ const Parser = struct { // MARK: Parser
|
||||
if(map.fetchPut(key, value) catch unreachable) |old| {
|
||||
printError(chars, index.*, "Duplicate key.");
|
||||
allocator.free(old.key);
|
||||
old.value.free(allocator);
|
||||
old.value.deinit(allocator);
|
||||
}
|
||||
skipWhitespaces(chars, index);
|
||||
if(index.* < chars.len and chars[index.*] == ',') {
|
||||
@ -796,23 +796,23 @@ test "element parsing" {
|
||||
index = 0;
|
||||
var result: ZonElement = Parser.parseElement(allocator, "\"abcd\\\"\\\\ħσ→ ↑Φ∫€ ⌬ ε→Π\"", &index);
|
||||
try std.testing.expectEqualStrings("abcd\"\\ħσ→ ↑Φ∫€ ⌬ ε→Π", result.as([]const u8, ""));
|
||||
result.free(allocator);
|
||||
result.deinit(allocator);
|
||||
index = 0;
|
||||
result = Parser.parseElement(allocator, "\"12345", &index);
|
||||
try std.testing.expectEqualStrings("12345", result.as([]const u8, ""));
|
||||
result.free(allocator);
|
||||
result.deinit(allocator);
|
||||
|
||||
// Object:
|
||||
index = 0;
|
||||
result = Parser.parseElement(allocator, ".{.name = 1}", &index);
|
||||
try std.testing.expectEqual(.object, std.meta.activeTag(result));
|
||||
try std.testing.expectEqual(result.object.get("name"), ZonElement{.int = 1});
|
||||
result.free(allocator);
|
||||
result.deinit(allocator);
|
||||
index = 0;
|
||||
result = Parser.parseElement(allocator, ".{@\"object\"=.{},}", &index);
|
||||
try std.testing.expectEqual(.object, std.meta.activeTag(result));
|
||||
try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("object") orelse .null));
|
||||
result.free(allocator);
|
||||
result.deinit(allocator);
|
||||
index = 0;
|
||||
result = Parser.parseElement(allocator, ".{ .object1 = \"\" \n, .object2 =\t.{\n},.object3 =1.0e4\t,@\"\nobject1\"=.{},@\"\tobject1θ\"=.{},}", &index);
|
||||
try std.testing.expectEqual(.object, std.meta.activeTag(result));
|
||||
@ -820,7 +820,7 @@ test "element parsing" {
|
||||
try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.object.get("object1") orelse .null));
|
||||
try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("\nobject1") orelse .null));
|
||||
try std.testing.expectEqual(.array, std.meta.activeTag(result.object.get("\tobject1θ") orelse .null));
|
||||
result.free(allocator);
|
||||
result.deinit(allocator);
|
||||
|
||||
//Array:
|
||||
index = 0;
|
||||
@ -828,12 +828,12 @@ test "element parsing" {
|
||||
try std.testing.expectEqual(.array, std.meta.activeTag(result));
|
||||
try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.array.items[0]));
|
||||
try std.testing.expectEqual(ZonElement{.int=1}, result.array.items[1]);
|
||||
result.free(allocator);
|
||||
result.deinit(allocator);
|
||||
index = 0;
|
||||
result = Parser.parseElement(allocator, ".{ \"name\"\t1\n, 17.1}", &index);
|
||||
try std.testing.expectEqual(.array, std.meta.activeTag(result));
|
||||
try std.testing.expectEqual(.stringOwned, std.meta.activeTag(result.array.items[0]));
|
||||
try std.testing.expectEqual(ZonElement{.int=1}, result.array.items[1]);
|
||||
try std.testing.expectEqual(ZonElement{.float=17.1}, result.array.items[2]);
|
||||
result.free(allocator);
|
||||
result.deinit(allocator);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user