ResourcePackager.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. //
  5. // Portions Copyright (c) 2008-2014 the Urho3D project.
  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 "AtomicEditor.h"
  26. #include "Atomic/Core/StringUtils.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");
  46. SharedPtr<File> dest(new File(context_, destFilePath, FILE_WRITE));
  47. if (!dest->IsOpen())
  48. {
  49. buildBase_->BuildError("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_->BuildError("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_->BuildError("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_->BuildError("LZ4 compression failed for file " + entry->absolutePath_ + " at offset " + 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));
  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("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_->BuildError(Atomic::ToString("Could not open resource file %s", entry->absolutePath_.CString()));
  155. return;
  156. }
  157. if (!file.GetSize())
  158. {
  159. return;
  160. }
  161. entry->size_ = file.GetSize();
  162. }
  163. WritePackageFile(destFilePath);
  164. }
  165. void ResourcePackager::AddResourceEntry(BuildResourceEntry* entry)
  166. {
  167. resourceEntries_.Push(entry);
  168. }
  169. }