From 7ab97b64f32f52f32c035110341cabe8254a89c9 Mon Sep 17 00:00:00 2001 From: Luke Kelly Date: Mon, 15 Jan 2024 12:03:12 +0100 Subject: [PATCH 1/4] * Refactored built-in initialization to take into account the different variations offered by Total Initialization. * Now uses pcall for initialization calls instead of unsafe calling. * Removed fromObject and overloads for fromHandle. --- .eslintrc.json | 7 ++- globals/index.ts | 11 ++-- handles/camera.ts | 24 +-------- handles/destructable.ts | 50 +++++------------ handles/dialog.ts | 42 +++------------ handles/effect.ts | 71 ++++++------------------ handles/fogmodifier.ts | 17 +----- handles/force.ts | 14 +---- handles/frame.ts | 35 ++---------- handles/gamecache.ts | 18 +------ handles/group.ts | 17 +----- handles/handle.ts | 35 +++++++----- handles/image.ts | 14 +---- handles/item.ts | 18 +------ handles/leaderboard.ts | 19 +------ handles/multiboard.ts | 35 ++---------- handles/player.ts | 19 ++----- handles/point.ts | 14 +---- handles/quest.ts | 28 ++-------- handles/rect.ts | 14 +---- handles/region.ts | 14 +---- handles/sound.ts | 16 +----- handles/texttag.ts | 17 +----- handles/timer.ts | 14 +---- handles/timerdialog.ts | 19 +------ handles/trigger.ts | 17 ++---- handles/ubersplat.ts | 18 +------ handles/unit.ts | 25 ++------- handles/weathereffect.ts | 17 +----- handles/widget.ts | 4 -- hooks/index.ts | 113 +++++++++++++++++++++------------------ package.json | 2 +- system/file.ts | 5 -- system/gametime.ts | 4 +- system/host.ts | 6 +-- system/sync.ts | 2 +- 36 files changed, 182 insertions(+), 613 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index f020239..3e72d9e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,11 +23,16 @@ "@typescript-eslint" ], "rules": { - "prettier/prettier": "error", + "prettier/prettier": ["warn", { + "endOfLine": "auto" + }], + "default-param-last": 0, + "linebreak-style": 0, "camelcase": ["error", {"allow": ["((bj_)\\w+[A-Z]$)"]}], //exclude bj_CAPITAL_LETTER "eqeqeq": ["error", "always", {"null": "never"}], "@typescript-eslint/no-implicit-any-catch": "error", "@typescript-eslint/strict-boolean-expressions": ["warn"], + "@typescript-eslint/no-non-null-assertion": 0, "no-underscore-dangle": ["error", { "allowAfterThis": true, "allowAfterSuper": true }], "no-throw-literal": 0, //Maybe one day if wc3 gets a polyfill for the debug library "no-cond-assign": 0, diff --git a/globals/index.ts b/globals/index.ts index c60d845..11ebdc6 100644 --- a/globals/index.ts +++ b/globals/index.ts @@ -1,11 +1,6 @@ import { MapPlayer } from "../handles/player"; export * from "./order"; -export const Players: MapPlayer[] = []; - -for (let i = 0; i < bj_MAX_PLAYER_SLOTS; i++) { - const pl = MapPlayer.fromHandle(Player(i)); - if (pl) { - Players[i] = pl; - } -} +export const Players = Array.from({ length: bj_MAX_PLAYER_SLOTS }).map((_, i) => + MapPlayer.fromHandle(Player(i)) +); diff --git a/handles/camera.ts b/handles/camera.ts index f7e4a78..811d95b 100644 --- a/handles/camera.ts +++ b/handles/camera.ts @@ -4,11 +4,6 @@ import { Handle } from "./handle"; import { Point } from "./point"; export class Camera { - // eslint-disable-next-line no-useless-constructor - private constructor() { - // nothing - } - public static set visible(flag: boolean) { DisplayCineFilter(flag); } @@ -339,17 +334,8 @@ export class CameraSetup extends Handle { /** * Creates a new CameraSetup object. */ - public static create(): CameraSetup | undefined { - const handle = CreateCameraSetup(); - if (handle) { - const obj = this.getObject(handle) as CameraSetup; - - const values: Record = {}; - values.handle = handle; - - return Object.assign(obj, values); - } - return undefined; + public static create() { + return this.fromHandle(CreateCameraSetup()); } /** @@ -502,10 +488,4 @@ export class CameraSetup extends Handle { public setField(whichField: camerafield, value: number, duration: number) { CameraSetupSetField(this.handle, whichField, value, duration); } - - public static fromHandle( - handle: camerasetup | undefined - ): CameraSetup | undefined { - return handle ? this.getObject(handle) : undefined; - } } diff --git a/handles/destructable.ts b/handles/destructable.ts index bc1ab94..b901e97 100644 --- a/handles/destructable.ts +++ b/handles/destructable.ts @@ -54,15 +54,11 @@ export class Destructable extends Widget { objectId: number, x: number, y: number, - face?: number, - scale?: number, - variation?: number, + face = 0, + scale = 1, + variation = 0, skinId?: number - ): Destructable | undefined { - if (face === undefined) face = 0; - if (scale === undefined) scale = 1; - if (variation === undefined) variation = 0; - + ) { let handle: destructable | undefined; if (skinId !== undefined) { @@ -79,18 +75,10 @@ export class Destructable extends Widget { handle = CreateDestructable(objectId, x, y, face, scale, variation); } - if (handle) { - const obj = this.getObject(handle) as Destructable; - - const values: Record = {}; - values.handle = handle; - if (skinId !== undefined) { - values.skin = skinId; - } - - return Object.assign(obj, values); - } - return undefined; + return this.fromHandle( + handle, + skinId !== undefined ? { skin: skinId } : undefined + ); } /** @@ -134,18 +122,10 @@ export class Destructable extends Widget { handle = CreateDestructableZ(objectId, x, y, z, face, scale, variation); } - if (handle) { - const obj = this.getObject(handle) as Destructable; - - const values: Record = {}; - values.handle = handle; - if (skinId !== undefined) { - values.skin = skinId; - } - - return Object.assign(obj, values); - } - return undefined; + return this.fromHandle( + handle, + skinId !== undefined ? { skin: skinId } : undefined + ); } public set invulnerable(flag: boolean) { @@ -238,10 +218,4 @@ export class Destructable extends Widget { public static override fromEvent() { return this.fromHandle(GetTriggerDestructable()); } - - public static override fromHandle( - handle: destructable | undefined - ): Destructable | undefined { - return handle ? this.getObject(handle) : undefined; - } } diff --git a/handles/dialog.ts b/handles/dialog.ts index 1dba991..842ed4d 100644 --- a/handles/dialog.ts +++ b/handles/dialog.ts @@ -21,7 +21,7 @@ export class DialogButton extends Handle