inital support for hooks...

Currently the following hooks are available:
 - WinMapPre
 - WinMapPost
This commit is contained in:
Squibid 2025-11-21 23:28:40 -05:00
parent b45544c97a
commit b3322eeb90
Signed by: squibid
GPG key ID: BECE5684D3C4005D
7 changed files with 193 additions and 0 deletions

81
src/lua/hook.zig Normal file
View file

@ -0,0 +1,81 @@
const Hook = @This();
const std = @import("std");
const THook = @import("../types/hook.zig");
const zlua = @import("zlua");
const gpa = std.heap.c_allocator;
const server = &@import("../main.zig").server;
pub fn add_hook(L: *zlua.Lua) i32 {
L.checkType(2, .table);
var hook: *THook = gpa.create(THook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
hook.events = std.ArrayList([]const u8).initCapacity(gpa, 1) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
// We support both a string and a table of strings as the first value of
// add_hook. Regardless of which type is passed in we create an arraylist of
// []const u8's
if (L.isTable(1)) {
L.pushNil();
while (L.next(1)) {
if (L.isString(-1)) {
const s = L.toString(-1) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
hook.events.append(gpa, s) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
}
L.pop(1);
}
} else if (L.isString(1)) {
const s = L.toString(1) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
hook.events.append(gpa, s) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
}
_ = L.pushString("callback");
_ = L.getTable(2);
if (L.isFunction(-1)) {
hook.options.lua_cb_ref_idx = L.ref(zlua.registry_index) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
}
server.hooks.append(gpa, hook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
for (hook.events.items) |value| {
server.events.put(value, hook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
}
return 0;
}
pub fn del_hook(L: *zlua.Lua) i32 {
// TODO: impl
_ = L;
return 0;
}

View file

@ -8,6 +8,7 @@ const Bridge = @import("bridge.zig");
const Fs = @import("fs.zig");
const Input = @import("input.zig");
const Api = @import("api.zig");
const Hook = @import("hook.zig");
const gpa = std.heap.c_allocator;
@ -64,6 +65,11 @@ pub fn init(self: *Lua) !void {
self.state.newLib(input_funcs);
self.state.setField(-2, "input");
}
{
const hook_funcs = zlua.fnRegsFromType(Hook);
self.state.newLib(hook_funcs);
self.state.setField(-2, "hook");
}
{
const api_funcs = zlua.fnRegsFromType(Api);
self.state.newLib(api_funcs);