PackageTool.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // Copyright (c) 2008-2016 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. "\n"
  81. "Basepath is an optional prefix that will be added to the file entries.\n"
  82. );
  83. const String& dirName = arguments[0];
  84. const String& packageName = arguments[1];
  85. if (arguments.Size() > 2)
  86. {
  87. for (unsigned i = 2; i < arguments.Size(); ++i)
  88. {
  89. if (arguments[i][0] != '-')
  90. basePath_ = AddTrailingSlash(arguments[i]);
  91. else
  92. {
  93. if (arguments[i].Length() > 1)
  94. {
  95. switch (arguments[i][1])
  96. {
  97. case 'c':
  98. compress_ = true;
  99. break;
  100. case 'q':
  101. quiet_ = true;
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. if (!quiet_)
  109. PrintLine("Scanning directory " + dirName + " for files");
  110. // Get the file list recursively
  111. Vector<String> fileNames;
  112. fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
  113. if (!fileNames.Size())
  114. ErrorExit("No files found");
  115. // Check for extensions to ignore
  116. for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
  117. {
  118. String extension = GetExtension(fileNames[i]);
  119. for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
  120. {
  121. if (extension == ignoreExtensions_[j])
  122. {
  123. fileNames.Erase(fileNames.Begin() + i);
  124. break;
  125. }
  126. }
  127. }
  128. for (unsigned i = 0; i < fileNames.Size(); ++i)
  129. ProcessFile(fileNames[i], dirName);
  130. WritePackageFile(packageName, dirName);
  131. }
  132. void ProcessFile(const String& fileName, const String& rootDir)
  133. {
  134. String fullPath = rootDir + "/" + fileName;
  135. File file(context_);
  136. if (!file.Open(fullPath))
  137. ErrorExit("Could not open file " + fileName);
  138. if (!file.GetSize())
  139. return;
  140. FileEntry newEntry;
  141. newEntry.name_ = fileName;
  142. newEntry.offset_ = 0; // Offset not yet known
  143. newEntry.size_ = file.GetSize();
  144. newEntry.checksum_ = 0; // Will be calculated later
  145. entries_.Push(newEntry);
  146. }
  147. void WritePackageFile(const String& fileName, const String& rootDir)
  148. {
  149. if (!quiet_)
  150. PrintLine("Writing package");
  151. File dest(context_);
  152. if (!dest.Open(fileName, FILE_WRITE))
  153. ErrorExit("Could not open output file " + fileName);
  154. // Write ID, number of files & placeholder for checksum
  155. WriteHeader(dest);
  156. for (unsigned i = 0; i < entries_.Size(); ++i)
  157. {
  158. // Write entry (correct offset is still unknown, will be filled in later)
  159. dest.WriteString(basePath_ + entries_[i].name_);
  160. dest.WriteUInt(entries_[i].offset_);
  161. dest.WriteUInt(entries_[i].size_);
  162. dest.WriteUInt(entries_[i].checksum_);
  163. }
  164. unsigned totalDataSize = 0;
  165. // Write file data, calculate checksums & correct offsets
  166. for (unsigned i = 0; i < entries_.Size(); ++i)
  167. {
  168. entries_[i].offset_ = dest.GetSize();
  169. String fileFullPath = rootDir + "/" + entries_[i].name_;
  170. File srcFile(context_, fileFullPath);
  171. if (!srcFile.IsOpen())
  172. ErrorExit("Could not open file " + fileFullPath);
  173. unsigned dataSize = entries_[i].size_;
  174. totalDataSize += dataSize;
  175. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  176. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  177. ErrorExit("Could not read file " + fileFullPath);
  178. srcFile.Close();
  179. for (unsigned j = 0; j < dataSize; ++j)
  180. {
  181. checksum_ = SDBMHash(checksum_, buffer[j]);
  182. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  183. }
  184. if (!compress_)
  185. {
  186. if (!quiet_)
  187. PrintLine(entries_[i].name_ + " size " + String(dataSize));
  188. dest.Write(&buffer[0], entries_[i].size_);
  189. }
  190. else
  191. {
  192. SharedArrayPtr<unsigned char> compressBuffer(new unsigned char[LZ4_compressBound(blockSize_)]);
  193. unsigned pos = 0;
  194. unsigned totalPackedBytes = 0;
  195. while (pos < dataSize)
  196. {
  197. unsigned unpackedSize = blockSize_;
  198. if (pos + unpackedSize > dataSize)
  199. unpackedSize = dataSize - pos;
  200. unsigned packedSize = LZ4_compressHC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize);
  201. if (!packedSize)
  202. ErrorExit("LZ4 compression failed for file " + entries_[i].name_ + " at offset " + pos);
  203. dest.WriteUShort(unpackedSize);
  204. dest.WriteUShort(packedSize);
  205. dest.Write(compressBuffer.Get(), packedSize);
  206. totalPackedBytes += 6 + packedSize;
  207. pos += unpackedSize;
  208. }
  209. if (!quiet_)
  210. PrintLine(entries_[i].name_ + " in " + String(dataSize) + " out " + String(totalPackedBytes));
  211. }
  212. }
  213. // Write package size to the end of file to allow finding it linked to an executable file
  214. unsigned currentSize = dest.GetSize();
  215. dest.WriteUInt(currentSize + sizeof(unsigned));
  216. // Write header again with correct offsets & checksums
  217. dest.Seek(0);
  218. WriteHeader(dest);
  219. for (unsigned i = 0; i < entries_.Size(); ++i)
  220. {
  221. dest.WriteString(basePath_ + entries_[i].name_);
  222. dest.WriteUInt(entries_[i].offset_);
  223. dest.WriteUInt(entries_[i].size_);
  224. dest.WriteUInt(entries_[i].checksum_);
  225. }
  226. if (!quiet_)
  227. {
  228. PrintLine("Number of files " + String(entries_.Size()));
  229. PrintLine("File data size " + String(totalDataSize));
  230. PrintLine("Package size " + String(dest.GetSize()));
  231. }
  232. }
  233. void WriteHeader(File& dest)
  234. {
  235. if (!compress_)
  236. dest.WriteFileID("UPAK");
  237. else
  238. dest.WriteFileID("ULZ4");
  239. dest.WriteUInt(entries_.Size());
  240. dest.WriteUInt(checksum_);
  241. }