PackageTool.cpp 8.7 KB

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