/* * Copyright (c) 2010-2015 OTClient * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "application.h" #include #include #include #include #include #include #include "asyncdispatcher.h" #include #include #include #include #ifdef FW_NET #include #endif void exitSignalHandler(int sig) { static bool signaled = false; switch(sig) { case SIGTERM: case SIGINT: if(!signaled && !g_app.isStopping() && !g_app.isTerminated()) { signaled = true; g_dispatcher.addEvent(std::bind(&Application::close, &g_app)); } break; } } Application::Application() { m_appName = "application"; m_appCompactName = "app"; m_appVersion = "none"; m_charset = "cp1252"; m_stopping = false; } void Application::init(std::vector& args) { // capture exit signals signal(SIGTERM, exitSignalHandler); signal(SIGINT, exitSignalHandler); #ifdef CRASH_HANDLER installCrashHandler(); #endif // setup locale std::locale::global(std::locale()); // process args encoding g_platform.processArgs(args); g_asyncDispatcher.init(); std::string startupOptions; for(uint i=1;i 0) g_logger.info(stdext::format("Startup options: %s", startupOptions)); m_startupOptions = startupOptions; // initialize configs g_configs.init(); // initialize resources g_resources.init(args[0].c_str()); // initialize lua g_lua.init(); registerLuaFunctions(); } void Application::deinit() { g_lua.callGlobalField("g_app", "onTerminate"); // run modules unload events g_modules.unloadModules(); g_modules.clear(); // release remaining lua object references g_lua.collectGarbage(); // poll remaining events poll(); g_asyncDispatcher.terminate(); // disable dispatcher events g_dispatcher.shutdown(); } void Application::terminate() { #ifdef FW_NET // terminate network Connection::terminate(); #endif // release configs g_configs.terminate(); // release resources g_resources.terminate(); // terminate script environment g_lua.terminate(); m_terminated = true; signal(SIGTERM, SIG_DFL); signal(SIGINT, SIG_DFL); } void Application::poll() { #ifdef FW_NET Connection::poll(); #endif g_dispatcher.poll(); // poll connection again to flush pending write #ifdef FW_NET Connection::poll(); #endif } void Application::exit() { g_lua.callGlobalField("g_app", "onExit"); m_stopping = true; } void Application::close() { if(!g_lua.callGlobalField("g_app", "onClose")) exit(); } std::string Application::getOs() { #if defined(WIN32) return "windows"; #elif defined(__APPLE__) return "mac"; #elif __linux return "linux"; #else return "unknown"; #endif }