forked from opentibia-xx/otserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigmanager.cpp
179 lines (153 loc) · 5.89 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
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "configmanager.h"
ConfigManager::ConfigManager()
{
m_isLoaded = false;
}
ConfigManager::~ConfigManager()
{
//
}
bool ConfigManager::loadFile(const std::string& _filename)
{
lua_State* L = lua_open();
if(!L) return false;
if(luaL_dofile(L, _filename.c_str()))
{
lua_close(L);
return false;
}
// parse config
if(!m_isLoaded) // info that must be loaded one time (unless we reset the modules involved)
{
m_confString[CONFIG_FILE] = _filename;
m_confString[IP] = getGlobalString(L, "ip", "127.0.0.1");
m_confInteger[PORT] = getGlobalNumber(L, "port");
m_confString[DATA_DIRECTORY] = getGlobalString(L, "datadir");
m_confString[MAP_FILE] = getGlobalString(L, "map");
m_confString[MAP_STORE_FILE] = getGlobalString(L, "mapstore");
m_confString[HOUSE_STORE_FILE] = getGlobalString(L, "housestore");
m_confString[HOUSE_RENT_PERIOD] = getGlobalString(L, "houserentperiod", "monthly");
m_confString[MAP_KIND] = getGlobalString(L, "mapkind");
m_confString[BAN_FILE] = getGlobalString(L, "banIdentifier");
m_confString[MD5_PASS] = getGlobalString(L, "md5passwords");
m_confString[WORLD_TYPE] = getGlobalString(L, "worldtype");
m_confString[SQL_HOST] = getGlobalString(L, "sql_host");
m_confString[SQL_USER] = getGlobalString(L, "sql_user");
m_confString[SQL_PASS] = getGlobalString(L, "sql_pass");
m_confString[SQL_DB] = getGlobalString(L, "sql_db");
m_confString[SQLITE_DB] = getGlobalString(L, "sqlite_db");
m_confString[SQL_TYPE] = getGlobalString(L, "sql_type");
m_confString[MAP_HOST] = getGlobalString(L, "map_host");
m_confString[MAP_USER] = getGlobalString(L, "map_user");
m_confString[MAP_PASS] = getGlobalString(L, "map_pass");
m_confString[MAP_DB] = getGlobalString(L, "map_db");
}
m_confString[LOGIN_MSG] = getGlobalString(L, "loginmsg", "Welcome.");
m_confString[SERVER_NAME] = getGlobalString(L, "servername");
m_confString[OWNER_NAME] = getGlobalString(L, "ownername");
m_confString[OWNER_EMAIL] = getGlobalString(L, "owneremail");
m_confString[URL] = getGlobalString(L, "url");
m_confString[LOCATION] = getGlobalString(L, "location");
m_confInteger[LOGIN_TRIES] = getGlobalNumber(L, "logintries", 3);
m_confInteger[RETRY_TIMEOUT] = getGlobalNumber(L, "retrytimeout", 30 * 1000);
m_confInteger[LOGIN_TIMEOUT] = getGlobalNumber(L, "logintimeout", 5 * 1000);
m_confString[MOTD] = getGlobalString(L, "motd");
m_confInteger[MOTD_NUM] = getGlobalNumber(L, "motdnum");
m_confInteger[MAX_PLAYERS] = getGlobalNumber(L, "maxplayers");
m_confInteger[EXHAUSTED] = getGlobalNumber(L, "exhausted", 0);
m_confInteger[EXHAUSTED_ADD] = getGlobalNumber(L, "exhaustedadd", 0);
m_confInteger[PZ_LOCKED] = getGlobalNumber(L, "pzlocked", 0);
m_confInteger[ALLOW_CLONES] = getGlobalNumber(L, "allowclones", 0);
m_confInteger[RATE_EXPERIENCE] = getGlobalNumber(L, "rate_exp", 1);
m_confInteger[RATE_SKILL] = getGlobalNumber(L, "rate_skill", 1);
m_confInteger[RATE_LOOT] = getGlobalNumber(L, "rate_loot", 1);
m_confInteger[RATE_MAGIC] = getGlobalNumber(L, "rate_mag", 1);
m_confString[OTSERV_DB_HOST] = getGlobalString(L, "otserv_db_host", "default_db_host_here");
m_confInteger[OTSERV_DB_ENABLED] = getGlobalNumber(L, "otserv_db_enabled", 0);
/*
for(int i=0; i<4; ++i){
m_confVocationString[i] = getGlobalStringField(L, "vocations", i+1, "unknown");
}
*/
m_isLoaded = true;
lua_close(L);
return true;
}
bool ConfigManager::reload()
{
if(!m_isLoaded)
return false;
return loadFile(m_confString[CONFIG_FILE]);
}
int ConfigManager::getNumber(int _what)
{
if(m_isLoaded && _what < LAST_INTEGER_CONFIG)
return m_confInteger[_what];
else
{
fprintf(stderr, "WARNING: ConfigManager::getNumber()\n");
return 0;
}
}
bool ConfigManager::setNumber(int _what, int _value)
{
if(m_isLoaded && _what < LAST_INTEGER_CONFIG)
{
m_confInteger[_what] = _value;
return true;
}
else
{
fprintf(stderr, "WARNING: ConfigManager::setNumber()\n");
return false;
}
}
std::string ConfigManager::getGlobalString(lua_State* _L, const std::string& _identifier, const std::string& _default)
{
lua_getglobal(_L, _identifier.c_str());
if(!lua_isstring(_L, -1))
return _default;
int len = (int)lua_strlen(_L, -1);
std::string ret(lua_tostring(_L, -1), len);
lua_pop(_L,1);
return ret;
}
int ConfigManager::getGlobalNumber(lua_State* _L, const std::string& _identifier, const int _default)
{
lua_getglobal(_L, _identifier.c_str());
if(!lua_isnumber(_L, -1))
return _default;
int val = (int)lua_tonumber(_L, -1);
lua_pop(_L,1);
return val;
}
std::string ConfigManager::getGlobalStringField (lua_State* _L, const std::string& _identifier, const int _key, const std::string& _default) {
lua_getglobal(_L, _identifier.c_str());
lua_pushnumber(_L, _key);
lua_gettable(_L, -2); /* get table[key] */
if(!lua_isstring(_L, -1))
return _default;
std::string result = lua_tostring(_L, -1);
lua_pop(_L, 2); /* remove number and key*/
return result;
}