forked from Flatlander57/TheImaginedServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameservers.cpp
151 lines (130 loc) · 4.43 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
////////////////////////////////////////////////////////////////////////
// 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()
{
clear();
return loadFromXml(false);
}
bool GameServers::loadFromXml(bool result)
{
xmlDocPtr doc = xmlParseFile(getFilePath(FILE_TYPE_XML,"servers.xml").c_str());
if(!doc)
{
std::clog << "[Warning - GameServers::loadFromXml] Cannot load servers file." << std::endl;
std::clog << getLastXMLError() << std::endl;
return false;
}
xmlNodePtr root = xmlDocGetRootElement(doc);
if(xmlStrcmp(root->name,(const xmlChar*)"servers"))
{
std::clog << "[Error - GameServers::loadFromXml] Malformed servers file." << std::endl;
xmlFreeDoc(doc);
return false;
}
std::string strValue;
int32_t intValue;
for(xmlNodePtr p = root->children; p; p = p->next)
{
if(xmlStrcmp(p->name, (const xmlChar*)"server"))
continue;
std::string name, address;
uint32_t id, versionMin, versionMax;
IntegerVec ports;
if(readXMLInteger(p, "id", intValue))
id = intValue;
else
{
std::clog << "[Error - GameServers::loadFromXml] Missing id, skipping" << std::endl;
continue;
}
if(serverList.find(id) != serverList.end())
{
std::clog << "[Error - GameServers::loadFromXml] Duplicate server id " << id << ", skipping" << std::endl;
continue;
}
if(readXMLString(p, "name", strValue))
name = strValue;
else
{
name = "Server #" + id;
std::clog << "[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::clog << "[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::clog << "[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::clog << "[Warning - GameServers::loadFromXml] Missing address for server " << id << ", using default" << std::endl;
}
if(readXMLString(p, "port", strValue))
ports = vectorAtoi(explodeString(strValue, ","));
else
{
ports.push_back(7181);
std::clog << "[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()), ports))
serverList[id] = server;
else
std::clog << "[Error - GameServers::loadFromXml] Couldn't add server " << name << std::endl;
}
if(result)
{
std::clog << "> Servers loaded:" << std::endl;
for(GameServersMap::iterator it = serverList.begin(); it != serverList.end(); ++it)
{
IntegerVec games = it->second->getPorts();
for(IntegerVec::const_iterator tit = games.begin(); tit != games.end(); ++tit)
std::clog << it->second->getName() << " (" << it->second->getAddress() << ":" << *tit << ")" << std::endl;
}
}
xmlFreeDoc(doc);
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;
}