mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
Change mod packs to datastore
This commit is contained in:
4 files changed
+20
-26
No files matched your search
@@ -412,7 +412,7 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
|
|
||||||
async handleModPackDeleteRequest(request: lib.ModPackDeleteRequest) {
|
async handleModPackDeleteRequest(request: lib.ModPackDeleteRequest) {
|
||||||
let { id } = request;
|
let { id } = request;
|
||||||
let modPack = this._controller.modPacks.get(id);
|
let modPack = this._controller.modPacks.getCopy(id);
|
||||||
if (!modPack) {
|
if (!modPack) {
|
||||||
throw new lib.RequestError(`Mod pack with ID ${id} does not exist`);
|
throw new lib.RequestError(`Mod pack with ID ${id} does not exist`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,7 +115,11 @@ export default class Controller {
|
|||||||
const hosts = await Controller.loadHosts(path.join(databaseDirectory, "hosts.json"));
|
const hosts = await Controller.loadHosts(path.join(databaseDirectory, "hosts.json"));
|
||||||
const instances = await Controller.loadInstances(path.join(databaseDirectory, "instances.json"));
|
const instances = await Controller.loadInstances(path.join(databaseDirectory, "instances.json"));
|
||||||
const saves = await Controller.loadSaves(path.join(databaseDirectory, "saves.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);
|
const userManager = new UserManager(config);
|
||||||
await userManager.load(path.join(databaseDirectory, "users.json"));
|
await userManager.load(path.join(databaseDirectory, "users.json"));
|
||||||
|
|
||||||
@@ -153,7 +157,7 @@ export default class Controller {
|
|||||||
/** Mapping of save id to save details */
|
/** Mapping of save id to save details */
|
||||||
public saves = new Map<string, lib.SaveDetails>(),
|
public saves = new Map<string, lib.SaveDetails>(),
|
||||||
/** Mapping of mod pack id to mod pack */
|
/** 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 */
|
/** Mods stored on the controller */
|
||||||
public modStore = new lib.ModStore(config.get("controller.mods_directory"), new Map()),
|
public modStore = new lib.ModStore(config.get("controller.mods_directory"), new Map()),
|
||||||
/** User and roles manager for the cluster */
|
/** 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);
|
await Controller.saveSaves(path.join(databaseDirectory, "saves.json"), this.saves);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.modPacksDirty) {
|
await this.modPacks.save();
|
||||||
this.modPacksDirty = false;
|
|
||||||
await Controller.saveModPacks(path.join(databaseDirectory, "mod-packs.json"), this.modPacks);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.userManager.dirty) {
|
if (this.userManager.dirty) {
|
||||||
await this.userManager.save(path.join(databaseDirectory, "users.json"));
|
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"));
|
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> {
|
static async loadJsonObject(filePath: string, throwOnMissing: boolean = false): Promise<any> {
|
||||||
let manifest = {};
|
let manifest = {};
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ async function uploadExport(req: Request, res: Response) {
|
|||||||
res.sendStatus(400);
|
res.sendStatus(400);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let modPack = res.app.locals.controller.modPacks.get(modPackId);
|
let modPack = res.app.locals.controller.modPacks.getCopy(modPackId);
|
||||||
if (!modPack) {
|
if (!modPack) {
|
||||||
res.sendStatus(400);
|
res.sendStatus(400);
|
||||||
return;
|
return;
|
||||||
@@ -261,6 +261,7 @@ async function uploadExport(req: Request, res: Response) {
|
|||||||
|
|
||||||
modPack.exportManifest = new lib.ExportManifest(assets);
|
modPack.exportManifest = new lib.ExportManifest(assets);
|
||||||
modPack.fillDefaultSettings(settingPrototypes, logger);
|
modPack.fillDefaultSettings(settingPrototypes, logger);
|
||||||
|
res.app.locals.controller.modPacks.set(modPack.id, modPack);
|
||||||
res.app.locals.controller.modPacksUpdated([modPack]);
|
res.app.locals.controller.modPacksUpdated([modPack]);
|
||||||
|
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
|
|||||||
@@ -189,6 +189,11 @@ export class Datastore<
|
|||||||
this.data.set(k, v);
|
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
|
// 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);
|
this.data.set(value.id, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete a value in the datastore
|
||||||
|
delete(value: V) {
|
||||||
|
this.data.delete(value.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user