Commit 9bc87dfa authored by twanvl's avatar twanvl

simplification: Card::value(String) and Set::value(String) now return...

simplification: Card::value(String) and Set::value(String) now return ScriptValueP& instead of a specific T that extends Value.
parent 0e760ba0
......@@ -63,6 +63,23 @@ bool Card::contains(String const& query) const {
return false;
}
ScriptValueP& Card::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 card field with name '")+name+_("'"));
}
const ScriptValueP& Card::value(const String& name) const {
for (IndexMap<FieldP, ValueP>::const_iterator it = data.begin() ; it != data.end() ; ++it) {
if ((*it)->fieldP->name == name) {
return (*it)->value;
}
}
throw InternalError(_("Expected a card field with name '")+name+_("'"));
}
IndexMap<FieldP, ValueP>& Card::extraDataFor(const StyleSheet& stylesheet) {
return extra_data.get(stylesheet.name(), stylesheet.extra_card_fields);
}
......
......@@ -66,27 +66,9 @@ class Card : public IntrusivePtrVirtualBase {
/// Does this card contain each of the words in the query string?
bool contains_words(String const& query) const;
/// Find a value in the data by name and type
template <typename T> T& value(const String& name) {
for(IndexMap<FieldP, ValueP>::iterator it = data.begin() ; it != data.end() ; ++it) {
if ((*it)->fieldP->name == name) {
T* ret = dynamic_cast<T*>(it->get());
if (!ret) throw InternalError(_("Card field with name '")+name+_("' doesn't have the right type"));
return *ret;
}
}
throw InternalError(_("Expected a card field with name '")+name+_("'"));
}
template <typename T> const T& value(const String& name) const {
for(IndexMap<FieldP, ValueP>::const_iterator it = data.begin() ; it != data.end() ; ++it) {
if ((*it)->fieldP->name == name) {
const T* ret = dynamic_cast<const T*>(it->get());
if (!ret) throw InternalError(_("Card field with name '")+name+_("' doesn't have the right type"));
return *ret;
}
}
throw InternalError(_("Expected a card field with name '")+name+_("'"));
}
/// Find a (mutable) value in the data by name
ScriptValueP& value(const String& name);
const ScriptValueP& value(const String& name) const;
DECLARE_REFLECTION();
};
......
......@@ -424,15 +424,15 @@ class ApprCardRecord : public IntrusivePtrBase<ApprCardRecord> {
// conversion from MSE2 card
ApprCardRecord::ApprCardRecord(const Card& card, const String& sets_) {
name = untag_appr(card.value<TextValue>(_("name")).value);
sets = sets_ + _("-") + card_rarity_code(card.value<ChoiceValue>(_("rarity")).value->toString());
cc = untag_appr(card.value<TextValue>(_("casting_cost")).value);
type = untag_appr(card.value<TextValue>(_("super_type")).value);
String subType = untag_appr(card.value<TextValue>(_("sub_type")).value);
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<TextValue>(_("rule_text")).value);
flavor = untag_appr(card.value<TextValue>(_("flavor_text")).value);
pt = untag_appr(card.value<TextValue>(_("pt")).value);
text = untag_appr(card.value(_("rule_text")));
flavor = untag_appr(card.value(_("flavor_text")));
pt = untag_appr(card.value(_("pt")));
}
......@@ -728,7 +728,7 @@ bool ApprenticeExportWindow::exportSet() {
if (res == wxNO) return false; // abort export
}
// add our set
expan.expansions[set->apprentice_code] = set->value<TextValue>(_("title")).value->toString();
expan.expansions[set->apprentice_code] = set->value(_("title"))->toString();
expan.write();
// Format database
......
......@@ -56,9 +56,9 @@ SetP MSE1FileFormat::importSet(const String& filename) {
throw ParseError(_("Expected MSE format version 8\nTo convert files made with older versions of Magic Set Editor:\n 1. Download the latest version 1 from http:;//magicsetedtitor.sourceforge.net\n 2. Open the set, then save the set\n 3. Try to open them again in this program."));
}
// read general info
set->value<TextValue>(_("title")) .value = to_script(file.ReadLine());
set->value<TextValue>(_("artist")) .value = to_script(file.ReadLine());
set->value<TextValue>(_("copyright")).value = to_script(file.ReadLine());
set->value(_("title")) = to_script(file.ReadLine());
set->value(_("artist")) = to_script(file.ReadLine());
set->value(_("copyright")) = to_script(file.ReadLine());
file.ReadLine(); // border color, ignored
String stylesheet = file.ReadLine();
set->apprentice_code = file.ReadLine(); // apprentice prefix
......@@ -72,7 +72,7 @@ SetP MSE1FileFormat::importSet(const String& filename) {
if (line == _("\xFF")) break;
desc += line;
}
set->value<TextValue>(_("description")).value = to_script(desc);
set->value(_("description")) = to_script(desc);
// load stylesheet
if (stylesheet.substr(0,3) == _("old")) {
......@@ -118,42 +118,42 @@ void read_mse1_card(Set& set, wxFileInputStream& f, wxTextInputStream& file) {
set.cards.push_back(card);
return;
} case 'B': { // name
card->value<TextValue>(_("name")) .value = to_script(line);
card->value(_("name")) = to_script(line);
break;
} case 'C': case 'D': { // image filename
LocalFileName image_file = set.newFileName(_("image"),_("")); // a new unique name in the package
if (wxCopyFile(line, set.nameOut(image_file), true)) {
card->value<ImageValue>(_("image")).value = script_local_image_file(image_file);
card->value(_("image")) = script_local_image_file(image_file);
}
break;
} case 'E': { // super type
card->value<TextValue>(_("super type")) .value = to_script(line);
card->value(_("super type")) = to_script(line);
break;
} case 'F': { // sub type
card->value<TextValue>(_("sub type")) .value = to_script(line);
card->value(_("sub type")) = to_script(line);
break;
} case 'G': { // casting cost
card->value<TextValue>(_("casting cost")).value = to_script(line);
card->value(_("casting cost")) = to_script(line);
break;
} case 'H': { // rarity
String rarity;
if (line == _("(U)")) rarity = _("uncommon");
else if (line == _("(R)")) rarity = _("rare");
else rarity = _("common");
card->value<ChoiceValue>(_("rarity")) .value = to_script(rarity);
card->value(_("rarity")) = to_script(rarity);
break;
} case 'I': { // power/thoughness
size_t pos = line.find_first_of(_('/'));
if (pos != String::npos) {
card->value<TextValue>(_("power")) .value = to_script(line.substr(0, pos));
card->value<TextValue>(_("toughness")) .value = to_script(line.substr(pos+1));
card->value(_("power")) = to_script(line.substr(0, pos));
card->value(_("toughness")) = to_script(line.substr(pos+1));
}
break;
} case 'J': { // rule text or part of text
append_line(card->value<TextValue>(_("rule text")).value, line);
append_line(card->value(_("rule text")), line);
break;
} case 'K': { // flavor text or part of text
append_line(card->value<TextValue>(_("flavor text")).value, line);
append_line(card->value(_("flavor text")), line);
break;
} case 'L': { // card color (if not default)
// decode color
......@@ -167,7 +167,7 @@ void read_mse1_card(Set& set, wxFileInputStream& f, wxTextInputStream& file) {
else if (line == _("7")) color = _("land");
else if (line == _("9")) color = _("multicolor");
else color = _("colorless");
card->value<ChoiceValue>(_("card color")).value = to_script(color);
card->value(_("card color")) = to_script(color);
break;
} default: {
throw ParseError(_("Not a valid MSE1 file"));
......
......@@ -76,7 +76,7 @@ SetP MtgEditorFileFormat::importSet(const String& filename) {
if (!current_card) current_card = intrusive(new Card(*set->game));
String line = file.ReadLine();
if (line == _("#SET###########")) { // set.title
target = &set->value<TextValue>(_("title")).value;
target = &set->value(_("title"));
} else if (line == _("#SETDATE#######")) { // date
// remember date for generation of illustration filename
target = nullptr;
......@@ -103,70 +103,70 @@ SetP MtgEditorFileFormat::importSet(const String& filename) {
untag(current_card, _("power"));
untag(current_card, _("toughness"));
// translate mtg editor tags to mse2 tags
translateTags(current_card->value<TextValue>(_("rule text")).value);
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 = &current_card->value<TextValue>(_("name")).value;
target = &current_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 = &current_card->value<TextValue>(_("super type")).value;
target = &current_card->value(_("super type"));
} else if (line == _("#SUBTYPE#######")) { // sub type
target = &current_card->value<TextValue>(_("sub type")).value;
target = &current_card->value(_("sub type"));
} else if (line == _("#COST##########")) { // casting cost
target = &current_card->value<TextValue>(_("casting cost")).value;
target = &current_card->value(_("casting cost"));
} else if (line == _("#RARITY########") || line == _("#FREQUENCY#####")) { // rarity
target = 0;
line = file.ReadLine();
if (line == _("0")) current_card->value<ChoiceValue>(_("rarity")).value = to_script(_("common"));
else if (line == _("1")) current_card->value<ChoiceValue>(_("rarity")).value = to_script(_("uncommon"));
else current_card->value<ChoiceValue>(_("rarity")).value = to_script(_("rare"));
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<ChoiceValue>(_("card color")).value = to_script(line);
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<ChoiceValue>(_("card color")).value = script_default_nil;
current_card->value(_("card color")) = script_default_nil;
}
} else if (line == _("#RULETEXT######")) { // rule text
target = &current_card->value<TextValue>(_("rule text")).value;
target = &current_card->value(_("rule text"));
} else if (line == _("#FLAVORTEXT####")) { // flavor text
target = &current_card->value<TextValue>(_("flavor text")).value;
target = &current_card->value(_("flavor text"));
} else if (line == _("#ARTIST########")) { // illustrator
target = &current_card->value<TextValue>(_("illustrator")).value;
target = &current_card->value(_("illustrator"));
} else if (line == _("#COPYRIGHT#####")) { // copyright
target = &current_card->value<TextValue>(_("copyright")).value;
target = &current_card->value(_("copyright"));
} else if (line == _("#POWER#########")) { // power
target = &current_card->value<TextValue>(_("power")).value;
target = &current_card->value(_("power"));
} else if (line == _("#TOUGHNESS#####")) { // toughness
target = &current_card->value<TextValue>(_("toughness")).value;
target = &current_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<TextValue>(_("name")).value->toString()) + card_date + _(".jpg");
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<ImageValue>(_("image")).value = script_local_image_file(image_file);
current_card->value(_("image")) = script_local_image_file(image_file);
}
}
} else if (line == _("#TOMBSTONE#####")) { // tombstone
target = 0;
line = file.ReadLine();
current_card->value<ChoiceValue>(_("card symbol")).value = to_script(
current_card->value(_("card symbol")) = to_script(
line==_("TRUE") ? _("tombstone") : _("none")
);
} else {
......@@ -181,14 +181,14 @@ SetP MtgEditorFileFormat::importSet(const String& filename) {
// set defaults for artist and copyright to that of the first card
if (!set->cards.empty()) {
ScriptValueP& artist = set->cards[0]->value<TextValue>(_("illustrator")).value;
ScriptValueP& copyright = set->cards[0]->value<TextValue>(_("copyright")) .value;
set->value<TextValue>(_("artist")) .value = artist;
set->value<TextValue>(_("copyright")).value = copyright;
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_EACH(card, set->cards) {
ScriptValueP& card_artist = card->value<TextValue>(_("illustrator")).value;
ScriptValueP& card_copyright = card->value<TextValue>(_("copyright")) .value;
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);
}
......@@ -240,7 +240,7 @@ String MtgEditorFileFormat::filter2(const String& str) {
}
void MtgEditorFileFormat::untag(const CardP& card, const String& field) {
ScriptValueP& value = card->value<TextValue>(field).value;
ScriptValueP& value = card->value(field);
value = with_defaultness_of(value, to_script(untag_no_escape(value->toString())));
}
......
......@@ -75,7 +75,7 @@ void export_mws(Window* parent, const SetP& set) {
wxTextOutputStream file(f, wxEOL_DOS);
// Write header
file.WriteString(set->value<TextValue>(_("title")).value->toString() + _(" Spoiler List\n"));
file.WriteString(set->value(_("title"))->toString() + _(" Spoiler List\n"));
file.WriteString(_("Set exported using Magic Set Editor 2, version ") + app_version.toString() + _("\n\n"));
wxDateTime now = wxDateTime::Now();
file.WriteString(_("Spoiler List created on ") + now.FormatISODate() + _(" ") + now.FormatISOTime());
......@@ -84,31 +84,31 @@ void export_mws(Window* parent, const SetP& set) {
// Write cards
FOR_EACH(card, set->cards) {
file.WriteString(_("Card Name:\t"));
file.WriteString(untag_mws(card->value<TextValue>(_("name")).value));
file.WriteString(untag_mws(card->value(_("name"))));
file.WriteString(_("\nCard Color:\t"));
file.WriteString(card_color_mws(card->value<ChoiceValue>(_("card color")).value->toString()));
file.WriteString(card_color_mws(card->value(_("card color"))->toString()));
file.WriteString(_("\nMana Cost:\t"));
file.WriteString(untag_mws(card->value<TextValue>(_("casting cost")).value));
file.WriteString(untag_mws(card->value(_("casting cost"))));
file.WriteString(_("\nType & Class:\t"));
String sup_type = untag_mws(card->value<TextValue>(_("super type")).value);
String sub_type = untag_mws(card->value<TextValue>(_("sub type")).value);
String sup_type = untag_mws(card->value(_("super type")));
String sub_type = untag_mws(card->value(_("sub type")));
if (sub_type.empty()) {
file.WriteString(sup_type);
} else {
file.WriteString(sup_type + _(" - ") + sub_type);
}
file.WriteString(_("\nPow/Tou:\t"));
file.WriteString(untag_mws(card->value<TextValue>(_("pt")).value));
file.WriteString(untag_mws(card->value(_("pt"))));
file.WriteString(_("\nCard Text:\t"));
file.WriteString(untag_mws(card->value<TextValue>(_("rule text")).value));
file.WriteString(untag_mws(card->value(_("rule text"))));
file.WriteString(_("\nFlavor Text:\t"));
file.WriteString(untag_mws(card->value<TextValue>(_("flavor text")).value));
file.WriteString(untag_mws(card->value(_("flavor text"))));
file.WriteString(_("\nArtist:\t\t"));
file.WriteString(untag_mws(card->value<TextValue>(_("illustrator")).value));
file.WriteString(untag_mws(card->value(_("illustrator"))));
file.WriteString(_("\nRarity:\t\t"));
file.WriteString(card_rarity_code(card->value<ChoiceValue>(_("rarity")).value->toString()));
file.WriteString(card_rarity_code(card->value(_("rarity"))->toString()));
file.WriteString(_("\nCard #:\t\t"));
file.WriteString(untag_mws(card->value<TextValue>(_("card number")).value));
file.WriteString(untag_mws(card->value(_("card number"))));
file.WriteString(_("\n\n"));
}
}
......@@ -123,6 +123,15 @@ String Set::identification() const {
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; }
......
......@@ -98,16 +98,7 @@ class Set : public Packaged {
String identification() const;
/// Find a value in the data by name and type
template <typename T> T& value(const String& name) {
for(IndexMap<FieldP, ValueP>::iterator it = data.begin() ; it != data.end() ; ++it) {
if ((*it)->fieldP->name == name) {
T* ret = dynamic_cast<T*>(it->get());
if (!ret) throw InternalError(_("Set field with name '")+name+_("' doesn't have the right type"));
return *ret;
}
}
throw InternalError(_("Expected a set field with name '")+name+_("'"));
}
ScriptValueP& value(const String& name);
/// Find the position of a card in this set, when the card list is sorted using the given cirterium
int positionOfCard(const CardP& card, const ScriptValueP& order_by, const ScriptValueP& filter);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment