Skip to content

Commit

Permalink
[chore] add id in version's metadatas for future features
Browse files Browse the repository at this point in the history
  • Loading branch information
Zagrios committed Dec 26, 2023
1 parent 2bba3c5 commit 7b22387
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
50 changes: 25 additions & 25 deletions src/main/services/bs-local-version.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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";


export class BSLocalVersionService {
private static instance: BSLocalVersionService;
Expand Down Expand Up @@ -119,7 +121,14 @@ export class BSLocalVersionService {
folderVersion.ino = folderStats.ino;
}

folderVersion.metadata = await this.getAllVersionMetadata(folderVersion, { store: BsStore.STEAM });

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;
Expand All @@ -130,36 +139,27 @@ export class BSLocalVersionService {
return folderVersion;
}

public getAllVersionMetadata(version: BSVersion, defaultMetadata?: BSVersionMetadata): 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("Not a critical error", e);
return {};
}).then(metadata => ({...defaultMetadata, ...metadata}));
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 getVersionMetadata<K extends keyof BSVersionMetadata, T extends BSVersionMetadata[K]>(version: BSVersion, key: K, defaultValue?: T): Promise<T|undefined>{
return (async () => {
const metadata = await this.getAllVersionMetadata(version);
return metadata?.[key];
})().catch(e => {
log.warn("Not a critical error", e);
return null;
}).then(value => (value ?? defaultValue) as T);
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 setVersionMetadata<K extends keyof BSVersionMetadata, T extends BSVersionMetadata[K]>(version: BSVersion, key: K, value: T): Promise<void>{
public getAllVersionMetadata(version: BSVersion): Promise<BSVersionMetadata>{
return (async () => {
const versionPath = await this.getVersionPath(version);
const metadataPath = path.join(versionPath, this.METADATA_FILE);
const metadata = await this.getAllVersionMetadata(version) ?? ({} as BSVersionMetadata);
metadata[key] = value;
await writeFile(metadataPath, JSON.stringify(metadata));
const contents = await readFile(path.join(versionPath, this.METADATA_FILE), "utf-8");
return JSON.parse(contents);
})().catch(e => {
log.error(e);
log.warn("Error getAllVersionMetadata", e);
});
}

Expand Down Expand Up @@ -378,7 +378,7 @@ export class BSLocalVersionService {
}),
finalize(async () => {
if(failed){ return; }
await this.setVersionMetadata(versionDest.version, "store", store);
await this.initVersionMetadata(versionDest.version, { store });
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class BsOculusDownloaderService {
version: {
...version,
...(path.basename(dest) !== version.BSVersion && { name: path.basename(dest) }),
metadata: { store: BsStore.OCULUS }
metadata: { store: BsStore.OCULUS, id: "" }
},
dest
};
Expand Down Expand Up @@ -141,7 +141,7 @@ export class BsOculusDownloaderService {
progress => ({...progress, data: version})
));
}),
finalize(() => downloadVersion && this.versions.setVersionMetadata(downloadVersion, "store", BsStore.OCULUS)),
finalize(() => downloadVersion && this.versions.initVersionMetadata(downloadVersion, { store: BsStore.OCULUS })),
finalize(() => this.oculusDownloader.stopDownload())
);
}
Expand Down Expand Up @@ -171,7 +171,7 @@ export class BsOculusDownloaderService {
map(progress => ({...progress, data: version})),
);
}),
finalize(() => downloadVersion && this.versions.setVersionMetadata(downloadVersion, "store", BsStore.OCULUS)),
finalize(() => downloadVersion && this.versions.initVersionMetadata(downloadVersion, { store: BsStore.OCULUS })),
finalize(() => this.oculusDownloader.stopDownload()),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class BsSteamDownloaderService {
const downloadVersion: BSVersion = {
...downloadInfos.bsVersion,
...(path.basename(dest) !== downloadInfos.bsVersion.BSVersion && { name: path.basename(dest) }),
metadata: { store: BsStore.STEAM }
metadata: { store: BsStore.STEAM, id: "" }
};

const depotDownloaderOptions: DepotDownloaderArgsOptions = {
Expand Down Expand Up @@ -118,7 +118,7 @@ export class BsSteamDownloaderService {
}
return event;
}),
finalize(() => this.localVersionService.setVersionMetadata(version, "store", BsStore.STEAM))
finalize(() => this.localVersionService.initVersionMetadata(version, { store: BsStore.STEAM }))
).subscribe(sub);

}).catch(err => sub.error({
Expand Down
1 change: 1 addition & 0 deletions src/shared/bs-version.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface BSVersion extends PartialBSVersion {
}

export interface BSVersionMetadata {
id: string;
store: BsStore;
}

Expand Down

0 comments on commit 7b22387

Please sign in to comment.