M3DWrapper.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, assimp team
  5. Copyright (c) 2019 bzt
  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
  9. following 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. #ifndef ASSIMP_BUILD_NO_M3D_IMPORTER
  35. #if !(ASSIMP_BUILD_NO_EXPORT || ASSIMP_BUILD_NO_M3D_EXPORTER)
  36. #include "M3DWrapper.h"
  37. #include <assimp/DefaultIOSystem.h>
  38. #include <assimp/IOStreamBuffer.h>
  39. #include <assimp/ai_assert.h>
  40. #ifdef ASSIMP_USE_M3D_READFILECB
  41. #if (__cplusplus >= 201103L) || !defined(_MSC_VER) || (_MSC_VER >= 1900) // C++11 and MSVC 2015 onwards
  42. #define threadlocal thread_local
  43. #else
  44. #if defined(_MSC_VER) && (_MSC_VER >= 1800) // there's an alternative for MSVC 2013
  45. #define threadlocal __declspec(thread)
  46. #else
  47. #define threadlocal
  48. #endif
  49. #endif
  50. extern "C" {
  51. // workaround: the M3D SDK expects a C callback, but we want to use Assimp::IOSystem to implement that
  52. threadlocal void *m3dimporter_pIOHandler;
  53. unsigned char *m3dimporter_readfile(char *fn, unsigned int *size) {
  54. ai_assert(nullptr != fn);
  55. ai_assert(nullptr != size);
  56. std::string file(fn);
  57. std::unique_ptr<Assimp::IOStream> pStream(
  58. (reinterpret_cast<Assimp::IOSystem *>(m3dimporter_pIOHandler))->Open(file, "rb"));
  59. size_t fileSize = 0;
  60. unsigned char *data = nullptr;
  61. // sometimes pStream is nullptr in a single-threaded scenario too for some reason
  62. // (should be an empty object returning nothing I guess)
  63. if (pStream) {
  64. fileSize = pStream->FileSize();
  65. // should be allocated with malloc(), because the library will call free() to deallocate
  66. data = (unsigned char *)malloc(fileSize);
  67. if (!data || !pStream.get() || !fileSize || fileSize != pStream->Read(data, 1, fileSize)) {
  68. pStream.reset();
  69. *size = 0;
  70. // don't throw a deadly exception, it's not fatal if we can't read an external asset
  71. return nullptr;
  72. }
  73. pStream.reset();
  74. }
  75. *size = (int)fileSize;
  76. return data;
  77. }
  78. }
  79. #endif
  80. namespace Assimp {
  81. M3DWrapper::M3DWrapper() {
  82. // use malloc() here because m3d_free() will call free()
  83. m3d_ = (m3d_t *)calloc(1, sizeof(m3d_t));
  84. }
  85. M3DWrapper::M3DWrapper(IOSystem *pIOHandler, const std::vector<unsigned char> &buffer) {
  86. if (nullptr == pIOHandler) {
  87. ai_assert(nullptr != pIOHandler);
  88. }
  89. #ifdef ASSIMP_USE_M3D_READFILECB
  90. // pass this IOHandler to the C callback in a thread-local pointer
  91. m3dimporter_pIOHandler = pIOHandler;
  92. m3d_ = m3d_load(const_cast<unsigned char *>(buffer.data()), m3dimporter_readfile, free, nullptr);
  93. // Clear the C callback
  94. m3dimporter_pIOHandler = nullptr;
  95. #else
  96. m3d_ = m3d_load(const_cast<unsigned char *>(buffer.data()), nullptr, nullptr, nullptr);
  97. #endif
  98. }
  99. M3DWrapper::~M3DWrapper() {
  100. reset();
  101. }
  102. void M3DWrapper::reset() {
  103. ClearSave();
  104. if (m3d_) {
  105. m3d_free(m3d_);
  106. }
  107. m3d_ = nullptr;
  108. }
  109. unsigned char *M3DWrapper::Save(int quality, int flags, unsigned int &size) {
  110. #if (!(ASSIMP_BUILD_NO_EXPORT || ASSIMP_BUILD_NO_M3D_EXPORTER))
  111. ClearSave();
  112. saved_output_ = m3d_save(m3d_, quality, flags, &size);
  113. return saved_output_;
  114. #else
  115. (void)quality;
  116. (void)flags;
  117. (void)size;
  118. return nullptr;
  119. #endif
  120. }
  121. void M3DWrapper::ClearSave() {
  122. if (saved_output_) {
  123. M3D_FREE(saved_output_);
  124. }
  125. saved_output_ = nullptr;
  126. }
  127. } // namespace Assimp
  128. #endif
  129. #endif