Skip to content

Commit 3472fef

Browse files
committed
Clean up
1 parent 796707c commit 3472fef

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

src/debug/env.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,16 @@ pub fn dumpDoorstopPath(module: if (builtin.os.tag == .windows) std.os.windows.H
3030

3131
logger.debug("Doorstop library path: {}", .{util.fmtString(doorstop_path)});
3232
}
33+
34+
pub fn dumpStdHandle(name: []const u8, handle: ?std.os.windows.HANDLE) void {
35+
var buf: [std.os.windows.PATH_MAX_WIDE]u16 = undefined;
36+
if (handle) |h| {
37+
const path = std.os.windows.GetFinalPathNameByHandle(h, .{}, &buf) catch |e| {
38+
logger.debug("Standard {s} handle at {}, unable to determine path: {}", .{ name, util.fmtAddress(h), e });
39+
return;
40+
};
41+
logger.debug("Standard {s} handle at {}, {s}", .{ name, util.fmtAddress(handle), std.unicode.fmtUtf16Le(path) });
42+
} else {
43+
logger.debug("Standard {s} handle at null", .{name});
44+
}
45+
}

src/entrypoint.zig

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,16 @@ pub fn entrypoint(module: if (builtin.os.tag == .windows) std.os.windows.HMODULE
4040
debug_env.dumpWorkingDir();
4141
debug_env.dumpDoorstopPath(module);
4242

43+
if (builtin.os.tag == .windows) {
44+
root.hooks.windows.stdout_handle = std.os.windows.GetStdHandle(std.os.windows.STD_OUTPUT_HANDLE) catch null;
45+
root.hooks.windows.stderr_handle = std.os.windows.GetStdHandle(std.os.windows.STD_ERROR_HANDLE) catch null;
46+
47+
debug_env.dumpStdHandle("output", root.hooks.windows.stdout_handle);
48+
debug_env.dumpStdHandle("error", root.hooks.windows.stderr_handle);
49+
}
50+
4351
switch (builtin.os.tag) {
44-
// windows
45-
.windows => {
46-
root.hooks.windows.stdout_handle = std.os.windows.GetStdHandle(std.os.windows.STD_OUTPUT_HANDLE) catch null;
47-
root.hooks.windows.stderr_handle = std.os.windows.GetStdHandle(std.os.windows.STD_ERROR_HANDLE) catch null;
48-
49-
logger.debug("Standard output handle at {}", .{util.fmtAddress(root.hooks.windows.stdout_handle)});
50-
logger.debug("Standard error handle at {}", .{util.fmtAddress(root.hooks.windows.stderr_handle)});
51-
// char_t handle_path[MAX_PATH] = L"";
52-
// GetFinalPathNameByHandle(stdout_handle, handle_path, MAX_PATH, 0);
53-
// LOG("Standard output handle path: %" Ts, handle_path);
54-
55-
const target_module = std.os.windows.kernel32.GetModuleHandleW(std.unicode.utf8ToUtf16LeStringLiteral("UnityPlayer")) orelse blk: {
56-
logger.debug("No UnityPlayer module found! Using executable as the hook target.", .{});
57-
break :blk std.os.windows.kernel32.GetModuleHandleW(null).?;
58-
};
59-
60-
root.hooks.installHooksWindows(target_module);
61-
},
52+
.windows => root.hooks.installHooksWindows(),
6253
else => root.hooks.installHooksNix(),
6354
}
6455

src/hooks.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ fn tryIatHookUntyped(
9090
logger.err("Failed to hook {s}. Error: {}", .{ msg, e });
9191
return;
9292
};
93+
// Need to GetProcAddress instead of simply using the target_function address, because the target_function may be a
94+
// "stub" function embedded in our DLL that invokes the real function.
9395
const result: *const anyopaque = std.os.windows.kernel32.GetProcAddress(target_module, target_function_name) orelse {
9496
const e = std.os.windows.unexpectedError(std.os.windows.GetLastError()) catch {};
9597
logger.err("Failed to hook {s}. Error: {}", .{ msg, e });
@@ -100,7 +102,12 @@ fn tryIatHookUntyped(
100102
};
101103
}
102104

103-
pub fn installHooksWindows(module: std.os.windows.HMODULE) callconv(.c) void {
105+
pub fn installHooksWindows() void {
106+
const module = std.os.windows.kernel32.GetModuleHandleW(std.unicode.utf8ToUtf16LeStringLiteral("UnityPlayer")) orelse blk: {
107+
logger.debug("No UnityPlayer module found! Using executable as the hook target.", .{});
108+
break :blk std.os.windows.kernel32.GetModuleHandleW(null).?;
109+
};
110+
104111
tryIatHook(module, "kernel32.dll", "GetProcAddress", &std.os.windows.kernel32.GetProcAddress, @ptrCast(&dlsym_hook), "GetProcAddress");
105112
tryIatHook(module, "kernel32.dll", "CloseHandle", &windows.CloseHandle, &windows.close_handle_hook, "CloseHandle");
106113

@@ -118,7 +125,7 @@ fn tryPltHook(hook: *plthook.c.plthook_t, funcname: [:0]const u8, funcaddr: *any
118125
}
119126
}
120127

121-
pub fn installHooksNix() callconv(.c) void {
128+
pub fn installHooksNix() void {
122129
const hook = plthook.openByFilename(comptime "UnityPlayer" ++ builtin.os.tag.dynamicLibSuffix()) catch |e| {
123130
const s: [*:0]const u8 = switch (e) {
124131
error.FileNotFound => "FileNotFound",

0 commit comments

Comments
 (0)