1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
4 // This file is part of Scorched3D.
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 <common/Defines.h>
22 #include <engine/ModFileEntry.h>
23 #include <engine/ModFiles.h>
26 ModFileEntry::ModFileEntry() :
31 ModFileEntry::~ModFileEntry()
35 bool ModFileEntry::writeModFile(const std::string
&fileName
,
36 const std::string
&modName
)
38 // Check that this file is allowed to be sent
39 if (ModFiles::excludeFile(fileName
)) return true;
41 // Check the downloaded CRC matches the actual crc of the file
42 unsigned int crc
= crc32(0L, Z_NULL
, 0);
43 crc
= crc32(crc
, (unsigned char *)
44 compressedfile_
.getBuffer(), compressedfile_
.getBufferUsed());
45 if (crc
!= compressedcrc_
)
47 S3D::dialogMessage("WriteModFile",
48 "File crc missmatch error");
52 // Decompress the actual file contents
53 NetBuffer fileContents
;
54 if (compressedfile_
.getBufferUsed() > 0)
56 unsigned long destLen
= uncompressedSize_
+ 10;
57 fileContents
.allocate(destLen
);
58 unsigned uncompressResult
=
59 uncompress((unsigned char *) fileContents
.getBuffer(), &destLen
,
60 (unsigned char *) compressedfile_
.getBuffer(), compressedfile_
.getBufferUsed());
61 fileContents
.setBufferUsed(destLen
);
63 if (uncompressResult
== Z_MEM_ERROR
) S3D::dialogMessage(
64 "WriteModFile", "Memory error");
65 else if (uncompressResult
== Z_DATA_ERROR
) S3D::dialogMessage(
66 "WriteModFile", "Data error");
67 else if (uncompressResult
== Z_BUF_ERROR
) S3D::dialogMessage(
68 "WriteModFile", "Buffer error");
70 bool result
= (Z_OK
== uncompressResult
);
71 if (!result
) return false;
75 fileContents
.allocate(1);
76 fileContents
.setBufferUsed(0);
79 // Create any needed directories
80 char *dir
= (char *) fileName
.c_str();
81 while (dir
= strchr(dir
, '/'))
84 std::string needdir
= S3D::getModFile(S3D::formatStringBuffer("%s/%s",
85 modName
.c_str(), fileName
.c_str()));
86 if (!S3D::dirExists(needdir
)) S3D::dirMake(needdir
);
92 std::string needfile
= S3D::getModFile(S3D::formatStringBuffer("%s/%s",
93 modName
.c_str(), fileName
.c_str()));
94 FILE *file
= fopen(needfile
.c_str(), "wb");
97 S3D::dialogMessage("WriteModFile",
101 if (fwrite(fileContents
.getBuffer(), sizeof(unsigned char),
102 fileContents
.getBufferUsed(), file
) != fileContents
.getBufferUsed())
104 S3D::dialogMessage("WriteModFile",
114 bool ModFileEntry::loadModFile(const std::string
&filename
)
116 static NetBuffer fileContents
;
117 fileContents
.reset();
119 // Do translation on ASCII files to prevent CVS
120 // from being a biatch
121 bool translate
= false;
122 if (ModFiles::fileEnding(filename
, ".txt") ||
123 ModFiles::fileEnding(filename
, ".xml"))
128 // Load the file into a coms buffer
129 FILE *file
= fopen(filename
.c_str(), "rb");
130 if (!file
) return false;
132 unsigned char buffer
[256];
133 static NetBuffer tmpBuffer
;
137 newSize
= fread(buffer
,
138 sizeof(unsigned char),
141 tmpBuffer
.addDataToBuffer(buffer
, newSize
);
148 fileContents
.addDataToBuffer(
149 tmpBuffer
.getBuffer(),
150 tmpBuffer
.getBufferUsed());
154 for (unsigned i
=0; i
<tmpBuffer
.getBufferUsed(); i
++)
156 if (i
>= tmpBuffer
.getBufferUsed() - 1 ||
157 tmpBuffer
.getBuffer()[i
] != 13 ||
158 tmpBuffer
.getBuffer()[i
+1] != 10)
160 fileContents
.addDataToBuffer(
161 &tmpBuffer
.getBuffer()[i
] , 1);
167 uncompressedSize_
= fileContents
.getBufferUsed();
168 if (fileContents
.getBufferUsed() > 0)
170 // Allocate the needed space for the compressed file
171 unsigned long destLen
= fileContents
.getBufferUsed() + 100;
172 compressedfile_
.allocate(destLen
);
173 compressedfile_
.reset();
175 // Compress the file into the new buffer
177 (unsigned char *) compressedfile_
.getBuffer(), &destLen
,
178 (unsigned char *) fileContents
.getBuffer(), fileContents
.getBufferUsed(),
183 compressedfile_
.setBufferUsed(destLen
);
185 // Get the crc for the new file
186 compressedcrc_
= crc32(0L, Z_NULL
, 0);
187 compressedcrc_
= crc32(compressedcrc_
,
188 (unsigned char *) compressedfile_
.getBuffer(), compressedfile_
.getBufferUsed());
192 compressedfile_
.allocate(1);
193 compressedfile_
.setBufferUsed(0);