PackageTool.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. using namespace Urho3D;
  36. struct FileEntry
  37. {
  38. String name_;
  39. unsigned offset_;
  40. unsigned size_;
  41. unsigned checksum_;
  42. };
  43. SharedPtr<Context> context_(new Context());
  44. SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
  45. String basePath_;
  46. Vector<FileEntry> entries_;
  47. unsigned checksum_ = 0;
  48. String ignoreExtensions_[] = {
  49. ".bak",
  50. ".rule",
  51. ""
  52. };
  53. int main(int argc, char** argv);
  54. void Run(const Vector<String>& arguments);
  55. void ProcessFile(const String& fileName, const String& rootDir);
  56. void WritePackageFile(const String& fileName, const String& rootDir);
  57. int main(int argc, char** argv)
  58. {
  59. Vector<String> arguments;
  60. #ifdef WIN32
  61. arguments = ParseArguments(GetCommandLineW());
  62. #else
  63. arguments = ParseArguments(argc, argv);
  64. #endif
  65. Run(arguments);
  66. return 0;
  67. }
  68. void Run(const Vector<String>& arguments)
  69. {
  70. if (arguments.Size() < 2)
  71. ErrorExit("Usage: PackageTool <directory to process> <package name> [basepath]\n");
  72. const String& dirName = arguments[0];
  73. const String& packageName = arguments[1];
  74. if (arguments.Size() > 2)
  75. basePath_ = AddTrailingSlash(arguments[2]);
  76. // Get the file list recursively
  77. Vector<String> fileNames;
  78. fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
  79. if (!fileNames.Size())
  80. ErrorExit("No files found");
  81. // Check for extensions to ignore
  82. for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
  83. {
  84. String extension = GetExtension(fileNames[i]);
  85. for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
  86. {
  87. if (extension == ignoreExtensions_[j])
  88. {
  89. fileNames.Erase(fileNames.Begin() + i);
  90. break;
  91. }
  92. }
  93. }
  94. for (unsigned i = 0; i < fileNames.Size(); ++i)
  95. ProcessFile(fileNames[i], dirName);
  96. WritePackageFile(packageName, dirName);
  97. }
  98. void ProcessFile(const String& fileName, const String& rootDir)
  99. {
  100. String fullPath = rootDir + "/" + fileName;
  101. File file(context_);
  102. if (!file.Open(fullPath))
  103. ErrorExit("Could not open file " + fileName);
  104. if (!file.GetSize())
  105. return;
  106. FileEntry newEntry;
  107. newEntry.name_ = fileName;
  108. newEntry.offset_ = 0; // Offset not yet known
  109. newEntry.size_ = file.GetSize();
  110. newEntry.checksum_ = 0; // Will be calculated later
  111. entries_.Push(newEntry);
  112. }
  113. void WritePackageFile(const String& fileName, const String& rootDir)
  114. {
  115. PrintLine("Writing package");
  116. File dest(context_);
  117. if (!dest.Open(fileName, FILE_WRITE))
  118. ErrorExit("Could not open output file " + fileName);
  119. // Write ID, number of files & placeholder for checksum
  120. dest.WriteFileID("UPAK");
  121. dest.WriteUInt(entries_.Size());
  122. dest.WriteUInt(checksum_);
  123. for (unsigned i = 0; i < entries_.Size(); ++i)
  124. {
  125. // Write entry (correct offset is still unknown, will be filled in later)
  126. dest.WriteString(entries_[i].name_);
  127. dest.WriteUInt(entries_[i].offset_);
  128. dest.WriteUInt(entries_[i].size_);
  129. dest.WriteUInt(entries_[i].checksum_);
  130. }
  131. // Write file data, calculate checksums & correct offsets
  132. for (unsigned i = 0; i < entries_.Size(); ++i)
  133. {
  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. }