Repository navigation
Expand file tree
/
Copy pathset.cpp
More file actions
310 lines (277 loc) · 9.32 KB
/
Copy pathset.cpp
File metadata and controls
310 lines (277 loc) · 9.32 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
//+----------------------------------------------------------------------------+
//| 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/set.hpp>
#include <data/game.hpp>
#include <data/stylesheet.hpp>
#include <data/card.hpp>
#include <data/keyword.hpp>
#include <data/pack.hpp>
#include <data/field.hpp>
#include <data/field/text.hpp> // for 0.2.7 fix
#include <data/field/information.hpp>
#include <util/tagged_string.hpp> // for 0.2.7 fix
#include <util/order_cache.hpp>
#include <util/delayed_index_maps.hpp>
#include <script/script_manager.hpp>
#include <script/profiler.hpp>
#include <wx/sstream.h>
using std::make_pair;
// ----------------------------------------------------------------------------- : Set
Set::Set()
: vcs (intrusive(new VCS()))
, script_manager(new SetScriptManager(*this))
{}
Set::Set(const GameP& game)
: game(game)
, vcs (intrusive(new VCS()))
, script_manager(new SetScriptManager(*this))
{
data.init(game->set_fields);
}
Set::Set(const StyleSheetP& stylesheet)
: game(stylesheet->game)
, stylesheet(stylesheet)
, vcs (intrusive(new VCS()))
, script_manager(new SetScriptManager(*this))
{
data.init(game->set_fields);
}
Set::~Set() {}
Context& Set::getContext() {
assert(wxThread::IsMain());
return script_manager->getContext(stylesheet);
}
Context& Set::getContext(const CardP& card) {
assert(wxThread::IsMain());
return script_manager->getContext(card);
}
void Set::updateStyles(const CardP& card, bool only_content_dependent) {
script_manager->updateStyles(card, only_content_dependent);
}
void Set::updateDelayed() {
script_manager->updateDelayed();
}
Context& Set::getContextForThumbnails() {
assert(!wxThread::IsMain());
if (!thumbnail_script_context) {
thumbnail_script_context.reset(new SetScriptContext(*this));
}
return thumbnail_script_context->getContext(stylesheet);
}
Context& Set::getContextForThumbnails(const CardP& card) {
assert(!wxThread::IsMain());
if (!thumbnail_script_context) {
thumbnail_script_context.reset(new SetScriptContext(*this));
}
return thumbnail_script_context->getContext(card);
}
Context& Set::getContextForThumbnails(const StyleSheetP& stylesheet) {
assert(!wxThread::IsMain());
if (!thumbnail_script_context) {
thumbnail_script_context.reset(new SetScriptContext(*this));
}
return thumbnail_script_context->getContext(stylesheet);
}
const StyleSheet& Set::stylesheetFor(const CardP& card) {
if (card && card->stylesheet) return *card->stylesheet;
else return *stylesheet;
}
StyleSheetP Set::stylesheetForP(const CardP& card) {
if (card && card->stylesheet) return card->stylesheet;
else return stylesheet;
}
IndexMap<FieldP, ValueP>& Set::stylingDataFor(const StyleSheet& stylesheet) {
return styling_data.get(stylesheet.name(), stylesheet.styling_fields);
}
IndexMap<FieldP, ValueP>& Set::stylingDataFor(const CardP& card) {
if (card && card->has_styling) return card->styling_data;
else return stylingDataFor(stylesheetFor(card));
}
String Set::identification() const {
// an identifying field
for(const auto& v : data) {
if (v->fieldP->identifying) {
return v->toFriendlyString();
}
}
// otherwise the first non-information field
for(const auto& v : data) {
if (!dynamic_pointer_cast<InfoValue>(v)) {
return v->toFriendlyString();
}
}
return wxEmptyString;
}
ScriptValueP& Set::value(const String& name) {
for(IndexMap<FieldP, ValueP>::iterator it = data.begin() ; it != data.end() ; ++it) {
if ((*it)->fieldP->name == name) {
return (*it)->value;
}
}
throw InternalError(_("Expected a set field with name '")+name+_("'"));
}
String Set::typeName() const { return _("set"); }
Version Set::fileVersion() const { return file_version_set; }
void Set::validate(Version file_app_version) {
Packaged::validate(file_app_version);
// are the
if (!game) {
throw Error(_ERROR_1_("no game specified",_TYPE_("set")));
}
if (!stylesheet) {
// TODO : Allow user to select a different style
throw Error(_ERROR_("no stylesheet specified for the set"));
}
if (stylesheet->game != game) {
throw Error(_ERROR_("stylesheet and set refer to different game"));
}
// we want at least one card
if (cards.empty()) cards.push_back(intrusive(new Card(*game)));
// update scripts
script_manager->updateAll();
}
IMPLEMENT_REFLECTION(Set) {
REFLECT(game);
if (game) {
REFLECT_IF_READING {
data.init(game->set_fields);
}
WITH_DYNAMIC_ARG(game_for_reading, game.get());
REFLECT(stylesheet);
WITH_DYNAMIC_ARG(stylesheet_for_reading, stylesheet.get());
REFLECT_N("set_info", data);
if (stylesheet) {
REFLECT_N("styling", styling_data);
}
// Experimental: save each card to a different file
reflect_cards(reflector);
REFLECT(keywords);
REFLECT(pack_types);
}
reflect_set_info_get_member(reflector,data);
REFLECT_NO_SCRIPT_N("version_control", vcs);
REFLECT(apprentice_code);
}
// TODO: make this a more generic function to be used elsewhere
template <typename Reflector>
void Set::reflect_cards (Reflector& reflector) {
REFLECT(cards);
}
template <>
void Set::reflect_cards<Writer> (Writer& reflector) {
// When writing to a directory, we write each card in a separate file.
// We don't do this in zipfiles because it leads to bloat.
if (isZipfile()) {
REFLECT(cards);
} else {
set<String> used;
for(auto& card : cards) {
// pick a unique filename for this card
// can't use Package::newFileName, because then we get conflicts with the previous save of the same card
String filename = _("card ") + normalize_internal_filename(clean_filename(card->identification()));
String full_name = filename;
int i = 0;
while (used.find(full_name) != used.end()) {
full_name = String(filename) << _(".") << ++i;
}
used.insert(full_name);
// writeFile won't quite work because we'd need
// include file: card: filename
// to do that
{
OutputStreamP stream = openOut(full_name);
Writer writer(*stream, app_version);
writer.handle(_("card"), card);
}
referenceFile(full_name);
REFLECT_N("include_file", full_name);
}
}
}
// ----------------------------------------------------------------------------- : Script utilities
ScriptValueP make_iterator(const Set& set) {
return intrusive(new ScriptCollectionIterator<vector<CardP> >(&set.cards));
}
void mark_dependency_member(const Set& set, const String& name, const Dependency& dep) {
// is it the card list?
if (name == _("cards")) {
set.game->dependent_scripts_cards.add(dep);
return;
}
// is it the keywords?
if (name == _("keywords")) {
set.game->dependent_scripts_keywords.add(dep);
return;
}
// is it in the set data?
mark_dependency_member(set.data, name, dep);
}
// in scripts, set.something is read from the set_info
template <typename Reflector>
void reflect_set_info_get_member(Reflector& reflector, const IndexMap<FieldP, ValueP>& data) {}
void reflect_set_info_get_member(GetMember& reflector, const IndexMap<FieldP, ValueP>& data) {
REFLECT_NAMELESS(data);
}
int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by, const ScriptValueP& filter) {
// TODO : Lock the map?
assert(order_by);
OrderCacheP& order = order_cache[make_pair(order_by,filter)];
if (!order) {
// 1. make a list of the order value for each card
vector<String> values; values.reserve(cards.size());
vector<int> keep; if(filter) keep.reserve(cards.size());
for(const auto& c : cards) {
Context& ctx = getContext(c);
values.push_back(order_by->eval(ctx)->toString());
if (filter) {
keep.push_back(filter->eval(ctx)->toBool());
}
}
#if USE_SCRIPT_PROFILING
Timer t;
Profiler prof(t, order_by.get(), _("init order cache"));
#endif
// 3. initialize order cache
order = intrusive(new OrderCache<CardP>(cards, values, filter ? &keep : nullptr));
}
return order->find(card);
}
int Set::numberOfCards(const ScriptValueP& filter) {
if (!filter) return (int)cards.size();
map<ScriptValueP,int>::const_iterator it = filter_cache.find(filter);
if (it !=filter_cache.end()) {
return it->second;
} else {
int n = 0;
for(const auto& c : cards) {
if (filter->eval(getContext(c))->toBool()) ++n;
}
filter_cache.insert(make_pair(filter,n));
return n;
}
}
void Set::clearOrderCache() {
order_cache.clear();
filter_cache.clear();
}
// ----------------------------------------------------------------------------- : SetView
SetView::SetView() {}
SetView::~SetView() {
if (set) set->actions.removeListener(this);
}
void SetView::setSet(const SetP& newSet) {
// no longer listening to old set
if (set) {
onBeforeChangeSet();
set->actions.removeListener(this);
}
set = newSet;
// start listening to new set
if (set) set->actions.addListener(this);
onChangeSet();
}