client: Launch extensions/__menu if no server address is given on the command line

This commit is contained in:
Perttu Ahola 2014-09-26 08:56:21 +03:00
parent fd09ba59a3
commit b319f4f546
4 changed files with 41 additions and 3 deletions

View File

@ -94,7 +94,7 @@ struct CApp: public App, public magic::Application
void run_script(const ss_ &script)
{
log_v(MODULE, "run_script(): %s", cs(script));
log_v(MODULE, "run_script():\n%s", cs(script));
lua_getfield(L, LUA_GLOBALSINDEX, "__buildat_run_code_in_sandbox");
lua_pushlstring(L, script.c_str(), script.size());
@ -108,6 +108,20 @@ struct CApp: public App, public magic::Application
}
}
bool run_script_no_sandbox(const ss_ &script)
{
log_v(MODULE, "run_script_no_sandbox():\n%s", cs(script));
if(luaL_loadstring(L, script.c_str())){
ss_ error = lua_tocppstring(L, -1);
log_e("%s", cs(error));
lua_pop(L, 1);
return false;
}
error_logging_pcall(L, 0, 0);
return true;
}
void handle_packet(const ss_ &name, const ss_ &data)
{
log_v(MODULE, "handle_packet(): %s", cs(name));
@ -178,6 +192,19 @@ struct CApp: public App, public magic::Application
log_w(MODULE, "luaL_dofile: An error occurred: %s\n",
lua_tostring(L, -1));
lua_pop(L, 1);
throw AppStartupError("Could not initialize Lua environment");
}
if(g_client_config.boot_to_menu){
ss_ script =
"local m = require('buildat/extension/__menu')\n"
"if type(m) ~= 'table' then\n"
" error('Failed to load extension __menu')\n"
"end\n"
"m.boot()\n";
if(!run_script_no_sandbox(script)){
throw AppStartupError("Failed to load and run extension __menu");
}
}
}

View File

@ -12,6 +12,11 @@ namespace client {
namespace app
{
struct AppStartupError: public Exception {
ss_ msg;
AppStartupError(const ss_ &msg): Exception(msg){}
};
struct App
{
virtual ~App(){}
@ -19,6 +24,7 @@ namespace app
virtual int run() = 0;
virtual void shutdown() = 0;
virtual void run_script(const ss_ &script) = 0;
virtual bool run_script_no_sandbox(const ss_ &script) = 0;
virtual void handle_packet(const ss_ &name, const ss_ &data) = 0;
virtual void file_updated_in_cache(const ss_ &file_name,
const ss_ &file_hash, const ss_ &cached_path) = 0;

View File

@ -11,6 +11,7 @@ namespace client
ss_ share_path = "..";
ss_ cache_path = "../cache";
ss_ urho3d_path = "../../Urho3D";
bool boot_to_menu = false;
bool check_paths();
};

View File

@ -104,8 +104,12 @@ int main(int argc, char *argv[])
sp_<client::State> state(client::createState(app0));
app0->set_state(state);
if(config.server_address != ""){
if(!state->connect(config.server_address, "20000"))
return 1;
} else {
config.boot_to_menu = true;
}
return app0->run();
}