Change mod packs to datastore

This commit is contained in:
Cooldude2606 committed 2024-05-19 01:41:28 +01:00
1 parent 80b4a96e85
commit 6b11fb810a
4 files changed
+20 -26

No files matched your search

+1 -1
View File
@@ -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`);
}
+7 -24
View File
@@ -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<string, lib.SaveDetails>(),
/** Mapping of mod pack id to mod pack */
public modPacks = new Map<number, lib.ModPack>(),
public modPacks = new lib.Datastore<lib.ModPack["id"], lib.ModPack>(),
/** 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<Map<number, lib.ModPack>> {
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<number, lib.ModPack>) {
await lib.safeOutputFile(filePath, JSON.stringify([...modPacks.values()], null, "\t"));
}
static async loadJsonObject(filePath: string, throwOnMissing: boolean = false): Promise<any> {
let manifest = {};
try {
+2 -1
View File
@@ -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);
+10
View File
@@ -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);
}
}