mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Refactor JSON serialiable class pattern
Change the JSON seriasiable data classes to use an explicit static fromJSON method and an optional toJSON method for turning them to and from JSON. This improves over the previous pattern of passing the JSON to the constructor as it frees up the constructor to be able to take whichever argument types suits the code instead of being beholden to the JSON serialised form.
This commit is contained in:
1 parent
a448d87e3a
commit
9a967a5779
20 files changed
+222
-97
No files matched your search
@@ -385,7 +385,7 @@ class ControlConnection extends BaseConnection {
|
||||
}
|
||||
|
||||
async createModPackRequestHandler(message) {
|
||||
let modPack = new libData.ModPack(message.data.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
|
||||
if (this._controller.modPacks.has(modPack.id)) {
|
||||
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} already exist`);
|
||||
}
|
||||
@@ -394,7 +394,7 @@ class ControlConnection extends BaseConnection {
|
||||
}
|
||||
|
||||
async updateModPackRequestHandler(message) {
|
||||
let modPack = new libData.ModPack(message.data.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
|
||||
if (!this._controller.modPacks.has(modPack.id)) {
|
||||
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
|
||||
}
|
||||
|
||||
@@ -413,7 +413,7 @@ class Controller {
|
||||
}
|
||||
return new Map();
|
||||
}
|
||||
return new Map(json.map(e => [e.id, new libData.ModPack(e)]));
|
||||
return new Map(json.map(e => [e.id, libData.ModPack.fromJSON(e)]));
|
||||
}
|
||||
|
||||
static async saveModPacks(filePath, modPacks) {
|
||||
|
||||
@@ -26,11 +26,9 @@ class InstanceInfo {
|
||||
*/
|
||||
status;
|
||||
|
||||
// This is not actually json as a config can't be loaded synchronously but
|
||||
// that is likely to change in the future.
|
||||
constructor(json) {
|
||||
this.config = json["config"];
|
||||
this.status = json["status"];
|
||||
constructor({ config, status }) {
|
||||
this.config = config;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
||||
@@ -232,7 +232,7 @@ async function uploadExport(req, res) {
|
||||
await libFileOps.safeOutputFile(path.join("static", `${name}.${hash}${ext}`), await file.async("nodebuffer"));
|
||||
}
|
||||
|
||||
modPack.exportManifest = new libData.ExportManifest({ assets });
|
||||
modPack.exportManifest = new libData.ExportManifest(assets);
|
||||
modPack.fillDefaultSettings(settingPrototypes, logger);
|
||||
res.app.locals.controller.modPackUpdated(modPack);
|
||||
|
||||
|
||||
@@ -1002,7 +1002,7 @@ modPackCommands.add(new libCommand.Command({
|
||||
});
|
||||
}],
|
||||
handler: async function(args, control) {
|
||||
const modPack = new libData.ModPack();
|
||||
const modPack = libData.ModPack.fromJSON({});
|
||||
modPack.name = args.name;
|
||||
if (args.description) { modPack.description = args.description; }
|
||||
if (args.factorioVersion) {
|
||||
@@ -1038,7 +1038,7 @@ modPackCommands.add(new libCommand.Command({
|
||||
const response = await libLink.messages.getModPack.send(control, {
|
||||
id: await libCommand.resolveModPack(control, args.modPack),
|
||||
});
|
||||
const modPack = new libData.ModPack(response.mod_pack);
|
||||
const modPack = libData.ModPack.fromJSON(response.mod_pack);
|
||||
print(modPack.toModPackString());
|
||||
},
|
||||
}));
|
||||
@@ -1065,7 +1065,7 @@ modPackCommands.add(new libCommand.Command({
|
||||
let response = await libLink.messages.getModPack.send(control, {
|
||||
id: await libCommand.resolveModPack(control, args.modPack),
|
||||
});
|
||||
let modPack = new libData.ModPack(response.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(response.mod_pack);
|
||||
|
||||
if (args.name) { modPack.name = args.name; }
|
||||
if (args.description) { modPack.description = args.description; }
|
||||
@@ -1259,7 +1259,7 @@ modCommands.add(new libCommand.Command({
|
||||
}
|
||||
|
||||
if (result.body.mods && result.body.mods.length) {
|
||||
const mod = new libData.ModInfo(result.body.mods[0]);
|
||||
const mod = libData.ModInfo.fromJSON(result.body.mods[0]);
|
||||
logger.info(`Successfully uploaded ${mod.filename}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -604,7 +604,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
|
||||
} else {
|
||||
response = await libLink.messages.getModPack.send(this, { id: modPackId });
|
||||
}
|
||||
this.activeModPack = new libData.ModPack(response.mod_pack);
|
||||
this.activeModPack = libData.ModPack.fromJSON(response.mod_pack);
|
||||
|
||||
// TODO validate factorioVersion
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ class ExportManifest {
|
||||
*/
|
||||
assets = {};
|
||||
|
||||
constructor(assets) {
|
||||
if (assets) { this.assets = assets; }
|
||||
}
|
||||
|
||||
static jsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
@@ -34,12 +38,8 @@ class ExportManifest {
|
||||
},
|
||||
};
|
||||
|
||||
constructor(json = {}) {
|
||||
if (json.assets) { this.assets = json.assets; }
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return { assets: this.assets };
|
||||
static fromJSON(json) {
|
||||
return new this(json.assets);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -155,22 +155,26 @@ class ModInfo {
|
||||
|
||||
static validate = libSchema.compile(this.jsonSchema);
|
||||
|
||||
constructor(json = {}) {
|
||||
static fromJSON(json) {
|
||||
const modInfo = new this();
|
||||
|
||||
// info.json fields
|
||||
if (json.name) { this.name = json.name; }
|
||||
if (json.version) { this.version = json.version; }
|
||||
if (json.title) { this.title = json.title; }
|
||||
if (json.author) { this.author = json.author; }
|
||||
if (json.contact) { this.contact = json.contact; }
|
||||
if (json.homepage) { this.homepage = json.homepage; }
|
||||
if (json.description) { this.description = json.description; }
|
||||
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
|
||||
if (json.dependencies) { this.dependencies = json.dependencies; }
|
||||
if (json.name) { modInfo.name = json.name; }
|
||||
if (json.version) { modInfo.version = json.version; }
|
||||
if (json.title) { modInfo.title = json.title; }
|
||||
if (json.author) { modInfo.author = json.author; }
|
||||
if (json.contact) { modInfo.contact = json.contact; }
|
||||
if (json.homepage) { modInfo.homepage = json.homepage; }
|
||||
if (json.description) { modInfo.description = json.description; }
|
||||
if (json.factorio_version) { modInfo.factorioVersion = json.factorio_version; }
|
||||
if (json.dependencies) { modInfo.dependencies = json.dependencies; }
|
||||
|
||||
// Additional data
|
||||
if (json.size) { this.size = json.size; }
|
||||
if (json.sha1) { this.sha1 = json.sha1; }
|
||||
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
|
||||
if (json.size) { modInfo.size = json.size; }
|
||||
if (json.sha1) { modInfo.sha1 = json.sha1; }
|
||||
if (json.is_deleted) { modInfo.isDeleted = json.is_deleted; }
|
||||
|
||||
return modInfo;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
@@ -218,7 +222,7 @@ class ModInfo {
|
||||
throw new Error(`Mod's version (${modInfo.version}) is invalid`);
|
||||
}
|
||||
|
||||
return new this({
|
||||
return this.fromJSON({
|
||||
...modInfo,
|
||||
|
||||
size: (await fs.stat(modPath)).size,
|
||||
|
||||
@@ -142,29 +142,33 @@ class ModPack {
|
||||
|
||||
static validate = libSchema.compile(this.jsonSchema);
|
||||
|
||||
constructor(json = {}) {
|
||||
static fromJSON(json) {
|
||||
const modPack = new this();
|
||||
|
||||
if (json.id) {
|
||||
this.id = json.id;
|
||||
modPack.id = json.id;
|
||||
} else {
|
||||
this.id = Math.random() * 2**31 | 0;
|
||||
modPack.id = Math.random() * 2**31 | 0;
|
||||
}
|
||||
if (json.name) { this.name = json.name; }
|
||||
if (json.description) { this.description = json.description; }
|
||||
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
|
||||
if (json.mods) { this.mods = new Map(json.mods.map(m => [m.name, m])); }
|
||||
if (json.name) { modPack.name = json.name; }
|
||||
if (json.description) { modPack.description = json.description; }
|
||||
if (json.factorio_version) { modPack.factorioVersion = json.factorio_version; }
|
||||
if (json.mods) { modPack.mods = new Map(json.mods.map(m => [m.name, m])); }
|
||||
if (json.settings) {
|
||||
this.settings = {
|
||||
modPack.settings = {
|
||||
"startup": new Map(Object.entries(json.settings["startup"])),
|
||||
"runtime-global": new Map(Object.entries(json.settings["runtime-global"])),
|
||||
"runtime-per-user": new Map(Object.entries(json.settings["runtime-per-user"])),
|
||||
};
|
||||
}
|
||||
if (json.export_manifest) { this.exportManifest = new ExportManifest(json.export_manifest); }
|
||||
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
|
||||
if (json.export_manifest) { modPack.exportManifest = ExportManifest.fromJSON(json.export_manifest); }
|
||||
if (json.is_deleted) { modPack.isDeleted = json.is_deleted; }
|
||||
|
||||
if (!this.mods.has("base")) {
|
||||
this.mods.set("base", { name: "base", enabled: true, version: this.factorioVersion });
|
||||
if (!modPack.mods.has("base")) {
|
||||
modPack.mods.set("base", { name: "base", enabled: true, version: modPack.factorioVersion });
|
||||
}
|
||||
|
||||
return modPack;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
@@ -180,7 +184,7 @@ class ModPack {
|
||||
"runtime-per-user": Object.fromEntries(this.settings["runtime-per-user"]),
|
||||
},
|
||||
};
|
||||
if (this.exportManifest) { json.export_manifest = this.exportManifest.toJSON(); }
|
||||
if (this.exportManifest) { json.export_manifest = this.exportManifest; }
|
||||
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
||||
return json;
|
||||
}
|
||||
@@ -217,7 +221,7 @@ class ModPack {
|
||||
throw new Error("Malformed mod pack string: Schema validation failed");
|
||||
}
|
||||
|
||||
return new this(json);
|
||||
return this.fromJSON(json);
|
||||
}
|
||||
|
||||
toModSettingsDat() {
|
||||
|
||||
@@ -57,7 +57,7 @@ function SearchModsTable(props) {
|
||||
// In React < v18 this causes 3 renders and a partial state necessitating the use
|
||||
// of || 0 on modResultSelectedVersion.get() calls. Remove when updating React.
|
||||
setModResults(response.results.map(
|
||||
({ name, versions }) => ({ name, versions: versions.map(mod => new libData.ModInfo(mod)) })
|
||||
({ name, versions }) => ({ name, versions: versions.map(mod => libData.ModInfo.fromJSON(mod)) })
|
||||
));
|
||||
setModResultSelectedVersion(new Map(response.results.map(({ name, versions }) => [name, 0])));
|
||||
setModResultCount(response.result_count);
|
||||
@@ -630,7 +630,7 @@ export default function ModPackViewPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
const modified = new libData.ModPack(modPack.toJSON());
|
||||
const modified = libData.ModPack.fromJSON(JSON.parse(JSON.stringify(modPack)));
|
||||
for (let change of changes) {
|
||||
if (change.type === "name") {
|
||||
modified.name = change.value;
|
||||
|
||||
@@ -95,7 +95,7 @@ function CreateModPackButton() {
|
||||
} catch {
|
||||
return; // Validation failed
|
||||
}
|
||||
const modPack = new libData.ModPack();
|
||||
const modPack = libData.ModPack.fromJSON({});
|
||||
modPack.name = values.name;
|
||||
modPack.factorioVersion = values.factorioVersion;
|
||||
if (values.description) { modPack.description = values.description; }
|
||||
|
||||
@@ -12,7 +12,7 @@ export function useExportManifest() {
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
let result = await libLink.messages.getDefaultModPack.send(control);
|
||||
let modPack = new libData.ModPack(result.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(result.mod_pack);
|
||||
if (modPack.exportManifest) {
|
||||
exportManifestCache = modPack.exportManifest;
|
||||
setExportManifest(exportManifestCache);
|
||||
|
||||
@@ -12,7 +12,7 @@ export function useModPack(id) {
|
||||
|
||||
function updateModPack() {
|
||||
libLink.messages.getModPack.send(control, { id }).then(result => {
|
||||
setModPack(new libData.ModPack(result.mod_pack));
|
||||
setModPack(libData.ModPack.fromJSON(result.mod_pack));
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to get mod pack: ${err}`);
|
||||
setModPack({ missing: true });
|
||||
@@ -41,7 +41,7 @@ export function useModPackList() {
|
||||
|
||||
function updateModPackList() {
|
||||
libLink.messages.listModPacks.send(control).then(result => {
|
||||
setModPackList(result.list.map(pack => new libData.ModPack(pack)));
|
||||
setModPackList(result.list.map(pack => libData.ModPack.fromJSON(pack)));
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to list mod packs:\n${err}`);
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ export function useModList() {
|
||||
|
||||
function updateModList() {
|
||||
libLink.messages.listMods.send(control).then(result => {
|
||||
setModList(result.list.map(mod => new libData.ModInfo(mod)));
|
||||
setModList(result.list.map(mod => libData.ModInfo.fromJSON(mod)));
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to list mods:\n${err}`);
|
||||
});
|
||||
|
||||
@@ -312,7 +312,7 @@ export class Control extends libLink.Link {
|
||||
}
|
||||
|
||||
async modPackUpdateEventHandler(message) {
|
||||
let modPack = new libData.ModPack(message.data.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
|
||||
let handlers = [].concat(
|
||||
this.modPackUpdateHandlers.get(null) || [],
|
||||
this.modPackUpdateHandlers.get(modPack.id) || [],
|
||||
@@ -370,7 +370,7 @@ export class Control extends libLink.Link {
|
||||
}
|
||||
|
||||
async modUpdateEventHandler(message) {
|
||||
let mod = new libData.ModInfo(message.data.mod);
|
||||
let mod = libData.ModInfo.fromJSON(message.data.mod);
|
||||
let handlers = [].concat(
|
||||
this.modUpdateHandlers.get(null) || [],
|
||||
this.modUpdateHandlers.get(mod.name) || []
|
||||
|
||||
Reference in new issue
Block a user