BlobIOSystem.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Provides cheat implementations for IOSystem and IOStream to
  35. * redirect exporter output to a blob chain.*/
  36. #pragma once
  37. #ifndef AI_BLOBIOSYSTEM_H_INCLUDED
  38. #define AI_BLOBIOSYSTEM_H_INCLUDED
  39. #ifdef __GNUC__
  40. #pragma GCC system_header
  41. #endif
  42. #include <assimp/cexport.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. #include <assimp/IOStream.hpp>
  45. #include <assimp/IOSystem.hpp>
  46. #include <cstdint>
  47. #include <set>
  48. #include <vector>
  49. namespace Assimp {
  50. class BlobIOSystem;
  51. // --------------------------------------------------------------------------------------------
  52. /** Redirect IOStream to a blob */
  53. // --------------------------------------------------------------------------------------------
  54. class BlobIOStream : public IOStream {
  55. public:
  56. /// @brief The class constructor with all needed parameters
  57. /// @param creator Pointer to the creator instance
  58. /// @param file The filename
  59. /// @param initial The initial size
  60. BlobIOStream(BlobIOSystem *creator, const std::string &file, size_t initial = 4096) :
  61. buffer(),
  62. cur_size(),
  63. file_size(),
  64. cursor(),
  65. initial(initial),
  66. file(file),
  67. creator(creator) {
  68. // empty
  69. }
  70. /// @brief The class destructor.
  71. ~BlobIOStream() override;
  72. public:
  73. // -------------------------------------------------------------------
  74. aiExportDataBlob *GetBlob() {
  75. aiExportDataBlob *blob = new aiExportDataBlob();
  76. blob->size = file_size;
  77. blob->data = buffer;
  78. buffer = nullptr;
  79. return blob;
  80. }
  81. // -------------------------------------------------------------------
  82. size_t Read(void *, size_t, size_t) override {
  83. return 0;
  84. }
  85. // -------------------------------------------------------------------
  86. size_t Write(const void *pvBuffer, size_t pSize, size_t pCount) override {
  87. pSize *= pCount;
  88. if (cursor + pSize > cur_size) {
  89. Grow(cursor + pSize);
  90. }
  91. memcpy(buffer + cursor, pvBuffer, pSize);
  92. cursor += pSize;
  93. file_size = std::max(file_size, cursor);
  94. return pCount;
  95. }
  96. // -------------------------------------------------------------------
  97. aiReturn Seek(size_t pOffset, aiOrigin pOrigin) override {
  98. switch (pOrigin) {
  99. case aiOrigin_CUR:
  100. cursor += pOffset;
  101. break;
  102. case aiOrigin_END:
  103. cursor = file_size - pOffset;
  104. break;
  105. case aiOrigin_SET:
  106. cursor = pOffset;
  107. break;
  108. default:
  109. return AI_FAILURE;
  110. }
  111. if (cursor > file_size) {
  112. Grow(cursor);
  113. }
  114. file_size = std::max(cursor, file_size);
  115. return AI_SUCCESS;
  116. }
  117. // -------------------------------------------------------------------
  118. size_t Tell() const override {
  119. return cursor;
  120. }
  121. // -------------------------------------------------------------------
  122. size_t FileSize() const override {
  123. return file_size;
  124. }
  125. // -------------------------------------------------------------------
  126. void Flush() override {
  127. // ignore
  128. }
  129. private:
  130. // -------------------------------------------------------------------
  131. void Grow(size_t need = 0) {
  132. // 1.5 and phi are very heap-friendly growth factors (the first
  133. // allows for frequent re-use of heap blocks, the second
  134. // forms a fibonacci sequence with similar characteristics -
  135. // since this heavily depends on the heap implementation
  136. // and other factors as well, i'll just go with 1.5 since
  137. // it is quicker to compute).
  138. size_t new_size = std::max(initial, std::max(need, cur_size + (cur_size >> 1)));
  139. const uint8_t *const old = buffer;
  140. buffer = new uint8_t[new_size];
  141. if (old) {
  142. memcpy(buffer, old, cur_size);
  143. delete[] old;
  144. }
  145. cur_size = new_size;
  146. }
  147. private:
  148. uint8_t *buffer;
  149. size_t cur_size, file_size, cursor, initial;
  150. const std::string file;
  151. BlobIOSystem *const creator;
  152. };
  153. #define AI_BLOBIO_MAGIC "$blobfile"
  154. // --------------------------------------------------------------------------------------------
  155. /** Redirect IOSystem to a blob */
  156. // --------------------------------------------------------------------------------------------
  157. class BlobIOSystem : public IOSystem {
  158. friend class BlobIOStream;
  159. typedef std::pair<std::string, aiExportDataBlob *> BlobEntry;
  160. public:
  161. /// @brief The default class constructor.
  162. BlobIOSystem() :
  163. baseName{AI_BLOBIO_MAGIC} {
  164. }
  165. /// @brief The class constructor with the base name.
  166. /// @param baseName The base name.
  167. BlobIOSystem(const std::string &baseName) :
  168. baseName(baseName) {
  169. // empty
  170. }
  171. ~BlobIOSystem() override {
  172. for (BlobEntry &blobby : blobs) {
  173. delete blobby.second;
  174. }
  175. }
  176. public:
  177. // -------------------------------------------------------------------
  178. const char *GetMagicFileName() const {
  179. return baseName.c_str();
  180. }
  181. // -------------------------------------------------------------------
  182. aiExportDataBlob *GetBlobChain() {
  183. const auto magicName = std::string(this->GetMagicFileName());
  184. const bool hasBaseName = baseName != AI_BLOBIO_MAGIC;
  185. // one must be the master
  186. aiExportDataBlob *master = nullptr, *cur;
  187. for (const BlobEntry &blobby : blobs) {
  188. if (blobby.first == magicName) {
  189. master = blobby.second;
  190. master->name.Set(hasBaseName ? blobby.first : "");
  191. break;
  192. }
  193. }
  194. if (!master) {
  195. ASSIMP_LOG_ERROR("BlobIOSystem: no data written or master file was not closed properly.");
  196. return nullptr;
  197. }
  198. cur = master;
  199. for (const BlobEntry &blobby : blobs) {
  200. if (blobby.second == master) {
  201. continue;
  202. }
  203. cur->next = blobby.second;
  204. cur = cur->next;
  205. if (hasBaseName) {
  206. cur->name.Set(blobby.first);
  207. } else {
  208. // extract the file extension from the file written
  209. const std::string::size_type s = blobby.first.find_first_of('.');
  210. cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s + 1));
  211. }
  212. }
  213. // give up blob ownership
  214. blobs.clear();
  215. return master;
  216. }
  217. public:
  218. // -------------------------------------------------------------------
  219. bool Exists(const char *pFile) const override {
  220. return created.find(std::string(pFile)) != created.end();
  221. }
  222. // -------------------------------------------------------------------
  223. char getOsSeparator() const override {
  224. return '/';
  225. }
  226. // -------------------------------------------------------------------
  227. IOStream *Open(const char *pFile, const char *pMode) override {
  228. if (pMode[0] != 'w') {
  229. return nullptr;
  230. }
  231. created.insert(std::string(pFile));
  232. return new BlobIOStream(this, std::string(pFile));
  233. }
  234. // -------------------------------------------------------------------
  235. void Close(IOStream *pFile) override {
  236. delete pFile;
  237. }
  238. private:
  239. // -------------------------------------------------------------------
  240. void OnDestruct(const std::string &filename, BlobIOStream *child) {
  241. // we don't know in which the files are closed, so we
  242. // can't reliably say that the first must be the master
  243. // file ...
  244. blobs.emplace_back(filename, child->GetBlob());
  245. }
  246. private:
  247. std::string baseName;
  248. std::set<std::string> created;
  249. std::vector<BlobEntry> blobs;
  250. };
  251. // --------------------------------------------------------------------------------------------
  252. BlobIOStream::~BlobIOStream() {
  253. if (nullptr != creator) {
  254. creator->OnDestruct(file, this);
  255. }
  256. delete[] buffer;
  257. }
  258. } // namespace Assimp
  259. #endif