Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Donald Haase
Cockatrice
Commits
e5d9692d
Commit
e5d9692d
authored
Jun 24, 2014
by
Daenyth
Browse files
Fall back to plain text load when xml load fails
parent
0420f4f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
cockatrice/src/deck_loader.cpp
View file @
e5d9692d
#include
<QStringList>
#include
<QStringList>
#include
<QFile>
#include
<QFile>
#include
<QDebug>
#include
"deck_loader.h"
#include
"deck_loader.h"
#include
"decklist.h"
#include
"decklist.h"
...
@@ -44,18 +45,28 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt)
...
@@ -44,18 +45,28 @@ bool DeckLoader::loadFromFile(const QString &fileName, FileFormat fmt)
QFile
file
(
fileName
);
QFile
file
(
fileName
);
if
(
!
file
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
if
(
!
file
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
return
false
;
return
false
;
bool
result
=
false
;
bool
result
=
false
;
switch
(
fmt
)
{
switch
(
fmt
)
{
case
PlainTextFormat
:
result
=
loadFromFile_Plain
(
&
file
);
break
;
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
)
{
if
(
result
)
{
lastFileName
=
fileName
;
lastFileName
=
fileName
;
lastFileFormat
=
fmt
;
lastFileFormat
=
fmt
;
emit
deckLoaded
();
emit
deckLoaded
();
}
}
qDebug
()
<<
"Deck was loaded -"
<<
result
;
return
result
;
return
result
;
}
}
...
@@ -66,7 +77,7 @@ bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
...
@@ -66,7 +77,7 @@ bool DeckLoader::loadFromRemote(const QString &nativeString, int remoteDeckId)
lastFileName
=
QString
();
lastFileName
=
QString
();
lastFileFormat
=
CockatriceFormat
;
lastFileFormat
=
CockatriceFormat
;
lastRemoteDeckId
=
remoteDeckId
;
lastRemoteDeckId
=
remoteDeckId
;
emit
deckLoaded
();
emit
deckLoaded
();
}
}
return
result
;
return
result
;
...
...
common/decklist.cpp
View file @
e5d9692d
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
#include
<QTextStream>
#include
<QTextStream>
#include
<QVariant>
#include
<QVariant>
#include
<QCryptographicHash>
#include
<QCryptographicHash>
#include
<QDebug>
#include
"decklist.h"
#include
"decklist.h"
SideboardPlan
::
SideboardPlan
(
const
QString
&
_name
,
const
QList
<
MoveCard_ToZone
>
&
_moveList
)
SideboardPlan
::
SideboardPlan
(
const
QString
&
_name
,
const
QList
<
MoveCard_ToZone
>
&
_moveList
)
...
@@ -441,6 +442,11 @@ void DeckList::write(QXmlStreamWriter *xml)
...
@@ -441,6 +442,11 @@ void DeckList::write(QXmlStreamWriter *xml)
bool
DeckList
::
loadFromXml
(
QXmlStreamReader
*
xml
)
bool
DeckList
::
loadFromXml
(
QXmlStreamReader
*
xml
)
{
{
if
(
xml
->
error
())
{
qDebug
()
<<
"Error loading deck from xml: "
<<
xml
->
errorString
();
return
false
;
}
cleanList
();
cleanList
();
while
(
!
xml
->
atEnd
())
{
while
(
!
xml
->
atEnd
())
{
xml
->
readNext
();
xml
->
readNext
();
...
@@ -455,6 +461,10 @@ bool DeckList::loadFromXml(QXmlStreamReader *xml)
...
@@ -455,6 +461,10 @@ bool DeckList::loadFromXml(QXmlStreamReader *xml)
}
}
}
}
updateDeckHash
();
updateDeckHash
();
if
(
xml
->
error
())
{
qDebug
()
<<
"Error loading deck from xml: "
<<
xml
->
errorString
();
return
false
;
}
return
true
;
return
true
;
}
}
...
@@ -477,8 +487,7 @@ QString DeckList::writeToString_Native()
...
@@ -477,8 +487,7 @@ QString DeckList::writeToString_Native()
bool
DeckList
::
loadFromFile_Native
(
QIODevice
*
device
)
bool
DeckList
::
loadFromFile_Native
(
QIODevice
*
device
)
{
{
QXmlStreamReader
xml
(
device
);
QXmlStreamReader
xml
(
device
);
loadFromXml
(
&
xml
);
return
loadFromXml
(
&
xml
);
return
true
;
}
}
bool
DeckList
::
saveToFile_Native
(
QIODevice
*
device
)
bool
DeckList
::
saveToFile_Native
(
QIODevice
*
device
)
...
@@ -496,7 +505,7 @@ bool DeckList::saveToFile_Native(QIODevice *device)
...
@@ -496,7 +505,7 @@ bool DeckList::saveToFile_Native(QIODevice *device)
bool
DeckList
::
loadFromStream_Plain
(
QTextStream
&
in
)
bool
DeckList
::
loadFromStream_Plain
(
QTextStream
&
in
)
{
{
cleanList
();
cleanList
();
InnerDecklistNode
*
main
=
0
,
*
side
=
0
;
InnerDecklistNode
*
main
=
0
,
*
side
=
0
;
bool
inSideboard
=
false
;
bool
inSideboard
=
false
;
...
@@ -530,7 +539,7 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
...
@@ -530,7 +539,7 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
line
.
remove
(
rx
);
line
.
remove
(
rx
);
rx
.
setPattern
(
"
\\
(.*
\\
)"
);
rx
.
setPattern
(
"
\\
(.*
\\
)"
);
line
.
remove
(
rx
);
line
.
remove
(
rx
);
//Filter out post card name editions
//Filter out post card name editions
rx
.
setPattern
(
"
\\
|.*$"
);
rx
.
setPattern
(
"
\\
|.*$"
);
line
.
remove
(
rx
);
line
.
remove
(
rx
);
line
=
line
.
simplified
();
line
=
line
.
simplified
();
...
@@ -543,10 +552,10 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
...
@@ -543,10 +552,10 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
continue
;
continue
;
QString
cardName
=
line
.
mid
(
i
+
1
);
QString
cardName
=
line
.
mid
(
i
+
1
);
// Common differences between cockatrice's card names
// Common differences between cockatrice's card names
// and what's commonly used in decklists
// and what's commonly used in decklists
rx
.
setPattern
(
"’"
);
rx
.
setPattern
(
"’"
);
cardName
.
replace
(
rx
,
"'"
);
cardName
.
replace
(
rx
,
"'"
);
rx
.
setPattern
(
"Æ"
);
rx
.
setPattern
(
"Æ"
);
cardName
.
replace
(
rx
,
"AE"
);
cardName
.
replace
(
rx
,
"AE"
);
rx
.
setPattern
(
"^Aether"
);
rx
.
setPattern
(
"^Aether"
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment