PackageTool.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "SharedArrayPtr.h"
  27. #include "StringUtils.h"
  28. #include <cstdio>
  29. #include <cstdlib>
  30. #include <cstring>
  31. #include <iostream>
  32. #include <string>
  33. #include <vector>
  34. #include <Windows.h>
  35. #include "DebugNew.h"
  36. struct FileEntry
  37. {
  38. std::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. std::string basePath_;
  46. std::vector<FileEntry> entries_;
  47. unsigned checksum_ = 0;
  48. std::string ignoreExtensions_[] = {
  49. ".bak",
  50. ".rule",
  51. ""
  52. };
  53. int main(int argc, char** argv);
  54. void Run(const std::vector<std::string>& arguments);
  55. void ProcessFile(const std::string& fileName, const std::string& rootDir);
  56. void WritePackageFile(const std::string& fileName, const std::string& rootDir);
  57. void ErrorExit(const std::string& error);
  58. int main(int argc, char** argv)
  59. {
  60. std::vector<std::string> arguments;
  61. for (int i = 1; i < argc; ++i)
  62. arguments.push_back(std::string(argv[i]));
  63. Run(arguments);
  64. return 0;
  65. }
  66. void Run(const std::vector<std::string>& arguments)
  67. {
  68. if (arguments.size() < 2)
  69. ErrorExit("Usage: PackageTool <directory to process> <package name> [basepath]\n");
  70. const std::string& dirName = arguments[0];
  71. const std::string& packageName = arguments[1];
  72. if (arguments.size() > 2)
  73. basePath_ = AddTrailingSlash(arguments[2]);
  74. // Get the file list recursively
  75. std::vector<std::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. std::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 std::string& fileName, const std::string& rootDir)
  97. {
  98. std::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_back(newEntry);
  110. }
  111. void WritePackageFile(const std::string& fileName, const std::string& rootDir)
  112. {
  113. std::cout << "Writing package" << std::endl;
  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.WriteID("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. std::string fileFullPath = GetNativePath(rootDir + "/" + entries_[i].name_);
  134. FILE* handle = fopen(fileFullPath.c_str(), "rb");
  135. if (!handle)
  136. ErrorExit("Could not open file " + fileFullPath);
  137. SharedArrayPtr<unsigned char> buffer(new unsigned char[entries_[i].size_]);
  138. if (fread(&buffer[0], entries_[i].size_, 1, handle) != 1)
  139. ErrorExit("Could not read file " + fileFullPath);
  140. for (unsigned j = 0; j < entries_[i].size_; ++j)
  141. {
  142. checksum_ = SDBMHash(checksum_, buffer[j]);
  143. entries_[i].checksum_ = SDBMHash(entries_[i].checksum_, buffer[j]);
  144. }
  145. fclose(handle);
  146. dest.Write(&buffer[0], entries_[i].size_);
  147. }
  148. // Write header again with correct offsets & checksums
  149. dest.Seek(0);
  150. dest.WriteID("UPAK");
  151. dest.WriteUInt(entries_.size());
  152. dest.WriteUInt(checksum_);
  153. for (unsigned i = 0; i < entries_.size(); ++i)
  154. {
  155. dest.WriteString(entries_[i].name_);
  156. dest.WriteUInt(entries_[i].offset_);
  157. dest.WriteUInt(entries_[i].size_);
  158. dest.WriteUInt(entries_[i].checksum_);
  159. }
  160. std::cout << "Package total size " << dest.GetSize() << " bytes" << std::endl;
  161. }
  162. void ErrorExit(const std::string& error)
  163. {
  164. std::cout << error;
  165. exit(1);
  166. }