mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-18 23:03:13 +09:00
Rework features into a Feature class with per field events
Review points from Cooldude: - Feature.register(name[, config]) is required again and returns a feature object. Each feature has its config, its fields, and its overrides, so one registry holds all the data. Only the overrides are stored. On load each feature points at its own stored table again. - The methods are feature:guard(lib), feature:update_config(enabled, values), and feature:is_enabled(). Feature.get(name) looks up a feature, and commands flag the feature object. - update_config raises on_config_updated once per changed value, with the feature name, path, old value, and new value. The gui refresh only reacts to the "enabled" path. - apply only sets values. on_apply is gone. Lookup sets built from lists use Feature.to_set, which is cached by the list itself. A replaced list gets a new set, so the sets are also correct after a load. Protection exposes getters for its protected names and types. Web: settings use small inputs, the same size as the config pages. Fields are shaped like clusterio config field definitions. A field with an inputComponent renders the component registered with the web interface, so the role setting now uses core's role input, and other plugin inputs can be used later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4bc1773c82
commit
628e4df514
26 files changed
+468
-430
No files matched your search
+27
-10
@@ -8,17 +8,24 @@
|
||||
|
||||
export type FeatureValue = boolean | number | string | string[] | null;
|
||||
|
||||
/**
|
||||
* A setting of a feature, shaped like a clusterio config field definition so
|
||||
* the web interface can render it with the same inputs, including the input
|
||||
* components registered by plugins.
|
||||
*/
|
||||
export type FeatureField = {
|
||||
name: string,
|
||||
title: string,
|
||||
description: string,
|
||||
/** Allows null, the lua side uses Feature.optional */
|
||||
optional?: boolean,
|
||||
/** Name of an input component registered with the web interface, such as "role" */
|
||||
inputComponent?: string,
|
||||
} & (
|
||||
| { type: "boolean", default: boolean }
|
||||
| { type: "number", default: number, min?: number, unit?: string }
|
||||
| { type: "string", default: string }
|
||||
| { type: "number", default: number | null, min?: number, unit?: string }
|
||||
| { type: "string", default: string | null, enum?: string[] }
|
||||
| { type: "string_list", default: string[] }
|
||||
/** Id of a clusterio role, null when no role is picked */
|
||||
| { type: "role", default: null }
|
||||
);
|
||||
|
||||
export type Feature = {
|
||||
@@ -44,8 +51,9 @@ function list(name: string, title: string, description: string, value: string[])
|
||||
return { name, title, description, type: "string_list", default: value };
|
||||
}
|
||||
|
||||
/** Id of a clusterio role, null when no role is picked */
|
||||
function role(name: string, title: string, description: string): FeatureField {
|
||||
return { name, title, description, type: "role", default: null };
|
||||
return { name, title, description, type: "number", default: null, optional: true, inputComponent: "role" };
|
||||
}
|
||||
|
||||
export const features: Feature[] = [
|
||||
@@ -229,17 +237,18 @@ export function validateFeatureValues(name: string, values: Record<string, Featu
|
||||
}
|
||||
|
||||
function checkFieldValue(field: FeatureField, value: FeatureValue, label: string) {
|
||||
if (value === null) {
|
||||
if (!field.optional) {
|
||||
throw new Error(`${label} can not be null`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (field.type) {
|
||||
case "string_list":
|
||||
if (!Array.isArray(value) || value.some(item => typeof item !== "string")) {
|
||||
throw new Error(`${label} must be a list of strings`);
|
||||
}
|
||||
return;
|
||||
case "role":
|
||||
if (value !== null && !Number.isInteger(value)) {
|
||||
throw new Error(`${label} must be a role id or null`);
|
||||
}
|
||||
return;
|
||||
case "number":
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) {
|
||||
throw new Error(`${label} must be a finite number`);
|
||||
@@ -248,6 +257,14 @@ function checkFieldValue(field: FeatureField, value: FeatureValue, label: string
|
||||
throw new Error(`${label} must be at least ${field.min}`);
|
||||
}
|
||||
return;
|
||||
case "string":
|
||||
if (typeof value !== "string") {
|
||||
throw new Error(`${label} must be a string`);
|
||||
}
|
||||
if (field.enum && !field.enum.includes(value)) {
|
||||
throw new Error(`${label} must be one of ${field.enum.join(", ")}`);
|
||||
}
|
||||
return;
|
||||
default:
|
||||
if (typeof value !== field.type) {
|
||||
throw new Error(`${label} must be a ${field.type}`);
|
||||
|
||||
Reference in new issue
Block a user