M3DWrapper.cpp 4.3 KB

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