mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-21 00:55:09 -07:00
[DeckList] Extract card parsing from zone XML reader (#7319)
Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
faffb5a837
commit
1a6d9d7749
2 changed files with 25 additions and 8 deletions
|
|
@ -151,24 +151,29 @@ bool InnerDecklistNode::compareName(AbstractDecklistNode *other) const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int InnerDecklistNode::readCardElement(QXmlStreamReader *xml, int remainingBudget)
|
||||||
|
{
|
||||||
|
const int amount = qMin(xml->attributes().value("number").toString().toInt(), remainingBudget);
|
||||||
|
new DecklistCardNode(xml->attributes().value("name").toString(), amount, this, -1,
|
||||||
|
xml->attributes().value("setShortName").toString(),
|
||||||
|
xml->attributes().value("collectorNumber").toString(),
|
||||||
|
xml->attributes().value("uuid").toString());
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
int InnerDecklistNode::readElement(QXmlStreamReader *xml, int limit)
|
int InnerDecklistNode::readElement(QXmlStreamReader *xml, int limit)
|
||||||
{
|
{
|
||||||
int totalCards = 0;
|
int totalCards = 0;
|
||||||
while (!xml->atEnd()) {
|
while (!xml->atEnd()) {
|
||||||
xml->readNext();
|
xml->readNext();
|
||||||
const QString childName = xml->name().toString();
|
const QString childName = xml->name().toString();
|
||||||
|
const int remainingBudget = limit - totalCards;
|
||||||
if (xml->isStartElement()) {
|
if (xml->isStartElement()) {
|
||||||
if (childName == "zone") {
|
if (childName == "zone") {
|
||||||
auto *newZone = new InnerDecklistNode(xml->attributes().value("name").toString(), this);
|
auto *newZone = new InnerDecklistNode(xml->attributes().value("name").toString(), this);
|
||||||
totalCards += newZone->readElement(xml, limit - totalCards);
|
totalCards += newZone->readElement(xml, remainingBudget);
|
||||||
} else if (childName == "card") {
|
} else if (childName == "card") {
|
||||||
int amount = xml->attributes().value("number").toString().toInt();
|
totalCards += readCardElement(xml, remainingBudget);
|
||||||
amount = qMin(amount, limit - totalCards);
|
|
||||||
new DecklistCardNode(xml->attributes().value("name").toString(), amount, this, -1,
|
|
||||||
xml->attributes().value("setShortName").toString(),
|
|
||||||
xml->attributes().value("collectorNumber").toString(),
|
|
||||||
xml->attributes().value("uuid").toString());
|
|
||||||
totalCards += amount;
|
|
||||||
}
|
}
|
||||||
} else if (xml->isEndElement() && (childName == "zone")) {
|
} else if (xml->isEndElement() && (childName == "zone")) {
|
||||||
return totalCards;
|
return totalCards;
|
||||||
|
|
|
||||||
|
|
@ -236,6 +236,18 @@ public:
|
||||||
* @param xml Writer to append elements to.
|
* @param xml Writer to append elements to.
|
||||||
*/
|
*/
|
||||||
void writeElement(QXmlStreamWriter *xml) override;
|
void writeElement(QXmlStreamWriter *xml) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* @brief Reads a single `card` element and appends it to this node.
|
||||||
|
*
|
||||||
|
* The card's quantity is capped at @p remainingBudget so a malicious or
|
||||||
|
* oversized deck file cannot push the total card count past the deck size
|
||||||
|
* limit.
|
||||||
|
*
|
||||||
|
* @return The amount of cards actually added.
|
||||||
|
*/
|
||||||
|
int readCardElement(QXmlStreamReader *xml, int remainingBudget);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // COCKATRICE_INNER_DECK_LIST_NODE_H
|
#endif // COCKATRICE_INNER_DECK_LIST_NODE_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue