mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
@@ -0,0 +1,262 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| 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 <iomanip>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <util/prec.hpp>
|
||||
#include <util/window_id.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <data/card.hpp>
|
||||
#include <data/stylesheet.hpp>
|
||||
#include <data/action/set.hpp>
|
||||
#include <gui/add_json_window.hpp>
|
||||
#include <script/functions/construction_helper.hpp>
|
||||
#include <wx/statline.h>
|
||||
#include <boost/json/src.hpp>
|
||||
#include <boost/json.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : AddJSON
|
||||
|
||||
AddJSONWindow::AddJSONWindow(Window* parent, const SetP& set, bool sizer)
|
||||
: wxDialog(parent, wxID_ANY, _TITLE_("add card json"), 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_JSON_BROWSE, _BUTTON_("browse"));
|
||||
json_type = new wxChoice(this, ID_CARD_ADD_JSON_ARRAY, wxDefaultPosition, wxDefaultSize, 0, nullptr);
|
||||
json_type->Clear();
|
||||
FOR_EACH(type, set->game->json_paths) {
|
||||
int delimiter_pos = type.find("//");
|
||||
json_type->Append(type.substr(0, delimiter_pos).Trim().Trim(false));
|
||||
}
|
||||
json_type->Append(_LABEL_("add card json custom"));
|
||||
json_type->SetSelection(0);
|
||||
card_array_path = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
|
||||
setJSONType();
|
||||
// init sizers
|
||||
if (sizer) {
|
||||
wxSizer* s = new wxBoxSizer(wxVERTICAL);
|
||||
s->Add(new wxStaticText(this, -1, _LABEL_("add card json type")), 0, wxALL, 8);
|
||||
s->Add(json_type, 0, wxEXPAND | (wxALL & ~wxTOP), 8);
|
||||
s->Add(new wxStaticText(this, -1, _LABEL_("add card json path")), 0, wxALL & ~wxTOP, 8);
|
||||
s->Add(card_array_path, 0, wxEXPAND | (wxALL & ~wxTOP), 8);
|
||||
s->Add(new wxStaticText(this, -1, _LABEL_("add card json file")), 0, wxALL, 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);
|
||||
}
|
||||
}
|
||||
|
||||
static ScriptValueP json_to_script(boost::json::value jv) {
|
||||
if (jv == nullptr) return script_nil;
|
||||
else if (jv.is_null()) return script_nil;
|
||||
else if (jv.is_bool()) return to_script(jv.get_bool());
|
||||
else if (jv.is_double()) return to_script(jv.get_double());
|
||||
else if (jv.is_int64()) {
|
||||
int integer = jv.get_int64();
|
||||
return to_script(integer);
|
||||
}
|
||||
else if (jv.is_uint64()) {
|
||||
int integer = jv.get_uint64();
|
||||
return to_script(integer);
|
||||
}
|
||||
else if (jv.is_string()) {
|
||||
std::string stdstring = boost::json::value_to<std::string>(jv);
|
||||
String wxstring(stdstring.c_str(), wxConvUTF8);
|
||||
return to_script(wxstring);
|
||||
}
|
||||
else if (jv.is_array()) {
|
||||
boost::json::array array = jv.get_array();
|
||||
ScriptCustomCollectionP result = make_intrusive<ScriptCustomCollection>();
|
||||
for (int i = 0; i < array.size(); ++i) {
|
||||
boost::json::value jvalue = array[i];
|
||||
ScriptValueP value = json_to_script(jvalue);
|
||||
result->value.push_back(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else if (jv.is_object()) {
|
||||
boost::json::object object = jv.get_object();
|
||||
ScriptCustomCollectionP result = make_intrusive<ScriptCustomCollection>();
|
||||
for (auto it = object.begin(); it != object.end(); ++it) {
|
||||
boost::json::string_view jview = it->key();
|
||||
std::string_view stdview = std::string_view(jview.data(), jview.size());
|
||||
std::string stdstring = { stdview.begin(), stdview.end() };
|
||||
String key(stdstring.c_str(), wxConvUTF8);
|
||||
boost::json::value jvalue = it->value();
|
||||
ScriptValueP value = json_to_script(jvalue);
|
||||
result->key_value[key] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json unknown type"));
|
||||
return script_nil;
|
||||
}
|
||||
}
|
||||
|
||||
void AddJSONWindow::setJSONType() {
|
||||
int sel = json_type->GetSelection();
|
||||
if (sel == json_type->GetCount() - 1) { // Custom type
|
||||
card_array_path->ChangeValue(wxEmptyString);
|
||||
card_array_path->Enable();
|
||||
}
|
||||
else {
|
||||
String selection = json_type->GetString(sel);
|
||||
FOR_EACH(type, set->game->json_paths) {
|
||||
if (type.starts_with(selection)) {
|
||||
int delimiter_pos = type.find("//");
|
||||
card_array_path->ChangeValue(delimiter_pos + 2 < type.Length() ? type.substr(delimiter_pos + 2).Trim().Trim(false) : wxEmptyString);
|
||||
card_array_path->Enable(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AddJSONWindow::onJSONTypeChange(wxCommandEvent&) {
|
||||
setJSONType();
|
||||
}
|
||||
|
||||
void AddJSONWindow::onBrowseFiles(wxCommandEvent&) {
|
||||
wxFileDialog* dlg = new wxFileDialog(this, _TITLE_("add card json file"), settings.default_set_dir, wxEmptyString, _("JSON files|*.json|All files (*.*)|*"), wxFD_OPEN);
|
||||
if (dlg->ShowModal() == wxID_OK) {
|
||||
file_path->SetValue(dlg->GetPath());
|
||||
}
|
||||
}
|
||||
|
||||
bool AddJSONWindow::readJSON(std::ifstream& in, std::vector<String>& headers_out, std::vector<std::vector<ScriptValueP>>& table_out) {
|
||||
// Read file
|
||||
std::string input(std::istreambuf_iterator<char>(in), {});
|
||||
boost::json::value jv;
|
||||
try {
|
||||
jv = boost::json::parse(input);
|
||||
} catch (...) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json failed to parse"));
|
||||
return false;
|
||||
}
|
||||
// Split path into tokens
|
||||
std::string path = card_array_path->GetValue().ToStdString();
|
||||
std::vector<std::string> tokens;
|
||||
size_t pos = 0;
|
||||
std::string token;
|
||||
while ((pos = path.find(":")) != std::string::npos) {
|
||||
token = path.substr(0, pos);
|
||||
tokens.push_back(token);
|
||||
path.erase(0, pos + 1);
|
||||
}
|
||||
tokens.push_back(path);
|
||||
// Navigate path to card array
|
||||
for (int t = 0; t < tokens.size(); ++t) {
|
||||
if (tokens[t] == "") break;
|
||||
if (!jv.is_object()) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json path not valid"));
|
||||
return false;
|
||||
}
|
||||
auto& ov = jv.as_object();
|
||||
jv = ov[tokens[t]];
|
||||
if (jv == nullptr) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json path not valid"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!jv.is_array()) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json path not valid"));
|
||||
return false;
|
||||
}
|
||||
auto& card_array = jv.as_array();
|
||||
size_t count = card_array.size();
|
||||
if (count == 0) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json empty array"));
|
||||
return false;
|
||||
}
|
||||
auto& card = card_array[0];
|
||||
if (!card.is_object()) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json path not valid"));
|
||||
return false;
|
||||
}
|
||||
// Get headers
|
||||
for (int i = 0; i < count; ++i) {
|
||||
auto& card = card_array[i].as_object();
|
||||
for (auto it = card.begin(); it != card.end(); ++it) {
|
||||
boost::json::string_view jview = it->key();
|
||||
std::string_view stdview = std::string_view(jview.data(), jview.size());
|
||||
std::string stdstring = { stdview.begin(), stdview.end() };
|
||||
String key(stdstring.c_str(), wxConvUTF8);
|
||||
if (std::find(headers_out.begin(), headers_out.end(), key) == headers_out.end()) {
|
||||
headers_out.push_back(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Parse cards, add to table
|
||||
for (int i = 0; i < count; ++i) {
|
||||
std::vector<ScriptValueP> row;
|
||||
auto& card = card_array[i].as_object();
|
||||
for (int h = 0; h < headers_out.size(); ++h) {
|
||||
auto& value = card[headers_out[h].ToStdString()];
|
||||
row.push_back(json_to_script(value));
|
||||
}
|
||||
table_out.push_back(row);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AddJSONWindow::onOk(wxCommandEvent&) {
|
||||
/// Perform the import
|
||||
wxBusyCursor wait;
|
||||
// Read the file
|
||||
auto file = std::ifstream(file_path->GetValue().ToStdString());
|
||||
if (file.fail()) {
|
||||
queue_message(MESSAGE_ERROR, _ERROR_("add card json file not found"));
|
||||
EndModal(wxID_ABORT);
|
||||
return;
|
||||
}
|
||||
// Put values into a table
|
||||
std::vector<String> headers;
|
||||
std::vector<std::vector<ScriptValueP>> table;
|
||||
if (!readJSON(file, headers, table)) {
|
||||
EndModal(wxID_ABORT);
|
||||
return;
|
||||
}
|
||||
// Check for missing fields
|
||||
String missing_fields;
|
||||
check_table_headers(set->game, headers, _("JSON"), missing_fields);
|
||||
if (missing_fields.size() > 0) {
|
||||
queue_message(MESSAGE_WARNING, _ERROR_2_("import missing fields", _("JSON"), missing_fields));
|
||||
}
|
||||
// Produce cards from the table
|
||||
vector<CardP> cards;
|
||||
if (!cards_from_table(set, headers, table, true, _("JSON"), cards)) {
|
||||
EndModal(wxID_ABORT);
|
||||
return;
|
||||
}
|
||||
// Add cards 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(AddJSONWindow, wxDialog)
|
||||
EVT_BUTTON(wxID_OK, AddJSONWindow::onOk)
|
||||
EVT_BUTTON(ID_CARD_ADD_JSON_BROWSE, AddJSONWindow::onBrowseFiles)
|
||||
EVT_CHOICE(ID_CARD_ADD_JSON_ARRAY, AddJSONWindow::onJSONTypeChange)
|
||||
END_EVENT_TABLE()
|
||||
Reference in New Issue
Block a user