mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Implement CSV / TSV import (#45)
- add csv/tsv importer - add `make_map` script function - add `alt name` field property - add `construction script` field property - add `construction script` game property
This commit is contained in:
@@ -40,6 +40,7 @@ Field::~Field() {}
|
||||
|
||||
void Field::initDependencies(Context& ctx, const Dependency& dep) const {
|
||||
sort_script.initDependencies(ctx, dep);
|
||||
construction_script.initDependencies(ctx, dep);
|
||||
}
|
||||
|
||||
IMPLEMENT_REFLECTION(Field) {
|
||||
@@ -48,6 +49,7 @@ IMPLEMENT_REFLECTION(Field) {
|
||||
REFLECT(type);
|
||||
}
|
||||
REFLECT(name);
|
||||
REFLECT(alt_names);
|
||||
REFLECT_LOCALIZED(caption);
|
||||
REFLECT_LOCALIZED(description); // FIXME: This field is both unused and uninitialized.
|
||||
REFLECT_N("icon", icon_filename);
|
||||
@@ -62,6 +64,7 @@ IMPLEMENT_REFLECTION(Field) {
|
||||
REFLECT(card_list_allow);
|
||||
REFLECT_LOCALIZED(card_list_name);
|
||||
REFLECT(sort_script);
|
||||
REFLECT(construction_script);
|
||||
REFLECT_N("card_list_alignment", card_list_align);
|
||||
}
|
||||
|
||||
|
||||
+21
-19
@@ -42,25 +42,27 @@ public:
|
||||
Field();
|
||||
virtual ~Field();
|
||||
|
||||
size_t index; ///< Used by IndexMap
|
||||
String name; ///< Name of the field, for refering to it from scripts and files
|
||||
LocalizedString caption; ///< Caption for NativeLookEditor
|
||||
LocalizedString description;///< Description, used in status bar
|
||||
String icon_filename; ///< Filename for an icon (for list of fields)
|
||||
bool editable; ///< Can values of this field be edited?
|
||||
bool save_value; ///< Should values of this field be written to files? Can be false for script generated fields.
|
||||
bool show_statistics; ///< Should this field appear as a group by choice in the statistics panel?
|
||||
int position_hint; ///< Position in the statistics list
|
||||
bool identifying; ///< Does this field give Card::identification()?
|
||||
int card_list_column; ///< What column to use in the card list?
|
||||
UInt card_list_width; ///< Width of the card list column (pixels).
|
||||
bool card_list_visible;///< Is this field shown in the card list?
|
||||
bool card_list_allow; ///< Is this field allowed to appear in the card list?
|
||||
LocalizedString card_list_name; ///< Name to use in card list.
|
||||
Alignment card_list_align; ///< Alignment of the card list colummn.
|
||||
OptionalScript sort_script; ///< The script to use when sorting this, if not the value.
|
||||
Dependencies dependent_scripts; ///< Scripts that depend on values of this field
|
||||
String package_relative_filename;
|
||||
size_t index; ///< Used by IndexMap
|
||||
String name; ///< Name of the field, for refering to it from scripts and files
|
||||
vector<String> alt_names; ///< Other names this field might go by, mainly in CSV files
|
||||
LocalizedString caption; ///< Caption for NativeLookEditor
|
||||
LocalizedString description; ///< Description, used in status bar
|
||||
String icon_filename; ///< Filename for an icon (for list of fields)
|
||||
bool editable; ///< Can values of this field be edited?
|
||||
bool save_value; ///< Should values of this field be written to files? Can be false for script generated fields.
|
||||
bool show_statistics; ///< Should this field appear as a group by choice in the statistics panel?
|
||||
int position_hint; ///< Position in the statistics list
|
||||
bool identifying; ///< Does this field give Card::identification()?
|
||||
int card_list_column; ///< What column to use in the card list?
|
||||
UInt card_list_width; ///< Width of the card list column (pixels).
|
||||
bool card_list_visible; ///< Is this field shown in the card list?
|
||||
bool card_list_allow; ///< Is this field allowed to appear in the card list?
|
||||
LocalizedString card_list_name; ///< Name to use in card list.
|
||||
Alignment card_list_align; ///< Alignment of the card list colummn.
|
||||
OptionalScript sort_script; ///< The script to use when sorting this, if not the value.
|
||||
OptionalScript construction_script; ///< The script to apply to the supplied value, when creating a new card.
|
||||
Dependencies dependent_scripts; ///< Scripts that depend on values of this field
|
||||
String package_relative_filename;
|
||||
|
||||
/// Creates a new Value corresponding to this Field
|
||||
virtual ValueP newValue() = 0;
|
||||
|
||||
+24
-1
@@ -49,6 +49,7 @@ IMPLEMENT_REFLECTION(Game) {
|
||||
REFLECT_NO_SCRIPT(default_set_style);
|
||||
REFLECT_NO_SCRIPT(card_fields);
|
||||
REFLECT_NO_SCRIPT(card_list_color_script);
|
||||
REFLECT_NO_SCRIPT(construction_script);
|
||||
REFLECT_NO_SCRIPT(statistics_dimensions);
|
||||
REFLECT_NO_SCRIPT(statistics_categories);
|
||||
REFLECT_COMPAT(<308, "pack_item", pack_types);
|
||||
@@ -93,6 +94,28 @@ void Game::validate(Version v) {
|
||||
pack->filter = OptionalScript(_("true"));
|
||||
pack->select = SELECT_NO_REPLACE;
|
||||
pack_types.push_back(pack);
|
||||
}
|
||||
// alternate card field names map
|
||||
for (auto it = card_fields.begin(); it != card_fields.end(); ++it) {
|
||||
FieldP field = *it;
|
||||
String unified_name = unified_form(field->name);
|
||||
if (card_fields_alt_names.count(unified_name)) {
|
||||
queue_message(MESSAGE_WARNING, _("Duplicate alternate card field name: ") + unified_name);
|
||||
}
|
||||
else {
|
||||
card_fields_alt_names.emplace(unified_name, field->name);
|
||||
}
|
||||
//String column_name = field->card_list_name.get();
|
||||
//card_fields_alt_names.emplace(unified_form(column_name), field->name);
|
||||
for (auto it2 = field->alt_names.begin(); it2 != field->alt_names.end(); ++it2) {
|
||||
unified_name = unified_form(*it2);
|
||||
if (card_fields_alt_names.count(unified_name)) {
|
||||
queue_message(MESSAGE_WARNING, _("Duplicate alternate card field name: ") + unified_name);
|
||||
}
|
||||
else {
|
||||
card_fields_alt_names.emplace(unified_name, field->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +137,7 @@ void Game::initCardListColorScript() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// special behaviour of reading/writing GamePs: only read/write the name
|
||||
|
||||
|
||||
+12
-10
@@ -12,7 +12,7 @@
|
||||
#include <util/io/package.hpp>
|
||||
#include <script/scriptable.hpp>
|
||||
#include <script/dependency.hpp>
|
||||
#include <util/dynamic_arg.hpp>
|
||||
#include <util/dynamic_arg.hpp>
|
||||
|
||||
DECLARE_POINTER_TYPE(Field);
|
||||
DECLARE_POINTER_TYPE(Style);
|
||||
@@ -41,24 +41,26 @@ public:
|
||||
vector<FieldP> set_fields; ///< Fields for set information
|
||||
IndexMap<FieldP,StyleP> default_set_style; ///< Default style for the set fields, because it is often the same
|
||||
vector<FieldP> card_fields; ///< Fields on each card
|
||||
OptionalScript card_list_color_script; ///< Script that determines the color of items in the card list
|
||||
OptionalScript card_list_color_script; ///< Script that determines the color of items in the card list
|
||||
OptionalScript construction_script; ///< Script applied as the last step of the new_card function
|
||||
vector<StatsDimensionP> statistics_dimensions; ///< (Additional) statistics dimensions
|
||||
vector<StatsCategoryP> statistics_categories; ///< (Additional) statistics categories
|
||||
vector<PackTypeP> pack_types; ///< Types of random card packs to generate
|
||||
vector<WordListP> word_lists; ///< Word lists for editing with a drop down list
|
||||
vector<AddCardsScriptP> add_cards_scripts; ///< Scripts for adding multiple cards to the set
|
||||
vector<AutoReplaceP> auto_replaces; ///< Things to autoreplace in textboxes
|
||||
|
||||
vector<PackTypeP> pack_types; ///< Types of random card packs to generate
|
||||
vector<WordListP> word_lists; ///< Word lists for editing with a drop down list
|
||||
vector<AddCardsScriptP> add_cards_scripts; ///< Scripts for adding multiple cards to the set
|
||||
vector<AutoReplaceP> auto_replaces; ///< Things to autoreplace in textboxes
|
||||
map<String,String> card_fields_alt_names; ///< Other names that fields might go by, for example in CSV files
|
||||
|
||||
bool has_keywords; ///< Does this game use keywords?
|
||||
OptionalScript keyword_match_script; ///< For the keyword editor
|
||||
OptionalScript keyword_match_script; ///< For the keyword editor
|
||||
vector<KeywordParamP> keyword_parameter_types;///< Types of keyword parameters
|
||||
vector<KeywordModeP> keyword_modes; ///< Modes of keywords
|
||||
vector<KeywordP> keywords; ///< Keywords for use in text
|
||||
|
||||
Dependencies dependent_scripts_cards; ///< scripts that depend on the card list
|
||||
Dependencies dependent_scripts_keywords; ///< scripts that depend on the keywords
|
||||
Dependencies dependent_scripts_stylesheet; ///< scripts that depend on the card's stylesheet
|
||||
bool dependencies_initialized; ///< are the script dependencies comming from this game all initialized?
|
||||
Dependencies dependent_scripts_stylesheet; ///< scripts that depend on the card's stylesheet
|
||||
bool dependencies_initialized; ///< are the script dependencies comming from this game all initialized?
|
||||
|
||||
/// Loads the game with a particular name, for example "magic"
|
||||
static GameP byName(const String& name);
|
||||
|
||||
@@ -26,7 +26,9 @@ StyleSheet::StyleSheet()
|
||||
StyleSheetP StyleSheet::byGameAndName(const Game& game, const String& name) {
|
||||
/// Alternative stylesheets for game
|
||||
static map<String, String> stylesheet_alternatives;
|
||||
String full_name = game.name() + _("-") + name + _(".mse-style");
|
||||
String full_name = name;
|
||||
if (!full_name.EndsWith(_(".mse-style"))) full_name = full_name + _(".mse-style");
|
||||
if (!full_name.StartsWith(game.name() + _("-"))) full_name = game.name() + _("-") + full_name;
|
||||
try {
|
||||
map<String, String>::const_iterator it = stylesheet_alternatives.find(full_name);
|
||||
if (it != stylesheet_alternatives.end()) {
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) Twan van Laarhoven and the other MSE developers |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <util/prec.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <data/card.hpp>
|
||||
#include <data/stylesheet.hpp>
|
||||
#include <gui/add_csv_window.hpp>
|
||||
#include <util/window_id.hpp>
|
||||
#include <data/action/set.hpp>
|
||||
#include <wx/statline.h>
|
||||
|
||||
// ----------------------------------------------------------------------------- : AddCSV
|
||||
|
||||
AddCSVWindow::AddCSVWindow(Window* parent, const SetP& set, bool sizer)
|
||||
: wxDialog(parent, wxID_ANY, _TITLE_("add card csv"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
|
||||
, set(set)
|
||||
{
|
||||
// init controls
|
||||
file_path = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
|
||||
file_browse = new wxButton(this, ID_CARD_ADD_CSV_BROWSE, _BUTTON_("browse"));
|
||||
separator_type = new wxChoice(this, ID_CARD_ADD_CSV_SEP, wxDefaultPosition, wxDefaultSize, 0, nullptr);
|
||||
separator_type->Clear();
|
||||
separator_type->Append(_LABEL_("add card csv tab"));
|
||||
separator_type->Append(_LABEL_("add card csv comma"));
|
||||
separator_type->SetSelection(0);
|
||||
setSeparatorType();
|
||||
// init sizers
|
||||
if (sizer) {
|
||||
wxSizer* s = new wxBoxSizer(wxVERTICAL);
|
||||
s->Add(new wxStaticText(this, -1, _LABEL_("add card csv sep")), 0, wxALL, 8);
|
||||
s->Add(separator_type, 0, wxEXPAND | (wxALL & ~wxTOP), 8);
|
||||
s->Add(new wxStaticText(this, -1, _(" ") + _LABEL_("add card csv file")), 0, wxALL | (wxALL & ~wxTOP), 8);
|
||||
s->Add(file_path, 0, wxEXPAND | (wxALL & ~wxTOP), 8);
|
||||
wxSizer* s2 = new wxBoxSizer(wxHORIZONTAL);
|
||||
s2->Add(file_browse, 0, wxEXPAND | wxRIGHT, 8);
|
||||
s2->Add(CreateButtonSizer(wxOK | wxCANCEL), 1, wxEXPAND, 8);
|
||||
s->Add(s2, 0, wxEXPAND | wxALL, 12);
|
||||
file_browse->SetFocus();
|
||||
s->SetSizeHints(this);
|
||||
SetSizer(s);
|
||||
SetSize(500, 110);
|
||||
}
|
||||
}
|
||||
|
||||
void AddCSVWindow::setSeparatorType() {
|
||||
int sel = separator_type->GetSelection();
|
||||
if (sel == 0) separator = ' ';
|
||||
else separator = ',';
|
||||
}
|
||||
|
||||
void AddCSVWindow::onSeparatorTypeChange(wxCommandEvent&) {
|
||||
setSeparatorType();
|
||||
}
|
||||
|
||||
void AddCSVWindow::onBrowseFiles(wxCommandEvent&) {
|
||||
wxFileDialog* dlg = new wxFileDialog(this, _TITLE_("add card csv file"), settings.default_set_dir, wxEmptyString, _("CSV files|*.csv;*.tsv|All files (*.*)|*"), wxFD_OPEN);
|
||||
if (dlg->ShowModal() == wxID_OK) {
|
||||
file_path->SetValue(dlg->GetPath());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> AddCSVWindow::readCSVRow(const std::string& row) {
|
||||
CSVState state = CSVState::UnquotedField;
|
||||
std::vector<std::string> fields{ "" };
|
||||
size_t f = 0; // index of the current field
|
||||
for (char c : row) {
|
||||
switch (state) {
|
||||
case CSVState::UnquotedField:
|
||||
if (c == separator) { // end of field
|
||||
fields.push_back(""); f++;
|
||||
}
|
||||
else if (c == '"') {
|
||||
state = CSVState::QuotedField;
|
||||
}
|
||||
else {
|
||||
fields[f].push_back(c);
|
||||
}
|
||||
break;
|
||||
case CSVState::QuotedField:
|
||||
switch (c) {
|
||||
case '"': state = CSVState::QuotedQuote;
|
||||
break;
|
||||
default: fields[f].push_back(c);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CSVState::QuotedQuote:
|
||||
if (c == separator) { // separator after closing quote
|
||||
fields.push_back(""); f++;
|
||||
state = CSVState::UnquotedField;
|
||||
}
|
||||
else if (c == '"') { // "" -> "
|
||||
fields[f].push_back('"');
|
||||
state = CSVState::QuotedField;
|
||||
}
|
||||
else { // end of quote
|
||||
state = CSVState::UnquotedField;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
// escape " { }
|
||||
for (f = 0; f < fields.size(); ++f) {
|
||||
fields[f] = std::regex_replace(fields[f], std::regex("\""), "\\\"");
|
||||
fields[f] = std::regex_replace(fields[f], std::regex("\\{"), "\\{");
|
||||
fields[f] = std::regex_replace(fields[f], std::regex("\\}"), "\\}");
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
void AddCSVWindow::onOk(wxCommandEvent&) {
|
||||
/// Perform the import
|
||||
// Read the file, put it into a table
|
||||
auto in = std::ifstream(file_path->GetValue().ToStdString());
|
||||
if (in.fail()) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card csv file not found"));
|
||||
return;
|
||||
}
|
||||
std::vector<std::vector<std::string>> table;
|
||||
std::string row;
|
||||
while (!in.eof()) {
|
||||
std::getline(in, row);
|
||||
if (in.bad() || in.fail()) {
|
||||
break;
|
||||
}
|
||||
auto fields = readCSVRow(row);
|
||||
table.push_back(fields);
|
||||
}
|
||||
// ensure table is square
|
||||
int count = table[0].size();
|
||||
for (int y = 1; y < table.size(); ++y) {
|
||||
if (table[y].size() != count) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_1_("add card csv file malformed", wxString::Format(wxT("%i"), y+1)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
// produce script from table
|
||||
std::ostringstream stream;
|
||||
stream << "[";
|
||||
for (int y = 1; y < table.size(); ++y) {
|
||||
stream << "new_card([";
|
||||
for (int x = 0; x < count; ++x) {
|
||||
stream << "\"" << table[0][x] << "\": \"" << table[y][x] << "\"";
|
||||
if (x < count - 1) stream << ",";
|
||||
}
|
||||
stream << "])";
|
||||
if (y < table.size() - 1) stream << ",";
|
||||
}
|
||||
stream << "]";
|
||||
String string(stream.str().c_str(), wxConvUTF8);
|
||||
ScriptP script = parse(string, nullptr, false);
|
||||
Context& ctx = set->getContext();
|
||||
ScriptValueP result = script->eval(ctx, false);
|
||||
// create cards
|
||||
vector<CardP> cards;
|
||||
ScriptValueP it = result->makeIterator();
|
||||
while (ScriptValueP item = it->next()) {
|
||||
CardP card = from_script<CardP>(item);
|
||||
// is this a new card?
|
||||
if (contains(set->cards, card) || contains(cards, card)) {
|
||||
// make copy
|
||||
card = make_intrusive<Card>(*card);
|
||||
}
|
||||
cards.push_back(card);
|
||||
}
|
||||
// add to set
|
||||
if (!cards.empty()) {
|
||||
// TODO: change the name of the action somehow
|
||||
set->actions.addAction(make_unique<AddCardAction>(ADD, *set, cards));
|
||||
}
|
||||
// Done
|
||||
EndModal(wxID_OK);
|
||||
}
|
||||
|
||||
BEGIN_EVENT_TABLE(AddCSVWindow, wxDialog)
|
||||
EVT_BUTTON(wxID_OK, AddCSVWindow::onOk)
|
||||
EVT_BUTTON(ID_CARD_ADD_CSV_BROWSE, AddCSVWindow::onBrowseFiles)
|
||||
EVT_CHOICE(ID_CARD_ADD_CSV_SEP, AddCSVWindow::onSeparatorTypeChange)
|
||||
END_EVENT_TABLE()
|
||||
@@ -0,0 +1,50 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) Twan van Laarhoven and the other MSE developers |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
#pragma once
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
|
||||
DECLARE_POINTER_TYPE(Set);
|
||||
|
||||
// ----------------------------------------------------------------------------- : AddCSVWindow
|
||||
|
||||
/// A window for selecting a subset of the cards from a set,
|
||||
/** and selecting a link relation type.
|
||||
/** this is used when linking cards
|
||||
*/
|
||||
class AddCSVWindow : public wxDialog {
|
||||
public:
|
||||
AddCSVWindow(Window* parent, const SetP& set, bool sizer);
|
||||
|
||||
protected:
|
||||
DECLARE_EVENT_TABLE();
|
||||
|
||||
wxChoice* separator_type;
|
||||
wxTextCtrl* file_path;
|
||||
wxButton* file_browse;
|
||||
SetP set;
|
||||
char separator;
|
||||
|
||||
std::vector<std::string> readCSVRow(const std::string& row);
|
||||
|
||||
void setSeparatorType();
|
||||
|
||||
void onSeparatorTypeChange(wxCommandEvent&);
|
||||
|
||||
void onBrowseFiles(wxCommandEvent&);
|
||||
|
||||
void onOk(wxCommandEvent&);
|
||||
|
||||
};
|
||||
|
||||
enum class CSVState {
|
||||
UnquotedField,
|
||||
QuotedField,
|
||||
QuotedQuote
|
||||
};
|
||||
@@ -10,7 +10,8 @@
|
||||
#include <gui/control/card_list.hpp>
|
||||
#include <gui/control/card_list_column_select.hpp>
|
||||
#include <gui/set/window.hpp> // for sorting all cardlists in a window
|
||||
#include <gui/util.hpp>
|
||||
#include <gui/util.hpp>
|
||||
#include <gui/add_csv_window.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/field.hpp>
|
||||
#include <data/field/choice.hpp>
|
||||
@@ -180,6 +181,15 @@ bool CardListBase::doDelete() {
|
||||
set->actions.addAction(make_unique<AddCardAction>(REMOVE, *set, cards_to_delete));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CardListBase::doAddCSV() {
|
||||
AddCSVWindow wnd(this, set, true);
|
||||
if (wnd.ShowModal() == wxID_OK) {
|
||||
// The actual adding is done in this window's onOk function
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : CardListBase : Building the list
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ public:
|
||||
bool doCopy() override;
|
||||
bool doPaste() override;
|
||||
bool doDelete() override;
|
||||
|
||||
bool doAddCSV();
|
||||
|
||||
// --------------------------------------------------- : Set actions
|
||||
|
||||
void onBeforeChangeSet() override;
|
||||
|
||||
@@ -74,6 +74,7 @@ CardsPanel::CardsPanel(Window* parent, int id)
|
||||
// NOTE: space after "Del" prevents wx from making del an accellerator
|
||||
// otherwise we delete a card when delete is pressed inside the editor
|
||||
// Adding a space never hurts, please keep it just to be safe.
|
||||
add_menu_item(menuCard, ID_CARD_ADD_CSV, "card_add_multiple", _MENU_("add card csv") + _(" "), _HELP_("add card csv"));
|
||||
add_menu_item(menuCard, ID_CARD_REMOVE, "card_del", _MENU_("remove card")+_(" "), _HELP_("remove card"));
|
||||
menuCard->AppendSeparator();
|
||||
auto menuRotate = new wxMenu();
|
||||
@@ -311,6 +312,9 @@ void CardsPanel::onCommand(int id) {
|
||||
case ID_CARD_ADD:
|
||||
set->actions.addAction(make_unique<AddCardAction>(*set));
|
||||
break;
|
||||
case ID_CARD_ADD_CSV:
|
||||
card_list->doAddCSV();
|
||||
break;
|
||||
case ID_CARD_REMOVE:
|
||||
card_list->doDelete();
|
||||
break;
|
||||
|
||||
@@ -682,7 +682,31 @@ SCRIPT_FUNCTION(random_select_many) {
|
||||
ret->value.resize(count);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
SCRIPT_FUNCTION(make_map) {
|
||||
SCRIPT_PARAM(ScriptValueP, keys);
|
||||
SCRIPT_PARAM(ScriptValueP, values);
|
||||
ScriptValueP keys_it = keys->makeIterator();
|
||||
ScriptValueP key;
|
||||
ScriptValueP values_it = values->makeIterator();
|
||||
ScriptValueP value;
|
||||
ScriptCustomCollectionP map = make_intrusive<ScriptCustomCollection>();
|
||||
while (key = keys_it->next()) {
|
||||
if (key == script_nil) continue;
|
||||
if (value = values_it->next()) {
|
||||
map->key_value[key->toString()] = value;
|
||||
}
|
||||
else {
|
||||
queue_message(MESSAGE_WARNING, "More keys than values given in function make_map!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (value = values_it->next()) {
|
||||
queue_message(MESSAGE_WARNING, "More values than keys given in function make_map!");
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
SCRIPT_FUNCTION(get_card_styling) {
|
||||
SCRIPT_PARAM_C(ScriptValueP, input);
|
||||
@@ -795,6 +819,7 @@ void init_script_basic_functions(Context& ctx) {
|
||||
ctx.setVariable(_("to_date"), script_to_date);
|
||||
ctx.setVariable(_("to_code"), script_to_code);
|
||||
ctx.setVariable(_("type_name"), script_type_name);
|
||||
ctx.setVariable(_("make_map"), script_make_map);
|
||||
ctx.setVariable(_("get_card_styling"), script_get_card_styling);
|
||||
ctx.setVariable(_("get_card_stylesheet"), script_get_card_stylesheet);
|
||||
// math
|
||||
|
||||
@@ -15,44 +15,141 @@
|
||||
#include <data/field/package_choice.hpp>
|
||||
#include <data/field/color.hpp>
|
||||
#include <data/field/image.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/stylesheet.hpp>
|
||||
#include <data/card.hpp>
|
||||
#include <util/error.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : new_card
|
||||
// ----------------------------------------------------------------------------- : Helper functions
|
||||
|
||||
static Value* get_container(GameP& game, CardP& card, String key_name) {
|
||||
// find value container to update
|
||||
IndexMap<FieldP, ValueP>::const_iterator value_it = card->data.find(key_name);
|
||||
if (value_it == card->data.end()) {
|
||||
// look among alternate names
|
||||
map<String, String>::iterator alt_name_it = game->card_fields_alt_names.find(unified_form(key_name));
|
||||
if (alt_name_it != game->card_fields_alt_names.end()) {
|
||||
value_it = card->data.find(alt_name_it->second);
|
||||
}
|
||||
}
|
||||
if (value_it == card->data.end()) {
|
||||
throw ScriptError(_ERROR_1_("no field with name", key_name));
|
||||
}
|
||||
return value_it->get();
|
||||
}
|
||||
|
||||
static void set_container(Value* container, ScriptValueP& value, String key_name) {
|
||||
// set the given value into the container
|
||||
if (TextValue* tvalue = dynamic_cast<TextValue*>(container)) {
|
||||
tvalue->value = value->toString();
|
||||
}
|
||||
else if (ChoiceValue* cvalue = dynamic_cast<ChoiceValue*>(container)) {
|
||||
cvalue->value = value->toString();
|
||||
}
|
||||
else if (PackageChoiceValue* pvalue = dynamic_cast<PackageChoiceValue*>(container)) {
|
||||
pvalue->package_name = value->toString();
|
||||
}
|
||||
else if (ColorValue* cvalue = dynamic_cast<ColorValue*>(container)) {
|
||||
cvalue->value = value->toColor();
|
||||
}
|
||||
else if (ImageValue* ivalue = dynamic_cast<ImageValue*>(container)) {
|
||||
wxFileName fname(static_cast<ExternalImage*>(value.get())->toString());
|
||||
ivalue->filename = LocalFileName::fromReadString(fname.GetName(), "");
|
||||
}
|
||||
else {
|
||||
throw ScriptError(_ERROR_1_("can't set value", key_name));
|
||||
}
|
||||
}
|
||||
|
||||
static bool set_builtin_container(GameP& game, CardP& card, ScriptValueP& value, String key_name) {
|
||||
// check if the given value is for a built-in field, if found set it and return true
|
||||
key_name = unified_form(key_name);
|
||||
if (key_name == _("notes")) {
|
||||
card->notes = value->toString();
|
||||
return true;
|
||||
} else if (key_name == _("style") || key_name == _("stylesheet") || key_name == _("template")) {
|
||||
if (trim(value->toString()) != wxEmptyString) card->stylesheet = StyleSheet::byGameAndName(*game, value->toString());
|
||||
return true;
|
||||
}
|
||||
// else if (key_name == _("id") || key_name == _("multiverse_id")) {
|
||||
// card->id = value->toString();
|
||||
// return true;
|
||||
//}
|
||||
|
||||
//styling_data;
|
||||
//linked_card;
|
||||
//linked_relation_1;
|
||||
return false;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : new_card
|
||||
|
||||
SCRIPT_FUNCTION(new_card) {
|
||||
SCRIPT_PARAM(GameP, game);
|
||||
// create a new card object
|
||||
CardP new_card = make_intrusive<Card>(*game);
|
||||
// set field values
|
||||
// iterate on the given key/value pairs
|
||||
SCRIPT_PARAM(ScriptValueP, input);
|
||||
ScriptValueP it = input->makeIterator();
|
||||
ScriptValueP key;
|
||||
while (ScriptValueP v = it->next(&key)) {
|
||||
assert(key);
|
||||
String name = key->toString();
|
||||
// find value to update
|
||||
IndexMap<FieldP,ValueP>::const_iterator value_it = new_card->data.find(name);
|
||||
if (value_it == new_card->data.end()) {
|
||||
throw ScriptError(_ERROR_1_("no field with name", name));
|
||||
ScriptValueP key;
|
||||
while (ScriptValueP value = it->next(&key)) {
|
||||
assert(key);
|
||||
String key_name = key->toString();
|
||||
// check if the given value is for a built-in field
|
||||
if (set_builtin_container(game, new_card, value, key_name)) continue;
|
||||
// find the field value (container) that corresponds to the given value
|
||||
Value* container = get_container(game, new_card, key_name);
|
||||
FieldP field = container->fieldP;
|
||||
// if the field has a construction script, set the value and card context variables to be the given value and this card, run script
|
||||
if (field->construction_script) {
|
||||
ScriptValueP ctx_value = ctx.getVariableOpt(SCRIPT_VAR_value);
|
||||
ScriptValueP ctx_card = ctx.getVariableOpt(SCRIPT_VAR_card);
|
||||
ctx.setVariable(SCRIPT_VAR_value, value);
|
||||
ctx.setVariable(SCRIPT_VAR_card, to_script(new_card));
|
||||
ScriptValueP script_input = field->construction_script.invoke(ctx, true);
|
||||
// iterate on the key/value pairs given by the script
|
||||
ScriptValueP script_it = script_input->makeIterator();
|
||||
ScriptValueP script_key;
|
||||
while (ScriptValueP script_value = script_it->next(&script_key)) {
|
||||
assert(script_key);
|
||||
String script_key_name = script_key->toString();
|
||||
// check if the script value is for a built-in field
|
||||
if (set_builtin_container(game, new_card, script_value, script_key_name)) continue;
|
||||
// find the field value that corresponds to the script value
|
||||
Value* script_container = get_container(game, new_card, script_key_name);
|
||||
// set the field value to the script value
|
||||
set_container(script_container, script_value, script_key_name);
|
||||
}
|
||||
// restore old value and card context variables
|
||||
if (ctx_value) ctx.setVariable(SCRIPT_VAR_value, ctx_value);
|
||||
if (ctx_card) ctx.setVariable(SCRIPT_VAR_card, ctx_card);
|
||||
}
|
||||
Value* value = value_it->get();
|
||||
// set the value
|
||||
if (TextValue* tvalue = dynamic_cast<TextValue*>(value)) {
|
||||
tvalue->value = v->toString();
|
||||
} else if (ChoiceValue* cvalue = dynamic_cast<ChoiceValue*>(value)) {
|
||||
cvalue->value = v->toString();
|
||||
} else if (PackageChoiceValue* pvalue = dynamic_cast<PackageChoiceValue*>(value)) {
|
||||
pvalue->package_name = v->toString();
|
||||
} else if (ColorValue* cvalue = dynamic_cast<ColorValue*>(value)) {
|
||||
cvalue->value = v->toColor();
|
||||
} else if (ImageValue* ivalue = dynamic_cast<ImageValue*>(value)) {
|
||||
wxFileName fname( static_cast<ExternalImage*>(v.get())->toString() );
|
||||
ivalue->filename = LocalFileName::fromReadString( fname.GetName(), "");
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("can't set value", name));
|
||||
}
|
||||
}
|
||||
// if the field has no construction script, simply set the field value to the given value
|
||||
else {
|
||||
set_container(container, value, key_name);
|
||||
}
|
||||
}
|
||||
// if the game has a construction script, set the card context variable to be this card, run script
|
||||
if (game->construction_script) {
|
||||
ScriptValueP ctx_card = ctx.getVariableOpt(SCRIPT_VAR_card);
|
||||
ctx.setVariable(SCRIPT_VAR_card, to_script(new_card));
|
||||
ScriptValueP script_input = game->construction_script.invoke(ctx, true);
|
||||
// iterate on the key/value pairs given by the script
|
||||
ScriptValueP script_it = script_input->makeIterator();
|
||||
ScriptValueP script_key;
|
||||
while (ScriptValueP script_value = script_it->next(&script_key)) {
|
||||
assert(script_key);
|
||||
String script_key_name = script_key->toString();
|
||||
// check if the script value is for a built-in field
|
||||
if (set_builtin_container(game, new_card, script_value, script_key_name)) continue;
|
||||
// find the field value that corresponds to the script value
|
||||
Value* script_container = get_container(game, new_card, script_key_name);
|
||||
// set the field value to the script value
|
||||
set_container(script_container, script_value, script_key_name);
|
||||
}
|
||||
// restore old context card
|
||||
if (ctx_card) ctx.setVariable(SCRIPT_VAR_card, ctx_card);
|
||||
}
|
||||
SCRIPT_RETURN(new_card);
|
||||
}
|
||||
|
||||
|
||||
@@ -137,6 +137,15 @@ void uncanonical_name_form_in_place(String& str) {
|
||||
}
|
||||
}
|
||||
|
||||
String unified_form(String& str) {
|
||||
str = trim(str);
|
||||
for (String::iterator it = str.begin(); it != str.end(); ++it) {
|
||||
if (*it == ' ') *it = '_';
|
||||
else *it = toLower(*it);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
String name_to_caption(const String& str) {
|
||||
String ret;
|
||||
ret.reserve(str.size());
|
||||
|
||||
+4
-1
@@ -241,7 +241,10 @@ void uncanonical_name_form_in_place(String&);
|
||||
inline String uncanonical_name_form(String s) {
|
||||
uncanonical_name_form_in_place(s);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a field name to canonical form, then to lower case, then trim it
|
||||
String unified_form(String&);
|
||||
|
||||
/// Convert a field name to a string that can be shown to the user
|
||||
String name_to_caption(const String&);
|
||||
|
||||
@@ -108,6 +108,9 @@ enum ChildMenuID {
|
||||
ID_CARD_ROTATE_270,
|
||||
// CardList
|
||||
ID_SELECT_COLUMNS,
|
||||
ID_CARD_ADD_CSV,
|
||||
ID_CARD_ADD_CSV_SEP,
|
||||
ID_CARD_ADD_CSV_BROWSE,
|
||||
|
||||
// Keyword menu
|
||||
ID_KEYWORD_ADD = 6101,
|
||||
|
||||
Reference in New Issue
Block a user