/* core/application.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include #include #include #include #include #include "math/mathlib.h" #include "sys/sys.h" #include "filesystem/filesystem.h" #include "core/application.h" #include "core/core.h" #include "core/entity.h" #include "core/func.h" #include "core/cvar.h" #include "core/netserver.h" namespace core { Cvar sv_dedicated; Cvar sv_private; Cvar net_host; Cvar net_port; // return the global application object Application *application() { return Application::instance(); } // --------------- engine functions ------------------------------ void func_print(std::stringstream &args) { char text[MAXCMDSIZE]; if (args.getline(text, MAXCMDSIZE)) { // remove the leading space if (text[0] && text[1]) { con_print << text+1 << std::endl; } } } void func_help(std::stringstream &args) { con_print << "This is the help function" << std::endl; } void func_quit(std::stringstream &args) { if (Application::instance()) { Application::instance()->shutdown(); Application::instance()->quit(0); } } void func_connect(std::stringstream &args) { std::string host; if (!(args >> host)) host.clear(); if (Application::instance()) { Application::instance()->connect(host); } } void func_disconnect(std::stringstream &args) { if (Application::instance()) Application::instance()->disconnect(); } void func_list_func(std::stringstream &args) { func::list(); } void func_list_var(std::stringstream &args) { cvar::list(); } void func_list_ent(std::stringstream &args) { entity::list(); } void func_say(std::stringstream &args) { if (application()->netserver) { std::ostringstream osstream; osstream << "msg public " << localplayer.name << " " << args.str() << "\n"; application()->netserver->broadcast(osstream.str()); con_print << localplayer.name << ": " << args.str() << std::endl; } else if (application()->netconnection.connected()) { std::ostringstream osstream; osstream << args.str() << std::endl; application()->netconnection.send(osstream.str()); } else if (game() && game()->connected) { con_print << args.str() << std::endl; } else con_print << "Not connected."; } void func_name(std::stringstream &args) { std::string name; if (args >> name) { if (name.size() > 16) name = name.substr(0,16); } else { con_print << "name " << localplayer.name << std::endl; return; } if (name == localplayer.name) { con_print << "name " << name << std::endl; return; } if (application()->netserver) { std::ostringstream osstream; osstream << "msg info " << localplayer.name << " renamed to " << name << "\n"; application()->netserver->broadcast(osstream.str()); con_print << "msg info " << localplayer.name << " renamed to " << name << std::endl; } else if (application()->netconnection.connected()) { std::ostringstream osstream; osstream << args.str() << std::endl; application()->netconnection.send(osstream.str()); } else { con_print << "name " << name << std::endl; } localplayer.name = name; } // --------------- signal_handler ----------------------------------- extern "C" void signal_handler(int signum) { switch (signum) { case SIGHUP: case SIGINT: case SIGQUIT: case SIGTERM: if (Application::instance()) { con_warn << "Received signal " << signum << ", shutting down..." << std::endl; Application::instance()->shutdown(); Application::instance()->quit(0); } else { std::cerr << "Received signal " << signum << ", terminated..." << std::endl; sys::quit(1); } break; default: std::cerr << "Received signal " << signum << ", terminated..." << std::endl; sys::quit(1); break; } } // --------------- Application ----------------------------- Application *Application::application_instance = 0; Application::Application() { if (application_instance) { std::cerr << "multiple core::Application instances!" << std::endl; sys::quit(2); } application_instance = this; netserver = 0; gameinterface_preload = 0; sys::signal(SIGHUP, signal_handler); sys::signal(SIGINT, signal_handler); sys::signal(SIGQUIT, signal_handler); sys::signal(SIGTERM, signal_handler); } Application::~Application() { application_instance = 0; } Application *Application::instance() { return application_instance; } float Application::time() const { return game_time; } bool Application::connected() const { return (Application::instance()->netconnection.connected() || (GameInterface::instance() && GameInterface::instance()->connected)); } void Application::init() { con_debug << "Debug messages enabled" << std::endl; con_print << "Initializing core..." << std::endl; filesystem::init(); gameinterface_preload = core::GameInterface::gameinterface_instance; core::GameInterface::gameinterface_instance = 0; if (gameinterface_preload) { con_print << " preloaded game found: " << gameinterface_preload->name() << std::endl; } game_time = 0; // dedicated server, client should have set this to 0 core::sv_dedicated = core::cvar::get("sv_dedicated", "1", core::cvar::ReadOnly); // private server for the client, server should have set this to 0 core::sv_private = core::cvar::get("sv_private", "0"); if (sv_dedicated->value()) localplayer.name = "Console"; else localplayer.name = "Player0"; // network settings core::net_host = core::cvar::get("net_host", "0.0.0.0"); core::net_port = core::cvar::get("net_port", "8042"); // register our functions func::add("print", func_print); func::add("help", func_help); func::add("quit", func_quit); func::add("connect", func_connect); func::add("disconnect", func_disconnect); func::add("list_var", func_list_var); func::add("list_func", func_list_func); func::add("list_ent", func_list_ent); func::add("say", func_say); func::add("name", func_name); } void Application::shutdown() { con_print << "Shutting down core..." << std::endl; if (game() && game()->connected) disconnect(); if (netserver) { delete netserver; netserver = 0; } filesystem::shutdown(); } void Application::quit(int status) { sys::quit(status); } void Application::connect(std::string const &host) { if (game() && game()->connected || netconnection.connected()) { con_warn << "Connected. Disconnect first." << std::endl; return; } if (game()) { // unload previous game if (game() != gameinterface_preload) delete core::GameInterface::gameinterface_instance; } if (host.size()) { // connect to remote core core::GameInterface::gameinterface_instance = 0; std::string remotehost(host); size_t found = remotehost.find(':'); unsigned int port = 8042; if (found != std::string::npos) { std::istringstream str(remotehost.substr(found+1)); if (str >> port) { remotehost.erase(found, std::string::npos); } else { con_print << "Invalid hostname '" << remotehost << "'" << std::endl; return; } } netconnection.connect(remotehost, port); if (netconnection.connected()) { con_print << "Connected." << std::endl; } else { netconnection.disconnect(); con_warn << "Could not connect to '" << host << "'" << std::endl; } } else { // use preloaded game core::GameInterface::gameinterface_instance = gameinterface_preload; // reset everything entity::clear(); game_time = 0; if (game()->connected = game()->init()) { con_print << "Connected." << std::endl; } else { con_warn << "Could not connect." << std::endl; return; } if (!netserver && (core::sv_dedicated->value() || core::sv_private->value())) { netserver = new NetServer(net_host->text(), (unsigned int)net_port->value()); if (!netserver->valid()) { delete netserver; if (core::sv_dedicated->value()) shutdown(); } } } } void Application::disconnect() { if (netserver) { delete netserver; netserver = 0; } if (netconnection.connected()) { netconnection.disconnect(); con_print << "Disconnected." << std::endl; return; } if (game()) { if (!game()->connected) { con_warn << "Not connected." << std::endl; return; } game()->shutdown(); game()->connected = false; game_time = 0; // remove all entities entity::clear(); // remove all game functions for (std::map::iterator it = func::registry.begin(); it != func::registry.end(); it++) { if ( ((*it).second->flags() & func::Game) == func::Game) { delete (*it).second; func::registry.erase(it); } } // remove all game cvars for (std::map::iterator it = cvar::registry.begin(); it != cvar::registry.end(); it++) { if ( ((*it).second->flags() & cvar::Game) == cvar::Game) { delete (*it).second; cvar::registry.erase(it); } } con_print << "Disconnected." << std::endl; } } void Application::frame(float seconds) { if (seconds == 0.0f) return; if (netconnection.connected()) { netconnection.frame(seconds); // TODO this should come from server game_time += seconds; } else { if (netserver) { // TODO limit netserver frames in local mode netserver->frame(seconds); } if (game() && game()->connected) { entity::frame(seconds); game_time += seconds; game()->frame(seconds); } } // execute commands in the buffer commandbuffer::execute(); } }