PackageTool.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  23. #include "ArrayPtr.h"
  24. #include "File.h"
  25. #include "FileSystem.h"
  26. #include "ProcessUtils.h"
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #endif
  30. #include <cstdio>
  31. #include <cstring>
  32. #include "DebugNew.h"
  33. using namespace Urho3D;
  34. struct FileEntry
  35. {
  36. String name_;
  37. unsigned offset_;
  38. unsigned size_;
  39. unsigned checksum_;
  40. };
  41. SharedPtr<Context> context_(new Context());
  42. SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
  43. String basePath_;
  44. Vector<FileEntry> entries_;
  45. unsigned checksum_ = 0;
  46. String ignoreExtensions_[] = {
  47. ".bak",
  48. ".rule",
  49. ""
  50. };
  51. int main(int argc, char** argv);
  52. void Run(const Vector<String>& arguments);
  53. void ProcessFile(const String& fileName, const String& rootDir);
  54. void WritePackageFile(const String& fileName, const String& rootDir);
  55. int main(int argc, char** argv)
  56. {
  57. Vector<String> arguments;
  58. #ifdef WIN32
  59. arguments = ParseArguments(GetCommandLineW());
  60. #else
  61. arguments = ParseArguments(argc, argv);
  62. #endif
  63. Run(arguments);
  64. return 0;
  65. }
  66. void Run(const Vector<String>& arguments)
  67. {
  68. if (arguments.Size() < 2)
  69. ErrorExit("Usage: PackageTool <directory to process> <package name> [basepath]\n");
  70. const String& dirName = arguments[0];
  71. const String& packageName = arguments[1];
  72. if (arguments.Size() > 2)
  73. basePath_ = AddTrailingSlash(arguments[2]);
  74. PrintLine("Scanning directory " + dirName + " for files");
  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. PrintLine("Writing file " + entries_[i].name_);
  134. entries_[i].offset_ = dest.GetSize();
  135. String fileFullPath = rootDir + "/" + entries_[i].name_;
  136. File srcFile(context_, fileFullPath);
  137. if (!srcFile.IsOpen())
  138. ErrorExit("Could not open file " + fileFullPath);
  139. unsigned dataSize = entries_[i].size_;
  140. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  141. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  142. ErrorExit("Could not read file " + fileFullPath);
  143. srcFile.Close();
  144. for (unsigned j = 0; j < dataSize; ++j)
  145. {
  146. checksum_ = SDBMHash(checksum_, buffer[j]);
  147. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  148. }
  149. dest.Write(&buffer[0], entries_[i].size_);
  150. }
  151. // Write header again with correct offsets & checksums
  152. dest.Seek(0);
  153. dest.WriteFileID("UPAK");
  154. dest.WriteUInt(entries_.Size());
  155. dest.WriteUInt(checksum_);
  156. for (unsigned i = 0; i < entries_.Size(); ++i)
  157. {
  158. dest.WriteString(entries_[i].name_);
  159. dest.WriteUInt(entries_[i].offset_);
  160. dest.WriteUInt(entries_[i].size_);
  161. dest.WriteUInt(entries_[i].checksum_);
  162. }
  163. PrintLine("Package total size " + String(dest.GetSize()) + " bytes");
  164. }