import { createRequire } from 'node:module'; import { basename } from 'node:path'; import { inspectProcessIdentity as inspectWithRuntime, type ProcessIdentity, } from '@myclaw163/game-agent-runtime/owner/process-identity.js'; export * from '@myclaw163/game-agent-runtime/owner/process-identity.js'; const require = createRequire(import.meta.url); const windowsEpochTicks = 504911232000000000n; const unixEpochFileTime = 116444736000000000n; function readCurrentWindowsIdentity(): ProcessIdentity | null { const koffi = require('koffi') as typeof import('koffi'); const kernel = koffi.load('kernel32.dll'); const fileTime = koffi.struct({ low: 'uint32_t', high: 'uint32_t' }); const result = koffi.out(koffi.pointer(fileTime)); const getCurrentProcess = kernel.func('__stdcall', 'GetCurrentProcess', 'void *', []); const getProcessTimes = kernel.func('__stdcall', 'GetProcessTimes', 'int', [ 'void *', result, result, result, result, ]); const creation = { low: 0, high: 0 }; if (!getProcessTimes(getCurrentProcess(), creation, {}, {}, {})) return null; const ticks = (BigInt(creation.high) << 32n) | BigInt(creation.low); if (ticks < unixEpochFileTime) return null; return { birth: (ticks + windowsEpochTicks).toString(), startedAtMs: Number((ticks - unixEpochFileTime) / 10000n), executable: basename(process.execPath).replace(/\.exe$/iu, ''), }; } /** The current process has a stable Win32 pseudo-handle. Keep the portable inspector for existing owners. */ export function inspectProcessIdentity(pid: number): ProcessIdentity | null { if (process.platform === 'win32' && pid === process.pid) { try { const result = readCurrentWindowsIdentity(); if (result) return result; } catch { // Retain the original identity lookup if the native binding is unavailable. } } return inspectWithRuntime(pid); }