Skip to content

Commit

Permalink
Fix installer cmd color formatting using FFI (#1)
Browse files Browse the repository at this point in the history
* fix cmd color formatting

* fix runtime linux error

* fix stdout handle

* run deno fmt

---------

Co-authored-by: NeKz <[email protected]>
  • Loading branch information
hero622 and NeKzor committed May 14, 2024
1 parent 4478a35 commit 36305fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"tasks": {
"check": "deno fmt --check && deno lint && deno task check:types",
"check:types": "deno check --no-lock src/*.ts",
"run": "deno run -A src/cmm.ts",
"run": "deno run --allow-ffi --unstable-ffi -A src/cmm.ts",
"compile": "deno task compile:lin & deno task compile:win",
"compile:lin": "deno compile --target x86_64-unknown-linux-gnu --output cmm -A src/cmm.ts",
"compile:win": "deno compile --target x86_64-pc-windows-msvc --output cmm.exe -A src/cmm.ts",
"compile:win": "deno compile --target x86_64-pc-windows-msvc --allow-ffi --unstable-ffi --output cmm.exe -A src/cmm.ts",
"pack": "deno run -A src/vpkt.ts pack --output mods/portal_stories/pak01_dir.vpk --pack mods/portal_stories",
"pack:check": "deno run -A src/vpkt.ts list --input mods/portal_stories/pak01_dir.vpk"
},
Expand Down
35 changes: 35 additions & 0 deletions src/cmm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,37 @@ const validateApiKey = async (apiKey: string) => {
}
};

const enableVirtualTerminalProcessing = () => {
const kernel32 = Deno.dlopen('kernel32.dll', {
GetStdHandle: {
parameters: ['u32'],
result: 'pointer',
},
GetConsoleMode: {
parameters: ['pointer', 'buffer'],
result: 'bool',
},
SetConsoleMode: {
parameters: ['pointer', 'u32'],
result: 'bool',
},
});

const STD_OUTPUT_HANDLE = 4294967285;
const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;

const handle = kernel32.symbols.GetStdHandle(STD_OUTPUT_HANDLE);

const buffer = new Uint32Array(1).fill(0);
kernel32.symbols.GetConsoleMode(handle, buffer);

let consoleMode = buffer[0];
if (consoleMode) {
consoleMode = consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
kernel32.symbols.SetConsoleMode(handle, consoleMode);
}
};

const cli = new Command()
.name('cmm')
.version(ChallengeModeModVersion)
Expand All @@ -190,6 +221,10 @@ const cli = new Command()
.action(async (option) => {
verbose = !!option.verbose;

if (isWindows) {
enableVirtualTerminalProcessing();
}

const gameSelect = await Select.prompt({
message: 'Choose which game to install the mod to:',
options: Object.keys(supportedGames),
Expand Down

0 comments on commit 36305fb

Please sign in to comment.