mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 11:17:05 -04:00
I forgot to add the file
This commit is contained in:
parent
176c9d4c75
commit
b0e067a458
58
src/tag.zig
Normal file
58
src/tag.zig
Normal file
@ -0,0 +1,58 @@
|
||||
const std = @import("std");
|
||||
|
||||
const main = @import("main");
|
||||
|
||||
|
||||
var arena: main.heap.NeverFailingArenaAllocator = .init(main.globalAllocator);
|
||||
const allocator = arena.allocator();
|
||||
var tagList: main.List([]const u8) = .init(allocator);
|
||||
var tagIds: std.StringHashMap(Tag) = .init(allocator.allocator);
|
||||
|
||||
pub fn init() void {
|
||||
loadDefaults();
|
||||
}
|
||||
|
||||
pub fn deinit() void {
|
||||
arena.deinit();
|
||||
}
|
||||
|
||||
fn loadDefaults() void {
|
||||
inline for(comptime std.meta.fieldNames(Tag)) |tag| {
|
||||
std.debug.assert(Tag.find(tag) == @field(Tag, tag));
|
||||
}
|
||||
}
|
||||
|
||||
pub const Tag = enum(u32) {
|
||||
air = 0,
|
||||
fluid = 1,
|
||||
sbbChild = 2,
|
||||
_,
|
||||
|
||||
pub fn resetTags() void {
|
||||
tagList.clearAndFree();
|
||||
tagIds.clearAndFree();
|
||||
_ = arena.reset(.free_all);
|
||||
loadDefaults();
|
||||
}
|
||||
|
||||
pub fn find(tag: []const u8) Tag {
|
||||
if(tagIds.get(tag)) |res| return res;
|
||||
const result: Tag = @enumFromInt(tagList.items.len);
|
||||
const dupedTag = allocator.dupe(u8, tag);
|
||||
tagList.append(dupedTag);
|
||||
tagIds.put(dupedTag, result) catch unreachable;
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn loadTagsFromZon(_allocator: main.heap.NeverFailingAllocator, zon: main.ZonElement) []Tag {
|
||||
const result = _allocator.alloc(Tag, zon.toSlice().len);
|
||||
for(zon.toSlice(), 0..) |tagZon, i| {
|
||||
result[i] = Tag.find(tagZon.as([]const u8, "incorrect"));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn getName(tag: Tag) []const u8 {
|
||||
return tagList.items[@intFromEnum(tag)];
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user