UNPKG

@ciberus/find-steam-app

Version:
314 lines 18 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLibraryManifestsFolder = exports.getLibraryInstallsFolder = exports.findSteamPath = exports.SteamNotFoundError = exports.findSteam = exports.findSteamAppById = exports.findSteamAppByName = exports.findSteamAppManifest = exports.findSteamLibraries = exports.findSteamLibrariesPaths = void 0; var fs_extra_1 = __importDefault(require("fs-extra")); var p_filter_1 = __importDefault(require("p-filter")); var util_1 = __importDefault(require("util")); // @ts-ignore var get_folder_size_1 = __importDefault(require("get-folder-size")); var getFolderSizeAsync = util_1.default.promisify(get_folder_size_1.default); var libraries_1 = require("./libraries"); var manifest_1 = require("./manifest"); var steam_1 = require("./steam"); Object.defineProperty(exports, "findSteamPath", { enumerable: true, get: function () { return steam_1.findSteamPath; } }); Object.defineProperty(exports, "SteamNotFoundError", { enumerable: true, get: function () { return steam_1.SteamNotFoundError; } }); var utils_1 = require("./utils"); Object.defineProperty(exports, "getLibraryInstallsFolder", { enumerable: true, get: function () { return utils_1.getLibraryInstallsFolder; } }); Object.defineProperty(exports, "getLibraryManifestsFolder", { enumerable: true, get: function () { return utils_1.getLibraryManifestsFolder; } }); function findSteamLibrariesPaths() { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, libraries_1.loadSteamLibrariesPaths)()]; }); }); } exports.findSteamLibrariesPaths = findSteamLibrariesPaths; function findSteamLibraries() { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, libraries_1.loadSteamLibraries)()]; }); }); } exports.findSteamLibraries = findSteamLibraries; function findSteamAppManifest(appId) { return __awaiter(this, void 0, void 0, function () { var libs, library; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, findSteamLibrariesPaths()]; case 1: libs = _a.sent(); return [4 /*yield*/, (0, p_filter_1.default)(libs, function (lib) { return (0, manifest_1.hasManifest)(lib, appId); })]; case 2: library = (_a.sent())[0]; if (library == null) return [2 /*return*/, null]; return [2 /*return*/, (0, manifest_1.readManifest)(library, appId)]; } }); }); } exports.findSteamAppManifest = findSteamAppManifest; function findSteamAppByName(name) { return __awaiter(this, void 0, void 0, function () { var libsPaths, appsPaths, appsWithSize, resultLib; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, findSteamLibrariesPaths()]; case 1: libsPaths = _a.sent(); appsPaths = libsPaths.map(function (lib) { return (0, utils_1.getAppInstallFolder)(lib, name); }); if (!appsPaths.length) throw new Error("App not found"); return [4 /*yield*/, Promise.all(appsPaths.map(function (appPath) { return __awaiter(_this, void 0, void 0, function () { var appInstallFolder, isExists, size, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: appInstallFolder = appPath; return [4 /*yield*/, fs_extra_1.default.pathExists(appInstallFolder)]; case 1: isExists = _b.sent(); if (!isExists) return [3 /*break*/, 3]; return [4 /*yield*/, getFolderSizeAsync(appInstallFolder)]; case 2: _a = _b.sent(); return [3 /*break*/, 4]; case 3: _a = 0; _b.label = 4; case 4: size = _a; return [2 /*return*/, { appInstallFolder: appPath, size: size }]; } }); }); }))]; case 2: appsWithSize = _a.sent(); appsWithSize = appsWithSize.filter(Boolean); resultLib = appsWithSize.sort(function (a, b) { return b.size - a.size; })[0]; return [2 /*return*/, resultLib.appInstallFolder]; } }); }); } exports.findSteamAppByName = findSteamAppByName; function findSteamAppById(appId, forceV1) { if (forceV1 === void 0) { forceV1 = true; } return __awaiter(this, void 0, void 0, function () { var steamLibs, appLibrary, manifest, appLibs, appsWithSize, resultLib; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, findSteamLibraries()]; case 1: steamLibs = _a.sent(); if (!steamLibs) throw new Error("Steam libraries not found"); if (!(steamLibs.version === "v2" && !forceV1)) return [3 /*break*/, 3]; appLibrary = (0, utils_1.findAppLibraryInV2Libraries)(appId, steamLibs.libraries); if (!appLibrary) throw new Error("App not found"); return [4 /*yield*/, (0, manifest_1.readManifest)(appLibrary.path, appId)]; case 2: manifest = _a.sent(); if (!manifest) throw new Error("App manifest not found"); return [2 /*return*/, (0, utils_1.getAppInstallFolder)(appLibrary.path, manifest.installdir)]; case 3: if (!(steamLibs.version === "v1" || forceV1)) return [3 /*break*/, 6]; return [4 /*yield*/, (0, p_filter_1.default)(steamLibs.libraries, function (lib) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, manifest_1.hasManifest)(lib.path, appId)]; }); }); })]; case 4: appLibs = _a.sent(); if (!appLibs.length) throw new Error("App not found"); return [4 /*yield*/, Promise.all(appLibs.map(function (lib) { return __awaiter(_this, void 0, void 0, function () { var manifest, appInstallFolder, isExist, size, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, (0, manifest_1.readManifest)(lib.path, appId)]; case 1: manifest = _b.sent(); if (!manifest) return [2 /*return*/, undefined]; appInstallFolder = (0, utils_1.getAppInstallFolder)(lib.path, manifest.installdir); return [4 /*yield*/, fs_extra_1.default.pathExists(appInstallFolder)]; case 2: isExist = _b.sent(); if (!isExist) return [3 /*break*/, 4]; return [4 /*yield*/, getFolderSizeAsync(appInstallFolder)]; case 3: _a = _b.sent(); return [3 /*break*/, 5]; case 4: _a = -1; _b.label = 5; case 5: size = _a; return [2 /*return*/, { lib: lib, appInstallFolder: appInstallFolder, size: size }]; } }); }); }))]; case 5: appsWithSize = _a.sent(); appsWithSize = appsWithSize.filter(Boolean); resultLib = appsWithSize === null || appsWithSize === void 0 ? void 0 : appsWithSize.sort(function (a, b) { return (b === null || b === void 0 ? void 0 : b.size) - (a === null || a === void 0 ? void 0 : a.size); })[0]; if (!resultLib) throw new Error("App not found"); return [2 /*return*/, resultLib.appInstallFolder]; case 6: throw new Error("App not found"); } }); }); } exports.findSteamAppById = findSteamAppById; function findSteam(options) { if (options === void 0) { options = { forceV1: true }; } return __awaiter(this, void 0, void 0, function () { var steamPath, librariesVdfFilePath, steamLibs, appsPromises, apps, appsManifests_1, steamLibsWithApps_1, steamLibsWithApps, _loop_1, _i, _a, lib; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, (0, steam_1.findSteamPath)()]; case 1: steamPath = _b.sent(); if (!steamPath) throw new steam_1.SteamNotFoundError(); librariesVdfFilePath = (0, utils_1.getLibrariesVdfFilePath)(steamPath); return [4 /*yield*/, findSteamLibraries()]; case 2: steamLibs = _b.sent(); if (!(steamLibs.version === "v2" && steamLibs.libraries && !(options === null || options === void 0 ? void 0 : options.forceV1))) return [3 /*break*/, 4]; appsPromises = steamLibs.libraries.map(function (lib) { return lib.apps && Object.keys(lib.apps).map(function (appId) { return (0, manifest_1.readManifest)(lib.path, Number(appId)); }); }); return [4 /*yield*/, Promise.all(appsPromises.flat())]; case 3: apps = _b.sent(); appsManifests_1 = apps.filter(Boolean); steamLibsWithApps_1 = []; steamLibs.libraries.forEach(function (lib) { var steamApps = []; lib.apps && Object.keys(lib.apps).forEach(function (appId) { var appIdNumber = Number(appId); var manifest = appsManifests_1.find(function (app) { return app.appid === appIdNumber; }); if (manifest) { steamApps.push({ appId: appIdNumber, path: (0, utils_1.getAppInstallFolder)(lib.path, manifest.installdir), manifestPath: (0, utils_1.getManifestPath)(lib.path, appIdNumber), manifest: manifest, }); } }); steamLibsWithApps_1.push(__assign(__assign({}, lib), { apps: steamApps })); }); return [2 /*return*/, { version: steamLibs.version, steamPath: steamPath, librariesVdfFilePath: librariesVdfFilePath, libraries: steamLibsWithApps_1, }]; case 4: if (!((steamLibs.version === "v1" || (options === null || options === void 0 ? void 0 : options.forceV1)) && steamLibs.libraries)) return [3 /*break*/, 9]; steamLibsWithApps = []; _loop_1 = function (lib) { var apps; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, (0, manifest_1.findManifests)(lib.path)]; case 1: apps = _c.sent(); steamLibsWithApps.push({ path: lib.path, apps: apps.map(function (app) { return ({ appId: app.appid, path: (0, utils_1.getAppInstallFolder)(lib.path, app.installdir), manifestPath: (0, utils_1.getManifestPath)(lib.path, app.appid), manifest: app, }); }), }); return [2 /*return*/]; } }); }; _i = 0, _a = steamLibs.libraries; _b.label = 5; case 5: if (!(_i < _a.length)) return [3 /*break*/, 8]; lib = _a[_i]; return [5 /*yield**/, _loop_1(lib)]; case 6: _b.sent(); _b.label = 7; case 7: _i++; return [3 /*break*/, 5]; case 8: return [2 /*return*/, { version: (options === null || options === void 0 ? void 0 : options.forceV1) ? "v1" : steamLibs.version, steamPath: steamPath, librariesVdfFilePath: librariesVdfFilePath, libraries: steamLibsWithApps, }]; case 9: throw new Error("Apps not found"); } }); }); } exports.findSteam = findSteam; //# sourceMappingURL=index.js.map