ResourcePackager.cpp 6.7 KB

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