From 6b11fb810ad50cc9499bd2d24516e56fb9a0340d Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sun, 19 May 2024 01:41:28 +0100 Subject: [PATCH] Change mod packs to datastore --- packages/controller/src/ControlConnection.ts | 2 +- packages/controller/src/Controller.ts | 31 +++++--------------- packages/controller/src/routes.ts | 3 +- packages/lib/src/datastore.ts | 10 +++++++ 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/packages/controller/src/ControlConnection.ts b/packages/controller/src/ControlConnection.ts index 595f900b..5db6fcba 100644 --- a/packages/controller/src/ControlConnection.ts +++ b/packages/controller/src/ControlConnection.ts @@ -412,7 +412,7 @@ export default class ControlConnection extends BaseConnection { async handleModPackDeleteRequest(request: lib.ModPackDeleteRequest) { let { id } = request; - let modPack = this._controller.modPacks.get(id); + let modPack = this._controller.modPacks.getCopy(id); if (!modPack) { throw new lib.RequestError(`Mod pack with ID ${id} does not exist`); } diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index f256da32..d4950775 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -115,7 +115,11 @@ export default class Controller { const hosts = await Controller.loadHosts(path.join(databaseDirectory, "hosts.json")); const instances = await Controller.loadInstances(path.join(databaseDirectory, "instances.json")); const saves = await Controller.loadSaves(path.join(databaseDirectory, "saves.json")); - const modPacks = await Controller.loadModPacks(path.join(databaseDirectory, "mod-packs.json")); + + const modPacks = new lib.Datastore(...await new lib.JsonIdDatastoreProvider( + path.join(databaseDirectory, "mod-packs.json"), lib.ModPack + ).bootstrap()); + const userManager = new UserManager(config); await userManager.load(path.join(databaseDirectory, "users.json")); @@ -153,7 +157,7 @@ export default class Controller { /** Mapping of save id to save details */ public saves = new Map(), /** Mapping of mod pack id to mod pack */ - public modPacks = new Map(), + public modPacks = new lib.Datastore(), /** Mods stored on the controller */ public modStore = new lib.ModStore(config.get("controller.mods_directory"), new Map()), /** User and roles manager for the cluster */ @@ -566,10 +570,7 @@ export default class Controller { await Controller.saveSaves(path.join(databaseDirectory, "saves.json"), this.saves); } - if (this.modPacksDirty) { - this.modPacksDirty = false; - await Controller.saveModPacks(path.join(databaseDirectory, "mod-packs.json"), this.modPacks); - } + await this.modPacks.save(); if (this.userManager.dirty) { await this.userManager.save(path.join(databaseDirectory, "users.json")); @@ -683,24 +684,6 @@ export default class Controller { await lib.safeOutputFile(filePath, JSON.stringify([...saves.values()], null, "\t")); } - - static async loadModPacks(filePath: string): Promise> { - let json; - try { - json = JSON.parse(await fs.readFile(filePath, { encoding: "utf8" })); - } catch (err: any) { - if (err.code !== "ENOENT") { - throw err; - } - return new Map(); - } - return new Map(json.map((e: any) => [e.id, lib.ModPack.fromJSON(e)])); - } - - static async saveModPacks(filePath: string, modPacks: Map) { - await lib.safeOutputFile(filePath, JSON.stringify([...modPacks.values()], null, "\t")); - } - static async loadJsonObject(filePath: string, throwOnMissing: boolean = false): Promise { let manifest = {}; try { diff --git a/packages/controller/src/routes.ts b/packages/controller/src/routes.ts index 51b25bee..e4336bbb 100644 --- a/packages/controller/src/routes.ts +++ b/packages/controller/src/routes.ts @@ -218,7 +218,7 @@ async function uploadExport(req: Request, res: Response) { res.sendStatus(400); return; } - let modPack = res.app.locals.controller.modPacks.get(modPackId); + let modPack = res.app.locals.controller.modPacks.getCopy(modPackId); if (!modPack) { res.sendStatus(400); return; @@ -261,6 +261,7 @@ async function uploadExport(req: Request, res: Response) { modPack.exportManifest = new lib.ExportManifest(assets); modPack.fillDefaultSettings(settingPrototypes, logger); + res.app.locals.controller.modPacks.set(modPack.id, modPack); res.app.locals.controller.modPacksUpdated([modPack]); res.sendStatus(200); diff --git a/packages/lib/src/datastore.ts b/packages/lib/src/datastore.ts index fe17b37e..af82a446 100644 --- a/packages/lib/src/datastore.ts +++ b/packages/lib/src/datastore.ts @@ -189,6 +189,11 @@ export class Datastore< this.data.set(k, v); } } + + // Delete a value in the datastore + delete(key: K) { + this.data.delete(key); + } } // A special implementation of the datastore to work directly with subscribable values @@ -212,4 +217,9 @@ export class SubscribableDatastore< this.data.set(value.id, value); } } + + // Delete a value in the datastore + delete(value: V) { + this.data.delete(value.id); + } }