This repository has been archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMSConfig.cpp
138 lines (107 loc) · 3.96 KB
/
MSConfig.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
/*
This file is part of MasterServer.
MasterServer is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MasterServer 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with MasterServer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MSConfig.h"
#include "INIReader.h"
#include <stdio.h>
#include <string.h>
bool CConfig::Load(CModuleManager *mngr, const char *name)
{
// Create an INI instance
INIReader reader(name);
size_t i = 0, k = 0;
bool bC = true;
std::string str = "";
// Load the INI
if (reader.ParseError() < 0)
return false;
// Load the Database section
if (reader.GetInteger("Database", "Enabled", 1) == 1)
{
char dbType[MAX_INI_BUFFER+1];
dbType[0] = '\0';
m_DBPort = reader.GetInteger("Database", "Port", 3306);
if (m_DBPort < 0 || m_DBPort > 65535)
m_DBPort = 3306;
strncpy(m_szDBPass, reader.Get("Database", "Password", "").c_str(), sizeof(m_szDBPass));
m_szDBPass[sizeof(m_szDBPass) - 1] = '\0';
strncpy(m_szDBUser, reader.Get("Database", "Username", "masterserver").c_str(), sizeof(m_szDBUser));
m_szDBUser[sizeof(m_szDBUser) - 1] = '\0';
strncpy(m_szDBHost, reader.Get("Database", "Host", "localhost").c_str(), sizeof(m_szDBHost));
m_szDBHost[sizeof(m_szDBHost) - 1] = '\0';
strncpy(m_szDBSock, reader.Get("Database", "Socket", "").c_str(), sizeof(m_szDBSock));
m_szDBSock[sizeof(m_szDBSock) - 1] = '\0';
strncpy(dbType, reader.Get("Database", "Type", "MariaDB").c_str(), sizeof(dbType));
dbType[sizeof(dbType) - 1] = '\0';
if (strcmp("MariaDB", dbType) == 0)
m_eDatabaseType = DATABASE_TYPE_MARIADB;
else if (strcmp("SQLite", dbType) == 0)
m_eDatabaseType = DATABASE_TYPE_SQLITE;
if (m_eDatabaseType == DATABASE_TYPE_MARIADB)
strncpy(m_szDBName, reader.Get("Database", "Name", "masterserver").c_str(), sizeof(m_szDBName));
else
strncpy(m_szDBName, reader.Get("Database", "Name", "masterserver.db").c_str(), sizeof(m_szDBName));
m_szDBName[sizeof(m_szDBName) - 1] = '\0';
strncpy(m_szDBIP, reader.Get("Server", "DefaultIP", "localhost").c_str(), sizeof(m_szDBIP));
m_szDBIP[sizeof(m_szDBIP) - 1] = '\0';
m_bDBEnabled = true;
}
else
m_bDBEnabled = false;
// Load the modules
while (bC)
{
char mn[15];
snprintf(mn, sizeof(mn), "%zu", i);
// Try to get the module name
str = reader.Get("Modules", std::string(mn), "NOT_FOUND");
if (str.compare("NOT_FOUND") == 0)
{
bC = false; // This is the last module, exit
}
else
{
// Get the fields of the module section
std::set<std::string> set = reader.GetFields(str);
if (set.size() < 1)
// Just load without the configuration if we don't have any
mngr->LoadMSModule(str.c_str());
else
{
ModuleConfigMap map;
for (k = 0; k < set.size(); k++)
{
// Get the string on the specific position
std::set<std::string>::iterator it = set.begin();
std::advance(it, k);
// Set the map value to the field
map[*it] = reader.Get(str, *it, "");
}
// Load the module with the config
mngr->LoadMSModule(str.c_str(), map);
}
}
// Advance to the next load
i++;
}
return true;
}
int CConfig::m_DBPort = 0;
char CConfig::m_szDBHost[MAX_INI_BUFFER+1] = {0};
char CConfig::m_szDBUser[MAX_INI_BUFFER+1] = {0};
char CConfig::m_szDBPass[MAX_INI_BUFFER+1] = {0};
char CConfig::m_szDBSock[MAX_INI_BUFFER+1] = {0};
char CConfig::m_szDBName[MAX_INI_BUFFER+1] = {0};
char CConfig::m_szDBIP[MAX_INI_BUFFER+1] = {0};
bool CConfig::m_bDBEnabled = false;
EDatabaseType CConfig::m_eDatabaseType = DATABASE_TYPE_MARIADB;