|
1 | 1 | import { Injectable } from '@angular/core';
|
2 | 2 | import { HttpClient } from '@angular/common/http';
|
3 |
| -import { firstValueFrom, map } from 'rxjs'; |
| 3 | +import { firstValueFrom } from 'rxjs'; |
| 4 | +import { SharedLock } from '../utils/sharedLock'; |
| 5 | +import { CommandQueueService } from './commandQueue.service'; |
| 6 | +import { EmulatorService } from './emulator.service'; |
| 7 | + |
| 8 | +export enum VersionStatus { |
| 9 | + Fetching, |
| 10 | + Fetched, |
| 11 | + Loading, |
| 12 | + Extracting, |
| 13 | + Loaded, |
| 14 | +} |
4 | 15 |
|
5 | 16 | @Injectable({
|
6 | 17 | providedIn: 'root',
|
7 | 18 | })
|
8 | 19 | export class ByondService {
|
9 |
| - public latestVersion: Promise<{ beta?: ByondVersion; stable: ByondVersion }>; |
| 20 | + public latestVersion: Promise<{ beta?: string; stable: string }>; |
| 21 | + private lock = new SharedLock(); |
10 | 22 |
|
11 |
| - constructor(httpClient: HttpClient) { |
| 23 | + constructor( |
| 24 | + private httpClient: HttpClient, |
| 25 | + private commandQueueService: CommandQueueService, |
| 26 | + private emulatorService: EmulatorService, |
| 27 | + ) { |
12 | 28 | this.latestVersion = firstValueFrom(
|
13 |
| - httpClient |
14 |
| - .get('https://secure.byond.com/download/version.txt', { |
15 |
| - responseType: 'text', |
16 |
| - }) |
17 |
| - .pipe( |
18 |
| - map((x) => { |
19 |
| - const [stable, beta] = x |
20 |
| - .split('\n') |
21 |
| - .filter((x) => x) |
22 |
| - .map((x) => new ByondVersion(x)); |
23 |
| - return { stable, beta }; |
24 |
| - }), |
25 |
| - ), |
| 29 | + httpClient.get('https://secure.byond.com/download/version.txt', { |
| 30 | + responseType: 'text', |
| 31 | + }), |
| 32 | + ).then((x) => { |
| 33 | + const [stable, beta] = x.split('\n').filter((x) => x); |
| 34 | + return { stable, beta }; |
| 35 | + }); |
| 36 | + void this.lock.run(() => |
| 37 | + commandQueueService.runToSuccess( |
| 38 | + '/bin/mkdir', |
| 39 | + '-p\0/mnt/host/byond\0/var/lib/byond', |
| 40 | + ), |
26 | 41 | );
|
| 42 | + void this.lock.run(async () => { |
| 43 | + for await (const version of (await this.getByondFolder()).keys()) { |
| 44 | + this._versions.set(version, VersionStatus.Fetched); |
| 45 | + } |
| 46 | + }); |
27 | 47 | }
|
28 |
| -} |
29 | 48 |
|
30 |
| -export class ByondVersion { |
31 |
| - public readonly major: number; |
32 |
| - public readonly minor: number; |
33 |
| - |
34 |
| - constructor(version: string); |
35 |
| - constructor(major: number, minor: number); |
36 |
| - constructor(versionOrMajor: string | number, minor?: number) { |
37 |
| - if (typeof versionOrMajor === 'number') { |
38 |
| - this.major = versionOrMajor; |
39 |
| - this.minor = minor!; |
40 |
| - } else { |
41 |
| - console.log(versionOrMajor.split('.')); |
42 |
| - const [major, minor] = versionOrMajor.split('.').map((x) => parseInt(x)); |
43 |
| - this.major = major; |
44 |
| - this.minor = minor; |
45 |
| - } |
| 49 | + private _versions = new Map<string, VersionStatus>(); |
| 50 | + |
| 51 | + public get versions(): ReadonlyMap<string, VersionStatus> { |
| 52 | + return this._versions; |
46 | 53 | }
|
47 | 54 |
|
48 |
| - toString() { |
49 |
| - return `${this.major}.${this.minor}`; |
| 55 | + public deleteVersion = this.lock.wrap(async (version: string) => { |
| 56 | + const installs = await this.getByondFolder(); |
| 57 | + await installs.removeEntry(version.toString()); |
| 58 | + this._versions.delete(version.toString()); |
| 59 | + await this.commandQueueService.runToCompletion( |
| 60 | + '/bin/rm', |
| 61 | + `-rf\0/var/lib/byond/${version}.zip\0/var/lib/byond/${version}`, |
| 62 | + ); |
| 63 | + }); |
| 64 | + public getVersion = this.lock.wrap(async (version: string) => { |
| 65 | + try { |
| 66 | + const installs = await this.getByondFolder(); |
| 67 | + const handle = await installs.getFileHandle(version.toString(), { |
| 68 | + create: true, |
| 69 | + }); |
| 70 | + const readHandle = await handle.getFile(); |
| 71 | + if (readHandle.size != 0) return readHandle; |
| 72 | + |
| 73 | + this._versions.set(version.toString(), VersionStatus.Fetching); |
| 74 | + const major = version.split('.')[0]; |
| 75 | + const zipFile = await firstValueFrom( |
| 76 | + this.httpClient.get( |
| 77 | + `https://www.byond.com/download/build/${major}/${version}_byond_linux.zip`, |
| 78 | + { responseType: 'blob' }, |
| 79 | + ), |
| 80 | + ); |
| 81 | + const writeHandle = await handle.createWritable(); |
| 82 | + await writeHandle.write(zipFile); |
| 83 | + this._versions.set(version.toString(), VersionStatus.Fetched); |
| 84 | + await writeHandle.close(); |
| 85 | + return new File([zipFile], version); |
| 86 | + } catch (e) { |
| 87 | + void this.deleteVersion(version); |
| 88 | + this._versions.delete(version.toString()); |
| 89 | + throw e; |
| 90 | + } |
| 91 | + }); |
| 92 | + public setActive = this.lock.wrap(async (version: string) => { |
| 93 | + const status = this._versions.get(version); |
| 94 | + if (status == null || status < VersionStatus.Fetched) return; |
| 95 | + |
| 96 | + if (status < VersionStatus.Loaded) { |
| 97 | + try { |
| 98 | + this._versions.set(version, VersionStatus.Loading); |
| 99 | + const zipFile = await this.getVersion(version, true); |
| 100 | + await this.emulatorService.sendFile( |
| 101 | + `byond/${version}.zip`, |
| 102 | + new Uint8Array(await zipFile.arrayBuffer()), |
| 103 | + ); |
| 104 | + this._versions.set(version, VersionStatus.Extracting); |
| 105 | + await this.commandQueueService.runToSuccess( |
| 106 | + '/bin/mv', |
| 107 | + `/mnt/host/byond/${version}.zip\0/var/lib/byond/`, |
| 108 | + ); |
| 109 | + await this.commandQueueService.runToSuccess( |
| 110 | + '/bin/unzip', |
| 111 | + `/var/lib/byond/${version}.zip\0byond/bin*\0-j\0-d\0/var/lib/byond/${version}`, |
| 112 | + ); |
| 113 | + await this.commandQueueService.runToSuccess( |
| 114 | + '/bin/rm', |
| 115 | + `/var/lib/byond/${version}.zip`, |
| 116 | + ); |
| 117 | + this._versions.set(version, VersionStatus.Loaded); |
| 118 | + } catch (e) { |
| 119 | + this._versions.set(version, VersionStatus.Fetched); |
| 120 | + await this.commandQueueService.runToCompletion( |
| 121 | + '/bin/rm', |
| 122 | + `-rf\0/var/lib/byond/${version}.zip\0/var/lib/byond/${version}`, |
| 123 | + ); |
| 124 | + throw e; |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + private async getByondFolder() { |
| 130 | + const opfs = await navigator.storage.getDirectory(); |
| 131 | + return await opfs.getDirectoryHandle('byond', { create: true }); |
50 | 132 | }
|
51 | 133 | }
|
0 commit comments