PackageTool.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "Exception.h"
  24. #include "File.h"
  25. #include "Hash.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. int main(int argc, char** argv);
  37. void run(const std::vector<std::string>& arguments);
  38. void processFile(const std::string& fileName, const std::string& rootDir);
  39. void writePackageFile(const std::string& fileName, const std::string& rootDir);
  40. void errorExit(const std::string& error);
  41. struct FileEntry
  42. {
  43. std::string mName;
  44. unsigned mOffset;
  45. unsigned mSize;
  46. unsigned mChecksum;
  47. };
  48. std::string gBasePath;
  49. std::vector<FileEntry> gEntries;
  50. unsigned gChecksum = 0;
  51. int main(int argc, char** argv)
  52. {
  53. std::vector<std::string> arguments;
  54. for (int i = 1; i < argc; ++i)
  55. arguments.push_back(std::string(argv[i]));
  56. try
  57. {
  58. run(arguments);
  59. }
  60. catch (Exception& e)
  61. {
  62. std::cout << e.whatStr() << std::endl;
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. void run(const std::vector<std::string>& arguments)
  68. {
  69. if (arguments.size() < 2)
  70. errorExit("Usage: PackageTool <directory to process> <package name> [basepath]");
  71. const std::string& dirName = arguments[0];
  72. const std::string& packageName = arguments[1];
  73. if (arguments.size() > 2)
  74. gBasePath = fixPath(arguments[2]);
  75. // Get the file list recursively
  76. std::vector<std::string> fileNames = scanDirectory(dirName, "*.*", true, false, false);
  77. if (!fileNames.size())
  78. errorExit("No files found");
  79. for (unsigned i = 0; i < fileNames.size(); ++i)
  80. processFile(fileNames[i], dirName);
  81. writePackageFile(packageName, dirName);
  82. }
  83. void processFile(const std::string& fileName, const std::string& rootDir)
  84. {
  85. std::string fullPath = rootDir + "/" + fileName;
  86. File file(fullPath);
  87. if (!file.getSize())
  88. return;
  89. FileEntry newEntry;
  90. newEntry.mName = fileName;
  91. newEntry.mOffset = 0; // Offset not yet known
  92. newEntry.mSize = file.getSize();
  93. newEntry.mChecksum = 0; // Will be calculated later
  94. gEntries.push_back(newEntry);
  95. }
  96. void writePackageFile(const std::string& fileName, const std::string& rootDir)
  97. {
  98. std::cout << "Writing package" << std::endl;
  99. File dest(fileName, FILE_WRITE);
  100. // Write number of files & placeholder for checksum
  101. dest.writeUInt(gEntries.size());
  102. dest.writeUInt(gChecksum);
  103. for (unsigned i = 0; i < gEntries.size(); ++i)
  104. {
  105. // Write entry (correct offset is still unknown, will be filled in later)
  106. dest.writeString(gEntries[i].mName);
  107. dest.writeUInt(gEntries[i].mOffset);
  108. dest.writeUInt(gEntries[i].mSize);
  109. dest.writeUInt(gEntries[i].mChecksum);
  110. }
  111. // Write file data, calculate checksums & correct offsets
  112. for (unsigned i = 0; i < gEntries.size(); ++i)
  113. {
  114. gEntries[i].mOffset = dest.getSize();
  115. std::string fileFullPath = getOSPath(rootDir + "/" + gEntries[i].mName);
  116. FILE* handle = fopen(fileFullPath.c_str(), "rb");
  117. if (!handle)
  118. errorExit("Could not open file " + fileFullPath);
  119. SharedArrayPtr<unsigned char> buffer(new unsigned char[gEntries[i].mSize]);
  120. if (fread(&buffer[0], gEntries[i].mSize, 1, handle) != 1)
  121. errorExit("Could not read file " + fileFullPath);
  122. for (unsigned j = 0; j < gEntries[i].mSize; ++j)
  123. {
  124. updateHash(gChecksum, buffer[j]);
  125. updateHash(gEntries[i].mChecksum, buffer[j]);
  126. }
  127. fclose(handle);
  128. dest.write(&buffer[0], gEntries[i].mSize);
  129. }
  130. // Write header again with correct offsets & checksums
  131. dest.seek(0);
  132. dest.writeUInt(gEntries.size());
  133. dest.writeUInt(gChecksum);
  134. for (unsigned i = 0; i < gEntries.size(); ++i)
  135. {
  136. dest.writeString(gEntries[i].mName);
  137. dest.writeUInt(gEntries[i].mOffset);
  138. dest.writeUInt(gEntries[i].mSize);
  139. dest.writeUInt(gEntries[i].mChecksum);
  140. }
  141. std::cout << "Package total size " << dest.getSize() << " bytes" << std::endl;
  142. }
  143. void errorExit(const std::string& error)
  144. {
  145. throw Exception(error);
  146. }