diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2024-05-05 16:51:48 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2024-05-05 16:51:48 +0200 |
commit | 54365b02ef3e36a631354aa1af3bd33f90db3cf7 (patch) | |
tree | b7f02a04c28437a1c44c427c440fe3f0750bed04 /src/interfaces | |
download | SouthRPC-54365b02ef3e36a631354aa1af3bd33f90db3cf7.tar.gz SouthRPC-54365b02ef3e36a631354aa1af3bd33f90db3cf7.zip |
Zig rework
Diffstat (limited to 'src/interfaces')
-rw-r--r-- | src/interfaces/PluginCallbacks001.zig | 105 | ||||
-rw-r--r-- | src/interfaces/PluginId001.zig | 65 |
2 files changed, 170 insertions, 0 deletions
diff --git a/src/interfaces/PluginCallbacks001.zig b/src/interfaces/PluginCallbacks001.zig new file mode 100644 index 0000000..17d687d --- /dev/null +++ b/src/interfaces/PluginCallbacks001.zig @@ -0,0 +1,105 @@ +const std = @import("std"); +const windows = std.os.windows; + +const Class = @import("../class.zig").Class; + +const interface = @import("../interface.zig"); +const northstar = @import("../northstar.zig"); +const squirrel = @import("../squirrel.zig"); +const sys = @import("../sys.zig"); +const server = @import("../server.zig"); +const engine = @import("../engine.zig"); + +const CSquirrelVM = squirrel.CSquirrelVM; + +pub const plugin_interface = .{ + .name = "PluginCallbacks001", + .func = CreatePluginCallbacks, +}; + +pub fn CreatePluginCallbacks() *void { + return @ptrCast( + @constCast( + &IPluginCallbacks{ + .vtable = &.{ + .Init = Init, + .Finalize = Finalize, + .Unload = Unload, + .OnSqvmCreated = OnSqvmCreated, + .OnSqvmDestroyed = OnSqvmDestroyed, + .OnLibraryLoaded = OnLibraryLoaded, + .RunFrame = RunFrame, + }, + }, + ), + ); +} + +pub const IPluginCallbacks = Class(.{}, .{ + .Init = .{ .type = *const fn (*anyopaque, windows.HMODULE, *northstar.NorthstarData, u8) callconv(.C) void, .virtual = true }, + .Finalize = .{ .type = *const fn (*anyopaque) callconv(.C) void, .virtual = true }, + .Unload = .{ .type = *const fn (*anyopaque) callconv(.C) bool, .virtual = true }, + .OnSqvmCreated = .{ .type = *const fn (*anyopaque, *CSquirrelVM) callconv(.C) void, .virtual = true }, + .OnSqvmDestroyed = .{ .type = *const fn (*anyopaque, *CSquirrelVM) callconv(.C) void, .virtual = true }, + .OnLibraryLoaded = .{ .type = *const fn (*anyopaque, windows.HMODULE, [*:0]const u8) callconv(.C) void, .virtual = true }, + .RunFrame = .{ .type = *const fn (*anyopaque) callconv(.C) void, .virtual = true }, +}); + +pub fn Init(self: *anyopaque, module: windows.HMODULE, data: *northstar.NorthstarData, reloaded: u8) callconv(.C) void { + _ = self; + + northstar.init(module, data); + sys.init(); + + if (reloaded != 0) { + server.stop(); + } + + if (!server.running) { + server.start() catch std.log.err("Failed to start HTTP Server", .{}); + } + + std.log.info("Loaded", .{}); +} + +pub fn Finalize(self: *anyopaque) callconv(.C) void { + _ = self; +} + +pub fn Unload(self: *anyopaque) callconv(.C) bool { + _ = self; + + server.stop(); + + return true; +} + +pub fn OnSqvmCreated(self: *anyopaque, c_sqvm: *CSquirrelVM) callconv(.C) void { + _ = self; + + std.log.info("created {s} sqvm", .{@tagName(c_sqvm.context)}); +} + +pub fn OnSqvmDestroyed(self: *anyopaque, c_sqvm: *CSquirrelVM) callconv(.C) void { + _ = self; + + std.log.info("destroyed {s} sqvm", .{@tagName(c_sqvm.context)}); +} + +pub fn OnLibraryLoaded(self: *anyopaque, module: windows.HMODULE, name_ptr: [*:0]const u8) callconv(.C) void { + _ = self; + + const name = std.mem.span(name_ptr); + + if (std.mem.eql(u8, name, "engine.dll")) { + engine.Cbuf = .{ + .GetCurrentPlayer = @ptrFromInt(@intFromPtr(module) + 0x120630), + .AddText = @ptrFromInt(@intFromPtr(module) + 0x1203B0), + .Execute = @ptrFromInt(@intFromPtr(module) + 0x1204B0), + }; + } +} + +pub fn RunFrame(self: *anyopaque) callconv(.C) void { + _ = self; +} diff --git a/src/interfaces/PluginId001.zig b/src/interfaces/PluginId001.zig new file mode 100644 index 0000000..2246b8b --- /dev/null +++ b/src/interfaces/PluginId001.zig @@ -0,0 +1,65 @@ +const std = @import("std"); +const Class = @import("../class.zig").Class; + +pub const plugin_interface = .{ + .name = "PluginId001", + .func = CreatePluginId, +}; + +fn CreatePluginId() *void { + return @ptrCast( + @constCast( + &IPluginId{ + .vtable = &.{ + .GetString = GetString, + .GetField = GetField, + }, + }, + ), + ); +} + +pub const IPluginId = Class(.{}, .{ + .GetString = .{ .type = *const fn (*anyopaque, PluginString) callconv(.C) ?[*:0]const u8, .virtual = true }, + .GetField = .{ .type = *const fn (*anyopaque, PluginField) callconv(.C) i64, .virtual = true }, +}); + +const PluginString = enum(c_int) { + ID_NAME = 0, + ID_LOG_NAME, + ID_DEPENDENCY_NAME, + _, +}; + +const PluginField = enum(c_int) { + ID_CONTEXT = 0, + _, +}; + +const PluginContext = enum(i64) { + PCTX_DEDICATED = 0x1, // load on dedicated servers + PCTX_CLIENT = 0x2, // load on clients + _, +}; + +pub fn GetString(self: *anyopaque, prop: PluginString) callconv(.C) ?[*:0]const u8 { + _ = self; + + switch (prop) { + .ID_NAME => return @import("root").PLUGIN_NAME, + .ID_LOG_NAME => return @import("root").LOG_NAME, + .ID_DEPENDENCY_NAME => return @import("root").DEPENDENCY_NAME, + else => return null, + } +} + +pub fn GetField(self: *anyopaque, prop: PluginField) callconv(.C) i64 { + _ = self; + + switch (prop) { + .ID_CONTEXT => { + return @intFromEnum(PluginContext.PCTX_DEDICATED) | @intFromEnum(PluginContext.PCTX_CLIENT); + }, + else => return 0, + } +} |