/* 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 #include "sys/sys.h" #include "math/mathlib.h" #include "filesystem/filesystem.h" #include "core/application.h" #include "core/cvar.h" #include "core/entity.h" #include "core/func.h" #include "core/gameconnection.h" #include "core/gameserver.h" namespace core { // --------------- engine functions ------------------------------ void func_print(std::string const &args) { con_print << args << "\n"; } void func_help(std::string const &args) { con_print << "This is the help function\n"; } void func_quit(std::string const &args) { application()->shutdown(); application()->quit(0); } void func_connect(std::string const &args) { std::istringstream argstream(args); std::string host; if (!(argstream >> host)) host.clear(); application()->connect(host); } void func_disconnect(std::string const &args) { application()->disconnect(); } // --------------- signal_handler ----------------------------------- extern "C" void signal_handler(int signum) { #ifndef _WIN32 switch (signum) { case SIGHUP: case SIGINT: case SIGQUIT: case SIGTERM: if (Application::instance()) { con_warn << "Received signal " << signum << ", shutting down...\n"; application()->shutdown(); application()->quit(0); } else { std::cerr << "Received signal " << signum << ", terminated...\n"; application()->quit(1); } break; default: std::cerr << "Received signal " << signum << ", terminated...\n"; application()->quit(1); break; } #endif } // --------------- Application ----------------------------- Application *Application::application_instance = 0; Application::Application() { if (application_instance) { std::cerr << "multiple core::Application instances!\n"; sys::quit(2); } application_instance = this; application_time = 0; application_game = 0; #ifndef _WIN32 sys::signal(SIGHUP, signal_handler); sys::signal(SIGINT, signal_handler); sys::signal(SIGQUIT, signal_handler); sys::signal(SIGTERM, signal_handler); #endif } Application::~Application() { application_instance = 0; } void Application::init() { con_debug << "Debug messages enabled\n"; con_print << "Initializing core...\n"; filesystem::init(); CommandBuffer::init(); // dedicated server has set this to 1 Cvar::sv_dedicated = Cvar::get("sv_dedicated", "0", Cvar::ReadOnly); Cvar::sv_dedicated->set_info("[bool] indicates this is a dedicated server"); // client can set this to 1 Cvar::sv_private = Cvar::get("sv_private", "0"); Cvar::sv_private->set_info("[bool] indicates the client runs a networked server"); // load save cvars load_config(); // framerate settings Cvar::sv_framerate = Cvar::get("sv_framerate", "25"); Cvar::sv_framerate->set_info("[int] server framerate in frames/sec"); // network settings Cvar::net_host = Cvar::get("net_host", "0.0.0.0", Cvar::Archive); Cvar::net_host->set_info("[ip] IP address the network server binds to"); Cvar::net_port = Cvar::get("net_port", "8042", Cvar::Archive); Cvar::net_port->set_info("[int] default network port"); Cvar::net_maxclients = Cvar::get("net_maxclients", "16", Cvar::Archive); Cvar::net_maxclients->set_info("[int] maximum number of network clients"); Cvar::net_timeout = Cvar::get("net_timeout", "20", Cvar::Archive); Cvar::net_timeout->set_info("[int] network timeout in seconds"); #ifdef _WIN32 // Initialize win32 socket library WSADATA wsa_data; WORD wsa_version = MAKEWORD(2, 0); if (WSAStartup(wsa_version, &wsa_data) != 0 ) { con_warn << "Could not initialize socket library!" << std::endl; } #endif // register our engine functions Func *func = 0; func = Func::add("print", func_print); func->set_info("[str] print a message on the console"); func = Func::add("help", func_help); func->set_info("dummy help function"); func = Func::add("quit", func_quit); func->set_info("exit the application"); func = Func::add("connect", func_connect); func->set_info("[ip] without ip, create a game"); func = Func::add("disconnect", func_disconnect); func->set_info("leave the current game"); } void Application::shutdown() { con_print << "Shutting down core...\n"; if (connected()) disconnect(); if (application_game) { delete application_game; application_game = 0; } save_config(); // remove our engine functions Func::remove("print"); Func::remove("help"); Func::remove("quit"); Func::remove("connect"); Func::remove("disconnect"); #ifdef _WIN32 // shutdown win32 socket library WSACleanup(); #endif CommandBuffer::shutdown(); filesystem::shutdown(); } void Application::quit(int status) { sys::quit(status); } void Application::connect(std::string const &host) { if (connected()) { con_warn << "Connected. Disconnect first.\n"; return; } if (application_game) { delete application_game; application_game = 0; } if (host.size()) { application_game = new GameConnection(host); if (!application_game->running()) { delete application_game; application_game = 0; } } else { application_game = new GameServer(); if (application_game->running()) { con_print << "Connected to local game.\n"; } else { delete application_game; application_game = 0; con_warn << "Could not connect to local game!\n"; } } } void Application::disconnect() { if(application_game) { delete application_game; application_game = 0; con_print << "Disconnected.\n"; } } void Application::frame(float seconds) { // execute commands in the buffer CommandBuffer::exec(); application_time += seconds; // don't run zero lenght time frames if (seconds == 0.0f) return; if (!connected()) return; // run a game interface frame application_game->frame(seconds); if (!application_game->running()) disconnect(); } void Application::save_config() { std::string filename(filesystem::writedir); if (!Cvar::sv_dedicated->value()) filename.append("client.cfg"); else filename.append("server.cfg"); std::ofstream ofs(filename.c_str()); if (!ofs.is_open()) { con_warn << "Could not write " << filename << std::endl; return; } std::map::iterator it; for (it = Cvar::registry.begin(); it != Cvar::registry.end(); it++) { if (((*it).second->flags() & Cvar::Archive) == Cvar::Archive) ofs << "set " << (*it).first << " " << (*it).second->str() << std::endl; } ofs.close(); } void Application::load_config() { std::string filename(filesystem::writedir); if (!Cvar::sv_dedicated->value()) filename.append("client.cfg"); else filename.append("server.cfg"); std::ifstream ifs(filename.c_str(), std::ifstream::in); if (!ifs.is_open()) { con_warn << "Could not read " << filename << std::endl; return; } char line[MAXCMDSIZE]; while (ifs.getline(line, MAXCMDSIZE-1)) { cmd() << line << "\n"; } // execute commands in the buffer CommandBuffer::exec(); } }