PackageTool.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // Get the file list recursively
  75. Vector<String> fileNames;
  76. fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
  77. if (!fileNames.Size())
  78. ErrorExit("No files found");
  79. // Check for extensions to ignore
  80. for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
  81. {
  82. String extension = GetExtension(fileNames[i]);
  83. for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
  84. {
  85. if (extension == ignoreExtensions_[j])
  86. {
  87. fileNames.Erase(fileNames.Begin() + i);
  88. break;
  89. }
  90. }
  91. }
  92. for (unsigned i = 0; i < fileNames.Size(); ++i)
  93. ProcessFile(fileNames[i], dirName);
  94. WritePackageFile(packageName, dirName);
  95. }
  96. void ProcessFile(const String& fileName, const String& rootDir)
  97. {
  98. String fullPath = rootDir + "/" + fileName;
  99. File file(context_);
  100. if (!file.Open(fullPath))
  101. ErrorExit("Could not open file " + fileName);
  102. if (!file.GetSize())
  103. return;
  104. FileEntry newEntry;
  105. newEntry.name_ = fileName;
  106. newEntry.offset_ = 0; // Offset not yet known
  107. newEntry.size_ = file.GetSize();
  108. newEntry.checksum_ = 0; // Will be calculated later
  109. entries_.Push(newEntry);
  110. }
  111. void WritePackageFile(const String& fileName, const String& rootDir)
  112. {
  113. PrintLine("Writing package");
  114. File dest(context_);
  115. if (!dest.Open(fileName, FILE_WRITE))
  116. ErrorExit("Could not open output file " + fileName);
  117. // Write ID, number of files & placeholder for checksum
  118. dest.WriteFileID("UPAK");
  119. dest.WriteUInt(entries_.Size());
  120. dest.WriteUInt(checksum_);
  121. for (unsigned i = 0; i < entries_.Size(); ++i)
  122. {
  123. // Write entry (correct offset is still unknown, will be filled in later)
  124. dest.WriteString(entries_[i].name_);
  125. dest.WriteUInt(entries_[i].offset_);
  126. dest.WriteUInt(entries_[i].size_);
  127. dest.WriteUInt(entries_[i].checksum_);
  128. }
  129. // Write file data, calculate checksums & correct offsets
  130. for (unsigned i = 0; i < entries_.Size(); ++i)
  131. {
  132. entries_[i].offset_ = dest.GetSize();
  133. String fileFullPath = rootDir + "/" + entries_[i].name_;
  134. File srcFile(context_, fileFullPath);
  135. if (!srcFile.IsOpen())
  136. ErrorExit("Could not open file " + fileFullPath);
  137. unsigned dataSize = entries_[i].size_;
  138. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  139. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  140. ErrorExit("Could not read file " + fileFullPath);
  141. srcFile.Close();
  142. for (unsigned j = 0; j < dataSize; ++j)
  143. {
  144. checksum_ = SDBMHash(checksum_, buffer[j]);
  145. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  146. }
  147. dest.Write(&buffer[0], entries_[i].size_);
  148. }
  149. // Write header again with correct offsets & checksums
  150. dest.Seek(0);
  151. dest.WriteFileID("UPAK");
  152. dest.WriteUInt(entries_.Size());
  153. dest.WriteUInt(checksum_);
  154. for (unsigned i = 0; i < entries_.Size(); ++i)
  155. {
  156. dest.WriteString(entries_[i].name_);
  157. dest.WriteUInt(entries_[i].offset_);
  158. dest.WriteUInt(entries_[i].size_);
  159. dest.WriteUInt(entries_[i].checksum_);
  160. }
  161. PrintLine("Package total size " + String(dest.GetSize()) + " bytes");
  162. }