PackageTool.cpp 8.8 KB

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