-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconfigmanager.cpp
397 lines (345 loc) · 14.2 KB
/
configmanager.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
393
394
395
396
397
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
*
* 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 <algorithm>
#if defined(_MSC_VER)
extern "C"
{
#include <luajit/lua.h>
#include <luajit/lualib.h>
#include <luajit/lauxlib.h>
}
#else
#ifdef __has_include
#if __has_include(<luajit/lua.hpp>)
#include <luajit/lua.hpp>
#elif __has_include(<lua.hpp>)
#include <lua.hpp>
#else
#error "Cannot detect lua library"
#endif
#endif
#endif
#include "configmanager.h"
#include "game.h"
#include "pugicast.h"
#if LUA_VERSION_NUM >= 502
#undef lua_strlen
#define lua_strlen lua_rawlen
#endif
extern Game g_game;
namespace {
std::string getGlobalString(lua_State* L, const char* identifier, const char* defaultValue)
{
lua_getglobal(L, identifier);
if (!lua_isstring(L, -1)) {
lua_pop(L, 1);
return defaultValue;
}
size_t len = lua_strlen(L, -1);
std::string ret(lua_tostring(L, -1), len);
lua_pop(L, 1);
return ret;
}
int32_t getGlobalNumber(lua_State* L, const char* identifier, const int32_t defaultValue = 0)
{
lua_getglobal(L, identifier);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
return defaultValue;
}
int32_t val = lua_tonumber(L, -1);
lua_pop(L, 1);
return val;
}
bool getGlobalBoolean(lua_State* L, const char* identifier, const bool defaultValue)
{
lua_getglobal(L, identifier);
if (!lua_isboolean(L, -1)) {
if (!lua_isstring(L, -1)) {
lua_pop(L, 1);
return defaultValue;
}
size_t len = lua_strlen(L, -1);
std::string ret(lua_tostring(L, -1), len);
lua_pop(L, 1);
return booleanString(ret);
}
int val = lua_toboolean(L, -1);
lua_pop(L, 1);
return val != 0;
}
float getGlobalFloat(lua_State* L, const char* identifier, const float defaultValue = 0.0f)
{
lua_getglobal(L, identifier);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
return defaultValue;
}
float val = lua_tonumber(L, -1);
lua_pop(L, 1);
return val;
}
}
namespace {
ExperienceStages loadLuaStages(lua_State* L)
{
ExperienceStages stages;
lua_getglobal(L, "experienceStages");
if (!lua_istable(L, -1)) {
return {};
}
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const auto tableIndex = lua_gettop(L);
auto minLevel = LuaScriptInterface::getField<uint32_t>(L, tableIndex, "minlevel");
auto maxLevel = LuaScriptInterface::getField<uint32_t>(L, tableIndex, "maxlevel");
auto multiplier = LuaScriptInterface::getField<float>(L, tableIndex, "multiplier");
stages.emplace_back(minLevel, maxLevel, multiplier);
lua_pop(L, 4);
}
lua_pop(L, 1);
std::sort(stages.begin(), stages.end());
return stages;
}
ExperienceStages loadXMLStages()
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file("data/XML/stages.xml");
if (!result) {
printXMLError("Error - loadXMLStages", "data/XML/stages.xml", result);
return {};
}
ExperienceStages stages;
for (auto stageNode : doc.child("stages").children()) {
if (strcasecmp(stageNode.name(), "config") == 0) {
if (!stageNode.attribute("enabled").as_bool()) {
return {};
}
} else {
uint32_t minLevel, maxLevel, multiplier;
if (auto minLevelAttribute = stageNode.attribute("minlevel")) {
minLevel = pugi::cast<uint32_t>(minLevelAttribute.value());
} else {
minLevel = 1;
}
if (auto maxLevelAttribute = stageNode.attribute("maxlevel")) {
maxLevel = pugi::cast<uint32_t>(maxLevelAttribute.value());
}
if (auto multiplierAttribute = stageNode.attribute("multiplier")) {
multiplier = pugi::cast<uint32_t>(multiplierAttribute.value());
} else {
multiplier = 1;
}
stages.emplace_back(minLevel, maxLevel, multiplier);
}
}
std::sort(stages.begin(), stages.end());
return stages;
}
}
bool ConfigManager::load()
{
lua_State* L = luaL_newstate();
if (!L) {
throw std::runtime_error("Failed to allocate memory");
}
luaL_openlibs(L);
if (luaL_dofile(L, "config.lua")) {
std::cout << "[Error - ConfigManager::load] " << lua_tostring(L, -1) << std::endl;
lua_close(L);
return false;
}
//parse config
if (!loaded) { //info that must be loaded one time (unless we reset the modules involved)
boolean[BIND_ONLY_GLOBAL_ADDRESS] = getGlobalBoolean(L, "bindOnlyGlobalAddress", false);
boolean[OPTIMIZE_DATABASE] = getGlobalBoolean(L, "startupDatabaseOptimization", true);
string[IP_STRING] = getGlobalString(L, "ip", "127.0.0.1");
string[MAP_NAME] = getGlobalString(L, "mapName", "forgotten");
string[MAP_AUTHOR] = getGlobalString(L, "mapAuthor", "Unknown");
string[HOUSE_RENT_PERIOD] = getGlobalString(L, "houseRentPeriod", "never");
string[MYSQL_HOST] = getGlobalString(L, "mysqlHost", "127.0.0.1");
string[MYSQL_USER] = getGlobalString(L, "mysqlUser", "forgottenserver");
string[MYSQL_PASS] = getGlobalString(L, "mysqlPass", "");
string[MYSQL_DB] = getGlobalString(L, "mysqlDatabase", "forgottenserver");
string[MYSQL_SOCK] = getGlobalString(L, "mysqlSock", "");
std::string ipString = string[IP_STRING];
uint32_t ip = inet_addr(ipString.c_str());
if (ip == INADDR_NONE) {
hostent* hostname = gethostbyname(ipString.c_str());
if (hostname) {
ipString = std::string(inet_ntoa(**(in_addr**)hostname->h_addr_list));
ip = inet_addr(ipString.c_str());
}
}
if (ip == INADDR_NONE) {
std::cout << "[Error - ConfigManager::load] cannot resolve given ip address, make sure you typed correct IP or hostname." << std::endl;
lua_close(L);
return false;
}
integer[IP] = ip;
integer[SQL_PORT] = getGlobalNumber(L, "mysqlPort", 3306);
integer[GAME_PORT] = getGlobalNumber(L, "gameProtocolPort", 7172);
integer[LOGIN_PORT] = getGlobalNumber(L, "loginProtocolPort", 7171);
integer[STATUS_PORT] = getGlobalNumber(L, "statusProtocolPort", 7171);
}
boolean[ALLOW_CHANGEOUTFIT] = getGlobalBoolean(L, "allowChangeOutfit", true);
boolean[ONE_PLAYER_ON_ACCOUNT] = getGlobalBoolean(L, "onePlayerOnlinePerAccount", true);
boolean[AIMBOT_HOTKEY_ENABLED] = getGlobalBoolean(L, "hotkeyAimbotEnabled", true);
boolean[REMOVE_RUNE_CHARGES] = getGlobalBoolean(L, "removeChargesFromRunes", true);
boolean[REMOVE_WEAPON_AMMO] = getGlobalBoolean(L, "removeWeaponAmmunition", true);
boolean[REMOVE_WEAPON_CHARGES] = getGlobalBoolean(L, "removeWeaponCharges", true);
boolean[REMOVE_POTION_CHARGES] = getGlobalBoolean(L, "removeChargesFromPotions", true);
boolean[EXPERIENCE_FROM_PLAYERS] = getGlobalBoolean(L, "experienceByKillingPlayers", false);
boolean[FREE_PREMIUM] = getGlobalBoolean(L, "freePremium", false);
boolean[REPLACE_KICK_ON_LOGIN] = getGlobalBoolean(L, "replaceKickOnLogin", true);
boolean[ALLOW_CLONES] = getGlobalBoolean(L, "allowClones", false);
boolean[ALLOW_WALKTHROUGH] = getGlobalBoolean(L, "allowWalkthrough", true);
boolean[EMOTE_SPELLS] = getGlobalBoolean(L, "emoteSpells", false);
boolean[STAMINA_SYSTEM] = getGlobalBoolean(L, "staminaSystem", true);
boolean[WARN_UNSAFE_SCRIPTS] = getGlobalBoolean(L, "warnUnsafeScripts", true);
boolean[CONVERT_UNSAFE_SCRIPTS] = getGlobalBoolean(L, "convertUnsafeScripts", true);
boolean[CLASSIC_EQUIPMENT_SLOTS] = getGlobalBoolean(L, "classicEquipmentSlots", false);
boolean[CLASSIC_ATTACK_SPEED] = getGlobalBoolean(L, "classicAttackSpeed", false);
boolean[SCRIPTS_CONSOLE_LOGS] = getGlobalBoolean(L, "showScriptsLogInConsole", true);
boolean[SERVER_SAVE_NOTIFY_MESSAGE] = getGlobalBoolean(L, "serverSaveNotifyMessage", true);
boolean[SERVER_SAVE_CLEAN_MAP] = getGlobalBoolean(L, "serverSaveCleanMap", false);
boolean[SERVER_SAVE_CLOSE] = getGlobalBoolean(L, "serverSaveClose", false);
boolean[SERVER_SAVE_SHUTDOWN] = getGlobalBoolean(L, "serverSaveShutdown", true);
boolean[ONLINE_OFFLINE_CHARLIST] = getGlobalBoolean(L, "showOnlineStatusInCharlist", false);
boolean[YELL_ALLOW_PREMIUM] = getGlobalBoolean(L, "yellAlwaysAllowPremium", false);
boolean[FORCE_MONSTERTYPE_LOAD] = getGlobalBoolean(L, "forceMonsterTypesOnLoad", true);
boolean[SPOOF_ENABLED] = getGlobalBoolean(L, "spoofEnabled", false);
boolean[GM_FULL_LIGHT_ON_EQUIP_ITEM] = getGlobalBoolean(L, "GMFullLightOnEquipItem", false);
boolean[CLOSED_WORLD] = getGlobalBoolean(L, "closedWorld", false);
boolean[SHOW_MONSTER_EXIVA] = getGlobalBoolean(L, "showMonsterExiva", true);
boolean[REMOVE_ON_DESPAWN] = getGlobalBoolean(L, "removeOnDespawn", true);
boolean[PACKET_COMPRESSION] = getGlobalBoolean(L, "packetCompression", true);
boolean[ANTI_BOT] = getGlobalBoolean(L, "antiBot", true);
boolean[GUILD_LEADER_SQUARE] = getGlobalBoolean(L, "guildLeaderSquare", true);
boolean[PVP_BALANCE] = getGlobalBoolean(L, "pvpBalance", true);
boolean[PUSH_CRUZADO] = getGlobalBoolean(L, "pushCruzado", true);
string[DEFAULT_PRIORITY] = getGlobalString(L, "defaultPriority", "high");
string[SERVER_NAME] = getGlobalString(L, "serverName", "");
string[OWNER_NAME] = getGlobalString(L, "ownerName", "");
string[OWNER_EMAIL] = getGlobalString(L, "ownerEmail", "");
string[URL] = getGlobalString(L, "url", "");
string[LOCATION] = getGlobalString(L, "location", "");
string[MOTD] = getGlobalString(L, "motd", "");
string[WORLD_TYPE] = getGlobalString(L, "worldType", "pvp");
integer[MAX_PLAYERS] = getGlobalNumber(L, "maxPlayers");
integer[PZ_LOCKED] = getGlobalNumber(L, "pzLocked", 60000);
integer[DEFAULT_DESPAWNRANGE] = getGlobalNumber(L, "deSpawnRange", 2);
integer[DEFAULT_DESPAWNRADIUS] = getGlobalNumber(L, "deSpawnRadius", 50);
integer[RATE_EXPERIENCE] = getGlobalNumber(L, "rateExp", 5);
integer[RATE_SKILL] = getGlobalNumber(L, "rateSkill", 3);
integer[RATE_LOOT] = getGlobalNumber(L, "rateLoot", 2);
integer[RATE_MAGIC] = getGlobalNumber(L, "rateMagic", 3);
integer[RATE_SPAWN] = getGlobalNumber(L, "rateSpawn", 1);
integer[SPAWN_MULTIPLIER] = getGlobalNumber(L, "spawnMultiplier", 1);
integer[HOUSE_PRICE] = getGlobalNumber(L, "housePriceEachSQM", 1000);
integer[KILLS_TO_RED] = getGlobalNumber(L, "killsToRedSkull", 3);
integer[KILLS_TO_BLACK] = getGlobalNumber(L, "killsToBlackSkull", 6);
integer[ACTIONS_DELAY_INTERVAL] = getGlobalNumber(L, "timeBetweenActions", 200);
integer[EX_ACTIONS_DELAY_INTERVAL] = getGlobalNumber(L, "timeBetweenExActions", 1000);
integer[MAX_MESSAGEBUFFER] = getGlobalNumber(L, "maxMessageBuffer", 4);
integer[KICK_AFTER_MINUTES] = getGlobalNumber(L, "kickIdlePlayerAfterMinutes", 15);
integer[PROTECTION_LEVEL] = getGlobalNumber(L, "protectionLevel", 1);
integer[DEATH_LOSE_PERCENT] = getGlobalNumber(L, "deathLosePercent", -1);
integer[STATUSQUERY_TIMEOUT] = getGlobalNumber(L, "statusTimeout", 5000);
integer[FRAG_TIME] = getGlobalNumber(L, "timeToDecreaseFrags", 24 * 60 * 60 * 1000);
integer[WHITE_SKULL_TIME] = getGlobalNumber(L, "whiteSkullTime", 15 * 60 * 1000);
integer[STAIRHOP_DELAY] = getGlobalNumber(L, "stairJumpExhaustion", 2000);
integer[EXP_FROM_PLAYERS_LEVEL_RANGE] = getGlobalNumber(L, "expFromPlayersLevelRange", 75);
integer[MAX_PACKETS_PER_SECOND] = getGlobalNumber(L, "maxPacketsPerSecond", 25);
integer[SERVER_SAVE_NOTIFY_DURATION] = getGlobalNumber(L, "serverSaveNotifyDuration", 5);
integer[YELL_MINIMUM_LEVEL] = getGlobalNumber(L, "yellMinimumLevel", 2);
integer[SPOOF_DAILY_MIN_PLAYERS] = getGlobalNumber(L, "spoofDailyMinPlayers", 50);
integer[SPOOF_DAILY_MAX_PLAYERS] = getGlobalNumber(L, "spoofDailyMaxPlayers", 200);
integer[SPOOF_NOISE_INTERVAL] = getGlobalNumber(L, "spoofNoiseInterval", 10 * 60 * 1000);
integer[SPOOF_NOISE] = getGlobalNumber(L, "spoofNoise", 10);
integer[SPOOF_TIMEZONE] = getGlobalNumber(L, "spoofTimezone", -3);
integer[SPOOF_INTERVAL] = getGlobalNumber(L, "spoofInterval", 60 * 1000);
integer[SPOOF_CHANGE_CHANCE] = getGlobalNumber(L, "spoofChangeChance", 100);
integer[SPOOF_INCREMENT_CHANCE] = getGlobalNumber(L, "spoofIncrementChange", 5);
expStages = loadXMLStages();
if (expStages.empty()) {
expStages = loadLuaStages(L);
} else {
std::cout << "[Warning - ConfigManager::load] XML stages are deprecated, consider moving to config.lua." << std::endl;
}
expStages.shrink_to_fit();
floating[MLVL_BONUSDMG] = getGlobalFloat(L, "monsterBonusDamage", 0);
floating[MLVL_BONUSSPEED] = getGlobalFloat(L, "monsterBonusSpeed", 0);
floating[MLVL_BONUSHP] = getGlobalFloat(L, "monsterBonusHealth", 0);
loaded = true;
lua_close(L);
return true;
}
bool ConfigManager::reload()
{
bool result = load();
if (transformToSHA1(getString(ConfigManager::MOTD)) != g_game.getMotdHash()) {
g_game.incrementMotdNum();
}
return result;
}
static std::string dummyStr;
const std::string& ConfigManager::getString(string_config_t what) const
{
if (what >= LAST_STRING_CONFIG) {
std::cout << "[Warning - ConfigManager::getString] Accessing invalid index: " << what << std::endl;
return dummyStr;
}
return string[what];
}
int32_t ConfigManager::getNumber(integer_config_t what) const
{
if (what >= LAST_INTEGER_CONFIG) {
std::cout << "[Warning - ConfigManager::getNumber] Accessing invalid index: " << what << std::endl;
return 0;
}
return integer[what];
}
bool ConfigManager::getBoolean(boolean_config_t what) const
{
if (what >= LAST_BOOLEAN_CONFIG) {
std::cout << "[Warning - ConfigManager::getBoolean] Accessing invalid index: " << what << std::endl;
return false;
}
return boolean[what];
}
float ConfigManager::getFloat(floating_config_t what) const
{
if (what >= LAST_FLOATING_CONFIG) {
std::cout << "[Warning - ConfigManager::getFloat] Accessing invalid index: " << what << std::endl;
return 0.0f;
}
return floating[what];
}
float ConfigManager::getExperienceStage(uint32_t level) const
{
auto it = std::find_if(expStages.begin(), expStages.end(), [level](ExperienceStages::value_type stage) {
return level >= std::get<0>(stage) && level <= std::get<1>(stage);
});
if (it == expStages.end()) {
return getNumber(ConfigManager::RATE_EXPERIENCE);
}
return std::get<2>(*it);
}