From 61006184d18b8c2d876a63ce9099b8eff9857f17 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 28 May 2016 17:35:13 +0000 Subject: [PATCH] Search the current path for submodels when opening files. --- common/lc_library.cpp | 51 ++++++++++++++++++++++++++-------------- common/lc_library.h | 6 +++++ common/lc_mainwindow.cpp | 2 ++ common/pieceinf.cpp | 9 +++++-- common/project.cpp | 4 ++++ 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/common/lc_library.cpp b/common/lc_library.cpp index 616f13f7..b2b25552 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -871,6 +871,9 @@ bool lcPiecesLibrary::LoadPiece(PieceInfo* Info) lcLibraryMeshData MeshData; lcArray TextureStack; + bool Loaded = false; + bool SaveCache = false; + if (Info->mZipFileType != LC_NUM_ZIPFILES && mZipFiles[Info->mZipFileType]) { if (LoadCachePiece(Info)) @@ -878,15 +881,14 @@ bool lcPiecesLibrary::LoadPiece(PieceInfo* Info) lcMemFile PieceFile; - if (!mZipFiles[Info->mZipFileType]->ExtractFile(Info->mZipFileIndex, PieceFile)) - return false; + if (mZipFiles[Info->mZipFileType]->ExtractFile(Info->mZipFileIndex, PieceFile)) + { + const char* OldLocale = setlocale(LC_NUMERIC, "C"); + Loaded = ReadMeshData(PieceFile, lcMatrix44Identity(), 16, TextureStack, MeshData, LC_MESHDATA_SHARED, true); + setlocale(LC_NUMERIC, OldLocale); + } - const char* OldLocale = setlocale(LC_NUMERIC, "C"); - bool Ret = ReadMeshData(PieceFile, lcMatrix44Identity(), 16, TextureStack, MeshData, LC_MESHDATA_SHARED, true); - setlocale(LC_NUMERIC, OldLocale); - - if (!Ret) - return false; + SaveCache = Loaded && (Info->mZipFileType == LC_ZIPFILE_OFFICIAL); } else { @@ -899,20 +901,33 @@ bool lcPiecesLibrary::LoadPiece(PieceInfo* Info) sprintf(FileName, "%sparts/%s.dat", mLibraryPath, Name); - if (!PieceFile.Open(FileName, "rt")) - return false; - - const char* OldLocale = setlocale(LC_NUMERIC, "C"); - bool Ret = ReadMeshData(PieceFile, lcMatrix44Identity(), 16, TextureStack, MeshData, LC_MESHDATA_SHARED, true); - setlocale(LC_NUMERIC, OldLocale); - - if (!Ret) - return false; + if (PieceFile.Open(FileName, "rt")) + { + const char* OldLocale = setlocale(LC_NUMERIC, "C"); + Loaded = ReadMeshData(PieceFile, lcMatrix44Identity(), 16, TextureStack, MeshData, LC_MESHDATA_SHARED, true); + setlocale(LC_NUMERIC, OldLocale); + } } + if (!Loaded && !mCurrentModelPath.isEmpty()) + { + QFileInfo FileInfo(QDir(mCurrentModelPath), Info->m_strName); + + lcDiskFile PieceFile; + if (PieceFile.Open(FileInfo.absoluteFilePath().toLatin1().constData(), "rt")) // todo: qstring + { + const char* OldLocale = setlocale(LC_NUMERIC, "C"); + Loaded = ReadMeshData(PieceFile, lcMatrix44Identity(), 16, TextureStack, MeshData, LC_MESHDATA_SHARED, true); + setlocale(LC_NUMERIC, OldLocale); + } + } + + if (!Loaded) + return false; + CreateMesh(Info, MeshData); - if (mZipFiles[LC_ZIPFILE_OFFICIAL]) + if (SaveCache) SaveCachePiece(Info); return true; diff --git a/common/lc_library.h b/common/lc_library.h index f8f860fc..9383e3e8 100644 --- a/common/lc_library.h +++ b/common/lc_library.h @@ -158,6 +158,11 @@ public: mNumOfficialPieces = mPieces.GetSize(); } + void SetCurrentModelPath(const QString& ModelPath) + { + mCurrentModelPath = ModelPath; + } + bool ReadMeshData(lcFile& File, const lcMatrix44& CurrentTransform, lcuint32 CurrentColorCode, lcArray& TextureStack, lcLibraryMeshData& MeshData, lcMeshDataType MeshDataType, bool Optimize); lcMesh* CreateMesh(PieceInfo* Info, lcLibraryMeshData& MeshData); void UpdateBuffers(lcContext* Context); @@ -192,6 +197,7 @@ protected: bool LoadPrimitive(int PrimitiveIndex); QString mCachePath; + QString mCurrentModelPath; qint64 mArchiveCheckSum[4]; char mLibraryFileName[LC_MAXPATH]; char mUnofficialFileName[LC_MAXPATH]; diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index 1c48e3cf..0464f9e0 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -1848,6 +1848,8 @@ void lcMainWindow::NewProject() if (!SaveProjectIfModified()) return; + lcGetPiecesLibrary()->SetCurrentModelPath(QString()); + Project* NewProject = new Project(); g_App->SetProject(NewProject); lcGetPiecesLibrary()->UnloadUnusedParts(); diff --git a/common/pieceinf.cpp b/common/pieceinf.cpp index a2c535b9..db7d9932 100644 --- a/common/pieceinf.cpp +++ b/common/pieceinf.cpp @@ -122,8 +122,13 @@ void PieceInfo::Load() return; else if (mFlags & LC_PIECE_PLACEHOLDER) { - mFlags |= LC_PIECE_HAS_DEFAULT | LC_PIECE_HAS_LINES; - mBoundingBox = gPlaceholderMesh->mBoundingBox; + if (lcGetPiecesLibrary()->LoadPiece(this)) + mFlags &= ~LC_PIECE_PLACEHOLDER; + else + { + mFlags |= LC_PIECE_HAS_DEFAULT | LC_PIECE_HAS_LINES; + mBoundingBox = gPlaceholderMesh->mBoundingBox; + } } else { diff --git a/common/project.cpp b/common/project.cpp index 4fd62c61..d651da55 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -210,6 +210,8 @@ bool Project::Load(const QString& FileName) QFileInfo FileInfo(FileName); QString Extension = FileInfo.suffix().toLower(); + lcGetPiecesLibrary()->SetCurrentModelPath(FileInfo.absolutePath()); + if (Extension == QLatin1String("dat") || Extension == QLatin1String("ldr") || Extension == QLatin1String("mpd")) { QByteArray FileData = File.readAll(); @@ -284,6 +286,8 @@ bool Project::Save(const QString& FileName) return false; } + lcGetPiecesLibrary()->SetCurrentModelPath(QFileInfo(FileName).absolutePath()); + QTextStream Stream(&File); bool MPD = mModels.GetSize() > 1;