[Card] Pass localized card names and texts into CardInfo construction

Address review: instead of constructing the card and then calling
setLocalizedName/setLocalizedText (which emit a cardInfoChanged signal per
language), both constructors, both newInstance overloads and their callers
(cards.xml v4 parser and the binary cache reader) now pass the localized maps
as constructor arguments.
This commit is contained in:
Lukas Brübach 2026-09-18 05:11:27 +02:00
parent 8bb2337117
commit c35aff8b7f
4 changed files with 44 additions and 29 deletions

View file

@ -40,8 +40,11 @@ CardInfo::CardInfo(const QString &_name,
const QList<CardRelation *> &_relatedCards,
const QList<CardRelation *> &_reverseRelatedCards,
SetToPrintingsMap _sets,
const UiAttributes _uiAttributes)
: name(_name), text(_text), isToken(_isToken), properties(LazyPropertiesHash(_properties)),
const UiAttributes _uiAttributes,
QMap<QString, QString> _localizedNames,
QMap<QString, QString> _localizedTexts)
: name(_name), text(_text), isToken(_isToken), localizedNames(std::move(_localizedNames)),
localizedTexts(std::move(_localizedTexts)), properties(LazyPropertiesHash(_properties)),
relatedCards(_relatedCards), reverseRelatedCards(_reverseRelatedCards), setsToPrintings(std::move(_sets)),
uiAttributes(_uiAttributes)
{
@ -59,8 +62,11 @@ CardInfo::CardInfo(const QString &_name,
SetToPrintingsMap _sets,
const UiAttributes _uiAttributes,
QString _simpleName,
QSet<QString> _altNames)
QSet<QString> _altNames,
QMap<QString, QString> _localizedNames,
QMap<QString, QString> _localizedTexts)
: name(_name), simpleName(std::move(_simpleName)), text(_text), isToken(_isToken),
localizedNames(std::move(_localizedNames)), localizedTexts(std::move(_localizedTexts)),
properties(LazyPropertiesHash(_propertiesBlob)), relatedCards(_relatedCards),
reverseRelatedCards(_reverseRelatedCards), setsToPrintings(std::move(_sets)), uiAttributes(_uiAttributes),
altNames(std::move(_altNames))
@ -83,10 +89,12 @@ CardInfoPtr CardInfo::newInstance(const QString &_name,
const QList<CardRelation *> &_relatedCards,
const QList<CardRelation *> &_reverseRelatedCards,
SetToPrintingsMap _sets,
const UiAttributes _uiAttributes)
const UiAttributes _uiAttributes,
QMap<QString, QString> _localizedNames,
QMap<QString, QString> _localizedTexts)
{
CardInfoPtr ptr(
new CardInfo(_name, _text, _isToken, _properties, _relatedCards, _reverseRelatedCards, _sets, _uiAttributes));
CardInfoPtr ptr(new CardInfo(_name, _text, _isToken, _properties, _relatedCards, _reverseRelatedCards, _sets,
_uiAttributes, std::move(_localizedNames), std::move(_localizedTexts)));
ptr->setSmartPointer(ptr);
for (const auto &printings : _sets) {
@ -109,11 +117,13 @@ CardInfoPtr CardInfo::newInstance(const QString &_name,
const UiAttributes _uiAttributes,
QString _simpleName,
QSet<QString> _altNames,
bool _appendToSets)
bool _appendToSets,
QMap<QString, QString> _localizedNames,
QMap<QString, QString> _localizedTexts)
{
CardInfoPtr ptr(new CardInfo(_name, _text, _isToken, std::move(_propertiesBlob), _relatedCards,
_reverseRelatedCards, _sets, _uiAttributes, std::move(_simpleName),
std::move(_altNames)));
std::move(_altNames), std::move(_localizedNames), std::move(_localizedTexts)));
ptr->setSmartPointer(ptr);
if (_appendToSets) {

View file

@ -103,6 +103,8 @@ public:
* @param _reverseRelatedCards Backward references to related cards.
* @param _sets Map of set names to printing information.
* @param _uiAttributes Attributes that affect display and game logic
* @param _localizedNames Localized card names, keyed by language code.
* @param _localizedTexts Localized rules text, keyed by language code.
*/
explicit CardInfo(const QString &_name,
const QString &_text,
@ -111,7 +113,9 @@ public:
const QList<CardRelation *> &_relatedCards,
const QList<CardRelation *> &_reverseRelatedCards,
SetToPrintingsMap _sets,
UiAttributes _uiAttributes);
UiAttributes _uiAttributes,
QMap<QString, QString> _localizedNames = {},
QMap<QString, QString> _localizedTexts = {});
/**
* @brief Constructs a CardInfo from a cache snapshot with precomputed derived
@ -133,6 +137,8 @@ public:
* @param _uiAttributes Attributes that affect display and game logic.
* @param _simpleName Precomputed simplified name.
* @param _altNames Precomputed alternate names.
* @param _localizedNames Localized card names, keyed by language code.
* @param _localizedTexts Localized rules text, keyed by language code.
*/
explicit CardInfo(const QString &_name,
const QString &_text,
@ -143,7 +149,9 @@ public:
SetToPrintingsMap _sets,
UiAttributes _uiAttributes,
QString _simpleName,
QSet<QString> _altNames);
QSet<QString> _altNames,
QMap<QString, QString> _localizedNames = {},
QMap<QString, QString> _localizedTexts = {});
/**
* @brief Copy constructor for CardInfo.
@ -183,6 +191,8 @@ public:
* @param _reverseRelatedCards Reverse relationships.
* @param _sets Printing information per set.
* @param _uiAttributes Attributes that affect display and game logic
* @param _localizedNames Localized card names, keyed by language code.
* @param _localizedTexts Localized rules text, keyed by language code.
* @return Shared pointer to the new CardInfo instance.
*/
static CardInfoPtr newInstance(const QString &_name,
@ -192,7 +202,9 @@ public:
const QList<CardRelation *> &_relatedCards,
const QList<CardRelation *> &_reverseRelatedCards,
SetToPrintingsMap _sets,
UiAttributes _uiAttributes);
UiAttributes _uiAttributes,
QMap<QString, QString> _localizedNames = {},
QMap<QString, QString> _localizedTexts = {});
/**
* @brief Creates a new instance from a cache snapshot with precomputed
@ -212,6 +224,8 @@ public:
* its CardSets. Pass false when building cards in parallel so the
* (non-thread-safe) set membership is populated in a later
* single-threaded pass.
* @param _localizedNames Localized card names, keyed by language code.
* @param _localizedTexts Localized rules text, keyed by language code.
* @return Shared pointer to the new CardInfo instance.
*/
static CardInfoPtr newInstance(const QString &_name,
@ -224,7 +238,9 @@ public:
UiAttributes _uiAttributes,
QString _simpleName,
QSet<QString> _altNames,
bool _appendToSets = true);
bool _appendToSets = true,
QMap<QString, QString> _localizedNames = {},
QMap<QString, QString> _localizedTexts = {});
/**
* @brief Clones the current CardInfo instance.

View file

@ -301,9 +301,6 @@ CardInfoPtr readCard(QDataStream &in, const SetNameMap &sets)
reverse.append(readRelation(in));
}
CardInfoPtr card = CardInfo::newInstance(name, text, isToken, propertiesBlob, related, reverse, cardSets, ui,
simpleName, altNames, false);
const QMap<QString, QString> localizedNames = readStringMap(in);
if (in.status() != QDataStream::Ok) {
return nullptr;
@ -312,12 +309,9 @@ CardInfoPtr readCard(QDataStream &in, const SetNameMap &sets)
if (in.status() != QDataStream::Ok) {
return nullptr;
}
for (auto it = localizedNames.constBegin(); it != localizedNames.constEnd(); ++it) {
card->setLocalizedName(it.key(), it.value());
}
for (auto it = localizedTexts.constBegin(); it != localizedTexts.constEnd(); ++it) {
card->setLocalizedText(it.key(), it.value());
}
CardInfoPtr card = CardInfo::newInstance(name, text, isToken, propertiesBlob, related, reverse, cardSets, ui,
simpleName, altNames, false, localizedNames, localizedTexts);
return card;
}

View file

@ -439,14 +439,9 @@ void CockatriceXml4Parser::loadCardsFromXml(QXmlStreamReader &xml)
.landscapeOrientation = landscapeOrientation,
.tableRow = tableRow,
.upsideDownArt = upsideDown};
CardInfoPtr newCard = CardInfo::newInstance(name, text, isToken, properties, relatedCards,
reverseRelatedCards, _sets, attributes);
for (auto it = localizedNames.constBegin(); it != localizedNames.constEnd(); ++it) {
newCard->setLocalizedName(it.key(), it.value());
}
for (auto it = localizedTexts.constBegin(); it != localizedTexts.constEnd(); ++it) {
newCard->setLocalizedText(it.key(), it.value());
}
CardInfoPtr newCard =
CardInfo::newInstance(name, text, isToken, properties, relatedCards, reverseRelatedCards, _sets,
attributes, std::move(localizedNames), std::move(localizedTexts));
if (targetData) {
// Mirror CardDatabase::addCard: if a card with this name already
// exists, merge the new printings into it instead of replacing.