Otclient  14/8/2020
application.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "application.h"
24 #include <csignal>
25 #include <framework/core/clock.h>
30 #include "asyncdispatcher.h"
34 
35 #include <locale>
36 
37 #ifdef FW_NET
39 #endif
40 
41 void exitSignalHandler(int sig)
42 {
43  static bool signaled = false;
44  switch(sig) {
45  case SIGTERM:
46  case SIGINT:
47  if(!signaled && !g_app.isStopping() && !g_app.isTerminated()) {
48  signaled = true;
50  }
51  break;
52  }
53 }
54 
56 {
57  m_appName = "application";
58  m_appCompactName = "app";
59  m_appVersion = "none";
60  m_charset = "cp1252";
61  m_stopping = false;
62 }
63 
64 void Application::init(std::vector<std::string>& args)
65 {
66  // capture exit signals
67  signal(SIGTERM, exitSignalHandler);
68  signal(SIGINT, exitSignalHandler);
69 
70 #ifdef CRASH_HANDLER
71  installCrashHandler();
72 #endif
73 
74  // setup locale
75  std::locale::global(std::locale());
76 
77  // process args encoding
78  g_platform.processArgs(args);
79 
81 
82  std::string startupOptions;
83  for(uint i=1;i<args.size();++i) {
84  const std::string& arg = args[i];
85  startupOptions += " ";
86  startupOptions += arg;
87  }
88  if(startupOptions.length() > 0)
89  g_logger.info(stdext::format("Startup options: %s", startupOptions));
90 
91  m_startupOptions = startupOptions;
92 
93  // initialize configs
94  g_configs.init();
95 
96  // initialize resources
97  g_resources.init(args[0].c_str());
98 
99  // initialize lua
100  g_lua.init();
102 }
103 
105 {
106  g_lua.callGlobalField("g_app", "onTerminate");
107 
108  // run modules unload events
110  g_modules.clear();
111 
112  // release remaining lua object references
114 
115  // poll remaining events
116  poll();
117 
119 
120  // disable dispatcher events
122 }
123 
125 {
126 #ifdef FW_NET
127  // terminate network
129 #endif
130 
131  // release configs
133 
134  // release resources
136 
137  // terminate script environment
138  g_lua.terminate();
139 
140  m_terminated = true;
141 
142  signal(SIGTERM, SIG_DFL);
143  signal(SIGINT, SIG_DFL);
144 }
145 
147 {
148 #ifdef FW_NET
150 #endif
151 
152  g_dispatcher.poll();
153 
154  // poll connection again to flush pending write
155 #ifdef FW_NET
157 #endif
158 }
159 
161 {
162  g_lua.callGlobalField<bool>("g_app", "onExit");
163  m_stopping = true;
164 }
165 
167 {
168  if(!g_lua.callGlobalField<bool>("g_app", "onClose"))
169  exit();
170 }
171 
172 std::string Application::getOs()
173 {
174 #if defined(WIN32)
175  return "windows";
176 #elif defined(__APPLE__)
177  return "mac";
178 #elif __linux
179  return "linux";
180 #else
181  return "unknown";
182 #endif
183 }
184 
eventdispatcher.h
Application::terminate
virtual void terminate()
Definition: application.cpp:124
Application::Application
Application()
Definition: application.cpp:55
configmanager.h
Application::isStopping
bool isStopping()
Definition: application.h:49
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
Connection::poll
static void poll()
Definition: connection.cpp:53
LuaInterface::callGlobalField
void callGlobalField(const std::string &global, const std::string &field, const T &... args)
Definition: luainterface.h:445
crashhandler.h
resourcemanager.h
luainterface.h
Application::m_startupOptions
std::string m_startupOptions
Definition: application.h:72
g_configs
ConfigManager g_configs
Definition: configmanager.cpp:25
ResourceManager::init
void init(const char *argv0)
Definition: resourcemanager.cpp:34
LuaInterface::init
void init()
Definition: luainterface.cpp:46
ModuleManager::clear
void clear()
Definition: modulemanager.cpp:31
stdext::format
std::string format()
Definition: format.h:82
clock.h
asyncdispatcher.h
Application::deinit
virtual void deinit()
Definition: application.cpp:104
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
exitSignalHandler
void exitSignalHandler(int sig)
Definition: application.cpp:41
Application::poll
virtual void poll()
Definition: application.cpp:146
ModuleManager::unloadModules
void unloadModules()
Definition: modulemanager.cpp:99
uint
unsigned int uint
Definition: types.h:31
g_logger
Logger g_logger
Definition: logger.cpp:35
g_lua
LuaInterface g_lua
Definition: luainterface.cpp:31
ResourceManager::terminate
void terminate()
Definition: resourcemanager.cpp:40
Platform::processArgs
void processArgs(std::vector< std::string > &args)
Definition: unixplatform.cpp:34
EventDispatcher::addEvent
EventPtr addEvent(const std::function< void()> &callback, bool pushFront=false)
Definition: eventdispatcher.cpp:104
ConfigManager::terminate
void terminate()
Definition: configmanager.cpp:32
platform.h
EventDispatcher::poll
void poll()
Definition: eventdispatcher.cpp:43
g_app
ConsoleApplication g_app
Definition: consoleapplication.cpp:32
Application::close
virtual void close()
Definition: application.cpp:166
ConfigManager::init
void init()
Definition: configmanager.cpp:27
connection.h
LuaInterface::terminate
void terminate()
Definition: luainterface.cpp:71
Application::getOs
std::string getOs()
Definition: application.cpp:172
EventDispatcher::shutdown
void shutdown()
Definition: eventdispatcher.cpp:30
Application::m_charset
std::string m_charset
Definition: application.h:68
Application::isTerminated
bool isTerminated()
Definition: application.h:50
modulemanager.h
Application::exit
virtual void exit()
Definition: application.cpp:160
Application::init
virtual void init(std::vector< std::string > &args)
Definition: application.cpp:64
AsyncDispatcher::terminate
void terminate()
Definition: asyncdispatcher.cpp:32
Application::m_stopping
stdext::boolean< false > m_stopping
Definition: application.h:74
Connection::terminate
static void terminate()
Definition: connection.cpp:60
Application::m_appCompactName
std::string m_appCompactName
Definition: application.h:70
LuaInterface::collectGarbage
void collectGarbage()
Definition: luainterface.cpp:714
Application::m_terminated
stdext::boolean< false > m_terminated
Definition: application.h:75
AsyncDispatcher::init
void init()
Definition: asyncdispatcher.cpp:27
g_asyncDispatcher
AsyncDispatcher g_asyncDispatcher
Definition: asyncdispatcher.cpp:25
Application::registerLuaFunctions
void registerLuaFunctions()
Definition: luafunctions.cpp:64
Application::m_appName
std::string m_appName
Definition: application.h:69
Application::m_appVersion
std::string m_appVersion
Definition: application.h:71
Logger::info
void info(const std::string &what)
Definition: logger.h:52
g_modules
ModuleManager g_modules
Definition: modulemanager.cpp:29
application.h
g_platform
Platform g_platform
Definition: platform.cpp:25