From dcc0b8d52ec95922bbf3ab280d75860edba2d264 Mon Sep 17 00:00:00 2001 From: PP Date: Wed, 22 Mar 2023 10:19:39 +0800 Subject: [PATCH] support SafeArray and SafeRecord to optimize type checking (#14595) * support SafeArray and SafeRecord to optimize type checking --- @types/globals.d.ts | 11 +++++++++++ cocos/dragon-bones/ArmatureCache.ts | 12 ++++++------ cocos/dragon-bones/ArmatureDisplay.ts | 8 ++++---- cocos/spine/skeleton-cache.ts | 11 +++++------ cocos/spine/skeleton.ts | 8 ++++---- cocos/tiledmap/assembler/simple.ts | 6 +++--- cocos/tiledmap/tiled-layer.ts | 8 ++++---- 7 files changed, 37 insertions(+), 27 deletions(-) diff --git a/@types/globals.d.ts b/@types/globals.d.ts index 113f6ac366..8512e17b5f 100644 --- a/@types/globals.d.ts +++ b/@types/globals.d.ts @@ -115,3 +115,14 @@ declare const GameGlobal: any; * only implemented on Editor. */ declare const Editor: any; + +/** + * To provide a safe Record type, if you want to enable checking index access on Object, please use SafeRecord instead of Record. + * we provide this type because we don't want to enable `noUncheckedIndexedAccess` in tsconfig.json for it's noisy for a lot of code. + */ +type SafeRecord = Record; +/** + * To provide a safe Array type, if you want to enable checking index access on Array, please use SafeArray instead of Array. + * we provide this type because we don't want to enable `noUncheckedIndexedAccess` in tsconfig.json for it's noisy for a lot of code. + */ +type SafeArray = Array; diff --git a/cocos/dragon-bones/ArmatureCache.ts b/cocos/dragon-bones/ArmatureCache.ts index 90720fd28e..1924015233 100644 --- a/cocos/dragon-bones/ArmatureCache.ts +++ b/cocos/dragon-bones/ArmatureCache.ts @@ -109,7 +109,7 @@ export class AnimationCache { _inited = false; _invalid = true; _enableCacheAttachedInfo = false; - frames: ArmatureFrame[] = []; + frames: SafeArray = []; totalTime = 0; isCompleted = false; _frameIdx = -1; @@ -142,7 +142,7 @@ export class AnimationCache { clear () { this._inited = false; for (let i = 0, n = this.frames.length; i < n; i++) { - const frame = this.frames[i]; + const frame = this.frames[i]!; frame.segments.length = 0; } this.invalidAllFrame(); @@ -286,11 +286,11 @@ export class AnimationCache { segments: [], colors: [], boneInfos: [], - vertices: null, - uintVert: null, - indices: null, + vertices: new Float32Array(), + uintVert: new Uint32Array(), + indices: new Uint16Array(), }; - const frame = this.frames[index]; + const frame = this.frames[index]!; const segments = this._tempSegments = frame.segments; const colors = this._tempColors = frame.colors; diff --git a/cocos/dragon-bones/ArmatureDisplay.ts b/cocos/dragon-bones/ArmatureDisplay.ts index 53f30bb50c..a6d123414e 100644 --- a/cocos/dragon-bones/ArmatureDisplay.ts +++ b/cocos/dragon-bones/ArmatureDisplay.ts @@ -898,7 +898,7 @@ export class ArmatureDisplay extends UIRenderer { if (!this._playing) { if (frameCache.isInvalid()) { frameCache.updateToFrame(); - this._curFrame = frames[frames.length - 1]; + this._curFrame = frames[frames.length - 1]!; // Update render data size if needed if (this.renderData && (this.renderData.vertexCount < frameCache.maxVertexCount @@ -944,7 +944,7 @@ export class ArmatureDisplay extends UIRenderer { this._playCount++; if ((this.playTimes > 0 && this._playCount >= this.playTimes)) { // set frame to end frame. - this._curFrame = frames[frames.length - 1]; + this._curFrame = frames[frames.length - 1]!; this._accTime = 0; this._playing = false; this._playCount = 0; @@ -957,7 +957,7 @@ export class ArmatureDisplay extends UIRenderer { this._emitCacheCompleteEvent(); } - this._curFrame = frames[frameIdx]; + this._curFrame = frames[frameIdx]!; this.attachUtil._syncAttachedNode(); } /** @@ -1275,7 +1275,7 @@ export class ArmatureDisplay extends UIRenderer { } this._frameCache.updateToFrame(0); this._playing = true; - this._curFrame = this._frameCache.frames[0]; + this._curFrame = this._frameCache.frames[0]!; } } else if (this._armature) { return this._armature.animation.play(animName, this.playTimes); diff --git a/cocos/spine/skeleton-cache.ts b/cocos/spine/skeleton-cache.ts index a790a74e0e..d4fcaae500 100644 --- a/cocos/spine/skeleton-cache.ts +++ b/cocos/spine/skeleton-cache.ts @@ -112,7 +112,7 @@ export interface AnimationFrame { // Cache all frames in an animation export class AnimationCache { - public frames: AnimationFrame[] = []; + public frames: SafeArray = []; public totalTime = 0; public isCompleted = false; public maxVertexCount = 0; @@ -159,7 +159,7 @@ export class AnimationCache { public clear () { this._inited = false; for (let i = 0, n = this.frames.length; i < n; i++) { - const frame = this.frames[i]; + const frame = this.frames[i]!; frame.segments.length = 0; } this.invalidAllFrame(); @@ -366,11 +366,10 @@ export class AnimationCache { segments: [], colors: [], boneInfos: [], - vertices: null, - uintVert: null, - indices: null, + vertices: new Float32Array(), + indices: new Uint16Array(), }; - const frame = this.frames[index]; + const frame = this.frames[index]!; const segments = this._tempSegments = frame.segments; const colors = this._tempColors = frame.colors; diff --git a/cocos/spine/skeleton.ts b/cocos/spine/skeleton.ts index 55430e348e..d5af79e4c5 100644 --- a/cocos/spine/skeleton.ts +++ b/cocos/spine/skeleton.ts @@ -874,7 +874,7 @@ export class Skeleton extends UIRenderer { if (frameCache && frameCache.isInvalid()) { frameCache.updateToFrame(); const frames = frameCache.frames; - this._curFrame = frames[frames.length - 1]; + this._curFrame = frames[frames.length - 1]!; } return; } @@ -1141,7 +1141,7 @@ export class Skeleton extends UIRenderer { this._frameCache.enableCacheAttachedInfo(); } this._frameCache.updateToFrame(0); - this._curFrame = this._frameCache.frames[0]; + this._curFrame = this._frameCache.frames[0]!; } } else if (this._skeleton) { const animation = this._skeleton.data.findAnimation(name); @@ -1609,7 +1609,7 @@ export class Skeleton extends UIRenderer { this._playCount++; if (this._playTimes > 0 && this._playCount >= this._playTimes) { // set frame to end frame. - this._curFrame = frames[frames.length - 1]; + this._curFrame = frames[frames.length - 1]!; this._accTime = 0; this._playCount = 0; this._isAniComplete = true; @@ -1620,7 +1620,7 @@ export class Skeleton extends UIRenderer { frameIdx = 0; this._emitCacheCompleteEvent(); } - this._curFrame = frames[frameIdx]; + this._curFrame = frames[frameIdx]!; } protected _updateRealtime (dt: number) { diff --git a/cocos/tiledmap/assembler/simple.ts b/cocos/tiledmap/assembler/simple.ts index 8ba6a9309f..a6b5f1a822 100644 --- a/cocos/tiledmap/assembler/simple.ts +++ b/cocos/tiledmap/assembler/simple.ts @@ -345,7 +345,7 @@ function traverseGrids (leftDown: { col: number, row: number }, rightTop: { col: const vertStep3 = vertStep * 3; const vertices = comp.vertices; - let rowData: { [key: number]: { left: number, bottom: number; index: number }, maxCol: number, minCol: number }; + let rowData: { maxCol: number, minCol: number } & SafeRecord; let col: number; let cols: number; let row: number; @@ -387,7 +387,7 @@ function traverseGrids (leftDown: { col: number, row: number }, rightTop: { col: const vertexBuf = _tempBuffers; // traverse row for (; (rows - row) * rowMoveDir >= 0; row += rowMoveDir) { - rowData = vertices[row]; + rowData = vertices[row]!; colNodesCount = comp.getNodesCountByRow(row); isCheckColRange = rowData && colNodesCount === 0; @@ -402,7 +402,7 @@ function traverseGrids (leftDown: { col: number, row: number }, rightTop: { col: // traverse col for (; (cols - col) * colMoveDir >= 0; col += colMoveDir) { - colData = rowData && rowData[col]; + colData = rowData && rowData[col]!; if (colNodesCount > 0) { packRenderData(); diff --git a/cocos/tiledmap/tiled-layer.ts b/cocos/tiledmap/tiled-layer.ts index 3a1b7feb05..8c7bf35fb2 100644 --- a/cocos/tiledmap/tiled-layer.ts +++ b/cocos/tiledmap/tiled-layer.ts @@ -84,7 +84,7 @@ interface TiledSubNodeData { @ccclass('cc.TiledLayer') export class TiledLayer extends UIRenderer { // [row][col] = {count: 0, nodesList: []}; - protected _userNodeGrid: { [key: number]: { count: number;[key: number]: { count: number, list: (TiledUserNodeData | null)[] } } } = {}; + protected _userNodeGrid: SafeRecord> = {}; protected _userNodeMap: { [key: string]: TiledUserNodeData } = {};// [id] = node; protected _userNodeDirty = false; @@ -121,7 +121,7 @@ export class TiledLayer extends UIRenderer { public tiles: MixedGID[] = []; // vertex array - public vertices: { minCol: number, maxCol: number, [key: number]: { left: number, bottom: number, index: number } }[] = []; + public vertices: SafeArray<{ minCol: number, maxCol: number } & SafeRecord> = []; // vertices dirty protected _verticesDirty = true; @@ -353,7 +353,7 @@ export class TiledLayer extends UIRenderer { const rowData = this._userNodeGrid[row]; const colData = rowData && rowData[col]; if (colData) { - rowData.count--; + rowData!.count--; colData.count--; colData.list[index] = null; if (colData.count <= 0) { @@ -993,7 +993,7 @@ export class TiledLayer extends UIRenderer { } const rowData = vertices[cullingRow] = vertices[cullingRow] || { minCol: 0, maxCol: 0 }; - const colData = rowData[cullingCol] = rowData[cullingCol] || {}; + const colData = rowData[cullingCol] = rowData[cullingCol] || { left: 0, bottom: 0, index: 0 }; // record each row range, it will faster when culling grid if (rowData.minCol > cullingCol) {