Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix installer cmd color formatting using FFI #1

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading