PackageTool.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Context.h"
  23. #include "ArrayPtr.h"
  24. #include "File.h"
  25. #include "FileSystem.h"
  26. #include "ProcessUtils.h"
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #endif
  30. #include <cstdio>
  31. #include <cstring>
  32. #include <lz4.h>
  33. #include <lz4hc.h>
  34. #include "DebugNew.h"
  35. using namespace Urho3D;
  36. static const unsigned COMPRESSED_BLOCK_SIZE = 32768;
  37. struct FileEntry
  38. {
  39. String name_;
  40. unsigned offset_;
  41. unsigned size_;
  42. unsigned checksum_;
  43. };
  44. SharedPtr<Context> context_(new Context());
  45. SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
  46. String basePath_;
  47. Vector<FileEntry> entries_;
  48. unsigned checksum_ = 0;
  49. bool compress_ = false;
  50. unsigned blockSize_ = COMPRESSED_BLOCK_SIZE;
  51. String ignoreExtensions_[] = {
  52. ".bak",
  53. ".rule",
  54. ""
  55. };
  56. int main(int argc, char** argv);
  57. void Run(const Vector<String>& arguments);
  58. void ProcessFile(const String& fileName, const String& rootDir);
  59. void WritePackageFile(const String& fileName, const String& rootDir);
  60. void WriteHeader(File& dest);
  61. int main(int argc, char** argv)
  62. {
  63. Vector<String> arguments;
  64. #ifdef WIN32
  65. arguments = ParseArguments(GetCommandLineW());
  66. #else
  67. arguments = ParseArguments(argc, argv);
  68. #endif
  69. Run(arguments);
  70. return 0;
  71. }
  72. void Run(const Vector<String>& arguments)
  73. {
  74. if (arguments.Size() < 2)
  75. ErrorExit("Usage: PackageTool <directory to process> <package name> [basepath] [-c]\n");
  76. const String& dirName = arguments[0];
  77. const String& packageName = arguments[1];
  78. if (arguments.Size() > 2)
  79. {
  80. for (unsigned i = 2; i < arguments.Size(); ++i)
  81. {
  82. if (arguments[i][0] != '-')
  83. basePath_ = AddTrailingSlash(arguments[i]);
  84. else
  85. {
  86. if (arguments[i].Length() > 1)
  87. {
  88. switch (arguments[i][1])
  89. {
  90. case 'c':
  91. compress_ = true;
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. PrintLine("Scanning directory " + dirName + " for files");
  99. // Get the file list recursively
  100. Vector<String> fileNames;
  101. fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
  102. if (!fileNames.Size())
  103. ErrorExit("No files found");
  104. // Check for extensions to ignore
  105. for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
  106. {
  107. String extension = GetExtension(fileNames[i]);
  108. for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
  109. {
  110. if (extension == ignoreExtensions_[j])
  111. {
  112. fileNames.Erase(fileNames.Begin() + i);
  113. break;
  114. }
  115. }
  116. }
  117. for (unsigned i = 0; i < fileNames.Size(); ++i)
  118. ProcessFile(fileNames[i], dirName);
  119. WritePackageFile(packageName, dirName);
  120. }
  121. void ProcessFile(const String& fileName, const String& rootDir)
  122. {
  123. String fullPath = rootDir + "/" + fileName;
  124. File file(context_);
  125. if (!file.Open(fullPath))
  126. ErrorExit("Could not open file " + fileName);
  127. if (!file.GetSize())
  128. return;
  129. FileEntry newEntry;
  130. newEntry.name_ = fileName;
  131. newEntry.offset_ = 0; // Offset not yet known
  132. newEntry.size_ = file.GetSize();
  133. newEntry.checksum_ = 0; // Will be calculated later
  134. entries_.Push(newEntry);
  135. }
  136. void WritePackageFile(const String& fileName, const String& rootDir)
  137. {
  138. PrintLine("Writing package");
  139. File dest(context_);
  140. if (!dest.Open(fileName, FILE_WRITE))
  141. ErrorExit("Could not open output file " + fileName);
  142. // Write ID, number of files & placeholder for checksum
  143. WriteHeader(dest);
  144. for (unsigned i = 0; i < entries_.Size(); ++i)
  145. {
  146. // Write entry (correct offset is still unknown, will be filled in later)
  147. dest.WriteString(entries_[i].name_);
  148. dest.WriteUInt(entries_[i].offset_);
  149. dest.WriteUInt(entries_[i].size_);
  150. dest.WriteUInt(entries_[i].checksum_);
  151. }
  152. // Write file data, calculate checksums & correct offsets
  153. for (unsigned i = 0; i < entries_.Size(); ++i)
  154. {
  155. entries_[i].offset_ = dest.GetSize();
  156. String fileFullPath = rootDir + "/" + entries_[i].name_;
  157. File srcFile(context_, fileFullPath);
  158. if (!srcFile.IsOpen())
  159. ErrorExit("Could not open file " + fileFullPath);
  160. unsigned dataSize = entries_[i].size_;
  161. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  162. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  163. ErrorExit("Could not read file " + fileFullPath);
  164. srcFile.Close();
  165. for (unsigned j = 0; j < dataSize; ++j)
  166. {
  167. checksum_ = SDBMHash(checksum_, buffer[j]);
  168. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  169. }
  170. if (!compress_)
  171. {
  172. PrintLine(entries_[i].name_ + " size " + String(dataSize));
  173. dest.Write(&buffer[0], entries_[i].size_);
  174. }
  175. else
  176. {
  177. SharedArrayPtr<unsigned char> compressBuffer(new unsigned char[LZ4_compressBound(blockSize_)]);
  178. unsigned pos = 0;
  179. unsigned totalPackedBytes = 0;
  180. while (pos < dataSize)
  181. {
  182. unsigned unpackedSize = blockSize_;
  183. if (pos + unpackedSize > dataSize)
  184. unpackedSize = dataSize - pos;
  185. unsigned packedSize = LZ4_compressHC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize);
  186. if (!packedSize)
  187. ErrorExit("LZ4 compression failed for file " + entries_[i].name_ + " at offset " + pos);
  188. dest.WriteUShort(unpackedSize);
  189. dest.WriteUShort(packedSize);
  190. dest.Write(compressBuffer.Get(), packedSize);
  191. totalPackedBytes += 6 + packedSize;
  192. pos += unpackedSize;
  193. }
  194. PrintLine(entries_[i].name_ + " size " + String(dataSize) + " packed " + String(totalPackedBytes));
  195. }
  196. }
  197. // Write header again with correct offsets & checksums
  198. dest.Seek(0);
  199. WriteHeader(dest);
  200. for (unsigned i = 0; i < entries_.Size(); ++i)
  201. {
  202. dest.WriteString(entries_[i].name_);
  203. dest.WriteUInt(entries_[i].offset_);
  204. dest.WriteUInt(entries_[i].size_);
  205. dest.WriteUInt(entries_[i].checksum_);
  206. }
  207. PrintLine("Package total size " + String(dest.GetSize()));
  208. }
  209. void WriteHeader(File& dest)
  210. {
  211. if (!compress_)
  212. dest.WriteFileID("UPAK");
  213. else
  214. dest.WriteFileID("ULZ4");
  215. dest.WriteUInt(entries_.Size());
  216. dest.WriteUInt(checksum_);
  217. if (compress_)
  218. dest.WriteUInt(blockSize_);
  219. }