aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/PluginCallbacks001.zig
blob: 17d687dad5b05aa714b82cf2dc76c44f9ffe2b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
}