windows support for loading pods
This commit is contained in:
parent
92d17a0562
commit
9546d84b24
|
|
@ -44,5 +44,6 @@ pub export fn entrypoint(_core: *core_api) callconv(.c) i32 {
|
||||||
core = _core;
|
core = _core;
|
||||||
const category = core.declare_logging_category(Slice.make("test"), .warn);
|
const category = core.declare_logging_category(Slice.make("test"), .warn);
|
||||||
core.log_puts(category, .info, Slice.make("Game startup is called"));
|
core.log_puts(category, .info, Slice.make("Game startup is called"));
|
||||||
|
core.log_puts(category, .info, Slice.make("Game startup is called2"));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -174,6 +174,7 @@ pub const TinyString = extern struct {
|
||||||
pub const engine = @import("engine.zig");
|
pub const engine = @import("engine.zig");
|
||||||
pub const slugger = @import("slugger");
|
pub const slugger = @import("slugger");
|
||||||
pub const p2 = @import("p2");
|
pub const p2 = @import("p2");
|
||||||
|
pub const WinLib = @import("winlib.zig");
|
||||||
|
|
||||||
pub const DeclareTagPool = p2.DeclareTagPool;
|
pub const DeclareTagPool = p2.DeclareTagPool;
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
handle: windows.PVOID,
|
||||||
|
|
||||||
|
pub fn open(path: []const u8) !@This() {
|
||||||
|
var fbab: [4096]u8 = undefined;
|
||||||
|
var fba = std.heap.FixedBufferAllocator.init(&fbab);
|
||||||
|
const path_w = try std.unicode.wtf8ToWtf16LeAlloc(fba.allocator(), path);
|
||||||
|
|
||||||
|
var handle: windows.PVOID = undefined;
|
||||||
|
switch (windows.ntdll.LdrLoadDll(null, null, &.init(path_w), &handle)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.DLL_NOT_FOUND => return error.DllNotFound,
|
||||||
|
else => |status| return windows.unexpectedStatus(status),
|
||||||
|
}
|
||||||
|
return .{ .handle = handle };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lookup(self: *@This(), comptime T: type, name: [:0]const u8) ?T {
|
||||||
|
var addr: windows.PVOID = undefined;
|
||||||
|
switch (windows.ntdll.LdrGetProcedureAddress(self.handle, &.init(name), 0, &addr)) {
|
||||||
|
.SUCCESS => return @ptrCast(@alignCast(addr)),
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *@This()) void {
|
||||||
|
_ = windows.ntdll.LdrUnloadDll(self.handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const windows = std.os.windows;
|
||||||
|
|
@ -20,9 +20,11 @@ pub fn main(init: std.process.Init) !void {
|
||||||
|
|
||||||
core.print("press h or 'help' for help \n> ", .{});
|
core.print("press h or 'help' for help \n> ", .{});
|
||||||
|
|
||||||
|
const argv = try init.minimal.args.toSlice(init.arena.allocator());
|
||||||
|
|
||||||
var cliTest: bool = false;
|
var cliTest: bool = false;
|
||||||
for (init.minimal.args.vector) |a| {
|
for (argv) |a| {
|
||||||
if (std.mem.startsWith(u8, std.mem.span(a), "--cli-test")) cliTest = true;
|
if (std.mem.startsWith(u8, a, "--cli-test")) cliTest = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var readBuffer: [4096]u8 = undefined;
|
var readBuffer: [4096]u8 = undefined;
|
||||||
|
|
@ -48,7 +50,7 @@ pub fn main(init: std.process.Init) !void {
|
||||||
core.engine.gEngine.tick();
|
core.engine.gEngine.tick();
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
const line = std.mem.trim(u8, l, "\n ");
|
const line = std.mem.trim(u8, l, "\r\n ");
|
||||||
|
|
||||||
core.engine.gEngine.tick();
|
core.engine.gEngine.tick();
|
||||||
|
|
||||||
|
|
@ -180,11 +182,20 @@ fn loadPod(args: []const u8) i32 {
|
||||||
const fullPath = targetDir.realPathFileAlloc(io, outputPath.bytes(), ffba.allocator()) catch return -1;
|
const fullPath = targetDir.realPathFileAlloc(io, outputPath.bytes(), ffba.allocator()) catch return -1;
|
||||||
println("loading file {s}", .{fullPath});
|
println("loading file {s}", .{fullPath});
|
||||||
|
|
||||||
var lib = std.DynLib.open(fullPath) catch return -1;
|
|
||||||
const entrypointFn = *const fn (_core: *core.core_api) callconv(.c) i32;
|
const entrypointFn = *const fn (_core: *core.core_api) callconv(.c) i32;
|
||||||
if (lib.lookup(entrypointFn, "entrypoint")) |func| {
|
|
||||||
if (func(&core.gApi) == 0)
|
if (builtin.os.tag == .windows) {
|
||||||
println("module load success!", .{});
|
var lib = core.WinLib.open(fullPath) catch return -1;
|
||||||
|
if (lib.lookup(entrypointFn, "entrypoint")) |func| {
|
||||||
|
if (func(&core.gApi) == 0)
|
||||||
|
println("module load success!", .{});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var lib = std.DynLib.open(fullPath) catch return -1;
|
||||||
|
if (lib.lookup(entrypointFn, "entrypoint")) |func| {
|
||||||
|
if (func(&core.gApi) == 0)
|
||||||
|
println("module load success!", .{});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,4 @@
|
||||||
pub var gargs: std.process.Args = undefined;
|
pub var gargs: []const [:0]const u8 = undefined;
|
||||||
pub threadlocal var iter: std.process.Args.Iterator = undefined;
|
|
||||||
|
|
||||||
pub fn beginArgs() void {
|
|
||||||
iter = gargs.iterate();
|
|
||||||
}
|
|
||||||
pub fn nextArg() ?[:0]const u8 {
|
|
||||||
return iter.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const Opts = struct {
|
pub const Opts = struct {
|
||||||
targetFile: ?[]const u8 = null,
|
targetFile: ?[]const u8 = null,
|
||||||
|
|
@ -18,11 +10,10 @@ pub const Opts = struct {
|
||||||
end,
|
end,
|
||||||
};
|
};
|
||||||
|
|
||||||
beginArgs();
|
|
||||||
var opts: Opts = .{};
|
var opts: Opts = .{};
|
||||||
|
|
||||||
var state: ParseState = .exe;
|
var state: ParseState = .exe;
|
||||||
while (nextArg()) |arg| {
|
for (gargs) |arg| {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
.exe => state = .targetFile,
|
.exe => state = .targetFile,
|
||||||
.targetFile => {
|
.targetFile => {
|
||||||
|
|
@ -61,7 +52,7 @@ pub fn usage() void {}
|
||||||
pub fn main(init: std.process.Init) !void {
|
pub fn main(init: std.process.Init) !void {
|
||||||
const io = init.io;
|
const io = init.io;
|
||||||
|
|
||||||
gargs = init.minimal.args;
|
gargs = try init.minimal.args.toSlice(init.arena.allocator());
|
||||||
|
|
||||||
const opts = Opts.parse();
|
const opts = Opts.parse();
|
||||||
if (opts.targetFile == null) {
|
if (opts.targetFile == null) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue