Files
blitzkit/packages/scripts/src/core/vfs/steam.ts
T

158 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-11-26 19:27:58 -06:00
import { readDVPL } from "@blitzkit/core/src/blitz";
2025-11-26 12:58:19 -06:00
import { XMLParser } from "fast-xml-parser";
2025-11-26 13:38:40 -06:00
import { normalize } from "path/posix";
2025-11-25 22:14:09 -06:00
import SteamUser, { EConnectionProtocol } from "steam-user";
2025-11-26 12:58:19 -06:00
import { parse } from "yaml";
2025-11-29 14:34:06 -06:00
import { AbstractVFS } from "./abstract";
2025-11-25 21:37:31 -06:00
2025-11-25 22:16:39 -06:00
interface SteamManifestFile {
chunks: {
sha: string;
crc: number;
offset: string;
cb_original: number;
cb_compressed: number;
2025-11-25 22:14:09 -06:00
}[];
2025-11-25 22:16:39 -06:00
filename: string;
size: string;
flags: number;
sha_filename: string;
sha_content: string;
linktarget: unknown;
}
interface SteamManifest {
files: SteamManifestFile[];
2025-11-25 22:20:46 -06:00
depot_id: number;
gid_manifest: string;
creation_time: number;
filenames_encrypted: boolean;
cb_disk_original: string;
cb_disk_compressed: string;
unique_chunks: number;
crc_encrypted: number;
crc_clear: number;
2025-11-25 22:14:09 -06:00
}
2025-11-25 21:37:31 -06:00
2025-11-29 14:34:06 -06:00
export class SteamVFS extends AbstractVFS {
2025-11-25 22:14:09 -06:00
private steam = new SteamUser({ protocol: EConnectionProtocol.TCP });
2025-11-26 12:58:19 -06:00
private textDecoder = new TextDecoder();
private xmlParser = new XMLParser();
2025-11-25 22:14:09 -06:00
2025-11-25 22:16:39 -06:00
private manifest: Map<string, SteamManifestFile> = new Map();
2025-11-25 22:14:09 -06:00
constructor(
private username: string,
private password: string,
private app: number,
private depot: number
2025-11-29 14:34:06 -06:00
) {
super();
}
2025-11-25 22:14:09 -06:00
async init() {
this.steam.logOn({
accountName: this.username,
password: this.password,
2025-11-25 21:37:31 -06:00
});
2025-11-25 22:14:09 -06:00
await new Promise((resolve) => this.steam.once("loggedOn", resolve));
2025-11-25 21:37:31 -06:00
2025-11-25 22:14:09 -06:00
const productInfo = await this.steam.getProductInfo([this.app], []);
2025-11-25 22:16:39 -06:00
const manifest: SteamManifest = await new Promise((resolve) => {
2025-11-26 12:46:03 -06:00
// @ts-expect-error
2025-11-25 22:14:09 -06:00
this.steam.getManifest(
this.app,
this.depot,
productInfo.apps[this.app].appinfo.depots[this.depot].manifests.public
.gid,
"public",
2025-11-26 12:46:03 -06:00
// @ts-expect-error
2025-11-25 22:14:09 -06:00
(_, response) => {
resolve(response);
}
);
});
2025-11-25 22:21:58 -06:00
const paths: string[] = [];
2025-11-25 22:16:39 -06:00
for (const file of manifest.files) {
2025-11-25 22:21:58 -06:00
const path = file.filename.replaceAll("\\", "/");
paths.push(path);
2025-11-25 22:20:46 -06:00
this.manifest.set(path, file);
2025-11-25 22:16:39 -06:00
}
2025-11-26 12:46:03 -06:00
return this;
}
2025-11-25 22:16:39 -06:00
2025-11-26 12:58:19 -06:00
has(path: string) {
2025-11-26 13:38:40 -06:00
const normalized = normalize(path);
const dvplPath = `${normalized}.dvpl`;
2025-11-26 12:46:03 -06:00
2025-11-26 12:58:19 -06:00
return (
(this.manifest.has(dvplPath) && dvplPath) ||
2025-11-26 13:38:40 -06:00
(this.manifest.has(normalized) && normalized)
2025-11-26 12:58:19 -06:00
);
}
assert(requested: string) {
const path = this.has(requested);
2025-11-26 13:25:46 -06:00
if (!path) throw new Error(`File not found: ${requested}`);
2025-11-26 12:58:19 -06:00
return path;
}
2025-11-26 13:07:53 -06:00
dir(path: string) {
const parentSegments = path.split("/").length;
const children: string[] = [];
for (const child of this.manifest.keys()) {
const childSegments = child.split("/");
if (
child.startsWith(path) &&
parentSegments + 1 === childSegments.length
) {
children.push(childSegments.at(-1)!);
}
}
return children;
}
2025-11-26 12:58:19 -06:00
async file(requested: string) {
const path = this.assert(requested);
2025-11-26 12:46:03 -06:00
const fileManifest = this.manifest.get(path)!;
const downloaded: { type: "complete"; file: Buffer } =
// @ts-expect-error
await this.steam.downloadFile(this.app, this.depot, fileManifest);
2025-11-26 13:25:46 -06:00
let buffer = downloaded.file;
2025-11-26 12:46:03 -06:00
2025-11-26 13:25:46 -06:00
if (path.endsWith(".dvpl")) buffer = readDVPL(downloaded.file);
return new Uint8Array(buffer);
2025-11-26 12:46:03 -06:00
}
2025-11-26 12:58:19 -06:00
async text(path: string) {
const file = await this.file(path);
return this.textDecoder.decode(file);
}
async yaml<Type>(path: string) {
const file = await this.text(path);
return parse(file) as Type;
}
async xml<Type>(path: string) {
const file = await this.text(path);
return this.xmlParser.parse(file) as Type;
}
2025-11-26 12:46:03 -06:00
dispose() {
this.steam.logOff();
}
[Symbol.dispose]() {
this.dispose();
2025-11-25 21:37:31 -06:00
}
}