Repository navigation
Expand file tree
/
Copy pathmtg_editor.cpp
More file actions
284 lines (264 loc) · 10.4 KB
/
Copy pathmtg_editor.cpp
File metadata and controls
284 lines (264 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <data/format/formats.hpp>
#include <data/set.hpp>
#include <data/game.hpp>
#include <data/stylesheet.hpp>
#include <data/card.hpp>
#include <data/field/text.hpp>
#include <data/field/choice.hpp>
#include <data/field/image.hpp>
#include <util/tagged_string.hpp>
#include <wx/wfstream.h>
ScriptValueP script_local_image_file(LocalFileName const& filename);
// ----------------------------------------------------------------------------- : MtgEditorFileFormat
/// The file format of Mtg Editor files
class MtgEditorFileFormat : public FileFormat {
public:
virtual String extension() { return _("set"); }
virtual String name() { return _("Mtg Editor files (*.set)"); }
virtual bool canImport() { return true; }
virtual bool canExport(const Game&) { return false; }
virtual SetP importSet(const String& filename);
private:
// Filter: set filename -> image directory
// based on MtgEditor's: CardSet.getImageFolder
String filter1(const String& str);
// Filter: card name -> image name
// based on MtgEditor's: Tools::purgeSpecialChars
String filter2(const String& str);
// Remove mtg editor tags
void untag(const CardP& card, const String& field);
// Translate all tags, mana tags get converted to <sym>, other tags are removed
void translateTags(String& value);
void translateTags(ScriptValueP& value);
};
FileFormatP mtg_editor_file_format() {
return intrusive(new MtgEditorFileFormat());
}
// ----------------------------------------------------------------------------- : Importing
void append_line(ScriptValueP& target, String const& line);
SetP MtgEditorFileFormat::importSet(const String& filename) {
wxFileInputStream f(filename);
#ifdef UNICODE
wxTextInputStream file(f, _('\n'), wxConvLibc);
#else
wxTextInputStream file(f);
#endif
// create set
SetP set(new Set(Game::byName(_("magic"))));
// parsing state
CardP current_card;
ScriptValueP* target = nullptr; // value we are currently reading
String layout = _("8e");
String set_date, card_date;
bool first = true;
// read file
while (!f.Eof()) {
// read a line
if (!current_card) current_card = intrusive(new Card(*set->game));
String line = file.ReadLine();
if (line == _("#SET###########")) { // set.title
target = &set->value(_("title"));
} else if (line == _("#SETDATE#######")) { // date
// remember date for generation of illustration filename
target = nullptr;
set_date = file.ReadLine();
} else if (line == _("#SHOWRARITY####") ||
line == _("#FONTS#########") || line == _("#COUNT#########")) { // ignore
target = nullptr;
file.ReadLine();
} else if (line == _("#LAYOUT########")) { // layout type
target = nullptr;
layout = file.ReadLine();
} else if (line == _("#CARD##########") || line == _("#EOF###########")) { // begin/end of card
if (!first) {
// First [CARD##] indicates only the start of a card, subsequent ones also the end of the previous
// card. We only care about the latter
// remove all tags from all text values
untag(current_card, _("name"));
untag(current_card, _("super type"));
untag(current_card, _("sub type"));
untag(current_card, _("casting cost"));
untag(current_card, _("flavor text"));
untag(current_card, _("illustrator"));
untag(current_card, _("copyright"));
untag(current_card, _("power"));
untag(current_card, _("toughness"));
// translate mtg editor tags to mse2 tags
translateTags(current_card->value(_("rule text")));
// add the card to the set
set->cards.push_back(current_card);
}
first = false;
current_card = intrusive(new Card(*set->game));
target = ¤t_card->value(_("name"));
} else if (line == _("#DATE##########")) { // date
// remember date for generation of illustration filename
target = nullptr;
card_date = file.ReadLine();
} else if (line == _("#TYPE##########")) { // super type
target = ¤t_card->value(_("super type"));
} else if (line == _("#SUBTYPE#######")) { // sub type
target = ¤t_card->value(_("sub type"));
} else if (line == _("#COST##########")) { // casting cost
target = ¤t_card->value(_("casting cost"));
} else if (line == _("#RARITY########") || line == _("#FREQUENCY#####")) { // rarity
target = 0;
line = file.ReadLine();
if (line == _("0")) current_card->value(_("rarity")) = to_script(_("common"));
else if (line == _("1")) current_card->value(_("rarity")) = to_script(_("uncommon"));
else current_card->value(_("rarity")) = to_script(_("rare"));
} else if (line == _("#COLOR#########")) { // card color
target = 0;
line = file.ReadLine();
current_card->value(_("card color")) = to_script(line);
} else if (line == _("#AUTOBG########")) { // card color.isDefault
target = 0;
line = file.ReadLine();
if (line == _("TRUE")) {
current_card->value(_("card color")) = script_default_nil;
}
} else if (line == _("#RULETEXT######")) { // rule text
target = ¤t_card->value(_("rule text"));
} else if (line == _("#FLAVORTEXT####")) { // flavor text
target = ¤t_card->value(_("flavor text"));
} else if (line == _("#ARTIST########")) { // illustrator
target = ¤t_card->value(_("illustrator"));
} else if (line == _("#COPYRIGHT#####")) { // copyright
target = ¤t_card->value(_("copyright"));
} else if (line == _("#POWER#########")) { // power
target = ¤t_card->value(_("power"));
} else if (line == _("#TOUGHNESS#####")) { // toughness
target = ¤t_card->value(_("toughness"));
} else if (line == _("#ILLUSTRATION##") || line == _("#ILLUSTRATION8#")) { // image
target = 0;
line = file.ReadLine();
if (!wxFileExists(line)) {
// based on card name and date
line = filter1(filename) + set_date + _("/") +
filter2(current_card->value(_("name"))->toString()) + card_date + _(".jpg");
}
// copy image into set
if (wxFileExists(line)) {
FileName image_file = set->newFileName(_("image"),_(""));
if (wxCopyFile(line, set->nameOut(image_file), true)) {
current_card->value(_("image")) = script_local_image_file(image_file);
}
}
} else if (line == _("#TOMBSTONE#####")) { // tombstone
target = 0;
line = file.ReadLine();
current_card->value(_("card symbol")) = to_script(
line==_("TRUE") ? _("tombstone") : _("none")
);
} else {
// normal text
if (target != 0) { // value of a text field
append_line(*target, line);
} else {
throw ParseError(_("Error in Mtg Editor file, unexpected text:\n") + line);
}
}
}
// set defaults for artist and copyright to that of the first card
if (!set->cards.empty()) {
ScriptValueP& artist = set->cards[0]->value(_("illustrator"));
ScriptValueP& copyright = set->cards[0]->value(_("copyright"));
set->value(_("artist")) = artist;
set->value(_("copyright")) = copyright;
// which cards have this value?
for(auto& card : set->cards) {
ScriptValueP& card_artist = card->value(_("illustrator"));
ScriptValueP& card_copyright = card->value(_("copyright"));
if (equal(card_artist , artist)) card_artist = make_default(artist);
if (equal(card_copyright, copyright)) card_copyright = make_default(card_copyright);
}
}
// Load stylesheet
if (layout != _("8e")) {
try {
set->stylesheet = StyleSheet::byGameAndName(*set->game, _("old"));
} catch (const Error&) {
// If old style doesn't work try the new one
set->stylesheet = StyleSheet::byGameAndName(*set->game, _("new"));
}
} else {
set->stylesheet = StyleSheet::byGameAndName(*set->game, _("new"));
}
set->validate();
return set;
}
// ----------------------------------------------------------------------------- : Filtering
String MtgEditorFileFormat::filter1(const String& str) {
String before, after, ret;
// split path name
size_t pos = str.find_last_of(_("/\\"));
if (pos == String::npos) {
before = _(""); after = str;
} else {
before = str.substr(0, pos + 1);
after = str.substr(pos + 1);
}
// filter
for(auto& c : after) {
if (isAlnum(c)) ret += c;
else ret += _('_');
}
return before + ret;
}
String MtgEditorFileFormat::filter2(const String& str) {
String ret;
for(const auto& c : str) {
if (isAlnum(c)) ret += c;
else if (c==_(' ') || c==_('-')) ret += _('_');
}
return ret;
}
void MtgEditorFileFormat::untag(const CardP& card, const String& field) {
ScriptValueP& value = card->value(field);
value = with_defaultness_of(value, to_script(untag_no_escape(value->toString())));
}
void MtgEditorFileFormat::translateTags(String& value) {
// Translate tags
String ret;
size_t pos = 0;
while (pos < value.size()) {
Char c = value.GetChar(pos);
++pos;
if (c == _('<')) {
String tag;
while (pos < value.size()) {
c = value.GetChar(pos);
++pos;
if (c == _('>')) break;
else tag += c;
}
tag.MakeUpper();
unsigned long number;
if (tag==_("W") || tag==_("U") || tag==_("B") || tag==_("R") || tag==_("G") || tag==_("X") || tag==_("Y") || tag==_("Z")) {
ret += _("<sym>") + tag + _("</sym>");
} else if (tag.ToULong(&number)) {
ret += _("<sym>") + tag + _("</sym>");
} else if (tag==_("T") || tag==_("TAP")) {
ret += _("<sym>T</sym>");
} else if (tag==_("THIS")) {
ret += _("~");
}
} else {
ret += c;
}
}
// Join adjecent symbol sections
value = replace_all(ret, _("</sym><sym>"), _(""));
}
void MtgEditorFileFormat::translateTags(ScriptValueP& value) {
String val = value->toString();
translateTags(val);
value = to_script(val);
}