From e5d9692d780d964eb99bebaca8178521ee86ee32 Mon Sep 17 00:00:00 2001 From: Daenyth Date: Tue, 24 Jun 2014 00:14:59 -0400 Subject: [PATCH] Fall back to plain text load when xml load fails --- cockatrice/src/deck_loader.cpp | 19 +++++++++++++++---- common/decklist.cpp | 23 ++++++++++++++++------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/cockatrice/src/deck_loader.cpp b/cockatrice/src/deck_loader.cpp index 6a907ed7..4443b43b 100644 --- a/cockatrice/src/deck_loader.cpp +++ b/cockatrice/src/deck_loader.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "deck_loader.h" #include "decklist.h" @@ -44,18 +45,28 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt) QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return false; - + bool result = false; switch (fmt) { case PlainTextFormat: result = loadFromFile_Plain(&file); break; - case CockatriceFormat: result = loadFromFile_Native(&file); break; + case CockatriceFormat: + result = loadFromFile_Native(&file); + qDebug() << "Loaded from" << fileName << "-" << result; + if (!result) { + qDebug() << "Retying as plain format"; + file.seek(0); + result = loadFromFile_Plain(&file); + fmt = PlainTextFormat; + } + break; } if (result) { lastFileName = fileName; lastFileFormat = fmt; - + emit deckLoaded(); } + qDebug() << "Deck was loaded -" << result; return result; } @@ -66,7 +77,7 @@ bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId) lastFileName = QString(); lastFileFormat = CockatriceFormat; lastRemoteDeckId = remoteDeckId; - + emit deckLoaded(); } return result; diff --git a/common/decklist.cpp b/common/decklist.cpp index 8ba9378e..e81a0629 100644 --- a/common/decklist.cpp +++ b/common/decklist.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "decklist.h" SideboardPlan::SideboardPlan(const QString &_name, const QList &_moveList) @@ -441,6 +442,11 @@ void DeckList::write(QXmlStreamWriter *xml) bool DeckList::loadFromXml(QXmlStreamReader *xml) { + if (xml->error()) { + qDebug() << "Error loading deck from xml: " << xml->errorString(); + return false; + } + cleanList(); while (!xml->atEnd()) { xml->readNext(); @@ -455,6 +461,10 @@ bool DeckList::loadFromXml(QXmlStreamReader *xml) } } updateDeckHash(); + if (xml->error()) { + qDebug() << "Error loading deck from xml: " << xml->errorString(); + return false; + } return true; } @@ -477,8 +487,7 @@ QString DeckList::writeToString_Native() bool DeckList::loadFromFile_Native(QIODevice *device) { QXmlStreamReader xml(device); - loadFromXml(&xml); - return true; + return loadFromXml(&xml); } bool DeckList::saveToFile_Native(QIODevice *device) @@ -496,7 +505,7 @@ bool DeckList::saveToFile_Native(QIODevice *device) bool DeckList::loadFromStream_Plain(QTextStream &in) { cleanList(); - + InnerDecklistNode *main = 0, *side = 0; bool inSideboard = false; @@ -530,7 +539,7 @@ bool DeckList::loadFromStream_Plain(QTextStream &in) line.remove(rx); rx.setPattern("\\(.*\\)"); line.remove(rx); - //Filter out post card name editions + //Filter out post card name editions rx.setPattern("\\|.*$"); line.remove(rx); line = line.simplified(); @@ -543,10 +552,10 @@ bool DeckList::loadFromStream_Plain(QTextStream &in) continue; QString cardName = line.mid(i + 1); - // Common differences between cockatrice's card names - // and what's commonly used in decklists + // Common differences between cockatrice's card names + // and what's commonly used in decklists rx.setPattern("’"); - cardName.replace(rx, "'"); + cardName.replace(rx, "'"); rx.setPattern("Æ"); cardName.replace(rx, "AE"); rx.setPattern("^Aether");