add zig build for compendium packs
move document types from template.json into system.json change Talents into Items add rolls for Talents change the fallback language to german
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
const std = @import("std");
|
||||
const c = @cImport({
|
||||
@cInclude("leveldb/c.h");
|
||||
});
|
||||
|
||||
db: *c.leveldb_t,
|
||||
|
||||
pub const Diagnostic = ?[:0]const u8;
|
||||
pub const OpenOptions = struct {
|
||||
create_if_missing: bool = false,
|
||||
error_if_exists: bool = false,
|
||||
paranoid_checks: bool = false,
|
||||
|
||||
compression: enum(c_int) {
|
||||
None = c.leveldb_no_compression,
|
||||
Snappy = c.leveldb_snappy_compression,
|
||||
} = .Snappy,
|
||||
};
|
||||
|
||||
pub const ReadOptions = struct {
|
||||
verify_checksums: bool = false,
|
||||
fill_cache: bool = true,
|
||||
};
|
||||
|
||||
pub const WriteOptions = struct {
|
||||
sync: bool = false,
|
||||
};
|
||||
|
||||
pub const OpenArgs = struct {
|
||||
path: [:0]const u8,
|
||||
|
||||
options: OpenOptions = .{},
|
||||
diagnostic: ?*Diagnostic = null,
|
||||
};
|
||||
|
||||
pub fn open(args: OpenArgs) !@This() {
|
||||
const options = create_openoptions(args.options);
|
||||
defer c.leveldb_options_destroy(options);
|
||||
|
||||
var err: [*c]u8 = null;
|
||||
const result = c.leveldb_open(options, args.path, &err);
|
||||
if (err != 0) {
|
||||
if (args.diagnostic) |diag| diag.* = std.mem.sliceTo(err, 0);
|
||||
return error.FAILED;
|
||||
}
|
||||
|
||||
return .{ .db = result.? };
|
||||
}
|
||||
|
||||
pub fn close(self: @This()) void {
|
||||
c.leveldb_close(self.db);
|
||||
}
|
||||
|
||||
pub const DestroyArgs = struct {
|
||||
path: [:0]const u8,
|
||||
|
||||
options: OpenOptions = .{},
|
||||
diagnostic: ?*Diagnostic = null,
|
||||
};
|
||||
|
||||
pub fn destroy(args: DestroyArgs) !void {
|
||||
const options = create_openoptions(args.options);
|
||||
defer c.leveldb_options_destroy(options);
|
||||
|
||||
var err: [*c]u8 = null;
|
||||
c.leveldb_destroy_db(options, args.path, &err);
|
||||
if (err != 0) {
|
||||
if (args.diagnostic) |diag| diag.* = std.mem.sliceTo(err, 0);
|
||||
return error.FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
pub const PutArgs = struct {
|
||||
key: []const u8,
|
||||
value: []const u8,
|
||||
|
||||
options: WriteOptions = .{},
|
||||
diagnostic: ?*Diagnostic = null,
|
||||
};
|
||||
|
||||
pub fn put(self: @This(), args: PutArgs) !void {
|
||||
const options = create_writeoptions(args.options);
|
||||
defer c.leveldb_writeoptions_destroy(options);
|
||||
|
||||
var err: [*c]u8 = null;
|
||||
c.leveldb_put(self.db, options, args.key.ptr, args.key.len, args.value.ptr, args.value.len, &err);
|
||||
if (err != 0) {
|
||||
if (args.diagnostic) |diag| diag.* = std.mem.sliceTo(err, 0);
|
||||
return error.FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
pub const DeleteArgs = struct {
|
||||
key: []const u8,
|
||||
|
||||
options: WriteOptions = .{},
|
||||
diagnostic: ?*Diagnostic = null,
|
||||
};
|
||||
|
||||
pub fn delete(self: @This(), args: DeleteArgs) !void {
|
||||
const options = create_writeoptions(args.options);
|
||||
defer c.leveldb_writeoptions_destroy(options);
|
||||
|
||||
var err: [*c]u8 = null;
|
||||
c.leveldb_delete(self.db, options, args.key.ptr, args.key.len, &err);
|
||||
if (err != 0) {
|
||||
if (args.diagnostic) |diag| diag.* = std.mem.sliceTo(err, 0);
|
||||
return error.FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
pub const GetArgs = struct {
|
||||
key: []const u8,
|
||||
|
||||
options: ReadOptions = .{},
|
||||
diagnostic: ?*Diagnostic = null,
|
||||
};
|
||||
|
||||
pub fn get(self: @This(), args: GetArgs) ![]const u8 {
|
||||
const options = create_readoptions(args.options);
|
||||
defer c.leveldb_readoptions_destroy(options);
|
||||
|
||||
var value_length: usize = 0;
|
||||
var err: [*c]u8 = null;
|
||||
const value_ptr = c.leveldb_get(self.db, options, args.key.ptr, args.key.len, &value_length, &err);
|
||||
if (err != 0) {
|
||||
if (args.diagnostic) |diag| diag.* = std.mem.sliceTo(err, 0);
|
||||
return error.FAILED;
|
||||
}
|
||||
|
||||
return value_ptr[0..value_length];
|
||||
}
|
||||
|
||||
pub const Iterator = struct {
|
||||
iterator: *c.leveldb_iterator_t,
|
||||
|
||||
pub fn destroy(self: @This()) void {
|
||||
c.leveldb_iter_destroy(self.iterator);
|
||||
}
|
||||
|
||||
pub fn is_valid(self: @This()) bool {
|
||||
return c.leveldb_iter_valid(self.iterator) != 0;
|
||||
}
|
||||
|
||||
pub fn seek_to_first(self: @This()) void {
|
||||
c.leveldb_iter_seek_to_first(self.iterator);
|
||||
}
|
||||
|
||||
pub fn seek_to_last(self: @This()) void {
|
||||
c.leveldb_iter_seek_to_last(self.iterator);
|
||||
}
|
||||
|
||||
pub fn next(self: @This()) void {
|
||||
c.leveldb_iter_next(self.iterator);
|
||||
}
|
||||
|
||||
pub fn previous(self: @This()) void {
|
||||
c.leveldb_iter_prev(self.iterator);
|
||||
}
|
||||
|
||||
pub fn key(self: @This()) []const u8 {
|
||||
var length: usize = 0;
|
||||
const key_data = c.leveldb_iter_key(self.iterator, &length);
|
||||
return key_data[0..length];
|
||||
}
|
||||
|
||||
pub fn value(self: @This()) []const u8 {
|
||||
var length: usize = 0;
|
||||
const value_data = c.leveldb_iter_value(self.iterator, &length);
|
||||
return value_data[0..length];
|
||||
}
|
||||
};
|
||||
|
||||
pub fn iterator(self: @This(), options: ReadOptions) Iterator {
|
||||
const c_readoptions = create_readoptions(options);
|
||||
defer c.leveldb_readoptions_destroy(c_readoptions);
|
||||
|
||||
const c_iterator = c.leveldb_create_iterator(self.db, c_readoptions);
|
||||
return .{ .iterator = c_iterator.? };
|
||||
}
|
||||
|
||||
pub fn free(ptr: *anyopaque) void {
|
||||
c.leveldb_free(ptr);
|
||||
}
|
||||
|
||||
pub fn major_version() c_int {
|
||||
return c.leveldb_major_version();
|
||||
}
|
||||
|
||||
pub fn minor_version() c_int {
|
||||
return c.leveldb_minor_version();
|
||||
}
|
||||
|
||||
fn create_openoptions(options: OpenOptions) *c.leveldb_options_t {
|
||||
const c_options = c.leveldb_options_create().?;
|
||||
|
||||
c.leveldb_options_set_create_if_missing(c_options, if (options.create_if_missing) 1 else 0);
|
||||
c.leveldb_options_set_error_if_exists (c_options, if (options.error_if_exists) 1 else 0);
|
||||
|
||||
c.leveldb_options_set_compression(c_options, @intFromEnum(options.compression));
|
||||
|
||||
return c_options;
|
||||
}
|
||||
|
||||
|
||||
fn create_readoptions(options: ReadOptions) *c.leveldb_readoptions_t {
|
||||
const c_options = c.leveldb_readoptions_create().?;
|
||||
|
||||
c.leveldb_readoptions_set_verify_checksums(c_options, if (options.verify_checksums) 1 else 0);
|
||||
c.leveldb_readoptions_set_fill_cache (c_options, if (options.fill_cache) 1 else 0);
|
||||
|
||||
return c_options;
|
||||
}
|
||||
|
||||
fn create_writeoptions(options: WriteOptions) *c.leveldb_writeoptions_t {
|
||||
const c_options = c.leveldb_writeoptions_create().?;
|
||||
|
||||
c.leveldb_writeoptions_set_sync(c_options, if (options.sync) 1 else 0);
|
||||
|
||||
return c_options;
|
||||
}
|
||||
Reference in New Issue
Block a user