ResourcePackager.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Portions Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. //
  5. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #include "Atomic/Core/StringUtils.h"
  26. #include <Atomic/IO/FileSystem.h>
  27. #include <Atomic/Container/ArrayPtr.h>
  28. #include <LZ4/lz4.h>
  29. #include <LZ4/lz4hc.h>
  30. #include "BuildBase.h"
  31. #include "ResourcePackager.h"
  32. namespace ToolCore
  33. {
  34. ResourcePackager::ResourcePackager(Context* context, BuildBase* buildBase) : Object(context)
  35. , buildBase_(buildBase)
  36. , checksum_(0)
  37. {
  38. }
  39. ResourcePackager::~ResourcePackager()
  40. {
  41. }
  42. bool ResourcePackager::WritePackageFile(const String& destFilePath)
  43. {
  44. buildBase_->BuildLog("Writing package", false);
  45. SharedPtr<File> dest(new File(context_, destFilePath, FILE_WRITE));
  46. if (!dest->IsOpen())
  47. {
  48. buildBase_->FailBuild("Could not open output file " + destFilePath);
  49. return false;
  50. }
  51. // Write ID, number of files & placeholder for checksum
  52. WriteHeader(dest);
  53. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  54. {
  55. BuildResourceEntry* entry = resourceEntries_[i];
  56. // Write entry (correct offset is still unknown, will be filled in later)
  57. dest->WriteString(entry->packagePath_);
  58. dest->WriteUInt(entry->offset_);
  59. dest->WriteUInt(entry->size_);
  60. dest->WriteUInt(entry->checksum_);
  61. }
  62. unsigned totalDataSize = 0;
  63. // Write file data, calculate checksums & correct offsets
  64. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  65. {
  66. BuildResourceEntry* entry = resourceEntries_[i];
  67. entry->offset_ = dest->GetSize();
  68. File srcFile(context_, entry->absolutePath_);
  69. if (!srcFile.IsOpen())
  70. {
  71. buildBase_->FailBuild("Could not open input file " + entry->absolutePath_);
  72. return false;
  73. }
  74. unsigned dataSize = entry->size_;
  75. totalDataSize += dataSize;
  76. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  77. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  78. {
  79. buildBase_->FailBuild("Could not read input file " + entry->absolutePath_);
  80. return false;
  81. }
  82. srcFile.Close();
  83. for (unsigned j = 0; j < dataSize; ++j)
  84. {
  85. checksum_ = SDBMHash(checksum_, buffer[j]);
  86. entry->checksum_ = SDBMHash(entry->checksum_, buffer[j]);
  87. }
  88. // might not want to compress for WebGL
  89. //if (!compress_)
  90. //{
  91. // PrintLine(entries_[i].name_ + " size " + String(dataSize));
  92. // dest.Write(&buffer[0], entries_[i].size_);
  93. //}
  94. //else
  95. //{
  96. unsigned compressedBlockSize_ = 32768;
  97. SharedArrayPtr<unsigned char> compressBuffer(new unsigned char[LZ4_compressBound(compressedBlockSize_)]);
  98. unsigned pos = 0;
  99. unsigned totalPackedBytes = 0;
  100. while (pos < dataSize)
  101. {
  102. unsigned unpackedSize = compressedBlockSize_;
  103. if (pos + unpackedSize > dataSize)
  104. unpackedSize = dataSize - pos;
  105. unsigned packedSize = LZ4_compressHC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize);
  106. if (!packedSize)
  107. {
  108. buildBase_->FailBuild("LZ4 compression failed for file " + entry->absolutePath_ + " at offset " + pos);
  109. return false;
  110. }
  111. dest->WriteUShort(unpackedSize);
  112. dest->WriteUShort(packedSize);
  113. dest->Write(compressBuffer.Get(), packedSize);
  114. totalPackedBytes += 6 + packedSize;
  115. pos += unpackedSize;
  116. }
  117. buildBase_->BuildLog(entry->absolutePath_ + " in " + String(dataSize) + " out " + String(totalPackedBytes), false);
  118. }
  119. //}
  120. // Write package size to the end of file to allow finding it linked to an executable file
  121. unsigned currentSize = dest->GetSize();
  122. dest->WriteUInt(currentSize + sizeof(unsigned));
  123. // Write header again with correct offsets & checksums
  124. dest->Seek(0);
  125. WriteHeader(dest);
  126. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  127. {
  128. BuildResourceEntry* entry = resourceEntries_[i];
  129. dest->WriteString(entry->packagePath_);
  130. dest->WriteUInt(entry->offset_);
  131. dest->WriteUInt(entry->size_);
  132. dest->WriteUInt(entry->checksum_);
  133. }
  134. buildBase_->BuildLog("Resource Package:");
  135. buildBase_->BuildLog("Number of files " + String(resourceEntries_.Size()));
  136. buildBase_->BuildLog("File data size " + String(totalDataSize));
  137. buildBase_->BuildLog("Package size " + String(dest->GetSize()));
  138. return true;
  139. }
  140. void ResourcePackager::WriteHeader(File* dest)
  141. {
  142. dest->WriteFileID("ULZ4");
  143. dest->WriteUInt(resourceEntries_.Size());
  144. dest->WriteUInt(checksum_);
  145. }
  146. void ResourcePackager::GeneratePackage(const String& destFilePath)
  147. {
  148. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  149. {
  150. BuildResourceEntry* entry = resourceEntries_[i];
  151. File file(context_);
  152. if (!file.Open(entry->absolutePath_))
  153. {
  154. buildBase_->FailBuild(Atomic::ToString("Could not open resource file %s", entry->absolutePath_.CString()));
  155. return;
  156. }
  157. entry->size_ = file.GetSize();
  158. }
  159. WritePackageFile(destFilePath);
  160. }
  161. void ResourcePackager::AddResourceEntry(BuildResourceEntry* entry)
  162. {
  163. resourceEntries_.Push(entry);
  164. }
  165. }