more anys

This commit is contained in:
Olivier Gagnon 2022-07-19 23:26:21 -04:00
parent 30291f7899
commit 71d8e35bb5
5 changed files with 30 additions and 21 deletions

@ -31,7 +31,7 @@ declare global {
};
electronBridge: {
send: (channel: string, data?: unknown) => void;
receive: (channel: string, func: (...args: any[]) => void) => void;
receive: (channel: string, func: (...args: unknown[]) => void) => void;
};
}
interface Document {
@ -185,11 +185,14 @@ function initElectronBridge(): void {
const data = window.appSaveFns.getSaveData();
bridge.send("get-save-data-response", data);
});
bridge.receive("get-save-info-request", async (save: string) => {
bridge.receive("get-save-info-request", async (save: unknown) => {
if (typeof save !== "string") throw new Error("Error while trying to get save info");
const data = await window.appSaveFns.getSaveInfo(save);
bridge.send("get-save-info-response", data);
});
bridge.receive("push-save-request", ({ save, automatic = false }: { save: string; automatic: boolean }) => {
bridge.receive("push-save-request", (params: unknown) => {
if (typeof params !== "object") throw new Error("Error trying to push save request");
const { save, automatic = false } = params as { save: string; automatic: boolean };
window.appSaveFns.pushSaveData(save, automatic);
});
bridge.receive("trigger-save", () => {

@ -1,17 +1,20 @@
import { toNative } from "./toNative";
import libarg from "arg";
import { ScriptArg } from "../Netscript/ScriptArg";
export function Flags(vargs: string[]): any {
return () =>
(data: any): any => {
data = toNative(data);
// We always want the help flag.
type FlagType = StringConstructor | NumberConstructor | BooleanConstructor | StringConstructor[];
type FlagsRet = { [key: string]: ScriptArg };
export function Flags(vargs: string[]): () => (data: unknown) => FlagsRet {
return (/* ctx: NetscriptContext */) =>
(schema: unknown): FlagsRet => {
schema = toNative(schema);
if (!Array.isArray(schema)) throw new Error("flags schema passed in is invalid.");
const args: {
[key: string]: any;
[key: string]: FlagType;
} = {};
for (const d of data) {
let t: any = String;
for (const d of schema) {
let t: FlagType = String;
if (typeof d[1] === "number") {
t = Number;
} else if (typeof d[1] === "boolean") {
@ -22,8 +25,8 @@ export function Flags(vargs: string[]): any {
const numDashes = d[0].length > 1 ? 2 : 1;
args["-".repeat(numDashes) + d[0]] = t;
}
const ret = libarg(args, { argv: vargs });
for (const d of data) {
const ret: FlagsRet = libarg(args, { argv: vargs });
for (const d of schema) {
if (!ret.hasOwnProperty("--" + d[0]) || !ret.hasOwnProperty("-" + d[0])) ret[d[0]] = d[1];
}
for (const key of Object.keys(ret)) {

@ -49,7 +49,6 @@ export interface SaveData {
export interface ImportData {
base64: string;
parsed: any;
playerData?: ImportPlayerData;
}
@ -208,7 +207,6 @@ class BitburnerSaveObject {
const data: ImportData = {
base64: base64Save,
parsed: parsedSave,
};
const importedPlayer = PlayerObject.fromJSON(JSON.parse(parsedSave.data.PlayerSave));
@ -260,14 +258,19 @@ function evaluateVersionCompatibility(ver: string | number): void {
}
// The "companyName" property of all Companies is renamed to "name"
interface Company0_41_2 {
name: string | number;
companyName: string;
companyPositions: Record<number, boolean>;
}
for (const companyName of Object.keys(Companies)) {
const company: any = Companies[companyName];
const company = Companies[companyName] as unknown as Company0_41_2;
if (company.name == 0 && company.companyName != null) {
company.name = company.companyName;
}
if (company.companyPositions instanceof Array) {
const pos: any = {};
const pos: Record<number, boolean> = {};
for (let i = 0; i < company.companyPositions.length; ++i) {
pos[company.companyPositions[i]] = true;

@ -309,9 +309,9 @@ export async function determineAllPossibilitiesForTabCompletion(
return "--" + f[0];
});
try {
return flagFunc(schema);
return flagFunc()(schema);
} catch (err) {
return undefined;
return {};
}
},
};

@ -3,7 +3,7 @@ export type ObjectValidator<T> = {
};
interface ParameterValidatorObject<Type, Key extends keyof Type> {
default?: any;
default?: unknown;
min?: number;
max?: number;
func?: (obj: Type, validator: ObjectValidator<Type>, key: Key) => void;
@ -26,7 +26,7 @@ export function validateObject<Type extends Record<string, unknown>, Key extends
paramValidator.func(obj, validator, key);
} else {
if (typeof obj[key] !== typeof paramValidator.default) {
obj[key] = paramValidator.default;
obj[key] = paramValidator.default as Type[Key];
}
if (typeof obj[key] === "number" && paramValidator.min !== undefined) {
if (obj[key] < paramValidator.min) obj[key] = paramValidator.min as Type[Key];