ModFileEntry.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/Defines.h>
00022 #include <engine/ModFileEntry.h>
00023 #include <engine/ModFiles.h>
00024 #include <zlib.h>
00025 
00026 ModFileEntry::ModFileEntry() : 
00027         compressedcrc_(0)
00028 {
00029 }
00030 
00031 ModFileEntry::~ModFileEntry()
00032 {
00033 }
00034 
00035 bool ModFileEntry::writeModFile(const std::string &fileName, 
00036                                                                 const std::string &modName)
00037 {
00038         // Check that this file is allowed to be sent
00039         if (ModFiles::excludeFile(fileName)) return true;
00040 
00041         // Check the downloaded CRC matches the actual crc of the file
00042         unsigned int crc =  crc32(0L, Z_NULL, 0);
00043         crc = crc32(crc, (unsigned char *) 
00044                 compressedfile_.getBuffer(), compressedfile_.getBufferUsed());
00045         if (crc != compressedcrc_)
00046         {
00047                 S3D::dialogMessage("WriteModFile",
00048                         "File crc missmatch error");
00049                 return false;
00050         }
00051 
00052         // Decompress the actual file contents  
00053         NetBuffer fileContents;
00054         if (compressedfile_.getBufferUsed() > 0)
00055         {
00056                 unsigned long destLen = uncompressedSize_ + 10;
00057                 fileContents.allocate(destLen);
00058                 unsigned uncompressResult = 
00059                         uncompress((unsigned char *) fileContents.getBuffer(), &destLen, 
00060                         (unsigned char *) compressedfile_.getBuffer(), compressedfile_.getBufferUsed());
00061                 fileContents.setBufferUsed(destLen);
00062 
00063                 if (uncompressResult == Z_MEM_ERROR) S3D::dialogMessage(
00064                         "WriteModFile", "Memory error");
00065                 else if (uncompressResult == Z_DATA_ERROR) S3D::dialogMessage(
00066                         "WriteModFile", "Data error");
00067                 else if (uncompressResult == Z_BUF_ERROR) S3D::dialogMessage(
00068                         "WriteModFile", "Buffer error");
00069 
00070                 bool result = (Z_OK == uncompressResult);
00071                 if (!result) return false;
00072         }
00073         else
00074         {
00075                 fileContents.allocate(1);
00076                 fileContents.setBufferUsed(0);
00077         }
00078 
00079         // Create any needed directories
00080         char *dir = (char *) fileName.c_str();
00081         while (dir = strchr(dir, '/'))
00082         {
00083                 *dir = '\0';
00084                 std::string needdir = S3D::getModFile(S3D::formatStringBuffer("%s/%s", 
00085                         modName.c_str(), fileName.c_str()));
00086                 if (!S3D::dirExists(needdir)) S3D::dirMake(needdir);
00087                 *dir = '/';
00088                 dir++;
00089         }
00090 
00091         // Write the file 
00092         std::string needfile = S3D::getModFile(S3D::formatStringBuffer("%s/%s", 
00093                 modName.c_str(), fileName.c_str()));
00094         FILE *file = fopen(needfile.c_str(), "wb");
00095         if (!file)
00096         {
00097                 S3D::dialogMessage("WriteModFile",
00098                         "Create file error");
00099                 return false;
00100         }
00101         if (fwrite(fileContents.getBuffer(), sizeof(unsigned char), 
00102                 fileContents.getBufferUsed(), file) != fileContents.getBufferUsed())
00103         {
00104                 S3D::dialogMessage("WriteModFile",
00105                         "Write file error");
00106                 fclose(file);
00107                 return false;
00108         }
00109         fclose(file);
00110 
00111         return true;
00112 }
00113 
00114 bool ModFileEntry::loadModFile(const std::string &filename)
00115 {
00116         static NetBuffer fileContents;
00117         fileContents.reset();
00118         {
00119                 // Do translation on ASCII files to prevent CVS
00120                 // from being a biatch
00121                 bool translate = false;
00122                 if (ModFiles::fileEnding(filename, ".txt") ||
00123                         ModFiles::fileEnding(filename, ".xml"))
00124                 {
00125                         translate = true;
00126                 }
00127 
00128                 // Load the file into a coms buffer
00129                 FILE *file = fopen(filename.c_str(), "rb");
00130                 if (!file) return false;
00131                 int newSize = 0;
00132                 unsigned char buffer[256];
00133                 static NetBuffer tmpBuffer;
00134                 tmpBuffer.reset();
00135                 do
00136                 {
00137                         newSize = fread(buffer, 
00138                                 sizeof(unsigned char), 
00139                                 sizeof(buffer), 
00140                                 file);
00141                         tmpBuffer.addDataToBuffer(buffer, newSize);
00142                 }
00143                 while (newSize > 0);
00144                 fclose(file);
00145 
00146                 if (!translate)
00147                 {
00148                         fileContents.addDataToBuffer(
00149                                 tmpBuffer.getBuffer(),
00150                                 tmpBuffer.getBufferUsed());
00151                 }
00152                 else
00153                 {
00154                         for (unsigned i=0; i<tmpBuffer.getBufferUsed(); i++)
00155                         {
00156                                 if (i >= tmpBuffer.getBufferUsed() - 1 ||
00157                                         tmpBuffer.getBuffer()[i] != 13 ||
00158                                         tmpBuffer.getBuffer()[i+1] != 10)
00159                                 {       
00160                                         fileContents.addDataToBuffer(
00161                                                 &tmpBuffer.getBuffer()[i] , 1);
00162                                 }
00163                         }
00164                 }
00165         }
00166 
00167         uncompressedSize_ = fileContents.getBufferUsed();
00168         if (fileContents.getBufferUsed() > 0)
00169         {
00170                 // Allocate the needed space for the compressed file
00171                 unsigned long destLen = fileContents.getBufferUsed() + 100;
00172                 compressedfile_.allocate(destLen);
00173                 compressedfile_.reset();
00174 
00175                 // Compress the file into the new buffer
00176                 if (compress2(
00177                                 (unsigned char *) compressedfile_.getBuffer(), &destLen, 
00178                                 (unsigned char *) fileContents.getBuffer(), fileContents.getBufferUsed(), 
00179                                 6) != Z_OK)
00180                 {
00181                         return false;
00182                 }
00183                 compressedfile_.setBufferUsed(destLen);
00184 
00185                 // Get the crc for the new file
00186                 compressedcrc_ =  crc32(0L, Z_NULL, 0);
00187                 compressedcrc_ = crc32(compressedcrc_, 
00188                         (unsigned char *) compressedfile_.getBuffer(), compressedfile_.getBufferUsed());
00189         }
00190         else
00191         {
00192                 compressedfile_.allocate(1);
00193                 compressedfile_.setBufferUsed(0);
00194         }
00195 
00196         return true;
00197 }

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3