forked from Zagrios/bs-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbs-local-version.service.ts
More file actions
437 lines (349 loc) · 16.2 KB
/
Copy pathbs-local-version.service.ts
File metadata and controls
437 lines (349 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import { BSVersionLibService } from "./bs-version-lib.service";
import { BSVersion, BSVersionMetadata } from "shared/bs-version.interface";
import { InstallationLocationService } from "./installation-location.service";
import { SteamService } from "./steam.service";
import { BS_APP_ID, OCULUS_BS_BACKUP_DIR, OCULUS_BS_DIR } from "../constants";
import path from "path";
import { ConfigurationService } from "./configuration.service";
import { lstat, rename } from "fs/promises";
import log from "electron-log";
import { OculusService } from "./oculus.service";
import { DownloadLinkType } from "shared/models/mods";
import sanitize from "sanitize-filename";
import { Progression, copyDirectoryWithJunctions, deleteFolder, ensurePathNotAlreadyExist, getFoldersInFolder, rxCopy } from "../helpers/fs.helpers";
import { FolderLinkerService } from "./folder-linker.service";
import { ReadStream, createReadStream, pathExists, pathExistsSync, readFile, writeFile } from "fs-extra";
import readline from "readline";
import { Observable, Subject, catchError, finalize, from, map, switchMap, throwError } from "rxjs";
import { BsStore } from "../../shared/models/bs-store.enum";
import { CustomError } from "../../shared/models/exceptions/custom-error.class";
import crypto from "crypto";
import { StaticConfigurationService } from "./static-configuration.service";
export class BSLocalVersionService {
private static instance: BSLocalVersionService;
private readonly CUSTOM_VERSIONS_KEY = "custom-versions";
private readonly METADATA_FILE = "metadata.config";
private readonly installLocationService: InstallationLocationService;
private readonly steamService: SteamService;
private readonly oculusService: OculusService;
private readonly remoteVersionService: BSVersionLibService;
private readonly configService: ConfigurationService;
private readonly linker: FolderLinkerService;
private readonly staticConfig: StaticConfigurationService;
private readonly _loadedVersions$: Subject<BSVersion[]>;
public static getInstance(): BSLocalVersionService {
if (!BSLocalVersionService.instance) {
BSLocalVersionService.instance = new BSLocalVersionService();
}
return BSLocalVersionService.instance;
}
private constructor() {
this.installLocationService = InstallationLocationService.getInstance();
this.steamService = SteamService.getInstance();
this.oculusService = OculusService.getInstance();
this.remoteVersionService = BSVersionLibService.getInstance();
this.configService = ConfigurationService.getInstance();
this.linker = FolderLinkerService.getInstance();
this.staticConfig = StaticConfigurationService.getInstance();
this._loadedVersions$ = new Subject<BSVersion[]>();
}
private async getVersionFromGlobalGameManagerFile(versionFilePath: string): Promise<BSVersion> {
if(!(await pathExists(versionFilePath))){
log.info("globalgamemanagers file not found", versionFilePath);
return null;
}
const versionsDict = await this.remoteVersionService.getAvailableVersions();
let stream: ReadStream;
try{
stream = createReadStream(versionFilePath);
const rl = readline.createInterface({
input: stream,
crlfDelay: Infinity
});
for await (const line of rl) {
for (const bsVersion of versionsDict) {
if (line.includes(bsVersion.BSVersion)) {
stream.close();
return {...bsVersion};
}
}
}
} catch(e) {
log.error(e);
} finally {
stream?.close();
}
log.info("unable to get version from globalgamemanagers file", versionFilePath);
return null;
}
public async getVersionOfBSFolder(
bsPath: string,
options?: {
steam?: boolean;
oculus?: boolean;
}
): Promise<BSVersion>{
log.info("getVersionOfBSFolder", bsPath, options);
if(!bsPath){ return null; }
const versionFilePath = path.join(bsPath, 'Beat Saber_Data', 'globalgamemanagers');
const folderVersion = await this.getVersionFromGlobalGameManagerFile(versionFilePath);
folderVersion.path = bsPath;
if(!folderVersion){ return null; }
if(options?.steam || options?.oculus){
return {...folderVersion, ...options};
}
if(folderVersion.BSVersion !== path.basename(bsPath)){
folderVersion.name = path.basename(bsPath);
}
const folderStats = await lstat(bsPath);
if(folderStats.ino){
folderVersion.ino = folderStats.ino;
}
let metadata = await this.getAllVersionMetadata(folderVersion);
// Will be removed in future version. It just to prepare future features
if(!metadata?.id){
metadata = await this.initVersionMetadata(folderVersion, metadata ?? { store: BsStore.STEAM });
}
folderVersion.metadata = metadata;
const customVersion = this.getCustomVersions().find(customVersion => {
return customVersion.BSVersion === folderVersion.BSVersion && customVersion.name === folderVersion.name;
});
folderVersion.color = customVersion?.color;
return folderVersion;
}
private async writeVersionMetadata(version: BSVersion, metadata: BSVersionMetadata): Promise<void>{
const versionPath = await this.getVersionPath(version);
const metadataPath = path.join(versionPath, this.METADATA_FILE);
return writeFile(metadataPath, JSON.stringify(metadata));
}
public initVersionMetadata(version: BSVersion, metadata: Omit<BSVersionMetadata, "id">): Promise<BSVersionMetadata>{
const firstMetadata = { id: crypto.randomUUID(), ...metadata };
return this.writeVersionMetadata(version, firstMetadata).then(() => firstMetadata).catch(err => {
log.error("initVersionMetadata error", err);
return firstMetadata;
});
}
public getAllVersionMetadata(version: BSVersion): Promise<BSVersionMetadata>{
return (async () => {
const versionPath = await this.getVersionPath(version);
const contents = await readFile(path.join(versionPath, this.METADATA_FILE), "utf-8");
return JSON.parse(contents);
})().catch(e => {
log.warn("Error getAllVersionMetadata", e);
});
}
private setCustomVersions(versions: BSVersion[]): void{
this.configService.set(this.CUSTOM_VERSIONS_KEY, versions);
}
private addCustomVersion(version: BSVersion): void{
this.setCustomVersions([...this.getCustomVersions() ?? [], version]);
}
private updateLastVersionLaunched(version: BSVersion, editedVersion: BSVersion): void {
const lastVersion = this.staticConfig.get("last-version-launched");
if (!lastVersion) {
return;
}
if (
version.BSVersion !== lastVersion.BSVersion
|| version.name !== lastVersion.name
|| version.steam !== lastVersion.steam
|| version.oculus !== lastVersion.oculus
) {
return;
}
this.staticConfig.set("last-version-launched", editedVersion);
}
private getCustomVersions(): BSVersion[]{
return this.configService.get<BSVersion[]>(this.CUSTOM_VERSIONS_KEY) || [];
}
private deleteCustomVersion(version: BSVersion): void{
const customVersions = this.getCustomVersions() || [];
this.setCustomVersions(customVersions.filter(v => (v.name !== version.name || v.BSVersion !== version.BSVersion || v.color !== version.color)));
}
/**
* Return path of a version even if it's not installed.
* @param {BSVersion} version
* @returns {Promise<string>}
*/
public async getVersionPath(version: BSVersion): Promise<string>{
if(version.steam){ return this.steamService.getGameFolder(BS_APP_ID, "Beat Saber") }
if(version.oculus){ return this.oculusService.tryGetGameFolder([OCULUS_BS_DIR, OCULUS_BS_BACKUP_DIR]); }
return path.join(
this.installLocationService.versionsDirectory(),
this.getVersionFolder(version)
);
}
/**
* Return path of an installed version. Returns null if not found.
* @param {BSVersion} version
* @returns {Promise<string>}
*/
public async getInstalledVersionPath(version: BSVersion): Promise<string>{
const versionPath = await this.getVersionPath(version);
if(await pathExists(versionPath)){ return versionPath; }
const versionFolders = await getFoldersInFolder(this.installLocationService.versionsDirectory());
for(const folder of versionFolders){
const stats = await lstat(folder);
if(stats.ino === version.ino){
return folder;
}
}
return null;
}
public getVersionFolder(version: BSVersion): string{
return version.name ?? version.BSVersion;
}
public getVersionType(version: BSVersion): DownloadLinkType {
if (version.steam) {
return "steam";
}
if (version.oculus) {
return "oculus";
}
return "universal";
}
private async getSteamVersion(): Promise<BSVersion> {
const steamBsFolder = await this.steamService.getGameFolder(BS_APP_ID, "Beat Saber");
if (!steamBsFolder || !(await pathExists(steamBsFolder))) {
return null;
}
return this.getVersionOfBSFolder(steamBsFolder, { steam: true });
}
private async getOculusVersion(): Promise<BSVersion> {
const oculusBsFolder = await this.oculusService.tryGetGameFolder([OCULUS_BS_DIR, OCULUS_BS_BACKUP_DIR]);
if (!oculusBsFolder) {
return null;
}
return this.getVersionOfBSFolder(oculusBsFolder, { oculus: true });
}
public async getInstalledVersions(): Promise<BSVersion[]> {
const versions: BSVersion[] = [];
const steamVersion = await this.getSteamVersion().catch(e => {
log.error("unable to get original Steam version", e);
});
if (steamVersion) {
versions.push(steamVersion);
}
const oculusVersion = await this.getOculusVersion().catch(e => {
log.error("unable to get original Oculus version", e);
});
if (oculusVersion) {
versions.push(oculusVersion);
}
if (!(await pathExists(this.installLocationService.versionsDirectory()))) {
return versions;
}
const folderInInstallation = await getFoldersInFolder(this.installLocationService.versionsDirectory());
log.info("Finded versions folders", folderInInstallation);
for (const f of folderInInstallation) {
const version = await this.getVersionOfBSFolder(f).catch(e => {
log.error("unable to get version of folder", f, e);
});
if(!version){ continue; }
versions.push(version);
};
this.setCustomVersions(versions.filter(v => !!v.color));
this._loadedVersions$.next(versions);
return versions;
}
public async deleteVersion(version: BSVersion): Promise<boolean>{
if(version.steam || version.oculus){ return false; }
const versionFolder = await this.getVersionPath(version);
if(!(await pathExists(versionFolder))){ return true; }
return deleteFolder(versionFolder)
.then(() => { return true; })
.catch(() => { return false; })
}
public async editVersion(version: BSVersion, name: string, color: string): Promise<BSVersion>{
if(version.steam || version.oculus){ throw new CustomError("Do not edit official Beat Saber versions", "CantEditSteam") }
const oldPath = await this.getVersionPath(version);
const editedVersion: BSVersion = version.BSVersion === name
? {...version, name: undefined, color}
: {...version, name: sanitize(name), color};
const newPath = await this.getVersionPath(editedVersion);
if(oldPath === newPath){
this.deleteCustomVersion(version);
this.addCustomVersion(editedVersion);
this.updateLastVersionLaunched(version, editedVersion);
return editedVersion;
}
if(pathExistsSync(newPath)){
throw new CustomError("Unable to edit the version, path already exist", "VersionAlreadExist");
}
return rename(oldPath, newPath).then(() => {
this.deleteCustomVersion(version);
this.addCustomVersion(editedVersion);
this.updateLastVersionLaunched(version, editedVersion);
return editedVersion;
}).catch((err: Error) => {
log.error("edit version error", err, version, name, color);
throw CustomError.fromError(err, "CantRename");
});
}
public async cloneVersion(version: BSVersion, name: string, color: string): Promise<BSVersion>{
const originPath = await this.getVersionPath(version);
const cloneVersion: BSVersion = version.BSVersion === name
? {...version, name: undefined, color, steam: false, oculus: false}
: {...version, name: sanitize(name), color, steam: false, oculus: false};
const newPath = await this.getVersionPath(cloneVersion);
if(pathExistsSync(newPath)){
throw new CustomError("Unable to clone the version, path already exist", "VersionAlreadExist");
}
if(originPath === newPath){
this.deleteCustomVersion(version);
this.addCustomVersion(cloneVersion);
}
return copyDirectoryWithJunctions(originPath, newPath).then(() => {
this.addCustomVersion(cloneVersion);
return cloneVersion;
}).catch((err: Error) => {
log.error("Error occured while cloning the version", err, version, name, color);
throw CustomError.fromError(err, "CantClone");
})
}
public importVersion(opt: ImportVersionOptions): Observable<Progression<BSVersion>>{
const { fromPath, store } = opt;
let failed = false;
let versionDest: {version: BSVersion, dest: string} = null;
return from(this.getVersionOfBSFolder(fromPath)).pipe(
map(version => version || CustomError.throw(new Error("Unable to get BS version of path"), "NOT_BS_FOLDER")),
switchMap(version => this.getVersionPath(version).then(dest => ({version, dest}))),
switchMap(({version, dest}) => ensurePathNotAlreadyExist(dest).then(uniquePath => {
const res = dest === uniquePath ? {version, dest} : {version: {...version, name: path.basename(uniquePath)}, dest: uniquePath} as {version: BSVersion, dest: string};
versionDest = res;
return res;
})),
switchMap(({version, dest}) => rxCopy(fromPath, dest, { dereference: true }).pipe(
map(progress => ({...progress, data: version}))
)),
catchError(err => {
failed = true;
return throwError(() => err);
}),
finalize(async () => {
if(failed){ return; }
await this.initVersionMetadata(versionDest.version, { store });
})
);
}
public async getLinkedFolders(version: BSVersion): Promise<string[]>{
const versionPath = await this.getVersionPath(version);
const [rootFolders, beatSaberDataFolders] = await Promise.all([getFoldersInFolder(versionPath), getFoldersInFolder(path.join(versionPath, "Beat Saber_Data"))]);
const linkedFolder = Promise.all(
[...rootFolders, ...beatSaberDataFolders].map(async folder => {
if (!(await this.linker.isFolderSymlink(folder))) {
return null;
}
return folder;
})
);
return (await linkedFolder).filter(folder => folder);
}
public get loadedVersions$(): Observable<BSVersion[]>{
return this._loadedVersions$.asObservable();
}
}
export interface ImportVersionOptions {
fromPath: string;
store: BsStore
}