Skip to content

Commit

Permalink
take cmdline arg for lua scripts to execute at application start
Browse files Browse the repository at this point in the history
If a lua script is passed as an argument to wesnoth, wesnoth will
actually instantiate the application_lua_kernel defined in the
previous commits, initialize it, and run the script in that
environment.
  • Loading branch information
cbeck88 committed Nov 10, 2014
1 parent ecc714e commit 4164a24
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/commandline_options.cpp
Expand Up @@ -185,6 +185,7 @@ commandline_options::commandline_options (const std::vector<std::string>& args)
("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")
("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.")
("password", po::value<std::string>(), "uses <password> when connecting to a server, ignoring other preferences.")
Expand Down Expand Up @@ -430,6 +431,8 @@ commandline_options::commandline_options (const std::vector<std::string>& args)
screenshot_map_file = vm["screenshot"].as<two_strings>().get<0>();
screenshot_output_file = vm["screenshot"].as<two_strings>().get<1>();
}
if (vm.count("script"))
script_file = vm["script"].as<std::string>();
if (vm.count("server"))
server = vm["server"].as<std::string>();
if (vm.count("username"))
Expand Down
2 changes: 2 additions & 0 deletions src/commandline_options.hpp
Expand Up @@ -188,6 +188,8 @@ friend std::ostream& operator<<(std::ostream &os, const commandline_options& cmd
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.
boost::optional<std::string> script_file;
/// 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.
bool strict_validation;
/// Non-empty if --test was given on the command line. Goes directly into test mode, into a scenario, if specified.
Expand Down
31 changes: 31 additions & 0 deletions src/game_launcher.cpp
Expand Up @@ -50,6 +50,7 @@
#include "preferences_display.hpp" // for detect_video_settings, etc
#include "resources.hpp" // for config_manager
#include "savegame.hpp" // for clean_saves, etc
#include "scripting/application_lua_kernel.hpp"
#include "sdl/utils.hpp" // for surface
#include "serialization/compression.hpp" // for format::NONE
#include "serialization/string_utils.hpp" // for split
Expand Down Expand Up @@ -443,6 +444,36 @@ bool game_launcher::init_video()
return true;
}

bool game_launcher::init_lua_script()
{
// start the application lua kernel, register it in resources, and load script file, if script file is present
if (cmdline_opts_.script_file)
{
filesystem::scoped_istream sf = filesystem::istream_file(*cmdline_opts_.script_file);

if (!sf->fail()) {
/* Cancel all "jumps" to editor / campaign / multiplayer */
jump_to_multiplayer_ = false;
jump_to_editor_ = false;
jump_to_campaign_.jump_ = false;

std::string full_script((std::istreambuf_iterator<char>(*sf)), std::istreambuf_iterator<char>());

std::cerr << "\nRunning lua script: " << *cmdline_opts_.script_file << std::endl;

resources::app_lua_kernel = new application_lua_kernel();
resources::app_lua_kernel->initialize(this);

resources::app_lua_kernel->run(full_script.c_str());

return true;
} else {
std::cerr << "Scripting disabled, encountered failure when opening " << *cmdline_opts_.script_file << std::endl;
}
}
return false;
}

bool game_launcher::play_test()
{
static bool first_time = true;
Expand Down
1 change: 1 addition & 0 deletions src/game_launcher.hpp
Expand Up @@ -62,6 +62,7 @@ class game_launcher
bool init_video();
bool init_language();
bool init_joystick();
bool init_lua_script();

bool play_test();
bool play_screenshot_mode();
Expand Down
1 change: 1 addition & 0 deletions src/resources.cpp
Expand Up @@ -23,6 +23,7 @@ namespace resources
game_data *gamedata = NULL;
filter_context *filter_con = NULL;
LuaKernel *lua_kernel = NULL;
application_lua_kernel *app_lua_kernel = NULL;
persist_manager *persist = NULL;
game_display *screen = NULL;
soundsource::manager *soundsources = NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/resources.hpp
Expand Up @@ -25,6 +25,7 @@ class gamemap;
class game_data;
class filter_context;
class LuaKernel;
class application_lua_kernel;
class play_controller;
class team;
class fake_unit_manager;
Expand All @@ -50,6 +51,7 @@ namespace resources
extern game_board *gameboard;
extern game_data *gamedata;
extern LuaKernel *lua_kernel; // Set by game_events::manager.
extern application_lua_kernel *app_lua_kernel;
extern persist_manager *persist;
extern game_classification *classification;
extern game_display *screen;
Expand Down
19 changes: 19 additions & 0 deletions src/wesnoth.cpp
Expand Up @@ -508,6 +508,23 @@ static void warn_early_init_failure()
<< "in the command line with the --data-dir switch or as the only argument.\n";
}

/**
* 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 bool first_time = true;

if (!first_time) return;

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;
}
}

/**
* Setups the game environment and enters
* the titlescreen or game loops.
Expand Down Expand Up @@ -625,6 +642,8 @@ static int do_gameloop(const std::vector<std::string>& args)

loadscreen_manager.reset();

handle_lua_script_args(&*game,cmdline_opts);

if(cmdline_opts.unit_test) {
if(cmdline_opts.timeout) {
std::cerr << "The wesnoth built-in timeout feature has been removed.\n" << std::endl;
Expand Down

0 comments on commit 4164a24

Please sign in to comment.