customservers.cpp
1 //------------------------------------------------------------------------------
2 // customservers.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "customservers.h"
24 #include "configuration/doomseekerconfig.h"
25 #include "plugins/engineplugin.h"
26 #include "plugins/pluginloader.h"
27 #include "serverapi/server.h"
28 #include "log.h"
29 #include <QHostInfo>
30 #include <QUrl>
31 
32 void CustomServers::decodeConfigEntries(const QString& str, QList<CustomServerInfo>& outCustomServerInfoList)
33 {
34  outCustomServerInfoList.clear();
35 
36  int openingBracketIndex = 0;
37  int closingBracketIndex = 0;
38  bool bSeekClosingBracket = false;
39  for (int i = 0; i < str.length(); ++i)
40  {
41  if (!bSeekClosingBracket && str[i] == '(')
42  {
43  openingBracketIndex = i;
44  bSeekClosingBracket = true;
45  }
46  else if (bSeekClosingBracket && str[i] == ')')
47  {
48  closingBracketIndex = i;
49  bSeekClosingBracket = false;
50 
51  QString entry = str.mid(openingBracketIndex + 1, closingBracketIndex - (openingBracketIndex + 1));
52  QStringList entryList = entry.split(";");
53 
54  if (entryList.size() == 3)
55  {
56  CustomServerInfo customServerInfo;
57  customServerInfo.engine = QUrl::fromPercentEncoding(entryList[0].toAscii());
58 
59  int engineIndex = gPlugins->pluginIndexFromName(customServerInfo.engine);
60  customServerInfo.engineIndex = engineIndex;
61 
62  customServerInfo.host = QUrl::fromPercentEncoding(entryList[1].toAscii());
63 
64  bool ok = false;
65  int port = QString(entryList[2]).toInt(&ok);
66  if (ok && port >= 1 && port <= 65535)
67  {
68  customServerInfo.port = port;
69  }
70  else if (engineIndex >= 0)
71  {
72  const PluginLoader::Plugin* pPlugin = gPlugins->plugin(engineIndex);
73  customServerInfo.port = pPlugin->info()->data()->defaultServerPort;
74  }
75  else
76  {
77  customServerInfo.port = 1;
78  }
79 
80  outCustomServerInfoList << customServerInfo;
81  } // end of if
82  } // end of else if
83  } // end of for
84 }
85 
86 void CustomServers::readConfig(QObject* receiver, const char* slotUpdated, const char* slotBegunRefreshing)
87 {
88  QList<CustomServerInfo> customServerInfoList = gConfig.doomseeker.customServers.toList();
89  setServers(customServerInfoList, receiver, slotUpdated, slotBegunRefreshing);
90 }
91 
92 void CustomServers::setServers(const QList<CustomServerInfo>& csiList, QObject* receiver, const char* slotUpdated, const char* slotBegunRefreshing)
93 {
95 
96  foreach (const CustomServerInfo& customServerInfo, csiList)
97  {
98  if (customServerInfo.engineIndex < 0)
99  {
100  // Unknown engine.
101  gLog << tr("Unknown game for custom server %1:%2")
102  .arg(customServerInfo.host).arg(customServerInfo.port);
103  continue;
104  }
105 
106  QHostAddress address;
107  if (!address.setAddress(customServerInfo.host))
108  {
109  QHostInfo hostInfo(QHostInfo::fromName(customServerInfo.host));
110  if (hostInfo.addresses().size() == 0)
111  {
112  // Can't decipher address.
113  gLog << tr("Failed to resolve address for custom server %1:%2")
114  .arg(customServerInfo.host).arg(customServerInfo.port);
115  continue;
116  }
117  address = hostInfo.addresses().first();
118  }
119 
120  const EnginePlugin* pInterface = gPlugins->plugin(customServerInfo.engineIndex)->info();
121  ServerPtr p = pInterface->server(address, customServerInfo.port);
122  if(p == NULL)
123  {
124  gLog << tr("Plugin returned NULL \"Server*\" for custom server %1:%2. "
125  "This is a problem with the plugin.")
126  .arg(customServerInfo.host).arg(customServerInfo.port);
127  continue;
128  }
129  p->setCustom(true);
130 
131  connect(p.data(), SIGNAL( updated(ServerPtr, int) ), receiver, slotUpdated);
132  connect(p.data(), SIGNAL( begunRefreshing(ServerPtr) ), receiver, slotBegunRefreshing);
134  }
135 }
void registerNewServer(ServerPtr server)
Registers new server with this MasterClient.
void setServers(const QList< CustomServerInfo > &csiList, QObject *receiver, const char *slotUpdated, const char *slotBegunRefreshing)
void readConfig(QObject *receiver, const char *slotUpdated, const char *slotBegunRefreshing)
static void decodeConfigEntries(const QString &str, QList< CustomServerInfo > &outCustomServerInfoList)
void emptyServerList()
virtual ServerPtr server(const QHostAddress &address, unsigned short port) const
Creates an instance of server object from this plugin.
EnginePlugin * info() const
Main plugin interface.