M3DWrapper.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef AI_M3D_USE_STDMUTEX
  40. #if (__cplusplus >= 201103L) || (_MSC_VER >= 1900) // C++11 and MSVC 2015 onwards
  41. #define AI_M3D_USE_STDMUTEX 1
  42. #else
  43. #define AI_M3D_USE_STDMUTEX 0
  44. #endif
  45. #endif
  46. #if AI_M3D_USE_STDMUTEX
  47. #include <mutex>
  48. std::mutex file_mutex;
  49. #endif
  50. // workaround: the M3D SDK expects a C callback, but we want to use Assimp::IOSystem to implement that
  51. // This makes it non-rentrant so lock a mutex (requires C++11)
  52. extern "C" {
  53. void *m3dimporter_pIOHandler;
  54. unsigned char *m3dimporter_readfile(char *fn, unsigned int *size) {
  55. ai_assert(nullptr != fn);
  56. ai_assert(nullptr != size);
  57. std::string file(fn);
  58. std::unique_ptr<Assimp::IOStream> pStream(
  59. (reinterpret_cast<Assimp::IOSystem *>(m3dimporter_pIOHandler))->Open(file, "rb"));
  60. size_t fileSize = 0;
  61. unsigned char *data = NULL;
  62. // sometimes pStream is nullptr for some reason (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. namespace Assimp {
  80. M3DWrapper::M3DWrapper() {
  81. // use malloc() here because m3d_free() will call free()
  82. m3d_ = (m3d_t *)calloc(1, sizeof(m3d_t));
  83. }
  84. M3DWrapper::M3DWrapper(IOSystem *pIOHandler, const std::vector<unsigned char> &buffer) {
  85. #if AI_M3D_USE_STDMUTEX
  86. // M3D is NOT thread-safe, so lock the global mutex
  87. const std::lock_guard<std::mutex> lock(file_mutex);
  88. #endif
  89. // pass this IOHandler to the C callback
  90. m3dimporter_pIOHandler = pIOHandler;
  91. m3d_ = m3d_load(const_cast<unsigned char *>(buffer.data()), m3dimporter_readfile, free, nullptr);
  92. // Clear the C callback
  93. m3dimporter_pIOHandler = nullptr;
  94. }
  95. M3DWrapper::~M3DWrapper() {
  96. reset();
  97. }
  98. void M3DWrapper::reset() {
  99. ClearSave();
  100. if (m3d_)
  101. m3d_free(m3d_);
  102. m3d_ = nullptr;
  103. }
  104. unsigned char *M3DWrapper::Save(int quality, int flags, unsigned int &size) {
  105. #if (!(ASSIMP_BUILD_NO_EXPORT || ASSIMP_BUILD_NO_M3D_EXPORTER))
  106. ClearSave();
  107. saved_output_ = m3d_save(m3d_, quality, flags, &size);
  108. return saved_output_;
  109. #else
  110. return nullptr;
  111. #endif
  112. }
  113. void M3DWrapper::ClearSave() {
  114. if (saved_output_)
  115. M3D_FREE(saved_output_);
  116. saved_output_ = nullptr;
  117. }
  118. } // namespace Assimp
  119. #endif