1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
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.
10 //
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.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <client/ClientFileHandler.h>
22 #include <client/ClientState.h>
23 #include <client/ScorchedClient.h>
24 #include <engine/ModFiles.h>
25 #include <engine/ModFileEntryLoader.h>
26 #include <dialogs/ProgressDialog.h>
27 #include <common/Logger.h>
28 #include <common/Defines.h>
29 #include <common/OptionsScorched.h>
30 #include <coms/ComsMessageSender.h>
31 #include <coms/ComsFileMessage.h>
32 #include <coms/ComsFileAkMessage.h>
33 #include <lang/LangResource.h>
34
35 ClientFileHandler *ClientFileHandler::instance_ = 0;
36
instance()37 ClientFileHandler *ClientFileHandler::instance()
38 {
39 if (!instance_)
40 {
41 instance_ = new ClientFileHandler;
42 }
43 return instance_;
44 }
45
ClientFileHandler()46 ClientFileHandler::ClientFileHandler() : totalBytes_(0)
47 {
48 ScorchedClient::instance()->getComsMessageHandler().addHandler(
49 ComsFileMessage::ComsFileMessageType,
50 this);
51 }
52
~ClientFileHandler()53 ClientFileHandler::~ClientFileHandler()
54 {
55 }
56
processMessage(NetMessage & netMessage,const char * messageType,NetBufferReader & mainreader)57 bool ClientFileHandler::processMessage(
58 NetMessage &netMessage,
59 const char *messageType,
60 NetBufferReader &mainreader)
61 {
62 ComsFileMessage message;
63 if (!message.readMessage(mainreader)) return false;
64 NetBufferReader reader(message.fileBuffer);
65
66 std::map<std::string, ModFileEntry *> &files =
67 ScorchedClient::instance()->getModFiles().getFiles();
68
69 // Read the files one at a time
70 for (;;)
71 {
72 // Read the filename
73 // a zero length name means last file
74 std::string fileName;
75 reader.getFromBuffer(fileName);
76 if (fileName.size() == 0) break;
77
78 // Check if we are adding or removing this file
79 bool addFile = true;
80 reader.getFromBuffer(addFile);
81 if (addFile)
82 {
83 // Read flags
84 bool firstChunk = false;
85 bool lastChunk = false;
86 reader.getFromBuffer(firstChunk);
87 reader.getFromBuffer(lastChunk);
88
89 // Read file count
90 unsigned int bytesLeft = 0;
91 reader.getFromBuffer(bytesLeft);
92 if (totalBytes_ == 0) totalBytes_ = bytesLeft;
93
94 // Update progress
95 unsigned int doneBytes = totalBytes_ - bytesLeft;
96 unsigned int percentage = (unsigned int)(((doneBytes / 1024) * 100) / (totalBytes_ / 1024));
97 ProgressDialog::instance()->progressChange(
98 LANG_RESOURCE_3("DOWNLOADING_FILE", "Downloading mod, {0}% {1}/{2} KB",
99 percentage, (doneBytes / 1024), (totalBytes_ / 1024)), float(percentage));
100
101 // Read the size
102 unsigned int uncompressedSize = 0;
103 unsigned int chunkSize = 0;
104 reader.getFromBuffer(uncompressedSize);
105 reader.getFromBuffer(chunkSize);
106
107 // The first part
108 if (firstChunk)
109 {
110 recvBuffer_.reset();
111 }
112
113 // Add the bytes to the file
114 recvBuffer_.addDataToBuffer(
115 reader.getBuffer() + reader.getReadSize(), chunkSize);
116 reader.setReadSize(reader.getReadSize() + chunkSize);
117
118 // Check if we have finished this file
119 if (lastChunk)
120 {
121 // Finished
122 Logger::log(S3D::formatStringBuffer(" %u/%u %s - %i bytes",
123 doneBytes,
124 totalBytes_,
125 fileName.c_str(),
126 recvBuffer_.getBufferUsed()));
127
128 // Wrong size
129 if (recvBuffer_.getBufferUsed() != uncompressedSize)
130 {
131 Logger::log(S3D::formatStringBuffer("Downloaded mod file incorrect size \"%s\".\n"
132 "Expected %u, got %u.",
133 fileName.c_str(), recvBuffer_.getBufferUsed(), uncompressedSize));
134 return false;
135 }
136
137 // Write file
138 if (!ModFileEntryLoader::writeModFile(recvBuffer_, fileName.c_str(),
139 ScorchedClient::instance()->getOptionsGame().getMod()))
140 {
141 Logger::log(S3D::formatStringBuffer("Failed to write mod file \"%s\"",
142 fileName.c_str()));
143 return false;
144 }
145
146
147 // Remove the file if it already exists
148 std::map<std::string, ModFileEntry *>::iterator findItor =
149 files.find(fileName);
150 if (findItor != files.end())
151 {
152 delete (*findItor).second;
153 files.erase(findItor);
154 }
155
156 // Create a new file
157 ModFileEntry *fileEntry = new ModFileEntry(fileName, recvBuffer_.getCrc(), recvBuffer_.getBufferUsed());
158 files[fileName] = fileEntry;
159 }
160 }
161 else
162 {
163 // Remove the file
164 std::map<std::string, ModFileEntry *>::iterator findItor =
165 files.find(fileName);
166 if (findItor != files.end())
167 {
168 ModFileEntryLoader::removeModFile(fileName,
169 ScorchedClient::instance()->getOptionsGame().getMod());
170
171 Logger::log(S3D::formatStringBuffer(" %s - removed",
172 fileName.c_str()));
173
174 delete (*findItor).second;
175 files.erase(findItor);
176 }
177 }
178 }
179
180 // Send the ak for this file
181 ComsFileAkMessage akMessage;
182 ComsMessageSender::sendToServer(akMessage);
183
184 return true;
185 }
186