-
-
Notifications
You must be signed in to change notification settings - Fork 666
/
Copy pathcanary_server.cpp
392 lines (332 loc) · 13.4 KB
/
canary_server.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <opentibiabr@outlook.com>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#include "pch.hpp"
#include "canary_server.hpp"
#include "declarations.hpp"
#include "creatures/players/grouping/familiars.hpp"
#include "creatures/players/storages/storages.hpp"
#include "database/databasemanager.hpp"
#include "game/game.hpp"
#include "game/zones/zone.hpp"
#include "game/scheduling/dispatcher.hpp"
#include "game/scheduling/events_scheduler.hpp"
#include "io/iomarket.hpp"
#include "lib/thread/thread_pool.hpp"
#include "lua/creature/events.hpp"
#include "lua/modules/modules.hpp"
#include "lua/scripts/lua_environment.hpp"
#include "lua/scripts/scripts.hpp"
#include "server/network/protocol/protocollogin.hpp"
#include "server/network/protocol/protocolstatus.hpp"
#include "server/network/webhook/webhook.hpp"
#include "io/ioprey.hpp"
#include "io/io_bosstiary.hpp"
#include "core.hpp"
CanaryServer::CanaryServer(
Logger &logger,
RSA &rsa,
ServiceManager &serviceManager
) :
logger(logger),
rsa(rsa),
serviceManager(serviceManager) {
logInfos();
toggleForceCloseButton();
g_game().setGameState(GAME_STATE_STARTUP);
std::set_new_handler(badAllocationHandler);
srand(static_cast<unsigned int>(OTSYS_TIME()));
g_dispatcher().init();
#ifdef _WIN32
SetConsoleTitleA(ProtocolStatus::SERVER_NAME.c_str());
#endif
}
int CanaryServer::run() {
g_dispatcher().addEvent(
[this] {
try {
loadConfigLua();
logger.info("Server protocol: {}.{}{}", CLIENT_VERSION_UPPER, CLIENT_VERSION_LOWER, g_configManager().getBoolean(OLD_PROTOCOL, __FUNCTION__) ? " and 10x allowed!" : "");
#ifdef FEATURE_METRICS
metrics::Options metricsOptions;
metricsOptions.enablePrometheusExporter = g_configManager().getBoolean(METRICS_ENABLE_PROMETHEUS, __FUNCTION__);
if (metricsOptions.enablePrometheusExporter) {
metricsOptions.prometheusOptions.url = g_configManager().getString(METRICS_PROMETHEUS_ADDRESS, __FUNCTION__);
}
metricsOptions.enableOStreamExporter = g_configManager().getBoolean(METRICS_ENABLE_OSTREAM, __FUNCTION__);
if (metricsOptions.enableOStreamExporter) {
metricsOptions.ostreamOptions.export_interval_millis = std::chrono::milliseconds(g_configManager().getNumber(METRICS_OSTREAM_INTERVAL, __FUNCTION__));
}
g_metrics().init(metricsOptions);
#endif
rsa.start();
initializeDatabase();
loadModules();
setWorldType();
loadMaps();
logger.info("Initializing gamestate...");
g_game().setGameState(GAME_STATE_INIT);
setupHousesRent();
g_game().transferHouseItemsToDepot();
IOMarket::checkExpiredOffers();
IOMarket::getInstance().updateStatistics();
logger.info("Loaded all modules, server starting up...");
#ifndef _WIN32
if (getuid() == 0 || geteuid() == 0) {
logger.warn("{} has been executed as root user, "
"please consider running it as a normal user",
ProtocolStatus::SERVER_NAME);
}
#endif
g_game().start(&serviceManager);
g_game().setGameState(GAME_STATE_NORMAL);
if (g_configManager().getBoolean(TOGGLE_MAINTAIN_MODE, __FUNCTION__)) {
g_game().setGameState(GAME_STATE_CLOSED);
g_logger().warn("Initialized in maintain mode!");
g_webhook().sendMessage(":yellow_square: Server is now **online** _(access restricted to staff)_");
} else {
g_game().setGameState(GAME_STATE_NORMAL);
g_webhook().sendMessage(":green_circle: Server is now **online**");
}
loaderStatus = LoaderStatus::LOADED;
} catch (FailedToInitializeCanary &err) {
loaderStatus = LoaderStatus::FAILED;
logger.error(err.what());
logger.error("The program will close after pressing the enter key...");
if (isatty(STDIN_FILENO)) {
getchar();
}
}
loaderStatus.notify_one();
},
"CanaryServer::run"
);
loaderStatus.wait(LoaderStatus::LOADING);
if (loaderStatus == LoaderStatus::FAILED || !serviceManager.is_running()) {
logger.error("No services running. The server is NOT online!");
shutdown();
return EXIT_FAILURE;
}
logger.info("{} {}", g_configManager().getString(SERVER_NAME, __FUNCTION__), "server online!");
serviceManager.run();
shutdown();
return EXIT_SUCCESS;
}
void CanaryServer::setWorldType() {
const std::string worldType = asLowerCaseString(g_configManager().getString(WORLD_TYPE, __FUNCTION__));
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 {
throw FailedToInitializeCanary(
fmt::format(
"Unknown world type: {}, valid world types are: pvp, no-pvp and pvp-enforced",
g_configManager().getString(WORLD_TYPE, __FUNCTION__)
)
);
}
logger.debug("World type set as {}", asUpperCaseString(worldType));
}
void CanaryServer::loadMaps() const {
try {
g_game().loadMainMap(g_configManager().getString(MAP_NAME, __FUNCTION__));
// If "mapCustomEnabled" is true on config.lua, then load the custom map
if (g_configManager().getBoolean(TOGGLE_MAP_CUSTOM, __FUNCTION__)) {
g_game().loadCustomMaps(g_configManager().getString(DATA_DIRECTORY, __FUNCTION__) + "/world/custom/");
}
Zone::refreshAll();
} catch (const std::exception &err) {
throw FailedToInitializeCanary(err.what());
}
}
void CanaryServer::setupHousesRent() {
RentPeriod_t rentPeriod;
std::string strRentPeriod = asLowerCaseString(g_configManager().getString(HOUSE_RENT_PERIOD, __FUNCTION__));
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);
}
void CanaryServer::logInfos() {
#if defined(GIT_RETRIEVED_STATE) && GIT_RETRIEVED_STATE
logger.debug("{} - Version [{}] dated [{}]", ProtocolStatus::SERVER_NAME, SERVER_RELEASE_VERSION, GIT_COMMIT_DATE_ISO8601);
#if GIT_IS_DIRTY
logger.debug("DIRTY - NOT OFFICIAL RELEASE");
#endif
#else
logger.info("{} - Version {}", ProtocolStatus::SERVER_NAME, SERVER_RELEASE_VERSION);
#endif
logger.debug("Compiled with {}, on {} {}, for platform {}\n", getCompiler(), __DATE__, __TIME__, getPlatform());
#if defined(LUAJIT_VERSION)
logger.debug("Linked with {} for Lua support", LUAJIT_VERSION);
#endif
logger.info("A server developed by: {}", ProtocolStatus::SERVER_DEVELOPERS);
logger.info("Visit our website for updates, support, and resources: "
"https://docs.opentibiabr.com/");
}
/**
*It is preferable to keep the close button off as it closes the server without saving (this can cause the player to lose items from houses and others informations, since windows automatically closes the process in five seconds, when forcing the close)
* Choose to use "CTROL + C" or "CTROL + BREAK" for security close
* To activate/deactivate window;
* \param MF_GRAYED Disable the "x" (force close) button
* \param MF_ENABLED Enable the "x" (force close) button
*/
void CanaryServer::toggleForceCloseButton() {
#ifdef OS_WINDOWS
const HWND hwnd = GetConsoleWindow();
const HMENU hmenu = GetSystemMenu(hwnd, FALSE);
EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED);
#endif
}
void CanaryServer::badAllocationHandler() {
// Use functions that only use stack allocation
g_logger().error("Allocation failed, server out of memory, "
"decrease the size of your map or compile in 64 bits mode");
if (isatty(STDIN_FILENO)) {
getchar();
}
shutdown();
exit(-1);
}
std::string CanaryServer::getPlatform() {
#if defined(__amd64__) || defined(_M_X64)
return "x64";
#elif defined(__i386__) || defined(_M_IX86) || defined(_X86_)
return "x86";
#elif defined(__arm__)
return "ARM";
#else
return "unknown";
#endif
}
std::string CanaryServer::getCompiler() {
std::string compiler;
#if defined(__clang__)
return compiler = fmt::format("Clang++ {}.{}.{}", __clang_major__, __clang_minor__, __clang_patchlevel__);
#elif defined(_MSC_VER)
return compiler = fmt::format("Microsoft Visual Studio {}", _MSC_VER);
#elif defined(__GNUC__)
return compiler = fmt::format("G++ {}.{}.{}", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#else
return compiler = "unknown";
#endif
}
void CanaryServer::loadConfigLua() {
std::string configName = "config.lua";
// Check if config or config.dist exist
std::ifstream c_test("./" + configName);
if (!c_test.is_open()) {
std::ifstream config_lua_dist(configName + ".dist");
if (config_lua_dist.is_open()) {
logger.info("Copying {}.dist to {}", configName, configName);
std::ofstream config_lua(configName);
config_lua << config_lua_dist.rdbuf();
config_lua.close();
config_lua_dist.close();
}
} else {
c_test.close();
}
g_configManager().setConfigFileLua(configName);
modulesLoadHelper(g_configManager().load(), g_configManager().getConfigFileLua());
#ifdef _WIN32
const std::string &defaultPriority = g_configManager().getString(DEFAULT_PRIORITY, __FUNCTION__);
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
}
void CanaryServer::initializeDatabase() {
logger.info("Establishing database connection... ");
if (!Database::getInstance().connect()) {
throw FailedToInitializeCanary("Failed to connect to database!");
}
logger.debug("MySQL Version: {}", Database::getClientVersion());
logger.debug("Running database manager...");
if (!DatabaseManager::isDatabaseSetup()) {
throw FailedToInitializeCanary(fmt::format(
"The database you have specified in {} is empty, please import the schema.sql to your database.",
g_configManager().getConfigFileLua()
));
}
DatabaseManager::updateDatabase();
if (g_configManager().getBoolean(OPTIMIZE_DATABASE, __FUNCTION__)
&& !DatabaseManager::optimizeTables()) {
logger.debug("No tables were optimized");
}
}
void CanaryServer::loadModules() {
// If "USE_ANY_DATAPACK_FOLDER" is set to true then you can choose any datapack folder for your server
const auto useAnyDatapack = g_configManager().getBoolean(USE_ANY_DATAPACK_FOLDER, __FUNCTION__);
auto datapackName = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__);
if (!useAnyDatapack && (datapackName != "data-canary" && datapackName != "data-otservbr-global" || datapackName != "data-otservbr-global" && datapackName != "data-canary")) {
throw FailedToInitializeCanary(fmt::format(
"The datapack folder name '{}' is wrong, please select valid "
"datapack name 'data-canary' or 'data-otservbr-global "
"or enable in config.lua to use any datapack folder",
datapackName
));
}
logger.debug("Initializing lua environment...");
if (!g_luaEnvironment().getLuaState()) {
g_luaEnvironment().initState();
}
auto coreFolder = g_configManager().getString(CORE_DIRECTORY, __FUNCTION__);
// Load appearances.dat first
modulesLoadHelper((g_game().loadAppearanceProtobuf(coreFolder + "/items/appearances.dat") == ERROR_NONE), "appearances.dat");
// Load XML folder dependencies (order matters)
modulesLoadHelper(g_vocations().loadFromXml(), "XML/vocations.xml");
modulesLoadHelper(g_eventsScheduler().loadScheduleEventFromXml(), "XML/events.xml");
modulesLoadHelper(Outfits::getInstance().loadFromXml(), "XML/outfits.xml");
modulesLoadHelper(Familiars::getInstance().loadFromXml(), "XML/familiars.xml");
modulesLoadHelper(g_imbuements().loadFromXml(), "XML/imbuements.xml");
modulesLoadHelper(g_storages().loadFromXML(), "XML/storages.xml");
modulesLoadHelper(Item::items.loadFromXml(), "items.xml");
const auto datapackFolder = g_configManager().getString(DATA_DIRECTORY, __FUNCTION__);
logger.debug("Loading core scripts on folder: {}/", coreFolder);
// Load first core Lua libs
modulesLoadHelper((g_luaEnvironment().loadFile(coreFolder + "/core.lua", "core.lua") == 0), "core.lua");
modulesLoadHelper(g_scripts().loadScripts(coreFolder + "/scripts/lib", true, false), coreFolder + "/scripts/libs");
modulesLoadHelper(g_scripts().loadScripts(coreFolder + "/scripts", false, false), coreFolder + "/scripts");
modulesLoadHelper((g_npcs().load(true, false)), "npclib");
modulesLoadHelper(g_events().loadFromXml(), "events/events.xml");
modulesLoadHelper(g_modules().loadFromXml(), "modules/modules.xml");
logger.debug("Loading datapack scripts on folder: {}/", datapackName);
// Load scripts
modulesLoadHelper(g_scripts().loadScripts(datapackFolder + "/scripts", false, false), datapackFolder + "/scripts");
// Load monsters
modulesLoadHelper(g_scripts().loadScripts(datapackFolder + "/monster", false, false), datapackFolder + "/monster");
modulesLoadHelper((g_npcs().load(false, true)), "npc");
g_game().loadBoostedCreature();
g_ioBosstiary().loadBoostedBoss();
g_ioprey().initializeTaskHuntOptions();
}
void CanaryServer::modulesLoadHelper(bool loaded, std::string moduleName) {
logger.debug("Loading {}", moduleName);
if (!loaded) {
throw FailedToInitializeCanary(fmt::format("Cannot load: {}", moduleName));
}
}
void CanaryServer::shutdown() {
g_dispatcher().shutdown();
g_metrics().shutdown();
inject<ThreadPool>().shutdown();
}