PackageTool.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "File.h"
  25. #include "FileSystem.h"
  26. #include "ProcessUtils.h"
  27. #include "SharedArrayPtr.h"
  28. #include "StringUtils.h"
  29. #include "Vector.h"
  30. #include <cstdio>
  31. #include <cstdlib>
  32. #include <cstring>
  33. #include <Windows.h>
  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. for (int i = 1; i < argc; ++i)
  60. arguments.Push(String(argv[i]));
  61. Run(arguments);
  62. return 0;
  63. }
  64. void Run(const Vector<String>& arguments)
  65. {
  66. if (arguments.Size() < 2)
  67. ErrorExit("Usage: PackageTool <directory to process> <package name> [basepath]\n");
  68. const String& dirName = arguments[0];
  69. const String& packageName = arguments[1];
  70. if (arguments.Size() > 2)
  71. basePath_ = AddTrailingSlash(arguments[2]);
  72. // Get the file list recursively
  73. Vector<String> fileNames;
  74. fileSystem_->ScanDir(fileNames, dirName, "*.*", SCAN_FILES, true);
  75. if (!fileNames.Size())
  76. ErrorExit("No files found");
  77. // Check for extensions to ignore
  78. for (unsigned i = fileNames.Size() - 1; i < fileNames.Size(); --i)
  79. {
  80. String extension = GetExtension(fileNames[i]);
  81. for (unsigned j = 0; ignoreExtensions_[j].Length(); ++j)
  82. {
  83. if (extension == ignoreExtensions_[j])
  84. {
  85. fileNames.Erase(fileNames.Begin() + i);
  86. break;
  87. }
  88. }
  89. }
  90. for (unsigned i = 0; i < fileNames.Size(); ++i)
  91. ProcessFile(fileNames[i], dirName);
  92. WritePackageFile(packageName, dirName);
  93. }
  94. void ProcessFile(const String& fileName, const String& rootDir)
  95. {
  96. String fullPath = rootDir + "/" + fileName;
  97. File file(context_);
  98. if (!file.Open(fullPath))
  99. ErrorExit("Could not open file " + fileName);
  100. if (!file.GetSize())
  101. return;
  102. FileEntry newEntry;
  103. newEntry.name_ = fileName;
  104. newEntry.offset_ = 0; // Offset not yet known
  105. newEntry.size_ = file.GetSize();
  106. newEntry.checksum_ = 0; // Will be Calculated later
  107. entries_.Push(newEntry);
  108. }
  109. void WritePackageFile(const String& fileName, const String& rootDir)
  110. {
  111. PrintLine("Writing package");
  112. File dest(context_);
  113. if (!dest.Open(fileName, FILE_WRITE))
  114. ErrorExit("Could not open output file " + fileName);
  115. // Write ID, number of files & placeholder for checksum
  116. dest.WriteID("UPAK");
  117. dest.WriteUInt(entries_.Size());
  118. dest.WriteUInt(checksum_);
  119. for (unsigned i = 0; i < entries_.Size(); ++i)
  120. {
  121. // Write entry (correct offset is still unknown, will be filled in later)
  122. dest.WriteString(entries_[i].name_);
  123. dest.WriteUInt(entries_[i].offset_);
  124. dest.WriteUInt(entries_[i].size_);
  125. dest.WriteUInt(entries_[i].checksum_);
  126. }
  127. // Write file data, Calculate checksums & correct offsets
  128. for (unsigned i = 0; i < entries_.Size(); ++i)
  129. {
  130. entries_[i].offset_ = dest.GetSize();
  131. String fileFullPath = GetNativePath(rootDir + "/" + entries_[i].name_);
  132. FILE* handle = fopen(fileFullPath.CString(), "rb");
  133. if (!handle)
  134. ErrorExit("Could not open file " + fileFullPath);
  135. SharedArrayPtr<unsigned char> buffer(new unsigned char[entries_[i].size_]);
  136. if (fread(&buffer[0], entries_[i].size_, 1, handle) != 1)
  137. ErrorExit("Could not read file " + fileFullPath);
  138. for (unsigned j = 0; j < entries_[i].size_; ++j)
  139. {
  140. checksum_ = SDBMHash(checksum_, buffer[j]);
  141. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  142. }
  143. fclose(handle);
  144. dest.Write(&buffer[0], entries_[i].size_);
  145. }
  146. // Write header again with correct offsets & checksums
  147. dest.Seek(0);
  148. dest.WriteID("UPAK");
  149. dest.WriteUInt(entries_.Size());
  150. dest.WriteUInt(checksum_);
  151. for (unsigned i = 0; i < entries_.Size(); ++i)
  152. {
  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. PrintLine("Package total size " + ToString(dest.GetSize()) + " bytes");
  159. }