BlobIOSystem.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, 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. #ifndef AI_BLOBIOSYSTEM_H_INCLUDED
  37. #define AI_BLOBIOSYSTEM_H_INCLUDED
  38. #include <assimp/cexport.h>
  39. #include <stdint.h>
  40. #include <assimp/DefaultLogger.hpp>
  41. #include <assimp/IOStream.hpp>
  42. #include <assimp/IOSystem.hpp>
  43. #include <set>
  44. #include <vector>
  45. namespace Assimp {
  46. class BlobIOSystem;
  47. // --------------------------------------------------------------------------------------------
  48. /** Redirect IOStream to a blob */
  49. // --------------------------------------------------------------------------------------------
  50. class BlobIOStream : public IOStream {
  51. public:
  52. BlobIOStream(BlobIOSystem *creator, const std::string &file, size_t initial = 4096) :
  53. buffer(),
  54. cur_size(),
  55. file_size(),
  56. cursor(),
  57. initial(initial),
  58. file(file),
  59. creator(creator) {
  60. // empty
  61. }
  62. virtual ~BlobIOStream();
  63. public:
  64. // -------------------------------------------------------------------
  65. aiExportDataBlob *GetBlob() {
  66. aiExportDataBlob *blob = new aiExportDataBlob();
  67. blob->size = file_size;
  68. blob->data = buffer;
  69. buffer = nullptr;
  70. return blob;
  71. }
  72. // -------------------------------------------------------------------
  73. virtual size_t Read(void *,
  74. size_t,
  75. size_t) {
  76. return 0;
  77. }
  78. // -------------------------------------------------------------------
  79. virtual size_t Write(const void *pvBuffer,
  80. size_t pSize,
  81. size_t pCount) {
  82. pSize *= pCount;
  83. if (cursor + pSize > cur_size) {
  84. Grow(cursor + pSize);
  85. }
  86. memcpy(buffer + cursor, pvBuffer, pSize);
  87. cursor += pSize;
  88. file_size = std::max(file_size, cursor);
  89. return pCount;
  90. }
  91. // -------------------------------------------------------------------
  92. virtual aiReturn Seek(size_t pOffset,
  93. aiOrigin pOrigin) {
  94. switch (pOrigin) {
  95. case aiOrigin_CUR:
  96. cursor += pOffset;
  97. break;
  98. case aiOrigin_END:
  99. cursor = file_size - pOffset;
  100. break;
  101. case aiOrigin_SET:
  102. cursor = pOffset;
  103. break;
  104. default:
  105. return AI_FAILURE;
  106. }
  107. if (cursor > file_size) {
  108. Grow(cursor);
  109. }
  110. file_size = std::max(cursor, file_size);
  111. return AI_SUCCESS;
  112. }
  113. // -------------------------------------------------------------------
  114. virtual size_t Tell() const {
  115. return cursor;
  116. }
  117. // -------------------------------------------------------------------
  118. virtual size_t FileSize() const {
  119. return file_size;
  120. }
  121. // -------------------------------------------------------------------
  122. virtual void Flush() {
  123. // ignore
  124. }
  125. private:
  126. // -------------------------------------------------------------------
  127. void Grow(size_t need = 0) {
  128. // 1.5 and phi are very heap-friendly growth factors (the first
  129. // allows for frequent re-use of heap blocks, the second
  130. // forms a fibonacci sequence with similar characteristics -
  131. // since this heavily depends on the heap implementation
  132. // and other factors as well, i'll just go with 1.5 since
  133. // it is quicker to compute).
  134. size_t new_size = std::max(initial, std::max(need, cur_size + (cur_size >> 1)));
  135. const uint8_t *const old = buffer;
  136. buffer = new uint8_t[new_size];
  137. if (old) {
  138. memcpy(buffer, old, cur_size);
  139. delete[] old;
  140. }
  141. cur_size = new_size;
  142. }
  143. private:
  144. uint8_t *buffer;
  145. size_t cur_size, file_size, cursor, initial;
  146. const std::string file;
  147. BlobIOSystem *const creator;
  148. };
  149. #define AI_BLOBIO_MAGIC "$blobfile"
  150. // --------------------------------------------------------------------------------------------
  151. /** Redirect IOSystem to a blob */
  152. // --------------------------------------------------------------------------------------------
  153. class BlobIOSystem : public IOSystem {
  154. friend class BlobIOStream;
  155. typedef std::pair<std::string, aiExportDataBlob *> BlobEntry;
  156. public:
  157. BlobIOSystem() :
  158. baseName{AI_BLOBIO_MAGIC} {
  159. }
  160. BlobIOSystem(const std::string &baseName) :
  161. baseName(baseName) {
  162. }
  163. virtual ~BlobIOSystem() {
  164. for (BlobEntry &blobby : blobs) {
  165. delete blobby.second;
  166. }
  167. }
  168. public:
  169. // -------------------------------------------------------------------
  170. const char *GetMagicFileName() const {
  171. return baseName.c_str();
  172. }
  173. // -------------------------------------------------------------------
  174. aiExportDataBlob *GetBlobChain() {
  175. const auto magicName = std::string(this->GetMagicFileName());
  176. const bool hasBaseName = baseName != AI_BLOBIO_MAGIC;
  177. // one must be the master
  178. aiExportDataBlob *master = nullptr, *cur;
  179. for (const BlobEntry &blobby : blobs) {
  180. if (blobby.first == magicName) {
  181. master = blobby.second;
  182. master->name.Set(hasBaseName ? blobby.first : "");
  183. break;
  184. }
  185. }
  186. if (!master) {
  187. ASSIMP_LOG_ERROR("BlobIOSystem: no data written or master file was not closed properly.");
  188. return nullptr;
  189. }
  190. cur = master;
  191. for (const BlobEntry &blobby : blobs) {
  192. if (blobby.second == master) {
  193. continue;
  194. }
  195. cur->next = blobby.second;
  196. cur = cur->next;
  197. if (hasBaseName) {
  198. cur->name.Set(blobby.first);
  199. } else {
  200. // extract the file extension from the file written
  201. const std::string::size_type s = blobby.first.find_first_of('.');
  202. cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s + 1));
  203. }
  204. }
  205. // give up blob ownership
  206. blobs.clear();
  207. return master;
  208. }
  209. public:
  210. // -------------------------------------------------------------------
  211. virtual bool Exists(const char *pFile) const {
  212. return created.find(std::string(pFile)) != created.end();
  213. }
  214. // -------------------------------------------------------------------
  215. virtual char getOsSeparator() const {
  216. return '/';
  217. }
  218. // -------------------------------------------------------------------
  219. virtual IOStream *Open(const char *pFile,
  220. const char *pMode) {
  221. if (pMode[0] != 'w') {
  222. return nullptr;
  223. }
  224. created.insert(std::string(pFile));
  225. return new BlobIOStream(this, std::string(pFile));
  226. }
  227. // -------------------------------------------------------------------
  228. virtual void Close(IOStream *pFile) {
  229. delete pFile;
  230. }
  231. private:
  232. // -------------------------------------------------------------------
  233. void OnDestruct(const std::string &filename, BlobIOStream *child) {
  234. // we don't know in which the files are closed, so we
  235. // can't reliably say that the first must be the master
  236. // file ...
  237. blobs.push_back(BlobEntry(filename, child->GetBlob()));
  238. }
  239. private:
  240. std::string baseName;
  241. std::set<std::string> created;
  242. std::vector<BlobEntry> blobs;
  243. };
  244. // --------------------------------------------------------------------------------------------
  245. BlobIOStream ::~BlobIOStream() {
  246. creator->OnDestruct(file, this);
  247. delete[] buffer;
  248. }
  249. } // namespace Assimp
  250. #endif