PackageTool.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // Copyright (c) 2008-2015 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 <Urho3D/Core/Context.h>
  23. #include <Urho3D/Container/ArrayPtr.h>
  24. #include <Urho3D/Core/ProcessUtils.h>
  25. #include <Urho3D/IO/File.h>
  26. #include <Urho3D/IO/FileSystem.h>
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #endif
  30. #include <LZ4/lz4.h>
  31. #include <LZ4/lz4hc.h>
  32. #include <Urho3D/DebugNew.h>
  33. using namespace Urho3D;
  34. static const unsigned COMPRESSED_BLOCK_SIZE = 32768;
  35. struct FileEntry
  36. {
  37. String name_;
  38. unsigned offset_;
  39. unsigned size_;
  40. unsigned checksum_;
  41. };
  42. SharedPtr<Context> context_(new Context());
  43. SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
  44. String basePath_;
  45. Vector<FileEntry> entries_;
  46. unsigned checksum_ = 0;
  47. bool compress_ = false;
  48. bool quiet_ = false;
  49. unsigned blockSize_ = COMPRESSED_BLOCK_SIZE;
  50. String ignoreExtensions_[] = {
  51. ".bak",
  52. ".rule",
  53. ""
  54. };
  55. int main(int argc, char** argv);
  56. void Run(const Vector<String>& arguments);
  57. void ProcessFile(const String& fileName, const String& rootDir);
  58. void WritePackageFile(const String& fileName, const String& rootDir);
  59. void WriteHeader(File& dest);
  60. int main(int argc, char** argv)
  61. {
  62. Vector<String> arguments;
  63. #ifdef WIN32
  64. arguments = ParseArguments(GetCommandLineW());
  65. #else
  66. arguments = ParseArguments(argc, argv);
  67. #endif
  68. Run(arguments);
  69. return 0;
  70. }
  71. void Run(const Vector<String>& arguments)
  72. {
  73. if (arguments.Size() < 2)
  74. ErrorExit(
  75. "Usage: PackageTool <directory to process> <package name> [basepath] [options]\n"
  76. "\n"
  77. "Options:\n"
  78. "-c Enable package file LZ4 compression\n"
  79. "-q Enable quiet mode\n"
  80. );
  81. const String& dirName = arguments[0];
  82. const String& packageName = arguments[1];
  83. if (arguments.Size() > 2)
  84. {
  85. for (unsigned i = 2; i < arguments.Size(); ++i)
  86. {
  87. if (arguments[i][0] != '-')
  88. basePath_ = AddTrailingSlash(arguments[i]);
  89. else
  90. {
  91. if (arguments[i].Length() > 1)
  92. {
  93. switch (arguments[i][1])
  94. {
  95. case 'c':
  96. compress_ = true;
  97. break;
  98. case 'q':
  99. quiet_ = true;
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. if (!quiet_)
  107. PrintLine("Scanning directory " + dirName + " for files");
  108. // Get the file list recursively
  109. Vector<String> fileNames;
  110. fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
  111. if (!fileNames.Size())
  112. ErrorExit("No files found");
  113. // Check for extensions to ignore
  114. for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
  115. {
  116. String extension = GetExtension(fileNames[i]);
  117. for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
  118. {
  119. if (extension == ignoreExtensions_[j])
  120. {
  121. fileNames.Erase(fileNames.Begin() + i);
  122. break;
  123. }
  124. }
  125. }
  126. for (unsigned i = 0; i < fileNames.Size(); ++i)
  127. ProcessFile(fileNames[i], dirName);
  128. WritePackageFile(packageName, dirName);
  129. }
  130. void ProcessFile(const String& fileName, const String& rootDir)
  131. {
  132. String fullPath = rootDir + "/" + fileName;
  133. File file(context_);
  134. if (!file.Open(fullPath))
  135. ErrorExit("Could not open file " + fileName);
  136. if (!file.GetSize())
  137. return;
  138. FileEntry newEntry;
  139. newEntry.name_ = fileName;
  140. newEntry.offset_ = 0; // Offset not yet known
  141. newEntry.size_ = file.GetSize();
  142. newEntry.checksum_ = 0; // Will be calculated later
  143. entries_.Push(newEntry);
  144. }
  145. void WritePackageFile(const String& fileName, const String& rootDir)
  146. {
  147. if (!quiet_)
  148. PrintLine("Writing package");
  149. File dest(context_);
  150. if (!dest.Open(fileName, FILE_WRITE))
  151. ErrorExit("Could not open output file " + fileName);
  152. // Write ID, number of files & placeholder for checksum
  153. WriteHeader(dest);
  154. for (unsigned i = 0; i < entries_.Size(); ++i)
  155. {
  156. // Write entry (correct offset is still unknown, will be filled in later)
  157. dest.WriteString(entries_[i].name_);
  158. dest.WriteUInt(entries_[i].offset_);
  159. dest.WriteUInt(entries_[i].size_);
  160. dest.WriteUInt(entries_[i].checksum_);
  161. }
  162. unsigned totalDataSize = 0;
  163. // Write file data, calculate checksums & correct offsets
  164. for (unsigned i = 0; i < entries_.Size(); ++i)
  165. {
  166. entries_[i].offset_ = dest.GetSize();
  167. String fileFullPath = rootDir + "/" + entries_[i].name_;
  168. File srcFile(context_, fileFullPath);
  169. if (!srcFile.IsOpen())
  170. ErrorExit("Could not open file " + fileFullPath);
  171. unsigned dataSize = entries_[i].size_;
  172. totalDataSize += dataSize;
  173. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  174. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  175. ErrorExit("Could not read file " + fileFullPath);
  176. srcFile.Close();
  177. for (unsigned j = 0; j < dataSize; ++j)
  178. {
  179. checksum_ = SDBMHash(checksum_, buffer[j]);
  180. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  181. }
  182. if (!compress_)
  183. {
  184. if (!quiet_)
  185. PrintLine(entries_[i].name_ + " size " + String(dataSize));
  186. dest.Write(&buffer[0], entries_[i].size_);
  187. }
  188. else
  189. {
  190. SharedArrayPtr<unsigned char> compressBuffer(new unsigned char[LZ4_compressBound(blockSize_)]);
  191. unsigned pos = 0;
  192. unsigned totalPackedBytes = 0;
  193. while (pos < dataSize)
  194. {
  195. unsigned unpackedSize = blockSize_;
  196. if (pos + unpackedSize > dataSize)
  197. unpackedSize = dataSize - pos;
  198. unsigned packedSize = LZ4_compressHC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize);
  199. if (!packedSize)
  200. ErrorExit("LZ4 compression failed for file " + entries_[i].name_ + " at offset " + pos);
  201. dest.WriteUShort(unpackedSize);
  202. dest.WriteUShort(packedSize);
  203. dest.Write(compressBuffer.Get(), packedSize);
  204. totalPackedBytes += 6 + packedSize;
  205. pos += unpackedSize;
  206. }
  207. if (!quiet_)
  208. PrintLine(entries_[i].name_ + " in " + String(dataSize) + " out " + String(totalPackedBytes));
  209. }
  210. }
  211. // Write package size to the end of file to allow finding it linked to an executable file
  212. unsigned currentSize = dest.GetSize();
  213. dest.WriteUInt(currentSize + sizeof(unsigned));
  214. // Write header again with correct offsets & checksums
  215. dest.Seek(0);
  216. WriteHeader(dest);
  217. for (unsigned i = 0; i < entries_.Size(); ++i)
  218. {
  219. dest.WriteString(entries_[i].name_);
  220. dest.WriteUInt(entries_[i].offset_);
  221. dest.WriteUInt(entries_[i].size_);
  222. dest.WriteUInt(entries_[i].checksum_);
  223. }
  224. if (!quiet_)
  225. {
  226. PrintLine("Number of files " + String(entries_.Size()));
  227. PrintLine("File data size " + String(totalDataSize));
  228. PrintLine("Package size " + String(dest.GetSize()));
  229. }
  230. }
  231. void WriteHeader(File& dest)
  232. {
  233. if (!compress_)
  234. dest.WriteFileID("UPAK");
  235. else
  236. dest.WriteFileID("ULZ4");
  237. dest.WriteUInt(entries_.Size());
  238. dest.WriteUInt(checksum_);
  239. }