From 49144100cd169c85b1a3c89b131b15649ab4ad1c Mon Sep 17 00:00:00 2001 From: Tim Kurvers Date: Thu, 11 Apr 2024 22:08:02 +0200 Subject: [PATCH] feat: add status logging for (most) loadXML methods --- src/ui/UIContext.ts | 6 +-- src/ui/components/abstract/LayoutFrame.ts | 14 ++--- src/ui/components/abstract/ScriptRegion.ts | 6 +-- src/ui/components/simple/Button.ts | 13 ++--- src/ui/components/simple/Frame.ts | 19 +++---- src/ui/components/simple/ScrollFrame.ts | 11 ++-- src/ui/components/simple/Texture.ts | 60 ++++++++++++---------- 7 files changed, 68 insertions(+), 61 deletions(-) diff --git a/src/ui/UIContext.ts b/src/ui/UIContext.ts index 80ead2d..065b8eb 100644 --- a/src/ui/UIContext.ts +++ b/src/ui/UIContext.ts @@ -82,7 +82,7 @@ class UIContext { // TODO: Handle unique factories frame.preLoadXML(node); - frame.loadXML(node); + frame.loadXML(node, status); frame.postLoadXML(node, status); return frame; @@ -96,10 +96,10 @@ class UIContext { return fontString; } - createTexture(node: XMLNode, frame: Frame) { + createTexture(node: XMLNode, frame: Frame, status = new Status()) { const texture = new Texture(frame, DrawLayerType.ARTWORK, true); texture.preLoadXML(node); - texture.loadXML(node); + texture.loadXML(node, status); texture.postLoadXML(node); return texture; } diff --git a/src/ui/components/abstract/LayoutFrame.ts b/src/ui/components/abstract/LayoutFrame.ts index 958927b..8038222 100644 --- a/src/ui/components/abstract/LayoutFrame.ts +++ b/src/ui/components/abstract/LayoutFrame.ts @@ -14,6 +14,7 @@ import { LinkedListNode, NDCtoDDCHeight, NDCtoDDCWidth, + Status, extractDimensionsFrom, stringToBoolean, } from '../../../utils'; @@ -366,7 +367,7 @@ class LayoutFrame { return rect; } - loadXML(node: XMLNode) { + loadXML(node: XMLNode, status: Status) { const size = node.getChildByName('Size'); if (size) { const { x, y } = extractDimensionsFrom(size); @@ -387,7 +388,7 @@ class LayoutFrame { const anchors = node.getChildByName('Anchors'); if (anchors) { if (setAllPoints) { - // TODO: Error handling + status.warning('setAllPoints set to true in frame with anchors (ignored)'); } for (const child of anchors.children) { @@ -398,14 +399,14 @@ class LayoutFrame { const pointType = stringToFramePointType(pointValue); let relativePointType = pointType; if (pointType === undefined) { - // TODO: Error handling + status.warning(`invalid anchor point in frame: ${pointValue}`); continue; } if (relativePointValue) { relativePointType = stringToFramePointType(relativePointValue); if (relativePointType === undefined) { - // TODO: Error handling + status.warning(`invalid relative anchor point in frame: ${relativePointValue}`); continue; } } @@ -415,13 +416,12 @@ class LayoutFrame { const fqname = this.fullyQualifyName(relativeValue)!; relative = ScriptRegion.getObjectByName(fqname); if (!relative) { - // TODO: Error handling - console.warn(`could not find relative frame: ${fqname}`); + status.warning(`could not find relative frame: ${relativeValue}`); continue; } if (relative === this) { - // TODO: Error handling + status.warning(`frame anchored to itself: ${relativeValue}`); continue; } } diff --git a/src/ui/components/abstract/ScriptRegion.ts b/src/ui/components/abstract/ScriptRegion.ts index d166538..58fa87a 100644 --- a/src/ui/components/abstract/ScriptRegion.ts +++ b/src/ui/components/abstract/ScriptRegion.ts @@ -1,7 +1,7 @@ import Frame from '../simple/Frame'; import UIRoot from '../UIRoot'; import XMLNode from '../../XMLNode'; -import { multipleClasses } from '../../../utils'; +import { Status, multipleClasses } from '../../../utils'; import LayoutFrame from './LayoutFrame'; import ScriptObject from './ScriptObject'; @@ -40,8 +40,8 @@ class ScriptRegion extends multipleClasses(ScriptObject, LayoutFrame) { return this.parent; } - loadXML(node: XMLNode) { - LayoutFrame.prototype.loadXML.call(this, node); + loadXML(node: XMLNode, status: Status) { + super.loadXML(node, status); const parentKey = node.attributes.get('parentKey'); if (parentKey) { diff --git a/src/ui/components/simple/Button.ts b/src/ui/components/simple/Button.ts index 67791a4..2d26a80 100644 --- a/src/ui/components/simple/Button.ts +++ b/src/ui/components/simple/Button.ts @@ -3,6 +3,7 @@ import Script from '../../scripting/Script'; import UIContext from '../../UIContext'; import XMLNode from '../../XMLNode'; import { BlendMode } from '../../../gfx/types'; +import { Status } from '../../../utils'; import ButtonState from './ButtonState'; import FontString from './FontString'; @@ -52,8 +53,8 @@ class Button extends Frame { this.state = ButtonState.DISABLED; } - loadXML(node: XMLNode) { - super.loadXML(node); + loadXML(node: XMLNode, status: Status) { + super.loadXML(node, status); const ui = UIContext.instance; @@ -61,22 +62,22 @@ class Button extends Frame { const iname = child.name.toLowerCase(); switch (iname) { case 'normaltexture': { - const texture = ui.createTexture(child, this); + const texture = ui.createTexture(child, this, status); this.setStateTexture(ButtonState.NORMAL, texture); break; } case 'pushedtexture': { - const texture = ui.createTexture(child, this); + const texture = ui.createTexture(child, this, status); this.setStateTexture(ButtonState.PUSHED, texture); break; } case 'disabledtexture': { - const texture = ui.createTexture(child, this); + const texture = ui.createTexture(child, this, status); this.setStateTexture(ButtonState.DISABLED, texture); break; } case 'highlighttexture': { - const texture = ui.createTexture(child, this); + const texture = ui.createTexture(child, this, status); // TODO: Blend mode this.setHighlight(texture, null); break; diff --git a/src/ui/components/simple/Frame.ts b/src/ui/components/simple/Frame.ts index 7838815..bd95463 100644 --- a/src/ui/components/simple/Frame.ts +++ b/src/ui/components/simple/Frame.ts @@ -254,7 +254,8 @@ class Frame extends ScriptRegion { } } - loadXML(node: XMLNode) { + loadXML(node: XMLNode, status: Status) { + // TODO: Group attribute extraction together with usage const dontSavePosition = node.attributes.get('dontSavePosition'); const frameLevel = node.attributes.get('frameLevel'); const frameStrata = node.attributes.get('frameStrata'); @@ -266,22 +267,22 @@ class Frame extends ScriptRegion { if (inherits) { const templates = UIContext.instance.templates.filterByList(inherits); - for (const { template } of templates) { + for (const { name, template } of templates) { if (template) { if (template.locked) { - // TODO: Error handling + status.warning(`recursively inherited node: ${name}`); } else { template.lock(); - this.loadXML(template.node); + this.loadXML(template.node, status); template.release(); } } else { - // TODO: Error handling + status.warning(`could not find inherited node: ${name}`); } } } - super.loadXML(node); + super.loadXML(node, status); if (hidden) { if (stringToBoolean(hidden)) { @@ -348,7 +349,7 @@ class Frame extends ScriptRegion { this.setBackdrop(backdrop); } break; case 'layers': - this.loadXMLLayers(child); + this.loadXMLLayers(child, status); break; case 'attributes': // TODO: Load attributes @@ -360,7 +361,7 @@ class Frame extends ScriptRegion { } } - loadXMLLayers(node: XMLNode) { + loadXMLLayers(node: XMLNode, status: Status) { const ui = UIContext.instance; for (const layer of node.children) { @@ -377,7 +378,7 @@ class Frame extends ScriptRegion { const iname = layerChild.name.toLowerCase(); switch (iname) { case 'texture': { - const texture = ui.createTexture(layerChild, this); + const texture = ui.createTexture(layerChild, this, status); texture.setFrame(this, drawLayerType, texture.shown); } break; case 'fontstring': { diff --git a/src/ui/components/simple/ScrollFrame.ts b/src/ui/components/simple/ScrollFrame.ts index 3d7eb89..dec90bb 100644 --- a/src/ui/components/simple/ScrollFrame.ts +++ b/src/ui/components/simple/ScrollFrame.ts @@ -1,6 +1,7 @@ import Script from '../../scripting/Script'; import UIContext from '../../UIContext'; import XMLNode from '../../XMLNode'; +import { Status } from '../../../utils'; import Frame from './Frame'; @@ -28,20 +29,20 @@ class ScrollFrame extends Frame { ); } - loadXML(node: XMLNode) { - super.loadXML(node); + loadXML(node: XMLNode, status: Status) { + super.loadXML(node, status); const scrollChild = node.getChildByName('ScrollChild'); if (scrollChild) { const child = scrollChild.firstChild; if (child) { - const frame = UIContext.instance.createFrame(child, this); + const frame = UIContext.instance.createFrame(child, this, status); if (frame) { this.scrollChild = frame; } } else { - // TODO: Error handling - console.warn('scroll frame created without child'); + const name = this.name || ''; + status.warning(`frame ${name}: scroll frame created without scroll child`); } } } diff --git a/src/ui/components/simple/Texture.ts b/src/ui/components/simple/Texture.ts index ede2526..9359763 100644 --- a/src/ui/components/simple/Texture.ts +++ b/src/ui/components/simple/Texture.ts @@ -14,6 +14,7 @@ import { stringToBlendMode } from '../../utils'; import { NDCtoDDCHeight, NDCtoDDCWidth, + Status, maxAspectCompensation, stringToBoolean, stringToFloat, @@ -111,7 +112,8 @@ class Texture extends Region { super.height = height; } - loadXML(node: XMLNode) { + loadXML(node: XMLNode, status: Status) { + // TODO: Group attribute extraction together with usage const alphaMode = node.attributes.get('alphaMode'); const file = node.attributes.get('file'); const hidden = node.attributes.get('hidden'); @@ -123,18 +125,18 @@ class Texture extends Region { const template = UIContext.instance.templates.get(inherits); if (template) { if (template.locked) { - // TODO: Error handling + status.warning(`recursively inherited node: ${inherits}`); } else { template.lock(); - this.loadXML(template.node); + this.loadXML(template.node, status); template.release(); } } else { - // TODO: Error handling + status.warning(`could not find inherited node: ${inherits}`); } } - super.loadXML(node); + super.loadXML(node, status); if (hidden) { if (stringToBoolean(hidden)) { @@ -170,31 +172,31 @@ class Texture extends Region { bottom: 1.0, }; - // TODO: Handle name in error handling - // const name = this.name || ''; + const name = this.name || ''; - // TODO: Handle rectangle if (child.getChildByName('Rect')) { - continue; - } - - for (const side of Object.keys(rect) as (keyof typeof rect)[]) { - const attr = child.attributes.get(side); - if (attr) { - if ( - ((side === 'left' || side === 'right') && this.tileHorizontally) - || ((side === 'top' || side === 'bottom') && this.tileVertically) - ) { - // TODO: error handling - valid = false; - } - - const value = stringToFloat(attr); - if (value < -10000 || value > 10000) { - // TODO: Error handling - valid = false; + // TODO: Handle rectangle + } else { + for (const side of Object.keys(rect) as (keyof typeof rect)[]) { + const attr = child.attributes.get(side); + if (attr) { + if ( + ((side === 'left' || side === 'right') && this.tileHorizontally) + || ((side === 'top' || side === 'bottom') && this.tileVertically) + ) { + status.error( + `texture ${name}: invalid TexCoords value (horizTile: ${this.tileHorizontally}; vertTile: ${this.tileVertically}` + ); + valid = false; + } + + const value = stringToFloat(attr); + if (value < -10000 || value > 10000) { + status.error(`texture ${name}: invalid TexCoords value (out of range)`); + valid = false; + } + rect[side] = value; } - rect[side] = value; } } @@ -234,7 +236,8 @@ class Texture extends Region { if (success) { // TODO: Set colors } else { - // TODO: Error handling + const name = this.name || ''; + status.warning(`texture ${name}: unable to load texture file ${file}`); } } @@ -245,6 +248,7 @@ class Texture extends Region { } } + // TODO: Alpha // TODO: Non-blocking }