From f457b148f97ebd73e77581651eb1bc2c682f136b Mon Sep 17 00:00:00 2001 From: Titanium Brain Date: Fri, 29 May 2026 17:12:47 +0100 Subject: [PATCH] feat: WIP add lua config parser Non-working implementation. --- ly-core/build.zig | 7 +++++ ly-core/build.zig.zon | 4 +++ ly-core/src/root.zig | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/ly-core/build.zig b/ly-core/build.zig index f62fb2e..37b8668 100644 --- a/ly-core/build.zig +++ b/ly-core/build.zig @@ -21,6 +21,13 @@ pub fn build(b: *std.Build) void { const zigini = b.dependency("zigini", .{ .target = target, .optimize = optimize }); mod.addImport("zigini", zigini.module("zigini")); + const zlua = b.dependency("zlua", .{ + .target = target, + .optimize = optimize, + .lang = .luajit, + }); + mod.addImport("zlua", zlua.module("zlua")); + const translate_c = b.dependency("translate_c", .{ .target = target, }); diff --git a/ly-core/build.zig.zon b/ly-core/build.zig.zon index 0d84326..622037b 100644 --- a/ly-core/build.zig.zon +++ b/ly-core/build.zig.zon @@ -12,6 +12,10 @@ .url = "git+https://codeberg.org/ziglang/translate-c?ref=zig-0.16.x#6fe0ffc4549f15c5f2d9432c2b4460ba90ff85ac", .hash = "translate_c-1.0.0-Q_BUWo_5BgD4flHdUhA31zOz0XvZk9k7lQv1ouzyNXj2", }, + .zlua = .{ + .url = "git+https://github.com/natecraddock/ziglua?ref=zig-0.16#8f271c82baa5fc43aa02a72f6da020c2025d9436", + .hash = "zlua-0.1.0-hGRpC2aABQD4D9PBVH3wAW8k32-I4969MRQ0CpOwoley", + }, }, .paths = .{ "build.zig", diff --git a/ly-core/src/root.zig b/ly-core/src/root.zig index 42198f5..fab9bc3 100644 --- a/ly-core/src/root.zig +++ b/ly-core/src/root.zig @@ -1,6 +1,8 @@ const std = @import("std"); pub const ini = @import("zigini"); +pub const zlua = @import("zlua"); +pub const Lua = zlua.Lua; pub const interop = @import("interop.zig"); pub const UidRange = @import("UidRange.zig"); @@ -76,3 +78,60 @@ pub fn IniParser(comptime Struct: type) type { } }; } + +pub fn LuaParser(comptime Struct: type) type { + return struct { + const Self = @This(); + + structure: Struct, + + pub fn init( + allocator: std.mem.Allocator, + io: std.Io, + path: []const u8, + ) !Self { + var lua: Lua = try .init(allocator); + defer lua.deinit(); + + lua.openLibs(); + + // convert to sentinel terminated slice + const spath: [:0]const u8 = try allocator.dupeSentinel(u8, path, 0); + lua.doFile(spath) catch { + const error_str = lua.toString(-1) catch unreachable; + const lua_str = try allocator.dupeSentinel(u8, error_str, 0); + _ = lua_str; // TODO add error logging + }; + var data: Struct = .{}; + switch (@typeInfo(Struct)) { + .@"struct" => |struc| { + inline for (struc.fields) |field| { + try setField(allocator, lua, field, &data); + } + }, + else => @compileError("Expected a struct."), + } + + return data; + } + pub fn setField(allocator: std.mem.Allocator, lua: *Lua, comptime field: std.builtin.Type.StructField, data: *Struct) !void { + try lua.getGlobal("ly"); + defer lua.pop(1); // pop ly table + switch (@typeInfo(field.type)) { + .int => { + _ = lua.getField(-1, field.name); + const value = try lua.toInteger(-1); + lua.pop(1); + @field(data, field.name) = @intCast(@as(field.type, @truncate(value))); + }, + .float => { + _ = lua.getField(-1, field.name); + const value = try lua.toNumber(-1); + lua.pop(1); + @field(data, field.name) = @floatCast(@as(field.type, value)); + }, + else => @compileError("unimplemented!"), + } + } + }; +}