PackageTool.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Context.h"
  24. #include "ArrayPtr.h"
  25. #include "File.h"
  26. #include "FileSystem.h"
  27. #include "ProcessUtils.h"
  28. #ifdef WIN32
  29. #include <windows.h>
  30. #endif
  31. #include <cstdio>
  32. #include <cstdlib>
  33. #include <cstring>
  34. #include "DebugNew.h"
  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. String ignoreExtensions_[] = {
  48. ".bak",
  49. ".rule",
  50. ""
  51. };
  52. int main(int argc, char** argv);
  53. void Run(const Vector<String>& arguments);
  54. void ProcessFile(const String& fileName, const String& rootDir);
  55. void WritePackageFile(const String& fileName, const String& rootDir);
  56. int main(int argc, char** argv)
  57. {
  58. Vector<String> arguments;
  59. #ifdef WIN32
  60. arguments = ParseArguments(GetCommandLineW());
  61. #else
  62. arguments = ParseArguments(argc, argv);
  63. #endif
  64. Run(arguments);
  65. return 0;
  66. }
  67. void Run(const Vector<String>& arguments)
  68. {
  69. if (arguments.Size() < 2)
  70. ErrorExit("Usage: PackageTool <directory to process> <package name> [basepath]\n");
  71. const String& dirName = arguments[0];
  72. const String& packageName = arguments[1];
  73. if (arguments.Size() > 2)
  74. basePath_ = AddTrailingSlash(arguments[2]);
  75. // Get the file list recursively
  76. Vector<String> fileNames;
  77. fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
  78. if (!fileNames.Size())
  79. ErrorExit("No files found");
  80. // Check for extensions to ignore
  81. for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
  82. {
  83. String extension = GetExtension(fileNames[i]);
  84. for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
  85. {
  86. if (extension == ignoreExtensions_[j])
  87. {
  88. fileNames.Erase(fileNames.Begin() + i);
  89. break;
  90. }
  91. }
  92. }
  93. for (unsigned i = 0; i < fileNames.Size(); ++i)
  94. ProcessFile(fileNames[i], dirName);
  95. WritePackageFile(packageName, dirName);
  96. }
  97. void ProcessFile(const String& fileName, const String& rootDir)
  98. {
  99. String fullPath = rootDir + "/" + fileName;
  100. File file(context_);
  101. if (!file.Open(fullPath))
  102. ErrorExit("Could not open file " + fileName);
  103. if (!file.GetSize())
  104. return;
  105. FileEntry newEntry;
  106. newEntry.name_ = fileName;
  107. newEntry.offset_ = 0; // Offset not yet known
  108. newEntry.size_ = file.GetSize();
  109. newEntry.checksum_ = 0; // Will be calculated later
  110. entries_.Push(newEntry);
  111. }
  112. void WritePackageFile(const String& fileName, const String& rootDir)
  113. {
  114. PrintLine("Writing package");
  115. File dest(context_);
  116. if (!dest.Open(fileName, FILE_WRITE))
  117. ErrorExit("Could not open output file " + fileName);
  118. // Write ID, number of files & placeholder for checksum
  119. dest.WriteFileID("UPAK");
  120. dest.WriteUInt(entries_.Size());
  121. dest.WriteUInt(checksum_);
  122. for (unsigned i = 0; i < entries_.Size(); ++i)
  123. {
  124. // Write entry (correct offset is still unknown, will be filled in later)
  125. dest.WriteString(entries_[i].name_);
  126. dest.WriteUInt(entries_[i].offset_);
  127. dest.WriteUInt(entries_[i].size_);
  128. dest.WriteUInt(entries_[i].checksum_);
  129. }
  130. // Write file data, calculate checksums & correct offsets
  131. for (unsigned i = 0; i < entries_.Size(); ++i)
  132. {
  133. entries_[i].offset_ = dest.GetSize();
  134. String fileFullPath = rootDir + "/" + entries_[i].name_;
  135. File srcFile(context_, fileFullPath);
  136. if (!srcFile.IsOpen())
  137. ErrorExit("Could not open file " + fileFullPath);
  138. unsigned dataSize = entries_[i].size_;
  139. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  140. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  141. ErrorExit("Could not read file " + fileFullPath);
  142. srcFile.Close();
  143. for (unsigned j = 0; j < dataSize; ++j)
  144. {
  145. checksum_ = SDBMHash(checksum_, buffer[j]);
  146. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  147. }
  148. dest.Write(&buffer[0], entries_[i].size_);
  149. }
  150. // Write header again with correct offsets & checksums
  151. dest.Seek(0);
  152. dest.WriteFileID("UPAK");
  153. dest.WriteUInt(entries_.Size());
  154. dest.WriteUInt(checksum_);
  155. for (unsigned i = 0; i < entries_.Size(); ++i)
  156. {
  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. PrintLine("Package total size " + String(dest.GetSize()) + " bytes");
  163. }