forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotserv.cpp
304 lines (250 loc) · 9.02 KB
/
otserv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2016 Mark Samman <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include "server.h"
#include "game.h"
#include "iomarket.h"
#ifndef _WIN32
#include <csignal> // for sigemptyset()
#endif
#include "configmanager.h"
#include "scriptmanager.h"
#include "rsa.h"
#include "protocolold.h"
#include "protocollogin.h"
#include "protocolstatus.h"
#include "databasemanager.h"
#include "scheduler.h"
#include "databasetasks.h"
DatabaseTasks g_databaseTasks;
Dispatcher g_dispatcher;
Scheduler g_scheduler;
Game g_game;
ConfigManager g_config;
Monsters g_monsters;
Vocations g_vocations;
RSA g_RSA;
std::mutex g_loaderLock;
std::condition_variable g_loaderSignal;
std::unique_lock<std::mutex> g_loaderUniqueLock(g_loaderLock);
void startupErrorMessage(const std::string& errorStr)
{
std::cout << "> ERROR: " << errorStr << std::endl;
g_loaderSignal.notify_all();
}
void mainLoader(int argc, char* argv[], ServiceManager* servicer);
void badAllocationHandler()
{
// Use functions that only use stack allocation
puts("Allocation failed, server out of memory.\nDecrease the size of your map or compile in 64 bits mode.\n");
getchar();
exit(-1);
}
int main(int argc, char* argv[])
{
// Setup bad allocation handler
std::set_new_handler(badAllocationHandler);
#ifndef _WIN32
// ignore sigpipe...
struct sigaction sigh;
sigh.sa_handler = SIG_IGN;
sigh.sa_flags = 0;
sigemptyset(&sigh.sa_mask);
sigaction(SIGPIPE, &sigh, nullptr);
#endif
ServiceManager serviceManager;
g_dispatcher.start();
g_scheduler.start();
g_dispatcher.addTask(createTask(std::bind(mainLoader, argc, argv, &serviceManager)));
g_loaderSignal.wait(g_loaderUniqueLock);
if (serviceManager.is_running()) {
std::cout << ">> " << g_config.getString(ConfigManager::SERVER_NAME) << " Server Online!" << std::endl << std::endl;
#ifdef _WIN32
SetConsoleCtrlHandler([](DWORD) -> BOOL {
g_dispatcher.addTask(createTask([]() {
g_dispatcher.addTask(createTask(
std::bind(&Game::shutdown, &g_game)
));
g_scheduler.stop();
g_databaseTasks.stop();
g_dispatcher.stop();
}));
ExitThread(0);
}, 1);
#endif
serviceManager.run();
} else {
std::cout << ">> No services running. The server is NOT online." << std::endl;
g_scheduler.shutdown();
g_databaseTasks.shutdown();
g_dispatcher.shutdown();
}
g_scheduler.join();
g_databaseTasks.join();
g_dispatcher.join();
return 0;
}
void mainLoader(int, char*[], ServiceManager* services)
{
//dispatcher thread
g_game.setGameState(GAME_STATE_STARTUP);
srand(static_cast<unsigned int>(OTSYS_TIME()));
#ifdef _WIN32
SetConsoleTitle(STATUS_SERVER_NAME);
#endif
std::cout << STATUS_SERVER_NAME << " - Version " << STATUS_SERVER_VERSION << std::endl;
std::cout << "Compiled with " << BOOST_COMPILER << std::endl;
std::cout << "Compiled on " << __DATE__ << ' ' << __TIME__ << " for platform ";
#if defined(__amd64__) || defined(_M_X64)
std::cout << "x64" << std::endl;
#elif defined(__i386__) || defined(_M_IX86) || defined(_X86_)
std::cout << "x86" << std::endl;
#elif defined(__arm__)
std::cout << "ARM" << std::endl;
#else
std::cout << "unknown" << std::endl;
#endif
std::cout << std::endl;
std::cout << "A server developed by " << STATUS_SERVER_DEVELOPERS << std::endl;
std::cout << "Visit our forum for updates, support, and resources: http://otland.net/." << std::endl;
std::cout << std::endl;
// read global config
std::cout << ">> Loading config" << std::endl;
if (!g_config.load()) {
startupErrorMessage("Unable to load config.lua!");
return;
}
#ifdef _WIN32
const std::string& defaultPriority = g_config.getString(ConfigManager::DEFAULT_PRIORITY);
if (strcasecmp(defaultPriority.c_str(), "high") == 0) {
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
} else if (strcasecmp(defaultPriority.c_str(), "above-normal") == 0) {
SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
}
#endif
//set RSA key
const char* p("14299623962416399520070177382898895550795403345466153217470516082934737582776038882967213386204600674145392845853859217990626450972452084065728686565928113");
const char* q("7630979195970404721891201847792002125535401292779123937207447574596692788513647179235335529307251350570728407373705564708871762033017096809910315212884101");
g_RSA.setKey(p, q);
std::cout << ">> Establishing database connection..." << std::flush;
Database* db = Database::getInstance();
if (!db->connect()) {
startupErrorMessage("Failed to connect to database.");
return;
}
std::cout << " MySQL " << Database::getClientVersion() << std::endl;
// run database manager
std::cout << ">> Running database manager" << std::endl;
if (!DatabaseManager::isDatabaseSetup()) {
startupErrorMessage("The database you have specified in config.lua is empty, please import the schema.sql to your database.");
return;
}
g_databaseTasks.start();
DatabaseManager::updateDatabase();
if (g_config.getBoolean(ConfigManager::OPTIMIZE_DATABASE) && !DatabaseManager::optimizeTables()) {
std::cout << "> No tables were optimized." << std::endl;
}
//load vocations
std::cout << ">> Loading vocations" << std::endl;
if (!g_vocations.loadFromXml()) {
startupErrorMessage("Unable to load vocations!");
return;
}
// load item data
std::cout << ">> Loading items" << std::endl;
if (Item::items.loadFromOtb("data/items/items.otb") != ERROR_NONE) {
startupErrorMessage("Unable to load items (OTB)!");
return;
}
if (!Item::items.loadFromXml()) {
startupErrorMessage("Unable to load items (XML)!");
return;
}
std::cout << ">> Loading script systems" << std::endl;
if (!ScriptingManager::getInstance()->loadScriptSystems()) {
startupErrorMessage("Failed to load script systems");
return;
}
std::cout << ">> Loading monsters" << std::endl;
if (!g_monsters.loadFromXml()) {
startupErrorMessage("Unable to load monsters!");
return;
}
std::cout << ">> Loading outfits" << std::endl;
Outfits* outfits = Outfits::getInstance();
if (!outfits->loadFromXml()) {
startupErrorMessage("Unable to load outfits!");
return;
}
std::cout << ">> Checking world type... " << std::flush;
std::string worldType = asLowerCaseString(g_config.getString(ConfigManager::WORLD_TYPE));
if (worldType == "pvp") {
g_game.setWorldType(WORLD_TYPE_PVP);
} else if (worldType == "no-pvp") {
g_game.setWorldType(WORLD_TYPE_NO_PVP);
} else if (worldType == "pvp-enforced") {
g_game.setWorldType(WORLD_TYPE_PVP_ENFORCED);
} else {
std::cout << std::endl;
std::ostringstream ss;
ss << "> ERROR: Unknown world type: " << g_config.getString(ConfigManager::WORLD_TYPE) << ", valid world types are: pvp, no-pvp and pvp-enforced.";
startupErrorMessage(ss.str());
return;
}
std::cout << asUpperCaseString(worldType) << std::endl;
std::cout << ">> Loading map" << std::endl;
if (!g_game.loadMainMap(g_config.getString(ConfigManager::MAP_NAME))) {
startupErrorMessage("Failed to load map");
return;
}
std::cout << ">> Initializing gamestate" << std::endl;
g_game.setGameState(GAME_STATE_INIT);
// Game client protocols
services->add<ProtocolGame>(g_config.getNumber(ConfigManager::GAME_PORT));
services->add<ProtocolLogin>(g_config.getNumber(ConfigManager::LOGIN_PORT));
// OT protocols
services->add<ProtocolStatus>(g_config.getNumber(ConfigManager::STATUS_PORT));
// Legacy login protocol
services->add<ProtocolOld>(g_config.getNumber(ConfigManager::LOGIN_PORT));
RentPeriod_t rentPeriod;
std::string strRentPeriod = asLowerCaseString(g_config.getString(ConfigManager::HOUSE_RENT_PERIOD));
if (strRentPeriod == "yearly") {
rentPeriod = RENTPERIOD_YEARLY;
} else if (strRentPeriod == "weekly") {
rentPeriod = RENTPERIOD_WEEKLY;
} else if (strRentPeriod == "monthly") {
rentPeriod = RENTPERIOD_MONTHLY;
} else if (strRentPeriod == "daily") {
rentPeriod = RENTPERIOD_DAILY;
} else {
rentPeriod = RENTPERIOD_NEVER;
}
g_game.map.houses.payHouses(rentPeriod);
IOMarket::checkExpiredOffers();
IOMarket::getInstance()->updateStatistics();
std::cout << ">> Loaded all modules, server starting up..." << std::endl;
#ifndef _WIN32
if (getuid() == 0 || geteuid() == 0) {
std::cout << "> Warning: " << STATUS_SERVER_NAME << " has been executed as root user, please consider running it as a normal user." << std::endl;
}
#endif
g_game.start(services);
g_game.setGameState(GAME_STATE_NORMAL);
g_loaderSignal.notify_all();
}