forked from Jo-Tran/cryingdamson-0.3.6-8.60-V8.2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameservers.cpp
186 lines (159 loc) · 5.05 KB
/
gameservers.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
////////////////////////////////////////////////////////////////////////
// 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 3 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, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include <iostream>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "gameservers.h"
#include "tools.h"
void GameServers::clear()
{
for(GameServersMap::iterator it = serverList.begin(); it != serverList.end(); ++it)
delete it->second;
serverList.clear();
}
bool GameServers::reload(bool showResult/* = true*/)
{
clear();
return loadFromXml(showResult);
}
bool GameServers::loadFromXml(bool showResult/* = true*/)
{
xmlDocPtr doc = xmlParseFile(getFilePath(FILE_TYPE_XML, "servers.xml").c_str());
if(!doc)
{
std::cout << "[Warning - GameServers::loadFromXml] Cannot load servers file." << std::endl;
std::cout << getLastXMLError() << std::endl;
return false;
}
xmlNodePtr p, root = xmlDocGetRootElement(doc);
if(xmlStrcmp(root->name,(const xmlChar*)"servers"))
{
std::cout << "[Error - GameServers::loadFromXml] Malformed servers file." << std::endl;
xmlFreeDoc(doc);
return false;
}
std::string strValue;
int32_t intValue;
p = root->children;
while(p)
{
if(xmlStrcmp(p->name, (const xmlChar*)"server"))
{
p = p->next;
continue;
}
std::string name, address;
uint32_t id, versionMin, versionMax, port;
if(readXMLInteger(p, "id", intValue))
id = intValue;
else
{
std::cout << "[Error - GameServers::loadFromXml] Missing id, skipping" << std::endl;
p = p->next;
continue;
}
if(getServerById(id))
{
std::cout << "[Error - GameServers::loadFromXml] Duplicate server id " << id << ", skipping" << std::endl;
p = p->next;
continue;
}
if(readXMLString(p, "name", strValue))
name = strValue;
else
{
name = "Server #" + id;
std::cout << "[Warning - GameServers::loadFromXml] Missing name for server " << id << ", using default" << std::endl;
}
if(readXMLInteger(p, "versionMin", intValue))
versionMin = intValue;
else
{
versionMin = CLIENT_VERSION_MIN;
std::cout << "[Warning - GameServers::loadFromXml] Missing versionMin for server " << id << ", using default" << std::endl;
}
if(readXMLInteger(p, "versionMax", intValue))
versionMax = intValue;
else
{
versionMax = CLIENT_VERSION_MAX;
std::cout << "[Warning - GameServers::loadFromXml] Missing versionMax for server " << id << ", using default" << std::endl;
}
if(readXMLString(p, "address", strValue) || readXMLString(p, "ip", strValue))
address = strValue;
else
{
address = "localhost";
std::cout << "[Warning - GameServers::loadFromXml] Missing address for server " << id << ", using default" << std::endl;
}
if(readXMLInteger(p, "port", intValue))
port = intValue;
else
{
port = 7171;
std::cout << "[Warning - GameServers::loadFromXml] Missing port for server " << id << ", using default" << std::endl;
}
if(GameServer* server = new GameServer(name, versionMin, versionMax, inet_addr(address.c_str()), port))
serverList[id] = server;
else
std::cout << "[Error - GameServers::loadFromXml] Couldn't add server " << name << std::endl;
p = p->next;
}
xmlFreeDoc(doc);
if(showResult)
{
std::cout << "> Servers loaded:" << std::endl;
for(GameServersMap::iterator it = serverList.begin(); it != serverList.end(); it++)
std::cout << it->second->getName() << " (" << it->second->getAddress() << ":" << it->second->getPort() << ")" << std::endl;
}
return true;
}
GameServer* GameServers::getServerById(uint32_t id) const
{
GameServersMap::const_iterator it = serverList.find(id);
if(it != serverList.end())
return it->second;
return NULL;
}
GameServer* GameServers::getServerByName(std::string name) const
{
for(GameServersMap::const_iterator it = serverList.begin(); it != serverList.end(); ++it)
{
if(it->second->getName() == name)
return it->second;
}
return NULL;
}
GameServer* GameServers::getServerByAddress(uint32_t address) const
{
for(GameServersMap::const_iterator it = serverList.begin(); it != serverList.end(); ++it)
{
if(it->second->getAddress() == address)
return it->second;
}
return NULL;
}
GameServer* GameServers::getServerByPort(uint32_t port) const
{
for(GameServersMap::const_iterator it = serverList.begin(); it != serverList.end(); ++it)
{
if(it->second->getPort() == port)
return it->second;
}
return NULL;
}