mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-21 00:55:09 -07:00
* [Client] Show localized card names, texts and pictures
Localization wiring now runs end to end: the oracle importer collects
foreignData for the configured language and the client renders it.
- [Oracle] Import localized names and rules texts for the selected cardLang
- single-face cards store their foreignData name and full text
- multi-face (split/adventure/aftermath/prepare) cards collect the joined
name once and join each face's translated text with the same separator
as the English merge; an incomplete translation falls back to English;
the joined text follows the same highest-priority-set policy as the
single-face path and is only collected when localization is enabled
- the wizard switching languages re-imports the card database
- [Client] Display localized card info throughout the client
- card info text/picture widgets and the game board re-render on language
change
- pictures resolve cardLang art through Scryfall's named endpoint using the
localized name, falling back to id-based art when no match exists
- deck editor keeps canonical English names as card identity (EditRole)
while showing localized names (DisplayRole), so decks and wire names
stay stable
- [Card] Add CardLocalization-backed name/text lookup and cards.xml v4
localization elements with a bounded-size translation cache
- [Tests] Cover oracle foreignData import (incl. multi-face joins, priority
and fallback paths), XML v4 localization parsing, deck model localized
display and the language-aware settings default
Existing installations need to re-run Oracle to see translations: localized
data only lands in cards.xml when the Oracle app is started with the
preferred language selected — launch the separate "Oracle" program that
ships with Cockatrice, pick the language in the wizard and let it re-import
the card database.
The client's database cache (cards.xml.cache) is invalidated by the cache
format bump and the source-hash checks, but a cache written before the
re-import can still hold English-only entries (the hash uses file size and
mtime, so a same-size/same-timestamp rewrite may be served as-is); delete
cards.xml.cache and relaunch if no localized names/texts show up after
re-importing.
* [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.
* [Client] Rename LocalizedCard:: helpers namespace to CardLocalization
The namespace now matches its header file name, as the review pointed out;
LocalizedCard reads more like a class or struct. Callers (card info text
widget, board card name rendering) are updated to match.
* [Client] Drop unused info member from the card info text widget
The CardInfoPtr member was only ever initialized to nullptr and never read;
remove it together with its initializer.
* [PictureLoader] Add the localized picture URL explicitly, not implicitly
Address review: silently prepending the Scryfall named-picture URL to the
download list whenever a non-English card language was active was surprising,
consumed quota per card when it failed, and could grab the wrong (canon) art on
name collisions, with no way to turn it off.
The insert is now opt-in and user-controlled: changing the card language adds
the template to the top of the download URLs once (persisted, documented in the
re-import prompt, and editable/removable in the deck editor settings), while the
picture loader no longer injects it at request time.
* [Card] Show card languages in the same native (English) format as the UI
Address review: the card text & images language dropdown listed bare native
names, some in inconsistent lowercase (e.g. "čeština", "español de España"),
which makes the languages easy to mix up for users that do not read the script
(e.g. 日本語 vs 한국어). It now mirrors the UI language dropdown and always pairs
the native name with its English name (e.g. "Deutsch (German)",
"日本語 (Japanese)"), using the same fixed casing.
---------
Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
311 lines
9.9 KiB
C++
311 lines
9.9 KiB
C++
#include "card_info.h"
|
|
|
|
#include "game_specific_terms.h"
|
|
#include "printing/printing_info.h"
|
|
#include "relation/card_relation.h"
|
|
#include "set/card_set.h"
|
|
|
|
#include <QDir>
|
|
#include <QRegularExpression>
|
|
#include <QSharedPointer>
|
|
#include <QString>
|
|
#include <algorithm>
|
|
#include <utility>
|
|
|
|
class CardRelation;
|
|
class CardSet;
|
|
class CardInfo;
|
|
|
|
using CardInfoPtr = QSharedPointer<CardInfo>;
|
|
|
|
const QHash<QString, QString> &CardInfo::getPropertiesHash() const
|
|
{
|
|
return properties.getProperties();
|
|
}
|
|
|
|
void CardInfo::setProperty(const QString &_name, const QString &_value)
|
|
{
|
|
bool changed = properties.insert(_name, _value);
|
|
if (!changed) {
|
|
return;
|
|
}
|
|
|
|
emit cardInfoChanged(smartThis);
|
|
}
|
|
|
|
CardInfo::CardInfo(const QString &_name,
|
|
const QString &_text,
|
|
bool _isToken,
|
|
const QHash<QString, QString> &_properties,
|
|
const QList<CardRelation *> &_relatedCards,
|
|
const QList<CardRelation *> &_reverseRelatedCards,
|
|
SetToPrintingsMap _sets,
|
|
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)
|
|
{
|
|
simpleName = CardInfo::simplifyName(name);
|
|
|
|
refreshCachedSets();
|
|
}
|
|
|
|
CardInfo::CardInfo(const QString &_name,
|
|
const QString &_text,
|
|
bool _isToken,
|
|
const QByteArray &_propertiesBlob,
|
|
const QList<CardRelation *> &_relatedCards,
|
|
const QList<CardRelation *> &_reverseRelatedCards,
|
|
SetToPrintingsMap _sets,
|
|
const UiAttributes _uiAttributes,
|
|
QString _simpleName,
|
|
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))
|
|
{
|
|
// propertiesBlob is materialized lazily on first query; simpleName and
|
|
// altNames are supplied by the caller (binary cache). Only the set-name
|
|
// display list still depends on the current enabled-set state.
|
|
refreshCachedSetNames();
|
|
}
|
|
|
|
CardInfoPtr CardInfo::newInstance(const QString &_name)
|
|
{
|
|
return newInstance(_name, "", false, {}, {}, {}, {}, {});
|
|
}
|
|
|
|
CardInfoPtr CardInfo::newInstance(const QString &_name,
|
|
const QString &_text,
|
|
bool _isToken,
|
|
const QHash<QString, QString> &_properties,
|
|
const QList<CardRelation *> &_relatedCards,
|
|
const QList<CardRelation *> &_reverseRelatedCards,
|
|
SetToPrintingsMap _sets,
|
|
const UiAttributes _uiAttributes,
|
|
QMap<QString, QString> _localizedNames,
|
|
QMap<QString, QString> _localizedTexts)
|
|
{
|
|
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) {
|
|
for (const PrintingInfo &printing : printings) {
|
|
printing.getSet()->append(ptr);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return ptr;
|
|
}
|
|
|
|
CardInfoPtr CardInfo::newInstance(const QString &_name,
|
|
const QString &_text,
|
|
bool _isToken,
|
|
QByteArray _propertiesBlob,
|
|
const QList<CardRelation *> &_relatedCards,
|
|
const QList<CardRelation *> &_reverseRelatedCards,
|
|
SetToPrintingsMap _sets,
|
|
const UiAttributes _uiAttributes,
|
|
QString _simpleName,
|
|
QSet<QString> _altNames,
|
|
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(_localizedNames), std::move(_localizedTexts)));
|
|
ptr->setSmartPointer(ptr);
|
|
|
|
if (_appendToSets) {
|
|
for (const auto &printings : _sets) {
|
|
for (const PrintingInfo &printing : printings) {
|
|
printing.getSet()->append(ptr);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return ptr;
|
|
}
|
|
|
|
QString CardInfo::getCorrectedName() const
|
|
{
|
|
// remove all the characters reserved in windows file paths,
|
|
// other oses only disallow a subset of these so it covers all
|
|
static const QRegularExpression rmrx(R"(( // |[*<>:"\\?\x00-\x08\x10-\x1f]))");
|
|
static const QRegularExpression spacerx(R"([/\x09-\x0f])");
|
|
static const QString space(' ');
|
|
QString result = name;
|
|
// Fire // Ice, Circle of Protection: Red, "Ach! Hans, Run!", Who/What/When/Where/Why, Question Elemental?
|
|
return result.remove(rmrx).replace(spacerx, space);
|
|
}
|
|
|
|
QString CardInfo::getLegalityProp(const QString &format) const
|
|
{
|
|
return getProperty("format-" + format);
|
|
}
|
|
|
|
bool CardInfo::isLegalInFormat(const QString &format) const
|
|
{
|
|
if (format.isEmpty()) {
|
|
return true;
|
|
}
|
|
|
|
QString formatLegality = getLegalityProp(format);
|
|
return formatLegality == "legal" || formatLegality == "restricted";
|
|
}
|
|
|
|
void CardInfo::addToSet(const CardSetPtr &_set, const PrintingInfo &_info)
|
|
{
|
|
if (!_set->contains(smartThis)) {
|
|
_set->append(smartThis);
|
|
}
|
|
if (!setsToPrintings[_set->getShortName()].contains(_info)) {
|
|
setsToPrintings[_set->getShortName()].append(_info);
|
|
}
|
|
|
|
refreshCachedSets();
|
|
}
|
|
|
|
void CardInfo::combineLegalities(const QHash<QString, QString> &props)
|
|
{
|
|
QHashIterator it(props);
|
|
while (it.hasNext()) {
|
|
it.next();
|
|
if (it.key().startsWith("format-")) {
|
|
properties.insert(it.key(), it.value());
|
|
}
|
|
}
|
|
emit cardInfoChanged(smartThis);
|
|
}
|
|
|
|
void CardInfo::refreshCachedSets()
|
|
{
|
|
refreshCachedSetNames();
|
|
refreshCachedAltNames();
|
|
}
|
|
|
|
void CardInfo::refreshCachedSetNames()
|
|
{
|
|
QStringList setList;
|
|
// update the cached list of set names
|
|
for (const auto &printings : setsToPrintings) {
|
|
for (const auto &printing : printings) {
|
|
if (printing.getSet()->getEnabled()) {
|
|
setList << printing.getSet()->getShortName();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
setsNames = setList.join(", ");
|
|
}
|
|
|
|
void CardInfo::refreshCachedAltNames()
|
|
{
|
|
altNames.clear();
|
|
|
|
// update the altNames with the flavorNames
|
|
for (const auto &printings : setsToPrintings) {
|
|
for (const auto &printing : printings) {
|
|
QString flavorName = printing.getFlavorName();
|
|
if (!flavorName.isEmpty()) {
|
|
altNames.insert(flavorName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
QString CardInfo::simplifyName(const QString &name)
|
|
{
|
|
static const QRegularExpression spaceOrSplit("(\\s+|\\/\\/.*)");
|
|
static const QRegularExpression nonAlnum("[^a-z0-9]");
|
|
|
|
QString simpleName = name.toLower();
|
|
|
|
// remove spaces and right halves of split cards
|
|
simpleName.remove(spaceOrSplit);
|
|
|
|
// So Aetherling would work, but not Ætherling since 'Æ' would get replaced
|
|
// with nothing.
|
|
simpleName.replace("æ", "ae");
|
|
|
|
// Replace Jötun Grunt with Jotun Grunt.
|
|
simpleName = simpleName.normalized(QString::NormalizationForm_KD);
|
|
|
|
// remove all non alphanumeric characters from the name
|
|
simpleName.remove(nonAlnum);
|
|
return simpleName;
|
|
}
|
|
|
|
QChar CardInfo::getColorChar() const
|
|
{
|
|
QString colors = getColors();
|
|
switch (colors.size()) {
|
|
case 0:
|
|
return QChar();
|
|
case 1:
|
|
return colors.at(0);
|
|
default:
|
|
return QChar('m');
|
|
}
|
|
}
|
|
|
|
void CardInfo::resetReverseRelatedCards2Me()
|
|
{
|
|
for (CardRelation *cardRelation : this->getReverseRelatedCards2Me()) {
|
|
cardRelation->deleteLater();
|
|
}
|
|
reverseRelatedCardsToMe = QList<CardRelation *>();
|
|
}
|
|
|
|
// Back-compatibility methods. Remove ASAP
|
|
QString CardInfo::getCardType() const
|
|
{
|
|
return getProperty(Mtg::CardType);
|
|
}
|
|
void CardInfo::setCardType(const QString &value)
|
|
{
|
|
setProperty(Mtg::CardType, value);
|
|
}
|
|
QString CardInfo::getCmc() const
|
|
{
|
|
return getProperty(Mtg::ConvertedManaCost);
|
|
}
|
|
QString CardInfo::getColors() const
|
|
{
|
|
return getProperty(Mtg::Colors);
|
|
}
|
|
void CardInfo::setColors(const QString &value)
|
|
{
|
|
setProperty(Mtg::Colors, value);
|
|
}
|
|
QString CardInfo::getLoyalty() const
|
|
{
|
|
return getProperty(Mtg::Loyalty);
|
|
}
|
|
QString CardInfo::getMainCardType() const
|
|
{
|
|
return getProperty(Mtg::MainCardType);
|
|
}
|
|
QString CardInfo::getManaCost() const
|
|
{
|
|
return getProperty(Mtg::ManaCost);
|
|
}
|
|
QString CardInfo::getPowTough() const
|
|
{
|
|
return getProperty(Mtg::PowTough);
|
|
}
|
|
void CardInfo::setPowTough(const QString &value)
|
|
{
|
|
setProperty(Mtg::PowTough, value);
|
|
}
|