mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-18 07:18:38 +02:00
CODEBASE: Validate theme, editor theme, and styles (#1789)
This commit is contained in:
@@ -5,40 +5,10 @@ import { defaultTheme } from "../Themes/Themes";
|
||||
import { defaultStyles } from "../Themes/Styles";
|
||||
import { CONSTANTS } from "../Constants";
|
||||
import { commitHash } from "../utils/helpers/commitHash";
|
||||
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
||||
import { InternalAPI } from "../Netscript/APIWrapper";
|
||||
import { Terminal } from "../../src/Terminal";
|
||||
import { helpers } from "../Netscript/NetscriptHelpers";
|
||||
import { errorMessage } from "../Netscript/ErrorMessages";
|
||||
|
||||
/** Will probably remove the below function in favor of a different approach to object type assertion.
|
||||
* This method cannot be used to handle optional properties. */
|
||||
export function assertObjectType<T extends object>(
|
||||
ctx: NetscriptContext,
|
||||
name: string,
|
||||
obj: unknown,
|
||||
desiredObject: T,
|
||||
): asserts obj is T {
|
||||
if (typeof obj !== "object" || obj === null) {
|
||||
throw errorMessage(
|
||||
ctx,
|
||||
`Type ${obj === null ? "null" : typeof obj} provided for ${name}. Must be an object.`,
|
||||
"TYPE",
|
||||
);
|
||||
}
|
||||
for (const [key, val] of Object.entries(desiredObject)) {
|
||||
if (!Object.hasOwn(obj, key)) {
|
||||
throw errorMessage(ctx, `Object provided for argument ${name} is missing required property ${key}.`, "TYPE");
|
||||
}
|
||||
const objVal = (obj as Record<string, unknown>)[key];
|
||||
if (typeof val !== typeof objVal) {
|
||||
throw errorMessage(
|
||||
ctx,
|
||||
`Incorrect type ${typeof objVal} provided for property ${key} on ${name} argument. Should be type ${typeof val}.`,
|
||||
"TYPE",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
import { assertAndSanitizeMainTheme, assertAndSanitizeStyles } from "../JsonSchema/JSONSchemaAssertion";
|
||||
|
||||
export function NetscriptUserInterface(): InternalAPI<IUserInterface> {
|
||||
return {
|
||||
@@ -54,52 +24,37 @@ export function NetscriptUserInterface(): InternalAPI<IUserInterface> {
|
||||
},
|
||||
|
||||
setTheme: (ctx) => (newTheme) => {
|
||||
const themeValidator: Record<string, string | undefined> = {};
|
||||
assertObjectType(ctx, "newTheme", newTheme, themeValidator);
|
||||
const hex = /^(#)((?:[A-Fa-f0-9]{2}){3,4}|(?:[A-Fa-f0-9]{3}))$/;
|
||||
const currentTheme = { ...Settings.theme };
|
||||
const errors: string[] = [];
|
||||
for (const key of Object.keys(newTheme)) {
|
||||
if (!currentTheme[key]) {
|
||||
// Invalid key
|
||||
errors.push(`Invalid key "${key}"`);
|
||||
} else if (!hex.test(newTheme[key] ?? "")) {
|
||||
errors.push(`Invalid color "${key}": ${newTheme[key]}`);
|
||||
} else {
|
||||
currentTheme[key] = newTheme[key];
|
||||
}
|
||||
}
|
||||
|
||||
if (errors.length === 0) {
|
||||
Object.assign(Settings.theme, currentTheme);
|
||||
ThemeEvents.emit();
|
||||
helpers.log(ctx, () => `Successfully set theme`);
|
||||
} else {
|
||||
helpers.log(ctx, () => `Failed to set theme. Errors: ${errors.join(", ")}`);
|
||||
let newData: unknown;
|
||||
try {
|
||||
/**
|
||||
* assertAndSanitizeMainTheme may mutate its parameter, so we have to clone the user-provided data here.
|
||||
*/
|
||||
newData = structuredClone(newTheme);
|
||||
assertAndSanitizeMainTheme(newData);
|
||||
} catch (error) {
|
||||
helpers.log(ctx, () => `Failed to set theme. Errors: ${error}`);
|
||||
return;
|
||||
}
|
||||
Object.assign(Settings.theme, newData);
|
||||
ThemeEvents.emit();
|
||||
helpers.log(ctx, () => `Successfully set theme`);
|
||||
},
|
||||
|
||||
setStyles: (ctx) => (newStyles) => {
|
||||
const styleValidator: Record<string, string | number | undefined> = {};
|
||||
assertObjectType(ctx, "newStyles", newStyles, styleValidator);
|
||||
const currentStyles: Record<string, unknown> = { ...Settings.styles };
|
||||
const errors: string[] = [];
|
||||
for (const key of Object.keys(newStyles)) {
|
||||
if (!currentStyles[key]) {
|
||||
// Invalid key
|
||||
errors.push(`Invalid key "${key}"`);
|
||||
} else {
|
||||
currentStyles[key] = newStyles[key];
|
||||
}
|
||||
}
|
||||
|
||||
if (errors.length === 0) {
|
||||
Object.assign(Settings.styles, currentStyles);
|
||||
ThemeEvents.emit();
|
||||
helpers.log(ctx, () => `Successfully set styles`);
|
||||
} else {
|
||||
helpers.log(ctx, () => `Failed to set styles. Errors: ${errors.join(", ")}`);
|
||||
let newData: unknown;
|
||||
try {
|
||||
/**
|
||||
* assertAndSanitizeStyles may mutate its parameter, so we have to clone the user-provided data here.
|
||||
*/
|
||||
newData = structuredClone(newStyles);
|
||||
assertAndSanitizeStyles(newData);
|
||||
} catch (error) {
|
||||
helpers.log(ctx, () => `Failed to set styles. Errors: ${error}`);
|
||||
return;
|
||||
}
|
||||
Object.assign(Settings.styles, newData);
|
||||
ThemeEvents.emit();
|
||||
helpers.log(ctx, () => `Successfully set styles`);
|
||||
},
|
||||
|
||||
resetTheme: (ctx) => () => {
|
||||
|
||||
Reference in New Issue
Block a user