-
Notifications
You must be signed in to change notification settings - Fork 209
Replace native drivelist addon with enumeration via winapis + koffi #22909
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
Open
halgari
wants to merge
6
commits into
master
Choose a base branch
from
halgari/APP-403
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3092773
Replace native drivelist addon with PowerShell/proc enumeration
halgari a3193a2
Replace PowerShell shelling with koffi FFI for drive enumeration
halgari 0560587
Update src/renderer/src/extensions/gamemode_management/util/getDriveL…
halgari c12e03d
Merge master into halgari/APP-403
halgari 962c68b
Merge origin/master into halgari/APP-403
halgari db1d377
Add missing async keyword to getFixedDrivesWindows
halgari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 98 additions & 37 deletions
135
src/renderer/src/extensions/gamemode_management/util/getDriveList.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,111 @@ | ||
| import type { list as listT } from "drivelist"; | ||
| import { readFile } from "fs/promises"; | ||
| import type { IExtensionApi } from "../../../types/IExtensionContext"; | ||
|
|
||
| /** | ||
| * Returns mount paths for fixed (non-removable) system drives. | ||
| * Uses koffi FFI to call Win32 APIs on Windows, /proc/mounts on Linux. | ||
| */ | ||
| function getDriveList(api: IExtensionApi): Promise<string[]> { | ||
| let list: typeof listT; | ||
| try { | ||
| list = require("drivelist").list; | ||
| if (typeof list !== "function") { | ||
| throw new Error('Failed to load "drivelist" module'); | ||
| } | ||
| } catch (err) { | ||
| const impl = | ||
| process.platform === "win32" ? getFixedDrivesWindows : getFixedDrivesLinux; | ||
|
|
||
| return impl().catch((err) => { | ||
| api.showErrorNotification( | ||
| "Failed to query list of system drives", | ||
| { | ||
| message: | ||
| "Vortex was not able to query the operating system for the list of system drives. " + | ||
| "If this error persists, please configure the list manually.", | ||
| error: err, | ||
| }, | ||
| "Failed to determine list of disk drives. " + | ||
| "Please review the settings before scanning for games.", | ||
| err, | ||
| { allowReport: false }, | ||
| ); | ||
| return ["C:"]; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Enumerate fixed drives via Win32 GetLogicalDriveStringsW + GetDriveTypeW. | ||
| * | ||
| * GetLogicalDriveStringsW returns a double-null-terminated buffer of drive | ||
| * root paths ("C:\\\0D:\\\0\0"). GetDriveTypeW returns the type for each; | ||
| * DRIVE_FIXED (3) is what we want. | ||
| */ | ||
| function getFixedDrivesWindows(): Promise<string[]> { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const koffi = require("koffi"); | ||
| const kernel32 = koffi.load("kernel32.dll"); | ||
|
|
||
| const GetLogicalDriveStringsW = kernel32.func( | ||
| "uint32 GetLogicalDriveStringsW(uint32 nBufferLength, uint16 *lpBuffer)", | ||
| ); | ||
| const GetDriveTypeW = kernel32.func( | ||
| "uint32 GetDriveTypeW(const uint16 *lpRootPathName)", | ||
| ); | ||
|
|
||
| const DRIVE_FIXED = 3; | ||
|
|
||
| // Buffer for drive strings — 26 drives * 4 chars ("X:\\\0") = 104 UTF-16 code units max | ||
| const buf = Buffer.alloc(256); | ||
| const len = GetLogicalDriveStringsW(buf.length / 2, buf) as number; | ||
|
|
||
| if (len === 0) { | ||
| return Promise.resolve(["C:"]); | ||
| } | ||
|
|
||
| return list() | ||
| .then((disks) => | ||
| disks | ||
| .sort() | ||
| .filter((disk) => disk.isSystem && !disk.isRemovable) | ||
| .reduce((prev, disk) => { | ||
| if (disk.mountpoints) { | ||
| prev.push(...disk.mountpoints.map((mp) => mp.path)); | ||
| } else if (disk["mountpoint"] !== undefined) { | ||
| prev.push(disk["mountpoint"]); | ||
| } | ||
| return prev; | ||
| }, []), | ||
| const drives: string[] = []; | ||
| let offset = 0; | ||
|
|
||
| while (offset < len * 2) { | ||
| // Read null-terminated UTF-16LE string | ||
| const codes: number[] = []; | ||
| while (offset < buf.length) { | ||
| const code = buf.readUInt16LE(offset); | ||
| offset += 2; | ||
| if (code === 0) break; | ||
| codes.push(code); | ||
| } | ||
| if (codes.length === 0) break; | ||
|
|
||
| const rootPath = String.fromCharCode(...codes); // e.g. "C:\\" | ||
|
|
||
| // Check drive type | ||
| const widePath = Buffer.alloc((rootPath.length + 1) * 2); | ||
| for (let i = 0; i < rootPath.length; i++) { | ||
| widePath.writeUInt16LE(rootPath.charCodeAt(i), i * 2); | ||
| } | ||
|
|
||
| const driveType = GetDriveTypeW(widePath) as number; | ||
| if (driveType === DRIVE_FIXED) { | ||
| // Strip trailing backslash: "C:\\" → "C:" | ||
| drives.push(rootPath.replace(/\\$/, "")); | ||
| } | ||
| } | ||
|
|
||
| return Promise.resolve(drives.length > 0 ? drives : ["C:"]); | ||
| } | ||
|
|
||
| async function getFixedDrivesLinux(): Promise<string[]> { | ||
| const raw = await readFile("/proc/mounts", "utf8"); | ||
| const drives = raw | ||
| .split("\n") | ||
| .filter((line) => line.length > 0) | ||
| .map((line) => { | ||
| const parts = line.split(" "); | ||
| return { device: parts[0], mountpoint: parts[1], fstype: parts[2] }; | ||
| }) | ||
| .filter( | ||
| ({ device, fstype }) => | ||
| device.startsWith("/dev/") && | ||
| !device.startsWith("/dev/loop") && | ||
| ["ext4", "btrfs", "xfs", "zfs", "ext3", "ext2", "f2fs"].includes( | ||
| fstype, | ||
| ), | ||
| ) | ||
| .catch((err) => { | ||
| api.showErrorNotification( | ||
| "Failed to determine list of disk drives. " + | ||
| "Please review the settings before scanning for games.", | ||
| err, | ||
| { allowReport: false }, | ||
| ); | ||
| return ["C:"]; | ||
| }); | ||
| .map(({ mountpoint }) => | ||
| // Unescape octal sequences in mount paths (e.g. \040 for space) | ||
| mountpoint.replace(/\\([0-7]{3})/g, (_, oct) => | ||
| String.fromCharCode(parseInt(oct, 8)), | ||
| ), | ||
| ); | ||
|
|
||
| return drives.length > 0 ? drives : ["/"]; | ||
| } | ||
|
|
||
| export default getDriveList; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.