mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
Preserve upstream master and the original nine-file copy-limit change. Retain QIODevice alongside upstream Oracle test includes. Invalidate derived format rules where the new upstream import reset and failure paths clear their cards.
348 lines
11 KiB
C++
348 lines
11 KiB
C++
#ifndef ORACLEIMPORTER_H
|
|
#define ORACLEIMPORTER_H
|
|
|
|
#include "libcockatrice/card/printing/printing_info.h"
|
|
#include "libcockatrice/card/set/card_set.h"
|
|
#include "raw_json_scanner.h"
|
|
|
|
#include <QAtomicInt>
|
|
#include <QByteArray>
|
|
#include <QDate>
|
|
#include <QHash>
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
#include <QList>
|
|
#include <QMap>
|
|
#include <QObject>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <libcockatrice/card/card_info.h>
|
|
#include <qnamespace.h>
|
|
#include <qtmetamacros.h>
|
|
#include <utility>
|
|
|
|
class CardRelation;
|
|
class QRegularExpression;
|
|
|
|
// many users prefer not to see these sets with non english arts
|
|
// they will given priority PriorityLowest
|
|
const QStringList nonEnglishSets = {"4BB", "FBB", "PS11", "PSAL", "REN", "RIN"};
|
|
const QMap<QString, CardSet::Priority> setTypePriorities{
|
|
{"core", CardSet::PriorityPrimary},
|
|
{"expansion", CardSet::PriorityPrimary},
|
|
|
|
{"commander", CardSet::PrioritySecondary},
|
|
{"starter", CardSet::PrioritySecondary},
|
|
{"draft_innovation", CardSet::PrioritySecondary},
|
|
{"duel_deck", CardSet::PrioritySecondary},
|
|
|
|
{"archenemy", CardSet::PriorityReprint},
|
|
{"arsenal", CardSet::PriorityReprint},
|
|
{"box", CardSet::PriorityReprint},
|
|
{"eternal", CardSet::PriorityReprint},
|
|
{"from_the_vault", CardSet::PriorityReprint},
|
|
{"masterpiece", CardSet::PriorityReprint},
|
|
{"masters", CardSet::PriorityReprint},
|
|
{"memorabilia", CardSet::PriorityReprint},
|
|
{"planechase", CardSet::PriorityReprint},
|
|
{"premium_deck", CardSet::PriorityReprint},
|
|
{"promo", CardSet::PriorityReprint},
|
|
{"spellbook", CardSet::PriorityReprint},
|
|
{"token", CardSet::PriorityReprint},
|
|
{"treasure_chest", CardSet::PriorityReprint},
|
|
|
|
{"alchemy", CardSet::PriorityOther},
|
|
{"funny", CardSet::PriorityOther},
|
|
{"minigame", CardSet::PriorityOther},
|
|
{"vanguard", CardSet::PriorityOther},
|
|
};
|
|
|
|
class SetToDownload
|
|
{
|
|
private:
|
|
QString shortName, longName;
|
|
QDate releaseDate;
|
|
QString setType;
|
|
CardSet::Priority priority;
|
|
// Byte range of this set's object within the importer's raw JSON text. Parsing
|
|
// one set at a time keeps peak memory low instead of holding the whole document.
|
|
RawJson::SetDataRange rawRange;
|
|
|
|
public:
|
|
const QString &getShortName() const
|
|
{
|
|
return shortName;
|
|
}
|
|
const QString &getLongName() const
|
|
{
|
|
return longName;
|
|
}
|
|
const QString &getSetType() const
|
|
{
|
|
return setType;
|
|
}
|
|
const QDate &getReleaseDate() const
|
|
{
|
|
return releaseDate;
|
|
}
|
|
CardSet::Priority getPriority() const
|
|
{
|
|
return priority;
|
|
}
|
|
const RawJson::SetDataRange &getRawRange() const
|
|
{
|
|
return rawRange;
|
|
}
|
|
SetToDownload(QString _shortName,
|
|
QString _longName,
|
|
CardSet::Priority _priority,
|
|
QString _setType = QString(),
|
|
const QDate &_releaseDate = QDate())
|
|
: shortName(std::move(_shortName)), longName(std::move(_longName)), releaseDate(_releaseDate),
|
|
setType(std::move(_setType)), priority(_priority)
|
|
{
|
|
}
|
|
void setRawRange(const RawJson::SetDataRange &_rawRange)
|
|
{
|
|
rawRange = _rawRange;
|
|
}
|
|
bool operator<(const SetToDownload &set) const
|
|
{
|
|
return longName.compare(set.longName, Qt::CaseInsensitive) < 0;
|
|
}
|
|
};
|
|
|
|
class SplitCardPart
|
|
{
|
|
public:
|
|
SplitCardPart(const QString &_name,
|
|
const QString &_text,
|
|
const QHash<QString, QString> &_properties,
|
|
const PrintingInfo &_printingInfo,
|
|
const QString &_localizedText = QString());
|
|
inline const QString &getName() const
|
|
{
|
|
return name;
|
|
}
|
|
inline const QString &getText() const
|
|
{
|
|
return text;
|
|
}
|
|
/**
|
|
* @brief The cardLang rules text of this face's foreignData entry, if any.
|
|
*/
|
|
inline const QString &getLocalizedText() const
|
|
{
|
|
return localizedText;
|
|
}
|
|
inline const QHash<QString, QString> &getProperties() const
|
|
{
|
|
return properties;
|
|
}
|
|
inline const PrintingInfo &getPrintingInfo() const
|
|
{
|
|
return printingInfo;
|
|
}
|
|
|
|
private:
|
|
QString name;
|
|
QString text;
|
|
QString localizedText;
|
|
QHash<QString, QString> properties;
|
|
PrintingInfo printingInfo;
|
|
};
|
|
|
|
struct LocalizedCardEntry
|
|
{
|
|
QString name;
|
|
QString text;
|
|
CardSet::Priority priority = CardSet::PriorityLowest;
|
|
};
|
|
|
|
class OracleImporter : public QObject
|
|
{
|
|
Q_OBJECT
|
|
private:
|
|
static const QRegularExpression formatRegex;
|
|
|
|
/**
|
|
* The cards, indexed by name.
|
|
*/
|
|
CardNameMap cards;
|
|
|
|
// Shared defaults are immutable; card-specific copy limits belong to this import.
|
|
FormatRulesNameMap magicFormats;
|
|
bool magicFormatsDirty = true;
|
|
|
|
/**
|
|
* The sets, indexed by short name.
|
|
*/
|
|
SetNameMap sets;
|
|
|
|
QList<SetToDownload> allSets;
|
|
|
|
/**
|
|
* The raw JSON text of the source document, retained for lazy per-set
|
|
* parsing during startImport(). Frees the card data as each set is imported.
|
|
*/
|
|
QByteArray rawSetsData;
|
|
|
|
/**
|
|
* Whether readSetsFromByteArray() should report scan progress via
|
|
* dataReadProgress. The wizard streams that signal to stdout (for the
|
|
* hosting Cockatrice client to parse) in both interactive and background
|
|
* runs; the flag exists to skip the scanner instrumentation entirely when
|
|
* no consumer needs it.
|
|
*/
|
|
bool progressReporting = true;
|
|
|
|
/**
|
|
* Atomic "please stop importing" flag. startImport() checks it between sets
|
|
* so a wizard being closed mid-import can be torn down without waiting for
|
|
* the whole import (or racing it).
|
|
*/
|
|
QAtomicInt importCancelled;
|
|
bool importSuccessful = false;
|
|
QString importError;
|
|
|
|
// Discard the incomplete in-memory database; the installed file is untouched.
|
|
int failImport(const QString &message);
|
|
|
|
/**
|
|
* The ISO-639 language code whose foreignData is imported; "en" by default.
|
|
*/
|
|
QString cardLang = "en";
|
|
|
|
/**
|
|
* Whether cardLang is a supported language other than English, so per-card
|
|
* foreignData scanning can be skipped entirely when disabled.
|
|
*/
|
|
bool localizationEnabled = false;
|
|
|
|
/**
|
|
* Localized name/text collected per imported card key while parsing sets,
|
|
* applied to the CardInfo objects by applyLocalizedData() once all
|
|
* printings have been seen so the best-priority one wins.
|
|
*/
|
|
QMap<QString, LocalizedCardEntry> localizedEntries;
|
|
|
|
/**
|
|
* cardLang rules text collected for split-card names while parsing sets,
|
|
* applied by applyLocalizedData(). Kept apart from localizedEntries because
|
|
* MTGJSON emits each split face as its own card object with the joined name
|
|
* on every foreignData entry: names and the per-face text join have different
|
|
* completeness and must not overwrite each other under the same key.
|
|
*/
|
|
QMap<QString, LocalizedCardEntry> splitLocalizedTexts;
|
|
|
|
CardInfoPtr addCard(QString name,
|
|
const QString &text,
|
|
bool isToken,
|
|
QHash<QString, QString> properties,
|
|
const QList<CardRelation *> &relatedCards,
|
|
const PrintingInfo &printingInfo);
|
|
|
|
/**
|
|
* Records the first foreignData entry matching cardLang for the given card
|
|
* key, keeping the entry from the highest-priority set seen so far.
|
|
*
|
|
* Multi-face cards (split, adventure, aftermath, prepare) pass collectText =
|
|
* false: MTGJSON emits one foreignData entry per face with the same joined
|
|
* name but only that face's text, so the name is collected here while the
|
|
* per-face texts are joined during the split-card merge.
|
|
*/
|
|
void collectForeignData(const QString &cardKey,
|
|
const CardSetPtr ¤tSet,
|
|
const QJsonObject &card,
|
|
bool collectText = true);
|
|
signals:
|
|
void setIndexChanged(int cardsImported, int setIndex, const QString &setName);
|
|
void dataReadProgress(int bytesRead, int totalBytes);
|
|
|
|
public:
|
|
explicit OracleImporter(QObject *parent = nullptr);
|
|
/**
|
|
* @brief Controls whether readSetsFromByteArray() instruments the raw scan.
|
|
*
|
|
* When enabled (the default) the raw scanner reports progress via
|
|
* dataReadProgress(), which the wizard streams to stdout (so the hosting
|
|
* Cockatrice client can show it) and also displays on its own progress bar.
|
|
* Switch it off only when nothing will consume scan progress.
|
|
*/
|
|
void setProgressReporting(bool enabled)
|
|
{
|
|
progressReporting = enabled;
|
|
}
|
|
/**
|
|
* Selects the ISO-639 language code whose foreignData is imported.
|
|
* English (the default) and unsupported codes disable localization.
|
|
*/
|
|
void setCardLang(const QString &lang);
|
|
const QString &getCardLang() const
|
|
{
|
|
return cardLang;
|
|
}
|
|
/**
|
|
* Scans the given JSON document for set metadata. Takes the data by value so
|
|
* the wizard can hand over its decompressed buffer without copying it.
|
|
*/
|
|
bool readSetsFromByteArray(QByteArray data);
|
|
// Returns the complete selected-set count on success, zero on failure or cancellation.
|
|
int startImport();
|
|
bool hasSuccessfulImport() const
|
|
{
|
|
return importSuccessful;
|
|
}
|
|
const QString &getImportError() const
|
|
{
|
|
return importError;
|
|
}
|
|
// Call on the owning thread before scheduling a worker, so a cancellation
|
|
// arriving before that worker starts is not lost.
|
|
void prepareImport()
|
|
{
|
|
importCancelled.storeRelease(0);
|
|
importSuccessful = false;
|
|
importError.clear();
|
|
}
|
|
/**
|
|
* @brief Requests an in-flight startImport() to stop at the next set boundary.
|
|
*
|
|
* Works by setting an atomic flag that startImport() polls between sets, so
|
|
* cancelImport() followed by a short waitForFinished() on the running future is
|
|
* safe the moment the wizard is about to be destroyed.
|
|
*/
|
|
void cancelImport()
|
|
{
|
|
importCancelled.storeRelease(1);
|
|
}
|
|
/**
|
|
* Applies the collected localized names/texts to the imported cards.
|
|
* Called automatically at the end of startImport(); exposed separately so
|
|
* tests can drive it after importing sets directly.
|
|
*/
|
|
void applyLocalizedData();
|
|
bool saveToFile(const QString &fileName, const QString &sourceUrl, const QString &sourceVersion);
|
|
int importCardsFromSet(const CardSetPtr ¤tSet, const QJsonArray &cardsList);
|
|
/**
|
|
* @brief Returns format rules including finite copy allowances from imported cards.
|
|
* The result is cached until cards are added or cleared and must be treated as immutable.
|
|
*/
|
|
const FormatRulesNameMap &createDefaultMagicFormats();
|
|
const CardNameMap &getCardList() const
|
|
{
|
|
return cards;
|
|
}
|
|
QList<SetToDownload> &getSets()
|
|
{
|
|
return allSets;
|
|
}
|
|
const QByteArray &getRawSetsData() const
|
|
{
|
|
return rawSetsData;
|
|
}
|
|
void releaseSetData();
|
|
void clear();
|
|
};
|
|
|
|
#endif
|