git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / client / ClientConnectionAcceptHandler.cpp
blob0649b88ffac5f05bc126072176e74128b4f8f509
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <client/ClientConnectionAcceptHandler.h>
22 #include <client/ScorchedClient.h>
23 #include <dialogs/RulesDialog.h>
24 #include <dialogs/ConnectDialog.h>
25 #include <dialogs/ProgressDialog.h>
26 #include <engine/ModFiles.h>
27 #include <image/ImageFactory.h>
28 #include <image/ImagePng.h>
29 #include <tank/TankContainer.h>
30 #include <net/NetInterface.h>
31 #include <coms/ComsConnectAcceptMessage.h>
32 #include <coms/ComsHaveModFilesMessage.h>
33 #include <coms/ComsMessageSender.h>
34 #include <common/Logger.h>
35 #include <common/OptionsScorched.h>
36 #include <client/ClientParams.h>
37 #include <common/Defines.h>
39 ClientConnectionAcceptHandler *ClientConnectionAcceptHandler::instance_ = 0;
41 ClientConnectionAcceptHandler *ClientConnectionAcceptHandler::instance()
43 if (!instance_)
45 instance_ = new ClientConnectionAcceptHandler();
48 return instance_;
51 ClientConnectionAcceptHandler::ClientConnectionAcceptHandler()
53 ScorchedClient::instance()->getComsMessageHandler().addHandler(
54 "ComsConnectAcceptMessage",
55 this);
58 ClientConnectionAcceptHandler::~ClientConnectionAcceptHandler()
63 bool ClientConnectionAcceptHandler::processMessage(
64 NetMessage &netMessage,
65 const char *messageType,
66 NetBufferReader &reader)
68 ComsConnectAcceptMessage message;
69 if (!message.readMessage(reader)) return false;
71 if (ClientParams::instance()->getConnectedToServer())
73 unsigned int ip = netMessage.getIpAddress();
74 if (!ConnectDialog::instance()->getIdStore().saveUniqueId(
75 ip, message.getUniqueId(), message.getPublishAddress()))
77 Logger::log( "Server failed ip security check!");
78 return false;
82 // The server tells us what our id is.
83 // Set this id so we know what our players are
84 ScorchedClient::instance()->getTankContainer().
85 setCurrentDestinationId(message.getDestinationId());
87 // Tell the user to wait
88 Logger::log(S3D::formatStringBuffer(
89 "Connection accepted by \"%s\".\nPlease wait...",
90 message.getServerName()));
92 // Set the server specific png for the current server
94 GLTexture *texture = 0;
95 if (message.getServerPng().getBufferUsed() > 0)
97 // Use a custom icon
98 ImagePng map;
99 map.loadFromBuffer(message.getServerPng());
101 // Set the texture
102 texture = new GLTexture;
103 texture->create(map, false);
105 else
107 // Use the default icon
108 std::string file1(S3D::getDataFile("data/windows/scorched.png"));
109 std::string file2(S3D::getDataFile("data/windows/scorcheda.png"));
110 ImageHandle map = ImageFactory::loadImageHandle(
111 file1.c_str(), file2.c_str(), false);
113 // Set the texture
114 texture = new GLTexture;
115 texture->create(map, false);
118 RulesDialog::instance()->addIcon(texture);
121 // Set the mod
122 S3D::setDataFileMod(
123 ScorchedClient::instance()->getOptionsGame().getMod());
125 // Load any mod files we currently have for the mod
126 // the server is using.
127 if (ClientParams::instance()->getConnectedToServer())
129 if (!ScorchedClient::instance()->getModFiles().loadModFiles(
130 ScorchedClient::instance()->getOptionsGame().getMod(), true,
131 ProgressDialogSync::instance()))
133 S3D::dialogMessage("ModFiles",
134 S3D::formatStringBuffer("Failed to load mod \"%s\"",
135 ScorchedClient::instance()->getOptionsGame().getMod()));
136 return false;
140 // Tell the server what mod files we actually have
141 ComsHaveModFilesMessage comsFileMessage;
142 std::map<std::string, ModFileEntry *> &files =
143 ScorchedClient::instance()->getModFiles().getFiles();
144 std::map<std::string, ModFileEntry *>::iterator itor;
145 for (itor = files.begin();
146 itor != files.end();
147 itor++)
149 const std::string &name = (*itor).first;
150 ModFileEntry *file = (*itor).second;
152 comsFileMessage.getFiles().push_back(
153 ModIdentifierEntry(
154 name.c_str(),
155 file->getCompressedSize(),
156 file->getCompressedCrc()));
158 if (!ComsMessageSender::sendToServer(comsFileMessage)) return false;
160 return true;