Commit ccd96bb4 authored by twanvl's avatar twanvl

Added --export command line flag for using export templates, --export-image now export images

parent 57ea6d26
......@@ -34,7 +34,7 @@ CLISetInterface::CLISetInterface(const SetP& set, bool quiet)
setSet(set);
// show welcome logo
if (!quiet) showWelcome();
print_pending_errors();
cli.print_pending_errors();
}
CLISetInterface::~CLISetInterface() {
......@@ -90,7 +90,7 @@ void CLISetInterface::run_interactive() {
String command = cli.getLine();
if (command.empty() && !cli.canGetLine()) break;
handleCommand(command);
print_pending_errors();
cli.print_pending_errors();
cli.flush();
cli.flushRaw();
}
......@@ -272,11 +272,3 @@ void CLISetInterface::handleCommand(const String& command) {
}
}
#endif
void CLISetInterface::print_pending_errors() {
MessageType type;
String msg;
while (get_queued_message(type,msg)) {
cli.show_message(type,msg);
}
}
......@@ -40,7 +40,6 @@ class CLISetInterface : public SetView {
#if USE_SCRIPT_PROFILING
void showProfilingStats(const FunctionProfile& parent, int level = 0);
#endif
void print_pending_errors();
/// our own context, when no set is loaded
Context& getContext();
......
......@@ -55,7 +55,7 @@ void TextIOHandler::init() {
have_console = false;
have_stderr = false;
// Use console mode if one of the cli flags is passed
static const Char* redirect_flags[] = {_("-?"),_("--help"),_("-v"),_("--version"),_("--cli"),_("-c"),_("--export"),_("--create-installer")};
static const Char* redirect_flags[] = {_("-?"),_("--help"),_("-v"),_("--version"),_("--cli"),_("-c"),_("--export"),_("--export-images"),_("--create-installer")};
for (int i = 1 ; i < wxTheApp->argc ; ++i) {
for (int j = 0 ; j < sizeof(redirect_flags)/sizeof(redirect_flags[0]) ; ++j) {
if (String(wxTheApp->argv[i]) == redirect_flags[j]) {
......@@ -152,12 +152,12 @@ void TextIOHandler::flushRaw() {
if (!buffer.empty()) {
#ifdef UNICODE
wxCharBuffer buf = buffer.mb_str(wxConvUTF8);
fputs(buf,stdout);
fputs(buf,stream);
#else
fputs(buffer.c_str(),stdout);
fputs(buffer.c_str(),stream);
#endif
}
fflush(stdout);
fflush(stream);
// clear
buffer.clear();
raw_mode_status = 0;
......@@ -166,7 +166,8 @@ void TextIOHandler::flushRaw() {
// ----------------------------------------------------------------------------- : Errors
void TextIOHandler::show_message(MessageType type, String const& message) {
stream = stdout;
flush();
stream = stderr;
if (type == MESSAGE_WARNING) {
*this << YELLOW << _("WARNING: ") << NORMAL << replace_all(message,_("\n"),_("\n ")) << ENDL;
} else {
......@@ -176,3 +177,17 @@ void TextIOHandler::show_message(MessageType type, String const& message) {
stream = stdout;
if (raw_mode) raw_mode_status = max(raw_mode_status, type == MESSAGE_WARNING ? 1 : 2);
}
void TextIOHandler::print_pending_errors() {
MessageType type;
String msg;
while (get_queued_message(type,msg)) {
if (haveConsole()) {
cli.show_message(type,msg);
cli.flush();
} else {
// no console, use a messagebox instead
wxMessageBox(msg, wxMessageBoxCaptionStr, type == MESSAGE_INFO ? wxICON_INFORMATION : type == MESSAGE_WARNING ? wxICON_WARNING : wxICON_ERROR);
}
}
}
......@@ -38,6 +38,7 @@ class TextIOHandler {
/// Show an error or warning message
void show_message(MessageType type, String const& message);
void print_pending_errors();
/// Enable raw mode
void enableRaw();
......
......@@ -45,6 +45,7 @@ DWORD WINAPI TransferThread(Transfer*);
BOOL WINAPI HandleCtrlEvent(DWORD type);
void CopyFileBuffer(HANDLE output, char* buffer, DWORD size);
void InitEscapeTranslation(HANDLE console);
void FinishEscapeTranslation(HANDLE console);
void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]);
/// The child process
......@@ -54,14 +55,15 @@ PROCESS_INFORMATION child_process_info;
HANDLE in_mine, in_theirs, in_real;
HANDLE out_mine, out_theirs, out_real;
HANDLE err_mine, err_theirs, err_real;
bool need_redirection;
// ----------------------------------------------------------------------------- : Main function
const char* redirect_flags[] = {"-?","--help","-v","--version","--cli","-c","--export","--create-installer"};
const char* redirect_flags[] = {"-?","/?","--help","-v","--version","--cli","-c","--export","--export-images","--create-installer"};
int main(int argc, char** argv) {
// determine whether we need to wrap console i/o
bool need_redirection = false;
need_redirection = false;
for (int i = 1 ; i < argc ; ++i) {
for (int j = 0 ; j < sizeof(redirect_flags)/sizeof(redirect_flags[0]) ; ++j) {
if (strcmp(argv[i],redirect_flags[j]) == 0) {
......@@ -115,6 +117,7 @@ int main(int argc, char** argv) {
out_real = GetStdHandle(STD_OUTPUT_HANDLE);
err_real = GetStdHandle(STD_ERROR_HANDLE);
InitEscapeTranslation(out_real);
InitEscapeTranslation(err_real);
// start threads
Transfer tranfer_in = {in_real, in_mine, false};
......@@ -134,6 +137,7 @@ int main(int argc, char** argv) {
// start the child program
if (!CreateProcess(app_path,command_line,NULL,NULL,TRUE,0,NULL,NULL,&child_startup_info,&child_process_info)) {
fprintf(stderr, "Unable to start child process.\n");
ExitProcess(1);
}
......@@ -146,6 +150,10 @@ int main(int argc, char** argv) {
}
// That's all folks!
if (need_redirection) {
FinishEscapeTranslation(out_real);
FinishEscapeTranslation(err_real);
}
return exit_code;
}
......@@ -168,6 +176,10 @@ BOOL WINAPI HandleCtrlEvent(DWORD type) {
TerminateProcess(child_process_info.hProcess,1);
}
// exit this process
if (need_redirection) {
FinishEscapeTranslation(out_real);
FinishEscapeTranslation(err_real);
}
ExitProcess(exit_code);
return TRUE;
}
......@@ -251,6 +263,12 @@ void InitEscapeTranslation(HANDLE console) {
original_attributes = screen_buffer.wAttributes;
}
/// Cleanup for escape translation: put things back to normal
void FinishEscapeTranslation(HANDLE console) {
CONSOLE_SCREEN_BUFFER_INFO screen_buffer;
SetConsoleTextAttribute(console, original_attributes);
}
/// Perform an escape code
void PerformEscapeCode(HANDLE console, char command, int argc, int argv[]) {
switch (command) {
......
......@@ -28,6 +28,8 @@
#include <wx/txtstrm.h>
#include <wx/socket.h>
ScriptValueP export_set(SetP const& set, vector<CardP> const& cards, ExportTemplateP const& exp, String const& outname);
DECLARE_TYPEOF_COLLECTION(String);
// ----------------------------------------------------------------------------- : Main function/class
......@@ -98,44 +100,47 @@ int MSE::OnRun() {
nag_about_ascii_version();
// interpret command line
if (argc > 1) {
try {
String arg = argv[1];
{
vector<String> args;
for (int i = 1 ; i < argc ; ++i) {
args.push_back(argv[i]);
if (args.back() == _("--color")) args.pop_back(); // ingnore the --color argument, it is handled by cli.init()
}
if (!args.empty()) {
// Find the extension
wxFileName f(arg.Mid(0,arg.find_last_not_of(_("\\/"))+1));
wxFileName f(args[0].Mid(0,args[0].find_last_not_of(_("\\/"))+1));
if (f.GetExt() == _("mse-symbol")) {
// Show the symbol editor
Window* wnd = new SymbolWindow(nullptr, argv[1]);
Window* wnd = new SymbolWindow(nullptr, args[0]);
wnd->Show();
return runGUI();
} else if (f.GetExt() == _("mse-set") || f.GetExt() == _("mse") || f.GetExt() == _("set")) {
// Show the set window
Window* wnd = new SetWindow(nullptr, import_set(argv[1]));
Window* wnd = new SetWindow(nullptr, import_set(args[0]));
wnd->Show();
return runGUI();
} else if (f.GetExt() == _("mse-installer")) {
// Installer; install it
InstallType type = settings.install_type;
if (argc > 2) {
String arg = argv[2];
if (starts_with(argv[2], _("--")) && arg != _("--color")) {
parse_enum(String(argv[2]).substr(2), type);
if (args.size() >= 2) {
if (starts_with(args[1], _("--"))) {
parse_enum(String(args[1]).substr(2), type);
}
}
InstallerP installer = open_package<Installer>(argv[1]);
InstallerP installer = open_package<Installer>(args[0]);
PackagesWindow wnd(nullptr, installer);
wnd.ShowModal();
return EXIT_SUCCESS;
} else if (arg == _("--symbol-editor")) {
} else if (args[0] == _("--symbol-editor")) {
Window* wnd = new SymbolWindow(nullptr);
wnd->Show();
return runGUI();
} else if (arg == _("--create-installer")) {
} else if (args[0] == _("--create-installer")) {
// create an installer
Installer inst;
for (int i = 2 ; i < argc ; ++i) {
if (!starts_with(argv[i],_("--"))) {
inst.addPackage(argv[i]);
FOR_EACH(arg, args) {
if (!starts_with(arg,_("--"))) {
inst.addPackage(arg);
}
}
if (inst.prefered_filename.empty()) {
......@@ -144,43 +149,46 @@ int MSE::OnRun() {
inst.saveAs(inst.prefered_filename, false);
}
return EXIT_SUCCESS;
} else if (arg == _("--help") || arg == _("-?")) {
} else if (args[0] == _("--help") || args[0] == _("-?") || args[0] == _("/?")) {
// command line help
cli << _("Magic Set Editor\n\n");
cli << _("Usage: ") << BRIGHT << argv[0] << NORMAL << _(" [") << PARAM << _("OPTIONS") << NORMAL << _("]");
cli << _("\n\n no options");
cli << _("\n \tStart the MSE user interface showing the welcome window.");
cli << _("\n\n ") << BRIGHT << _("-?") << NORMAL << _(", ")
<< BRIGHT << _("--help") << NORMAL;
<< BRIGHT << _("--help") << NORMAL;
cli << _("\n \tShows this help screen.");
cli << _("\n\n ") << BRIGHT << _("-v") << NORMAL << _(", ")
<< BRIGHT << _("--version") << NORMAL;
<< BRIGHT << _("--version") << NORMAL;
cli << _("\n \tShow version information.");
cli << _("\n\n ") << PARAM << _("FILE") << FILE_EXT << _(".mse-set") << NORMAL << _(", ")
<< PARAM << _("FILE") << FILE_EXT << _(".set") << NORMAL << _(", ")
<< PARAM << _("FILE") << FILE_EXT << _(".mse") << NORMAL;
<< PARAM << _("FILE") << FILE_EXT << _(".set") << NORMAL << _(", ")
<< PARAM << _("FILE") << FILE_EXT << _(".mse") << NORMAL;
cli << _("\n \tLoad the set file in the MSE user interface.");
cli << _("\n\n ") << PARAM << _("FILE") << FILE_EXT << _(".mse-symbol") << NORMAL;
cli << _("\n \tLoad the symbol into the MSE symbol editor.");
cli << _("\n\n ") << PARAM << _("FILE") << FILE_EXT << _(".mse-installer")
<< NORMAL << _(" [") << BRIGHT << _("--local") << NORMAL << _("]");
<< NORMAL << _(" [") << BRIGHT << _("--local") << NORMAL << _("]");
cli << _("\n \tInstall the packages from the installer.");
cli << _("\n \tIf the ") << BRIGHT << _("--local") << NORMAL << _(" flag is passed, install packages for this user only.");
cli << _("\n\n ") << BRIGHT << _("--symbol-editor") << NORMAL;
cli << _("\n \tShow the symbol editor instead of the welcome window.");
cli << _("\n\n ") << BRIGHT << _("--create-installer") << NORMAL << _(" [")
<< PARAM << _("OUTFILE") << FILE_EXT << _(".mse-installer") << NORMAL << _("] [")
<< PARAM << _("PACKAGE") << NORMAL << _(" [") << PARAM << _("PACKAGE") << NORMAL << _(" ...]]");
<< PARAM << _("OUTFILE") << FILE_EXT << _(".mse-installer") << NORMAL << _("] [")
<< PARAM << _("PACKAGE") << NORMAL << _(" [") << PARAM << _("PACKAGE") << NORMAL << _(" ...]]");
cli << _("\n \tCreate an instaler, containing the listed packages.");
cli << _("\n \tIf no output filename is specified, the name of the first package is used.");
cli << _("\n\n ") << BRIGHT << _("--export") << NORMAL << PARAM << _(" FILE") << NORMAL << _(" [") << PARAM << _("IMAGE") << NORMAL << _("]");
cli << _("\n\n ") << BRIGHT << _("--export") << NORMAL << PARAM << _(" TEMPLATE SETFILE ") << NORMAL << _(" [") << PARAM << _("OUTFILE") << NORMAL << _("]");
cli << _("\n \tExport a set using an export template.");
cli << _("\n \tIf no output filename is specified, the result is written to stdout.");
cli << _("\n\n ") << BRIGHT << _("--export-images") << NORMAL << PARAM << _(" SETFILE") << NORMAL << _(" [") << PARAM << _("IMAGE") << NORMAL << _("]");
cli << _("\n \tExport the cards in a set to image files,");
cli << _("\n \tIMAGE is the same format as for 'export all card images'.");
cli << _("\n\n ") << BRIGHT << _("--cli") << NORMAL << _(" [")
<< BRIGHT << _("--quiet") << NORMAL << _("] [")
<< BRIGHT << _("--raw") << NORMAL << _("] [")
<< BRIGHT << _("--script ") << NORMAL << PARAM << _("FILE") << NORMAL << _("] [")
<< PARAM << _("SETFILE") << NORMAL << _("]");
<< BRIGHT << _("--quiet") << NORMAL << _("] [")
<< BRIGHT << _("--raw") << NORMAL << _("] [")
<< BRIGHT << _("--script ") << NORMAL << PARAM << _("FILE") << NORMAL << _("] [")
<< PARAM << _("SETFILE") << NORMAL << _("]");
cli << _("\n \tStart the command line interface for performing commands on the set file.");
cli << _("\n \tUse ") << BRIGHT << _("-q") << NORMAL << _(" or ") << BRIGHT << _("--quiet") << NORMAL << _(" to supress the startup banner and prompts.");
cli << _("\n \tUse ") << BRIGHT << _("--raw") << NORMAL << _(" for raw output mode.");
......@@ -195,20 +203,20 @@ int MSE::OnRun() {
cli << ENDL;
cli.flush();
return EXIT_SUCCESS;
} else if (arg == _("--version") || arg == _("-v")) {
} else if (args[0] == _("--version") || args[0] == _("-v") || args[0] == _("-V")) {
// dump version
cli << _("Magic Set Editor\n");
cli << _("Version ") << app_version.toString() << version_suffix << ENDL;
cli.flush();
return EXIT_SUCCESS;
} else if (arg == _("--cli")) {
} else if (args[0] == _("--cli")) {
// command line interface
SetP set;
vector<String> scripts;
bool quiet = false;
for (int i = 2 ; i < argc ; ++i) {
String arg = argv[i];
wxFileName f(argv[i]);
for (size_t i = 1 ; i < args.size() ; ++i) {
String const& arg = args[i];
wxFileName f(arg);
if (f.GetExt() == _("mse-set") || f.GetExt() == _("mse") || f.GetExt() == _("set")) {
set = import_set(arg);
} else if (arg == _("-q") || arg == _("--quiet") || arg == _("--silent")) {
......@@ -216,13 +224,13 @@ int MSE::OnRun() {
} else if (arg == _("-r") || arg == _("--raw")) {
quiet = true;
cli.enableRaw();
} else if ((arg == _("-s") || arg == _("--script")) && i+1 < argc) {
scripts.push_back(argv[i+1]);
} else if ((arg == _("-s") || arg == _("--script")) && i+1 < args.size()) {
scripts.push_back(args[i+1]);
++i;
} else if (arg == _("--color")) {
// ignore
} else {
handle_error(_("Invalid command line argument: ") + String(argv[i]));
throw Error(_("Invalid command line argument: ") + arg);
}
}
CLISetInterface cli_interface(set,quiet);
......@@ -234,16 +242,15 @@ int MSE::OnRun() {
}
}
return EXIT_SUCCESS;
} else if (arg == _("--export")) {
if (argc <= 2 || (argc <= 3 && starts_with(argv[2],_("--")))) {
handle_error(Error(_("No input file specified for --export")));
return EXIT_FAILURE;
} else if (args[0] == _("--export-images")) {
if (args.size() < 2) {
throw Error(_("No input file specified for --export-images"));
}
SetP set = import_set(argv[2]);
SetP set = import_set(args[1]);
// path
String out = argc >= 3 && !starts_with(argv[3],_("--"))
? argv[3]
: settings.gameSettingsFor(*set->game).images_export_filename;
String out = args.size() >= 3
? args[2]
: settings.gameSettingsFor(*set->game).images_export_filename;
String path = _(".");
size_t pos = out.find_last_of(_("/\\"));
if (pos != String::npos) {
......@@ -255,12 +262,24 @@ int MSE::OnRun() {
// export
export_images(set, set->cards, path, out, CONFLICT_NUMBER_OVERWRITE);
return EXIT_SUCCESS;
} else if (args[0] == _("--export")) {
if (args.size() < 2) {
throw Error(_("No export template specified for --export"));
} else if (args.size() < 3) {
throw Error(_("No input set file specified for --export"));
}
String export_template = args[1];
ExportTemplateP exp = package_manager.open<ExportTemplate>(export_template);
SetP set = import_set(args[2]);
String out = args.size() >= 4 ? args[3] : wxEmptyString;
ScriptValueP result = export_set(set, set->cards, exp, out);
if (out.empty()) {
cli << result->toString();
}
return EXIT_SUCCESS;
} else {
handle_error(_("Invalid command line argument: ") + String(argv[1]));
throw Error(_("Invalid command line argument: ") + args[0]);
}
} catch (const Error& e) {
handle_error(e);
return EXIT_FAILURE;
}
}
......@@ -269,6 +288,7 @@ int MSE::OnRun() {
return runGUI();
} CATCH_ALL_ERRORS(true);
cli.print_pending_errors();
return EXIT_FAILURE;
}
......
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