add --plugin option at command line (to replace script eventually)

This is still rough and will need to be cleaned up later.
This commit is contained in:
Chris Beck 2014-11-28 20:39:49 -05:00
parent c485edf7c6
commit f8f1979846
4 changed files with 73 additions and 9 deletions

View File

@ -183,10 +183,11 @@ commandline_options::commandline_options (const std::vector<std::string>& args)
("nomusic", "runs the game without music.")
("nosound", "runs the game without sounds and music.")
("path", "prints the path to the data directory and exits.")
("plugin", po::value<std::string>(), "(experimental) load a script which defines a wesnoth plugin. similar to --script below, but lua file should return a function which will be run as a coroutine and periodically woken up with updates.")
("render-image", po::value<two_strings>()->multitoken(), "takes two arguments: <image> <output>. Like screenshot, but instead of a map, takes a valid wesnoth 'image path string' with image path functions, and outputs to a windows .bmp file")
("rng-seed", po::value<unsigned int>(), "seeds the random number generator with number <arg>. Example: --rng-seed 0")
("screenshot", po::value<two_strings>()->multitoken(), "takes two arguments: <map> <output>. Saves a screenshot of <map> to <output> without initializing a screen. Editor must be compiled in for this to work.")
("script", po::value<std::string>(), "file containing a lua script to control the client")
("script", po::value<std::string>(), "(experimental) file containing a lua script to control the client")
("unsafe-scripts", "makes the \'package\' package available to lua scripts, so that they can load arbitrary packages. Do not do this with untrusted scripts! This action gives lua the same permissions as the wesnoth executable.")
("server,s", po::value<std::string>()->implicit_value(std::string()), "connects to the host <arg> if specified or to the first host in your preferences.")
("username", po::value<std::string>(), "uses <username> when connecting to a server, ignoring other preferences.")
@ -437,6 +438,8 @@ commandline_options::commandline_options (const std::vector<std::string>& args)
script_file = vm["script"].as<std::string>();
if (vm.count("unsafe-scripts"))
script_unsafe_mode = true;
if (vm.count("plugin"))
plugin_file = vm["plugin"].as<std::string>();
if (vm.count("server"))
server = vm["server"].as<std::string>();
if (vm.count("username"))

View File

@ -188,8 +188,10 @@ public:
boost::optional<std::string> screenshot_map_file;
/// Output file to put screenshot in. Second parameter given after --screenshot.
boost::optional<std::string> screenshot_output_file;
/// File to load lua script (mp-bot) from.
/// File to load lua script from.
boost::optional<std::string> script_file;
/// File to load a lua plugin (similar to a script) from. Experimental / may replace script.
boost::optional<std::string> plugin_file;
/// Whether to load the "package" package for the scripting environment. (This allows to load arbitrary lua packages, and gives untrusted lua the same permissions as wesnoth executable)
bool script_unsafe_mode;
/// True if --strict-validation was given on the command line. Makes Wesnoth trust validation errors as fatal WML errors and create WML exception, if so.

View File

@ -445,6 +445,12 @@ bool game_launcher::init_video()
bool game_launcher::init_lua_script()
{
std::cerr << "checking lua scripts\n";
if (cmdline_opts_.script_unsafe_mode) {
plugins_manager::get()->get_kernel_base()->load_package(); //load the "package" package, so that scripts can get what packages they want
}
// get the application lua kernel, load and execute script file, if script file is present
if (cmdline_opts_.script_file)
{
@ -460,10 +466,6 @@ bool game_launcher::init_lua_script()
std::cerr << "\nRunning lua script: " << *cmdline_opts_.script_file << std::endl;
if (cmdline_opts_.script_unsafe_mode) {
plugins_manager::get()->get_kernel_base()->load_package(); //load the "package" package, so that scripts can get what packages they want
}
plugins_manager::get()->get_kernel_base()->run(full_script.c_str());
return true;
@ -471,6 +473,62 @@ bool game_launcher::init_lua_script()
std::cerr << "Encountered failure when opening script '" << *cmdline_opts_.script_file << "'\n";
}
}
if (cmdline_opts_.plugin_file)
{
std::string filename = *cmdline_opts_.plugin_file;
std::cerr << "Loading a plugin file'" << filename << "'...\n";
filesystem::scoped_istream sf = filesystem::istream_file(filename);
try {
if (sf->fail()) {
throw std::runtime_error("failed to open plugin file");
}
/* Cancel all "jumps" to editor / campaign / multiplayer */
jump_to_multiplayer_ = false;
jump_to_editor_ = false;
jump_to_campaign_.jump_ = false;
std::string full_plugin((std::istreambuf_iterator<char>(*sf)), std::istreambuf_iterator<char>());
plugins_manager & pm = *plugins_manager::get();
size_t i = pm.add_plugin(filename, full_plugin);
for (size_t j = 0 ; j < pm.size(); ++j) {
std::cerr << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j) << std::endl;
}
std::cerr << "Starting a plugin...\n";
pm.start_plugin(i);
for (size_t j = 0 ; j < pm.size(); ++j) {
std::cerr << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j) << std::endl;
}
plugins_context pc("init");
for (size_t repeat = 0; repeat < 5; ++repeat) {
std::cerr << "Playing a slice...\n";
pc.play_slice();
for (size_t j = 0 ; j < pm.size(); ++j) {
std::cerr << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j) << std::endl;
}
}
return true;
} catch (std::exception & e) {
gui2::show_error_message(disp().video(), std::string("When loading a plugin, error:\n") + e.what());
}
}
else {
std::cerr << "no plugin file found\n";
}
return false;
}

View File

@ -524,7 +524,7 @@ static void warn_early_init_failure()
* Handles the lua script command line arguments if present.
* This function will only run once.
*/
static void handle_lua_script_args(game_launcher * game, commandline_options & cmdline_opts)
static void handle_lua_script_args(game_launcher * game, commandline_options & /*cmdline_opts*/)
{
static bool first_time = true;
@ -532,8 +532,9 @@ static void handle_lua_script_args(game_launcher * game, commandline_options & c
first_time = false;
if (cmdline_opts.script_file && !game->init_lua_script()) {
std::cerr << "could not load lua script: " << *cmdline_opts.script_file << std::endl;
if (!game->init_lua_script()) {
std::cerr << "error when loading lua scripts at startup\n";
//std::cerr << "could not load lua script: " << *cmdline_opts.script_file << std::endl;
}
}