PackageTool.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. unsigned totalDataSize = 0;
  153. // Write file data, calculate checksums & correct offsets
  154. for (unsigned i = 0; i < entries_.Size(); ++i)
  155. {
  156. entries_[i].offset_ = dest.GetSize();
  157. String fileFullPath = rootDir + "/" + entries_[i].name_;
  158. File srcFile(context_, fileFullPath);
  159. if (!srcFile.IsOpen())
  160. ErrorExit("Could not open file " + fileFullPath);
  161. unsigned dataSize = entries_[i].size_;
  162. totalDataSize += dataSize;
  163. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  164. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  165. ErrorExit("Could not read file " + fileFullPath);
  166. srcFile.Close();
  167. for (unsigned j = 0; j < dataSize; ++j)
  168. {
  169. checksum_ = SDBMHash(checksum_, buffer[j]);
  170. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  171. }
  172. if (!compress_)
  173. {
  174. PrintLine(entries_[i].name_ + " size " + String(dataSize));
  175. dest.Write(&buffer[0], entries_[i].size_);
  176. }
  177. else
  178. {
  179. SharedArrayPtr<unsigned char> compressBuffer(new unsigned char[LZ4_compressBound(blockSize_)]);
  180. unsigned pos = 0;
  181. unsigned totalPackedBytes = 0;
  182. while (pos < dataSize)
  183. {
  184. unsigned unpackedSize = blockSize_;
  185. if (pos + unpackedSize > dataSize)
  186. unpackedSize = dataSize - pos;
  187. unsigned packedSize = LZ4_compressHC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize);
  188. if (!packedSize)
  189. ErrorExit("LZ4 compression failed for file " + entries_[i].name_ + " at offset " + pos);
  190. dest.WriteUShort(unpackedSize);
  191. dest.WriteUShort(packedSize);
  192. dest.Write(compressBuffer.Get(), packedSize);
  193. totalPackedBytes += 6 + packedSize;
  194. pos += unpackedSize;
  195. }
  196. PrintLine(entries_[i].name_ + " in " + String(dataSize) + " out " + String(totalPackedBytes));
  197. }
  198. }
  199. // Write header again with correct offsets & checksums
  200. dest.Seek(0);
  201. WriteHeader(dest);
  202. for (unsigned i = 0; i < entries_.Size(); ++i)
  203. {
  204. dest.WriteString(entries_[i].name_);
  205. dest.WriteUInt(entries_[i].offset_);
  206. dest.WriteUInt(entries_[i].size_);
  207. dest.WriteUInt(entries_[i].checksum_);
  208. }
  209. PrintLine("Number of files " + String(entries_.Size()));
  210. PrintLine("File data size " + String(totalDataSize));
  211. PrintLine("Package size " + String(dest.GetSize()));
  212. }
  213. void WriteHeader(File& dest)
  214. {
  215. if (!compress_)
  216. dest.WriteFileID("UPAK");
  217. else
  218. dest.WriteFileID("ULZ4");
  219. dest.WriteUInt(entries_.Size());
  220. dest.WriteUInt(checksum_);
  221. }