ResourcePackager.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/Log.h>
  27. #include <Atomic/IO/FileSystem.h>
  28. #include <Atomic/Container/ArrayPtr.h>
  29. #include <LZ4/lz4.h>
  30. #include <LZ4/lz4hc.h>
  31. #include "BuildBase.h"
  32. #include "ResourcePackager.h"
  33. namespace ToolCore
  34. {
  35. ResourcePackager::ResourcePackager(Context* context, BuildBase* buildBase) : Object(context)
  36. , buildBase_(buildBase)
  37. , checksum_(0)
  38. {
  39. }
  40. ResourcePackager::~ResourcePackager()
  41. {
  42. }
  43. bool ResourcePackager::WritePackageFile(const String& destFilePath)
  44. {
  45. buildBase_->BuildLog("Writing package", false);
  46. SharedPtr<File> dest(new File(context_, destFilePath, FILE_WRITE));
  47. if (!dest->IsOpen())
  48. {
  49. buildBase_->FailBuild("Could not open output file " + destFilePath);
  50. return false;
  51. }
  52. // Write ID, number of files & placeholder for checksum
  53. WriteHeader(dest);
  54. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  55. {
  56. BuildResourceEntry* entry = resourceEntries_[i];
  57. // Write entry (correct offset is still unknown, will be filled in later)
  58. dest->WriteString(entry->packagePath_);
  59. dest->WriteUInt(entry->offset_);
  60. dest->WriteUInt(entry->size_);
  61. dest->WriteUInt(entry->checksum_);
  62. }
  63. unsigned totalDataSize = 0;
  64. // Write file data, calculate checksums & correct offsets
  65. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  66. {
  67. BuildResourceEntry* entry = resourceEntries_[i];
  68. entry->offset_ = dest->GetSize();
  69. File srcFile(context_, entry->absolutePath_);
  70. if (!srcFile.IsOpen())
  71. {
  72. buildBase_->FailBuild("Could not open input file " + entry->absolutePath_);
  73. return false;
  74. }
  75. unsigned dataSize = entry->size_;
  76. totalDataSize += dataSize;
  77. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  78. if (srcFile.Read(&buffer[0], dataSize) != dataSize)
  79. {
  80. buildBase_->FailBuild("Could not read input file " + entry->absolutePath_);
  81. return false;
  82. }
  83. srcFile.Close();
  84. for (unsigned j = 0; j < dataSize; ++j)
  85. {
  86. checksum_ = SDBMHash(checksum_, buffer[j]);
  87. entry->checksum_ = SDBMHash(entry->checksum_, buffer[j]);
  88. }
  89. // might not want to compress for WebGL
  90. //if (!compress_)
  91. //{
  92. // PrintLine(entries_[i].name_ + " size " + String(dataSize));
  93. // dest.Write(&buffer[0], entries_[i].size_);
  94. //}
  95. //else
  96. //{
  97. unsigned compressedBlockSize_ = 32768;
  98. SharedArrayPtr<unsigned char> compressBuffer(new unsigned char[LZ4_compressBound(compressedBlockSize_)]);
  99. unsigned pos = 0;
  100. unsigned totalPackedBytes = 0;
  101. while (pos < dataSize)
  102. {
  103. unsigned unpackedSize = compressedBlockSize_;
  104. if (pos + unpackedSize > dataSize)
  105. unpackedSize = dataSize - pos;
  106. unsigned packedSize = LZ4_compressHC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize);
  107. if (!packedSize)
  108. {
  109. buildBase_->FailBuild(ToString("LZ4 compression failed for file %s at offset %u", entry->absolutePath_.CString(), pos));
  110. return false;
  111. }
  112. dest->WriteUShort(unpackedSize);
  113. dest->WriteUShort(packedSize);
  114. dest->Write(compressBuffer.Get(), packedSize);
  115. totalPackedBytes += 6 + packedSize;
  116. pos += unpackedSize;
  117. }
  118. buildBase_->BuildLog(entry->absolutePath_ + " in " + String(dataSize) + " out " + String(totalPackedBytes), false);
  119. }
  120. //}
  121. // Write package size to the end of file to allow finding it linked to an executable file
  122. unsigned currentSize = dest->GetSize();
  123. dest->WriteUInt(currentSize + sizeof(unsigned));
  124. // Write header again with correct offsets & checksums
  125. dest->Seek(0);
  126. WriteHeader(dest);
  127. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  128. {
  129. BuildResourceEntry* entry = resourceEntries_[i];
  130. dest->WriteString(entry->packagePath_);
  131. dest->WriteUInt(entry->offset_);
  132. dest->WriteUInt(entry->size_);
  133. dest->WriteUInt(entry->checksum_);
  134. }
  135. buildBase_->BuildLog("Resource Package:");
  136. buildBase_->BuildLog("Number of files " + String(resourceEntries_.Size()));
  137. buildBase_->BuildLog("File data size " + String(totalDataSize));
  138. buildBase_->BuildLog("Package size " + String(dest->GetSize()));
  139. return true;
  140. }
  141. void ResourcePackager::WriteHeader(File* dest)
  142. {
  143. dest->WriteFileID("ULZ4");
  144. dest->WriteUInt(resourceEntries_.Size());
  145. dest->WriteUInt(checksum_);
  146. }
  147. void ResourcePackager::GeneratePackage(const String& destFilePath)
  148. {
  149. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  150. {
  151. BuildResourceEntry* entry = resourceEntries_[i];
  152. File file(context_);
  153. if (!file.Open(entry->absolutePath_))
  154. {
  155. buildBase_->FailBuild(Atomic::ToString("Could not open resource file %s", entry->absolutePath_.CString()));
  156. return;
  157. }
  158. entry->size_ = file.GetSize();
  159. }
  160. WritePackageFile(destFilePath);
  161. }
  162. void ResourcePackager::AddResourceEntry(BuildResourceEntry* entry)
  163. {
  164. resourceEntries_.Push(entry);
  165. }
  166. }