Fixed MMD Importer not loading files from buffer and enabled unit test

pull/4484/head
RedSkittleFox 2022-04-13 02:51:12 +02:00
parent b38c25fe09
commit f26dc74f21
2 changed files with 23 additions and 16 deletions

View File

@ -55,6 +55,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <fstream>
#include <iomanip>
#include <memory>
#include <strstream>
static const aiImporterDesc desc = { "MMD Importer",
"",
@ -102,26 +103,32 @@ const aiImporterDesc *MMDImporter::GetInfo() const {
// ------------------------------------------------------------------------------------------------
// MMD import implementation
void MMDImporter::InternReadFile(const std::string &file, aiScene *pScene,
IOSystem * /*pIOHandler*/) {
// Read file by istream
std::filebuf fb;
if (!fb.open(file, std::ios::in | std::ios::binary)) {
IOSystem* pIOHandler) {
auto streamCloser = [&](IOStream *pStream) {
pIOHandler->Close(pStream);
};
static const std::string mode = "rb";
const std::unique_ptr<IOStream, decltype(streamCloser)> fileStream(pIOHandler->Open(file, mode), streamCloser);
if (fileStream == nullptr) {
throw DeadlyImportError("Failed to open file ", file, ".");
}
std::istream fileStream(&fb);
// Get the file-size and validate it, throwing an exception when fails
fileStream.seekg(0, fileStream.end);
size_t fileSize = static_cast<size_t>(fileStream.tellg());
fileStream.seekg(0, fileStream.beg);
if (fileSize < sizeof(pmx::PmxModel)) {
const size_t fileSize = fileStream->FileSize();
if (fileSize < sizeof(pmx::PmxModel))
{
throw DeadlyImportError(file, " is too small.");
}
std::vector<char> contents(fileStream->FileSize());
fileStream->Read(std::data(contents), 1, std::size(contents));
std::istrstream is(static_cast<const char *>(std::data(contents)), std::size(contents));
pmx::PmxModel model;
model.Read(&fileStream);
model.Read(&is);
CreateDataFromImport(&model, pScene);
}

View File

@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "UnitTestPCH.h"
#include "AbstractImportExportBase.h"
#include "AssetLib/MMD/MMDImporter.h"
#include "assimp/postprocess.h"
#include <assimp/Importer.hpp>
@ -51,9 +52,8 @@ class utPMXImporter : public AbstractImportExportBase {
public:
virtual bool importerTest() {
Assimp::Importer importer;
/*const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/../models-nonbsd/MMD/Alicia_blade.pmx", aiProcess_ValidateDataStructure );
return nullptr != scene;*/
return true;
const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/../models-nonbsd/MMD/Alicia_blade.pmx", aiProcess_ValidateDataStructure );
return nullptr != scene;
}
};