Repository navigation
Expand file tree
/
Copy pathapprentice.cpp
More file actions
786 lines (682 loc) · 23.3 KB
/
Copy pathapprentice.cpp
File metadata and controls
786 lines (682 loc) · 23.3 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//+----------------------------------------------------------------------------+
//| 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/settings.hpp>
#include <data/set.hpp>
#include <data/game.hpp>
#include <data/card.hpp>
#include <data/field/text.hpp>
#include <data/field/choice.hpp>
#include <util/tagged_string.hpp>
#include <util/window_id.hpp>
#include <wx/progdlg.h>
#include <wx/wfstream.h>
#include <wx/datstrm.h>
#include <wx/filename.h>
String card_rarity_code(const String& rarity);
// ----------------------------------------------------------------------------- : Generic export dialog
/// Callback for updating a progress bar
class WithProgress {
public:
virtual void onProgress(float progress, const String& message) = 0;
virtual ~WithProgress () {}
};
/// Exception thrown to indicate exporting should be aborted
class AbortException {};
/// A dialog to show the progress of exporting
class ExportProgressDialog : public wxProgressDialog, public WithProgress {
public:
ExportProgressDialog(Window* parent, const String& title, const String& message);
/// Update the progress bar
/** if the operation should be aborted, throws an AbortException
*/
virtual void onProgress(float progress, const String& message);
};
ExportProgressDialog::ExportProgressDialog(Window* parent, const String& title, const String& message)
: wxProgressDialog(title, message, 1000, parent, wxPD_APP_MODAL | wxPD_SMOOTH | wxPD_CAN_ABORT)
{}
void ExportProgressDialog::onProgress(float progress, const String& message) {
if (!Update(int(progress * 1000), message)) {
throw AbortException();
}
}
// ----------------------------------------------------------------------------- : Generic apprentice database
/// An Apprentice database file, has read() and write() functions
class ApprDatabase {
public:
ApprDatabase(WithProgress* progress_target, const String& name);
virtual ~ApprDatabase();
/// Read the database
void read();
/// Write to a temporary file
void write();
/// Finalize the writing, swap the actual and the temporary file
void commit();
protected:
virtual void doRead(wxInputStream&) = 0;
virtual void doWrite(wxOutputStream&) = 0;
WithProgress* progress_target; ///< Write progress information to here
private:
bool in_progress; ///< Is writing in progress?
String filename; ///< Filename of database file
};
ApprDatabase::ApprDatabase(WithProgress* progress_target, const String& name)
: progress_target(progress_target)
, in_progress(false)
, filename(settings.apprentice_location + _("\\") + name)
{}
ApprDatabase::~ApprDatabase() {
// An exception is thrown while we were writing, clean up the temporary files
if (in_progress) {
// abort 'transaction'
wxRemoveFile(filename + _(".new"));
}
}
void ApprDatabase::read() {
wxFileInputStream in(filename);
if (!in.Ok()) {
throw Error(_("Can not open apprentice file for input\n'") + filename + _("'"));
}
doRead(in);
}
void ApprDatabase::write() {
// write to a .new file, doesn't commit yet
if (wxFileExists(filename + _(".new"))) {
wxRemoveFile(filename + _(".new"));
}
wxFileOutputStream out(filename + _(".new"));
in_progress = true;
if (!out.Ok()) {
throw Error(_("Can not open apprentice file for output\n'") + filename + _(".new'"));
}
doWrite(out);
}
void ApprDatabase::commit() {
// commit : rename .new to real filename
if (in_progress) {
wxRenameFile(filename, filename + _(".bak"));
wxRenameFile(filename + _(".new"), filename);
in_progress = false;
}
}
// ----------------------------------------------------------------------------- : Expansion database
/// An Apprentice expansion database (Expan.dat)
class ApprExpansionDatabase : public ApprDatabase {
public:
inline ApprExpansionDatabase(WithProgress* progress_target)
: ApprDatabase(progress_target, _("Expan.dat"))
{}
protected:
virtual void doRead(wxInputStream& in);
virtual void doWrite(wxOutputStream& out);
public:
map<String,String> expansions; ///< code -> name
vector<String> order; ///< order of codes
};
void ApprExpansionDatabase::doRead(wxInputStream& in) {
wxTextInputStream tin(in);
while (!in.Eof()) {
String l = tin.ReadLine();
if (l.size() < 3) continue;
order.push_back(l.substr(0,2));
expansions[l.substr(0,2)] = l.substr(3);
}
}
void ApprExpansionDatabase::doWrite(wxOutputStream& out) {
wxTextOutputStream tout(out, wxEOL_DOS);
// write in order first
for(auto& c : order) {
String code = c;
if (code.GetChar(0) != _('-')) {
// but not the rarities
tout << code << _("-") << expansions[c] << _("\n");
expansions.erase(c);
}
}
// the remaing expansions (our new set)
for(auto& e : expansions) {
String code = e.first;
if (code.GetChar(0) != _('-')) {
tout << code << _("-") << e.second << _("\n");
}
}
// and at last the rarities
for(auto& c : order) {
String code = c;
if (code.GetChar(0) == _('-')) {
tout << c << _("-") << expansions[c] << _("\n");
}
}
}
// ----------------------------------------------------------------------------- : Format database
class ApprFormat {
public:
ApprFormat(const String& name) : name(name) {}
String name, sets;
};
/// An Apprentice format database (Format.dat)
class ApprFormatDatabase : public ApprDatabase {
public:
inline ApprFormatDatabase(WithProgress* progress_target)
: ApprDatabase(progress_target, _("Format.dat"))
{}
/// Remove a set code from all formats
void removeSet(const String& code);
protected:
virtual void doRead(wxInputStream& in);
virtual void doWrite(wxOutputStream& out);
private:
vector<ApprFormat> formats;
};
void ApprFormatDatabase::removeSet(const String& code) {
// TODO?
}
void ApprFormatDatabase::doRead(wxInputStream& in) {
wxTextInputStream tin(in);
// read titles
while (!in.Eof()) {
String l = trim(tin.ReadLine());
if (l == _("<Titles>")) {
// ignore header
} else if (l == _("<Format>")) {
break; // to formatting step
} else {
size_t pos = l.find_first_of(_('='));
if (pos == String::npos) continue;
// assume formats are in order
formats.push_back( ApprFormat(l.substr(pos + 1)) );
}
}
// read formats
size_t i = 0;
while (!in.Eof() && i < formats.size()) {
String l = trim(tin.ReadLine());
size_t pos = l.find_first_of(_('='));
if (pos == String::npos) continue;
formats[i].sets = l.substr(pos + 1);
i += 1;
}
}
void ApprFormatDatabase::doWrite(wxOutputStream& out) {
wxTextOutputStream tout(out, wxEOL_DOS);
tout << _("<Titles>\n");
int i = 1;
for(auto& f : formats) {
tout << i++ << _("=") << f.name << _("\n");
}
tout << _("\n<Format>\n");
i = 1;
for(auto& f : formats) {
tout << i++ << _("=") << f.sets << _("\n");
}
}
// ----------------------------------------------------------------------------- : Distro database
// An entry in the Distro database
class ApprDistro {
public:
inline ApprDistro(int bc = 0, int bu = 0, int br = 0, int sc = 0, int su = 0, int sr = 0)
: bc(bc), bu(bu), br(br)
, sc(sc), su(su), sr(sr)
{}
int bc, bu, br; ///< # of cards in booster of each rarity
int sc, su, sr; ///< .. in starter
String scode, bcode; ///< alternative : numbers as a string (if numbers == 0)
void write(const String& code, wxTextOutputStream& tout);
private:
void writeD(wxTextOutputStream& tout, const String& name, int c, int u, int r);
};
/// An Apprentice distribution database (Distro.dat)
class ApprDistroDatabase : public ApprDatabase {
public:
inline ApprDistroDatabase(WithProgress* progress_target)
: ApprDatabase(progress_target, _("Distro.dat"))
{}
/// Remove a set code
void removeSet(const String& code);
protected:
virtual void doRead(wxInputStream& in);
virtual void doWrite(wxOutputStream& out);
public:
map<String,ApprDistro> distros;
vector<String> order; // order of codes
};
void ApprDistroDatabase::removeSet(const String& code) {
// TODO ?
}
void ApprDistroDatabase::doRead(InputStream& in) {
wxTextInputStream tin(in);
ApprDistro* last = 0;
while (!in.Eof()) {
String l = trim(tin.ReadLine());
if (l.size() > 2 && l.GetChar(0) == _('<')) {
// new code
l = l.substr(1, l.size() - 2);
order.push_back(l);
last = &distros[l];
} else if (last && starts_with(l, _("Starter"))) {
// no need to read the actual code, only to write it out later
// keep it as a string
last->scode = l;
} else if (last && starts_with(l, _("Booster"))) {
last->bcode = l;
}
}
}
void ApprDistroDatabase::doWrite(wxOutputStream& out) {
wxTextOutputStream tout(out, wxEOL_DOS);
// write in order
for(auto& c : order) {
distros[c].write(c, tout);
distros.erase(c);
}
// remaining distros (the newly added one)
for(auto& d : distros) {
d.second.write(d.first, tout);
}
}
void ApprDistro::write(const String& code, wxTextOutputStream& tout) {
tout.WriteString(_("<") + code + _(">\n"));
// starter
if (sc || su || sr) {
writeD(tout, _("Starter"), sc, su, sr);
} else if (!scode.empty()) {
tout.WriteString(_(" ") + scode + _("\n"));
}
// booster
if (bc || bu || br) {
writeD(tout, _("Booster"), bc, bu, br);
} else if (!bcode.empty()) {
tout.WriteString(_(" ") + bcode + _("\n"));
}
}
void ApprDistro::writeD(wxTextOutputStream& tout, const String& name, int c, int u, int r) {
tout.WriteString(_(" ")+name+_("=R") << r << _(",U") << u << _(",C") << c << _("\n"));
}
// ----------------------------------------------------------------------------- : Card database
/// Untag function for apprentice, replaces newlines with \r\n
String untag_appr(const String& s) {
return replace_all(untag(curly_quotes(s,false)), _("\n"), _("\r\n"));
}
inline String untag_appr(const ScriptValueP& str) {
return untag_appr(str->toString());
}
DECLARE_POINTER_TYPE(ApprCardRecord);
/// An Apprentice card database (cardinfo.dat)
class ApprCardDatabase : public ApprDatabase {
public:
inline ApprCardDatabase(WithProgress* progress_target)
: ApprDatabase(progress_target, _("sets\\cardinfo.dat"))
{}
/// Remove a set code
void removeSet(const String& code);
protected:
virtual void doRead(wxInputStream& in);
virtual void doWrite(wxOutputStream& out);
public:
vector<ApprCardRecordP> cards;
};
/// A single record in the apprentice card database
/** Each card has two records, a data record at the top of the file
* and a head record at the bottom
*/
class ApprCardRecord : public IntrusivePtrBase<ApprCardRecord> {
public:
String name, sets;
String type, cc, pt, text, flavor;
UInt data_pos;
Byte color; // bitmask:
enum Color {
white = 0x01,
blue = 0x02,
black = 0x04,
red = 0x08,
green = 0x10,
gold = 0x20,
arti = 0x40,
land = 0x80,
};
ApprCardRecord() {}
ApprCardRecord(const Card& card, const String& sets_);
void readHead(wxDataInputStream& strm);
void readCard(wxDataInputStream& strm);
void writeHead(wxDataOutputStream& strm);
void writeCard(wxDataOutputStream& strm);
/// Reads a string in apprentice format
String readString(wxDataInputStream& strm);
/// Writes a string in apprentice format
void writeString(wxDataOutputStream& strm, const String& out);
// Remove a code from the list of set codes
void removeSet(const String& code);
};
// conversion from MSE2 card
ApprCardRecord::ApprCardRecord(const Card& card, const String& sets_) {
name = untag_appr(card.value(_("name")));
sets = sets_ + _("-") + card_rarity_code(card.value(_("rarity"))->toString());
cc = untag_appr(card.value(_("casting_cost")));
type = untag_appr(card.value(_("super_type")));
String subType = untag_appr(card.value(_("sub_type")));
if (!subType.empty()) type += _(" - ") + subType;
text = untag_appr(card.value(_("rule_text")));
flavor = untag_appr(card.value(_("flavor_text")));
pt = untag_appr(card.value(_("pt")));
}
void ApprCardRecord::readHead(wxDataInputStream& strm) {
name = readString(strm);
data_pos = strm.Read32();
color = strm.Read8();
sets = readString(strm);
strm.Read16(); // 00 00
}
void ApprCardRecord::readCard(wxDataInputStream& strm) {
type = readString(strm);
cc = readString(strm);
pt = readString(strm);
text = readString(strm);
flavor = readString(strm);
}
void ApprCardRecord::writeHead(wxDataOutputStream& strm) {
name = name.substr(0, 27); // max length
// determine color
color = 0;
int cnt = 0;
if (cc.find_first_of(_('W')) != String::npos) { cnt +=1; color |= white; }
if (cc.find_first_of(_('U')) != String::npos) { cnt +=1; color |= blue; }
if (cc.find_first_of(_('B')) != String::npos) { cnt +=1; color |= black; }
if (cc.find_first_of(_('R')) != String::npos) { cnt +=1; color |= red; }
if (cc.find_first_of(_('G')) != String::npos) { cnt +=1; color |= green; }
if (cnt > 1) color |= gold;
if (cnt == 0) {
if (type.find(_("Land")) != String::npos) color = land;
else color = arti;
}
// write
writeString(strm, name);
strm.Write32(data_pos);
strm.Write8(color);
writeString(strm, sets);
strm.Write16(0x0000);
}
void ApprCardRecord::writeCard(wxDataOutputStream& strm) {
writeString(strm, type);
writeString(strm, cc);
writeString(strm, pt);
writeString(strm, text);
writeString(strm, flavor);
}
String ApprCardRecord::readString(wxDataInputStream& strm) {
size_t size = strm.Read16();
if (size > 1000) size = 1000; // sanity check
String ret;
ret.reserve(size);
for (size_t i = 0 ; i < size ; ++i) {
ret += (Char) strm.Read8();
}
return ret;
}
void ApprCardRecord::writeString(wxDataOutputStream& strm, const String& out) {
strm.Write16(UInt(out.size()));
for(const auto& c : out) {
strm.Write8(c);
}
}
void ApprCardRecord::removeSet(const String& code) {
size_t pos = sets.find(code);
if (pos == String::npos) return;
String before = sets.substr(0, pos);
String after = sets.substr(pos + code.size());
if (!before.empty() && before.GetChar(before.size()-1) == _(',')) {
// remove comma
before.resize(before.size() - 1);
}
sets = before + after;
}
// Is a card record unused, i.e. no sets refer to it?
bool unused_appr_card_record(const ApprCardRecordP& rec) {
return rec->sets.size() < 2 || // nothing
(rec->sets.size() < 4 && rec->sets.find_first_of(_('-')) != String::npos); // only a rarity code
}
void ApprCardDatabase::removeSet(const String& code) {
for(auto& c : cards) {
c->removeSet(code);
}
// cleanup
cards.erase(remove_if(cards.begin(), cards.end(), unused_appr_card_record), cards.end());
}
void ApprCardDatabase::doRead(wxInputStream& in) {
wxDataInputStream data(in);
size_t head_pos = data.Read32();
in.SeekI(head_pos);
size_t card_count = data.Read32();
cards.resize(card_count);
// read cards
int i = 0;
for(auto& card : cards) {
if (++i % 100 == 0) {
// report progress sometimes
progress_target->onProgress(0.4f * float(i) / cards.size(),
String(_("reading card ")) << i << _(" of ") << (int)cards.size());
}
card = intrusive(new ApprCardRecord());
card->readHead(data);
head_pos = in.TellI();
in.SeekI(card->data_pos);
card->readCard(data);
in.SeekI(head_pos);
}
}
void ApprCardDatabase::doWrite(wxOutputStream& out) {
wxDataOutputStream data(out);
data.Write32(0); // replaced with header pos
// write card data
int i = 0;
for(auto& card : cards) {
if (++i % 100 == 0) {
// report progress sometimes
progress_target->onProgress(0.4f + 0.4f * float(i) / cards.size(),
String(_("writing card ")) << i << _(" of ") << (int)cards.size());
}
card->data_pos = out.TellO();
card->writeCard(data);
}
// write header location
UInt head_start = out.TellO();
out.SeekO(0);
data.Write32(head_start);
// card count
out.SeekO(head_start);
data.Write32((UInt)cards.size());
// write card heads
i = 0;
for(auto& card : cards) {
if (++i % 100 == 0) {
// report progress sometimes
progress_target->onProgress(0.8f + 0.2f * float(i) / cards.size(),
String(_("writing header ")) << i << _(" of ") << (int)cards.size());
}
card->writeHead(data);
}
}
// ----------------------------------------------------------------------------- : Export dialog
/// Dialog for exporting a set to Apprentice
class ApprenticeExportWindow : public wxDialog, public WithProgress {
public:
ApprenticeExportWindow(Window* parent, const SetP& set);
virtual void onProgress(float p, const String& message);
void doStep(const String& s, float size);
private:
DECLARE_EVENT_TABLE();
SetP set;
// Gui controls
wxTextCtrl *apprentice, *set_code;
// In what step of the export process are we?
String step;
float step_begin, step_end;
ExportProgressDialog* progress_target;
void onApprenticeBrowse(wxCommandEvent& ev);
void onOk(wxCommandEvent& ev);
/// Export the set
bool exportSet();
};
void export_apprentice(Window* parent, const SetP& set) {
ApprenticeExportWindow wnd(parent, set);
wnd.ShowModal();
}
ApprenticeExportWindow::ApprenticeExportWindow(Window* parent, const SetP& set)
: wxDialog(parent, wxID_ANY, _("Export to Apprentice"), wxDefaultPosition)
, set(set)
, step_begin(0), step_end(0)
{
if (!set->game->isMagic()) throw Error(_("Can only export Magic sets to Apprentice"));
// create controls
apprentice = new wxTextCtrl(this, wxID_ANY);
set_code = new wxTextCtrl(this, wxID_ANY);
wxButton* browse = new wxButton(this, ID_APPRENTICE_BROWSE , _BUTTON_("browse"));
// set values
apprentice->SetValue(settings.apprentice_location);
set_code->SetValue(set->apprentice_code);
// init sizer
wxSizer* s = new wxBoxSizer(wxVERTICAL);
// Apprentice location
s->Add(new wxStaticText(this, wxID_ANY, _TITLE_("locate apprentice")), 0, wxALL, 4);
wxSizer* s2 = new wxBoxSizer(wxHORIZONTAL);
s2->Add(apprentice, 1, wxEXPAND | wxRIGHT, 4);
s2->Add(browse, 0, wxEXPAND);
s->Add(s2, 0, wxEXPAND | (wxALL & ~wxTOP), 4);
s->AddSpacer(8);
// Set code
s->Add(new wxStaticText(this, -1, _LABEL_("set code")), 0, wxALL, 4);
s->Add(set_code, 0, wxEXPAND | wxLEFT | wxRIGHT, 4);
s->Add(new wxStaticText(this, -1, _HELP_( "set code")), 0, wxALL, 4);
s->AddSpacer(4);
s->Add(CreateButtonSizer(wxOK | wxCANCEL), 0, wxEXPAND | (wxALL & ~wxTOP), 8);
s->SetSizeHints(this);
SetSizer(s);
}
void ApprenticeExportWindow::onApprenticeBrowse(wxCommandEvent& ev) {
// browse for appr.exe
// same as in DirsPreferencesPage
wxFileDialog dlg(this, _TITLE_("locate apprentice"), apprentice->GetValue(), _(""), _LABEL_("apprentice exe") + _("|appr.exe"), wxFD_OPEN);
if (dlg.ShowModal() == wxID_OK) {
wxFileName fn(dlg.GetPath());
apprentice->SetValue(fn.GetPath());
}
}
void ApprenticeExportWindow::onOk(wxCommandEvent& ev) {
// store settings
settings.apprentice_location = apprentice->GetValue();
// store new code
String new_set_code = set_code->GetValue();
if (new_set_code.size() != 2) {
wxMessageBox(_("The set code must be 2 characters long"),
_("Invalid set code"), wxOK | wxICON_ERROR);
return;
}
new_set_code.MakeUpper();
set->apprentice_code.MakeUpper();
if (set->apprentice_code != new_set_code) {
// changed something in the set
set->apprentice_code = new_set_code;
//set->actions.atSavePoint = false; // TODO: tell the user he needs to save
}
// Check if apprentice exists
if (!wxFileExists(settings.apprentice_location + _("\\appr.exe"))) {
wxMessageBox(_("Apprentice is not found in the specified location\n") + settings.apprentice_location,
_("Apprentice Not Found"), wxOK | wxICON_ERROR);
return;
}
// create progress dialog
progress_target = new ExportProgressDialog(this, _("Export to Apprentice"), _("Exporting to Apprentice, please wait"));
progress_target->Show();
// export!
try {
if (!exportSet()) {
// canceled, but allow to try again
progress_target->Hide();
progress_target->Close();
return;
}
} catch (const AbortException&) {
// aborted, cleanup is already handled by dtors
wxMessageBox(_LABEL_("apprentice export cancelled"), _TITLE_("export cancelled"), wxOK | wxICON_INFORMATION);
}
// Done, close progress window
progress_target->Hide();
progress_target->Close();
// Close this window
EndModal(wxID_OK);
}
bool ApprenticeExportWindow::exportSet() {
// Expan database
doStep(_("Exporting expansion"), 0.01f);
ApprExpansionDatabase expan(this);
expan.read();
// Is there already a set with the desired code?
map<String,String>::iterator expan_it = expan.expansions.find(set->apprentice_code);
if (expan_it != expan.expansions.end()) {
int res = wxMessageBox(
_("There is already a set with the code '")+ expan_it->first +_("' in the Apprentice database.\n") +
_("This set has the name '")+ expan_it->second +_("'\n") +
_("Do you want to continue, and overwrite that set?"),
_("Overwrite Set?"), wxYES_NO | wxICON_EXCLAMATION
);
if (res == wxNO) return false; // abort export
}
// add our set
expan.expansions[set->apprentice_code] = set->value(_("title"))->toString();
expan.write();
// Format database
doStep(_("Exporting format"), 0.01f);
ApprFormatDatabase format(this);
format.read();
// remove old with code, TODO add new?
format.removeSet(set->apprentice_code);
format.write();
// Distro database
doStep(_("Exporting distribution"), 0.01f);
ApprDistroDatabase distro(this);
distro.read();
// remove old with code, add new
distro.removeSet(set->apprentice_code);
distro.distros[set->apprentice_code] = ApprDistro(11,3,1); // booster size
distro.write();
// Card database
doStep(_("Exporting cards"), 0.96f);
ApprCardDatabase cardlist(this);
cardlist.read();
// remove old cards with same code
cardlist.removeSet(set->apprentice_code);
// add cards from set
for(auto& card : set->cards) {
ApprCardRecordP rec = intrusive(new ApprCardRecord(*card, set->apprentice_code));
cardlist.cards.push_back(rec);
}
cardlist.write();
// Commit everything
doStep(_("Committing"), 0.01f);
expan.commit();
format.commit();
distro.commit();
cardlist.commit();
return true; // all went well
}
void ApprenticeExportWindow::onProgress(float p, const String& m) {
progress_target->onProgress(
p * (step_end - step_begin) + step_begin,
m.empty() ? step : step + _(": ") + m
);
}
void ApprenticeExportWindow::doStep(const String& s, float size) {
step = s;
step_begin = step_end;
step_end += size;
onProgress(0.0f, _(""));
}
BEGIN_EVENT_TABLE(ApprenticeExportWindow, wxDialog)
EVT_BUTTON (wxID_OK, ApprenticeExportWindow::onOk)
EVT_BUTTON (ID_APPRENTICE_BROWSE, ApprenticeExportWindow::onApprenticeBrowse)
END_EVENT_TABLE ()