Skip to content

Commit

Permalink
warn or ignore certain missing xml elements (#3025)
Browse files Browse the repository at this point in the history
* warn or ignore certain missing xml elements

* fix codeclimate
  • Loading branch information
Geokureli authored Feb 6, 2024
1 parent 1c695e9 commit 3150c5d
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 67 deletions.
15 changes: 7 additions & 8 deletions flixel/graphics/frames/bmfont/BMFont.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package flixel.graphics.frames.bmfont;
import flixel.system.FlxAssets;
import haxe.io.Bytes;
import haxe.io.BytesInput;
import haxe.xml.Access;
import openfl.utils.Assets;

using StringTools;
Expand Down Expand Up @@ -34,16 +33,16 @@ class BMFont

public static function fromXml(xml:Xml)
{
final xmlAccess = new Access(xml);
final info = BMFontInfo.fromXml(xmlAccess.node.info);
final common = BMFontCommon.fromXml(xmlAccess.node.common);
final pages = BMFontPage.listFromXml(xmlAccess.node.pages);
final chars = BMFontChar.listFromXml(xmlAccess.node.chars);
final main = new BMFontXml(xml);
final info = BMFontInfo.fromXml(main.node.get("info"));
final common = BMFontCommon.fromXml(main.node.get("common"));
final pages = BMFontPage.listFromXml(main.node.get("pages"));
final chars = BMFontChar.listFromXml(main.node.get("chars"));
var kerning:Array<BMFontKerning> = null;

if (xmlAccess.hasNode.kernings)
if (main.hasNode("kernings"))
{
kerning = BMFontKerning.listFromXml(xmlAccess.node.kernings);
kerning = BMFontKerning.listFromXml(main.node.get("kernings"));
}

return new BMFont(info, common, pages, chars, kerning);
Expand Down
29 changes: 14 additions & 15 deletions flixel/graphics/frames/bmfont/BMFontChar.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flixel.graphics.frames.bmfont;

import haxe.io.BytesInput;
import haxe.xml.Access;
import UnicodeString;

using StringTools;
Expand Down Expand Up @@ -29,26 +28,26 @@ class BMFontChar
public var chnl:Int;
public var letter:Null<String> = null;

static inline function fromXml(charNode:Access):BMFontChar
static inline function fromXml(charNode:BMFontXml):BMFontChar
{
return {
id: Std.parseInt(charNode.att.id),
x: Std.parseInt(charNode.att.x),
y: Std.parseInt(charNode.att.y),
width: Std.parseInt(charNode.att.width),
height: Std.parseInt(charNode.att.height),
xoffset: (charNode.has.xoffset) ? Std.parseInt(charNode.att.xoffset) : 0,
yoffset: (charNode.has.yoffset) ? Std.parseInt(charNode.att.yoffset) : 0,
xadvance: (charNode.has.xadvance) ? Std.parseInt(charNode.att.xadvance) : 0,
page: Std.parseInt(charNode.att.page),
chnl: Std.parseInt(charNode.att.chnl),
letter: charNode.has.letter ? charNode.att.letter : null
id: charNode.att.int("id"),
x: charNode.att.int("x"),
y: charNode.att.int("y"),
width: charNode.att.int("width"),
height: charNode.att.int("height"),
xoffset: charNode.att.intSafe("xoffset", 0),
yoffset: charNode.att.intSafe("yoffset", 0),
xadvance: charNode.att.intSafe("xadvance", 0),
page: charNode.att.intWarn("page", -1),
chnl: charNode.att.intWarn("chnl", -1),
letter: charNode.att.stringSafe("letter")
};
}

static function listFromXml(charsNode:Access):Array<BMFontChar>
static function listFromXml(charsNode:BMFontXml):Array<BMFontChar>
{
final chars = charsNode.nodes.char;
final chars = charsNode.nodes("char");
return [ for (char in chars) fromXml(char) ];
}

Expand Down
31 changes: 15 additions & 16 deletions flixel/graphics/frames/bmfont/BMFontCommon.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flixel.graphics.frames.bmfont;

import haxe.io.BytesInput;
import haxe.xml.Access;

/**
* Common data used internally via `FlxBitmapFont.fromAngelCode` to serialize text, xml or binary
Expand All @@ -25,20 +24,20 @@ class BMFontCommon
public var greenChnl:Int;
public var blueChnl:Int;

static function fromXml(commonNode:Access):BMFontCommon
static function fromXml(commonNode:BMFontXml):BMFontCommon
{
return
{
lineHeight: Std.parseInt(commonNode.att.lineHeight),
base: Std.parseInt(commonNode.att.base),
scaleW: Std.parseInt(commonNode.att.scaleW),
scaleH: Std.parseInt(commonNode.att.scaleH),
pages: Std.parseInt(commonNode.att.pages),
packed: commonNode.att.packed != '0',
alphaChnl: (commonNode.has.alphaChnl) ? Std.parseInt(commonNode.att.alphaChnl) : 0,
redChnl: (commonNode.has.redChnl) ? Std.parseInt(commonNode.att.redChnl) : 0,
greenChnl: (commonNode.has.greenChnl) ? Std.parseInt(commonNode.att.greenChnl) : 0,
blueChnl: (commonNode.has.blueChnl) ? Std.parseInt(commonNode.att.blueChnl) : 0
lineHeight: commonNode.att.int("lineHeight"),
base: commonNode.att.intWarn("base", -1),
scaleW: commonNode.att.intWarn("scaleW", 1),
scaleH: commonNode.att.intWarn("scaleH", 1),
pages: commonNode.att.intSafe("pages", 0),
packed: commonNode.att.boolSafe("packed", false),
alphaChnl: commonNode.att.intSafe("alphaChnl", 0),
redChnl: commonNode.att.intSafe("redChnl", 0),
greenChnl: commonNode.att.intSafe("greenChnl", 0),
blueChnl: commonNode.att.intSafe("blueChnl", 0)
};
}

Expand All @@ -50,10 +49,10 @@ class BMFontCommon
var scaleH:Int = 1;
var pages:Int = 0;
var packed:Bool = false;
var alphaChnl:Int = -1;
var redChnl:Int = -1;
var greenChnl:Int = -1;
var blueChnl:Int = -1;
var alphaChnl:Int = 0;
var redChnl:Int = 0;
var greenChnl:Int = 0;
var blueChnl:Int = 0;

BMFontUtil.forEachAttribute(commonText,
function(key:String, value:String)
Expand Down
29 changes: 14 additions & 15 deletions flixel/graphics/frames/bmfont/BMFontInfo.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flixel.graphics.frames.bmfont;

import haxe.io.BytesInput;
import haxe.xml.Access;

/**
* Info data used internally via `FlxBitmapFont.fromAngelCode` to serialize text, xml or binary
Expand Down Expand Up @@ -29,22 +28,22 @@ class BMFontInfo
public var outline:Int = 0;
public var fixedHeight:Bool = false;

static function fromXml(infoNode:Access):BMFontInfo
static function fromXml(infoNode:BMFontXml):BMFontInfo
{
return {
face: infoNode.att.face,
size: Std.parseInt(infoNode.att.size),
bold: infoNode.att.bold != '0',
italic: infoNode.att.italic != '0',
smooth: infoNode.att.smooth != '0',
charset: infoNode.att.charset,
unicode: infoNode.att.unicode != '0',
stretchH: Std.parseInt(infoNode.att.stretchH),
aa: Std.parseInt(infoNode.att.aa),
padding: BMFontPadding.fromString(infoNode.att.padding),
spacing: BMFontSpacing.fromString(infoNode.att.spacing),
outline: infoNode.has.outline ? Std.parseInt(infoNode.att.outline) : 0,
fixedHeight: infoNode.has.fixedHeight && infoNode.att.fixedHeight != '0'
face: infoNode.att.string("face"),
size: infoNode.att.int("size"),
bold: infoNode.att.boolSafe("bold", false),
italic: infoNode.att.boolSafe("italic", false),
smooth: infoNode.att.boolSafe("smooth", false),
charset: infoNode.att.stringWarn("charset"),
unicode: infoNode.att.boolWarn("unicode", false),
stretchH: infoNode.att.intWarn("stretchH", 100),
aa: infoNode.att.intWarn("aa", 1),
padding: BMFontPadding.fromString(infoNode.att.stringWarn("padding")),
spacing: BMFontSpacing.fromString(infoNode.att.stringWarn("spacing")),
outline: infoNode.att.intSafe("outline", 0),
fixedHeight: infoNode.att.boolSafe("fixedHeight", false)
}
}

Expand Down
13 changes: 6 additions & 7 deletions flixel/graphics/frames/bmfont/BMFontKerning.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flixel.graphics.frames.bmfont;

import haxe.io.BytesInput;
import haxe.xml.Access;

/**
* Kerning data used internally via `FlxBitmapFont.fromAngelCode` to serialize text, xml or binary
Expand All @@ -25,19 +24,19 @@ class BMFontKerning
this.amount = amount;
}

static function fromXml(kerningNode:Access):BMFontKerning
static function fromXml(kerningNode:BMFontXml):BMFontKerning
{
return
{
first: Std.parseInt(kerningNode.att.first),
second: Std.parseInt(kerningNode.att.second),
amount: Std.parseInt(kerningNode.att.amount)
first: kerningNode.att.int("first"),
second: kerningNode.att.int("second"),
amount: kerningNode.att.int("amount")
}
}

static function listFromXml(kerningsNode:Access):Array<BMFontKerning>
static function listFromXml(kerningsNode:BMFontXml):Array<BMFontKerning>
{
final kernings = kerningsNode.nodes.kerning;
final kernings = kerningsNode.nodes("kerning");
return [ for (pair in kernings) fromXml(pair) ];
}

Expand Down
11 changes: 5 additions & 6 deletions flixel/graphics/frames/bmfont/BMFontPage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flixel.graphics.frames.bmfont;

import haxe.io.BytesInput;
import haxe.io.BytesBuffer;
import haxe.xml.Access;

/**
* Page data used internally via `FlxBitmapFont.fromAngelCode` to serialize text, xml or binary
Expand All @@ -24,18 +23,18 @@ class BMFontPage
this.file = file;
}

static function fromXml(pageNode:Access):BMFontPage
static function fromXml(pageNode:BMFontXml):BMFontPage
{
return
{
id: Std.parseInt(pageNode.att.id),
file: pageNode.att.file
id: pageNode.att.int("id"),
file: pageNode.att.string("file")
}
}

static function listFromXml(pagesNode:Access):Array<BMFontPage>
static function listFromXml(pagesNode:BMFontXml):Array<BMFontPage>
{
final pages = pagesNode.nodes.page;
final pages = pagesNode.nodes("page");
return [for (page in pages) fromXml(page) ];
}

Expand Down
Loading

0 comments on commit 3150c5d

Please sign in to comment.