From 728980c97a903bf313206fbd901fcfdc0ba87f2c Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sat, 23 Jan 2021 11:43:57 -0800 Subject: [PATCH] Fixed mesh reload after changing stud options. Fixes #608. --- common/lc_library.cpp | 58 +++++++++++++++++++--------------------- common/lc_library.h | 7 +++-- common/lc_meshloader.cpp | 2 +- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/common/lc_library.cpp b/common/lc_library.cpp index 6e473d39..48355d2f 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -305,6 +305,26 @@ void lcPiecesLibrary::LoadColors() emit ColorsLoaded(); } +bool lcPiecesLibrary::IsStudPrimitive(const char* FileName) +{ + return memcmp(FileName, "STU", 3) == 0; +} + +bool lcPiecesLibrary::IsStudStylePrimitive(const char* FileName) +{ + constexpr std::array StudStylePrimitives = + { + "2-4STUD4.DAT", "STUD.DAT", "STUD2.DAT", "STUD2A.DAT", "STUD3.DAT", "STUD4.DAT", "STUD4A.DAT", "STUD4H.DAT", + "8/STUD.DAT", "8/STUD2.DAT", "8/STUD2A.DAT", "8/STUD3.DAT", "8/STUD4.DAT", "8/STUD4A.DAT", "8/STUD4H.DAT" + }; + + for (const char* StudStylePrimitive : StudStylePrimitives) + if (!strcmp(StudStylePrimitive, FileName)) + return true; + + return false; +} + void lcPiecesLibrary::UpdateStudStyleSource() { if (!mSources.empty() && mSources.front()->Type == lcLibrarySourceType::StudStyle) @@ -430,13 +450,13 @@ bool lcPiecesLibrary::OpenArchive(std::unique_ptr File, lcZipFileType Zi Info->SetZipFile(ZipFileType, FileIdx); } else - Source->Primitives[Name] = new lcLibraryPrimitive(QString(), FileInfo.file_name + (Name - NameBuffer), ZipFileType, FileIdx, false, true); + Source->Primitives[Name] = new lcLibraryPrimitive(QString(), FileInfo.file_name + (Name - NameBuffer), ZipFileType, FileIdx, false, false, true); } else if (!memcmp(Name, "P/", 2)) { Name += 2; - Source->Primitives[Name] = new lcLibraryPrimitive(QString(), FileInfo.file_name + (Name - NameBuffer), ZipFileType, FileIdx, (memcmp(Name, "STU", 3) == 0), false); + Source->Primitives[Name] = new lcLibraryPrimitive(QString(), FileInfo.file_name + (Name - NameBuffer), ZipFileType, FileIdx, IsStudPrimitive(Name), IsStudStylePrimitive(Name), false); } } @@ -569,10 +589,10 @@ bool lcPiecesLibrary::OpenDirectory(const QDir& LibraryDir, bool ShowProgress) mHasUnofficial = true; const bool SubFile = SubFileDirectories[DirectoryIdx]; - Source->Primitives[Name] = new lcLibraryPrimitive(std::move(FileName), strchr(FileString, '/') + 1, lcZipFileType::Count, 0, !SubFile && (memcmp(Name, "STU", 3) == 0), SubFile); + Source->Primitives[Name] = new lcLibraryPrimitive(std::move(FileName), strchr(FileString, '/') + 1, lcZipFileType::Count, 0, !SubFile && IsStudPrimitive(Name), IsStudStylePrimitive(Name), SubFile); } } - + mSources.push_back(std::move(Source)); } @@ -1559,7 +1579,8 @@ void lcPiecesLibrary::SetStudStyle(lcStudStyle StudStyle, bool Reload) for (const auto& PrimitiveIt : Source->Primitives) { lcLibraryPrimitive* Primitive = PrimitiveIt.second; - if (Primitive->mMeshData.mHasStyleStud) + + if (Primitive->mStudStyle) Primitive->Unload(); } } @@ -1630,32 +1651,15 @@ bool lcPiecesLibrary::LoadPrimitive(lcLibraryPrimitive* Primitive) lcMeshLoader MeshLoader(Primitive->mMeshData, true, nullptr, false); - auto StudStylePrimitive = [this, &Primitive] () - { - if (!mSources.empty() && mSources.front()->Type == lcLibrarySourceType::StudStyle) - { - char Name[LC_PIECE_NAME_LEN]; - strcpy(Name, Primitive->mName); - strupr(Name); - const std::unique_ptr& Source = mSources.front(); - const auto& PrimitiveIt = Source->Primitives.find(Name); - if (PrimitiveIt != Source->Primitives.end()) - return true; - } - return false; - }; - if (mZipFiles[static_cast(lcZipFileType::Official)]) { lcLibraryPrimitive* LowPrimitive = nullptr; lcMemFile PrimFile; - if (Primitive->mStud) + if (Primitive->mStud && !Primitive->mStudStyle) { - if (StudStylePrimitive()) - Primitive->mMeshData.mHasStyleStud = true; - else if (strncmp(Primitive->mName, "8/", 2)) // todo: this is currently the only place that uses mName so use mFileName instead. this should also be done for the loose file libraries. + if (strncmp(Primitive->mName, "8/", 2)) // todo: this is currently the only place that uses mName so use mFileName instead. this should also be done for the loose file libraries. { char Name[LC_PIECE_NAME_LEN]; strcpy(Name, "8/"); @@ -1688,12 +1692,6 @@ bool lcPiecesLibrary::LoadPrimitive(lcLibraryPrimitive* Primitive) } else { - if (Primitive->mStud) - { - if (StudStylePrimitive()) - Primitive->mMeshData.mHasStyleStud = true; - } - if (Primitive->mZipFileType == lcZipFileType::Count) { lcDiskFile PrimFile(Primitive->mFileName); diff --git a/common/lc_library.h b/common/lc_library.h index d091128a..47716b57 100644 --- a/common/lc_library.h +++ b/common/lc_library.h @@ -48,7 +48,7 @@ enum class lcPrimitiveState class lcLibraryPrimitive { public: - explicit lcLibraryPrimitive(QString&& FileName, const char* Name, lcZipFileType ZipFileType, quint32 ZipFileIndex, bool Stud, bool SubFile) + explicit lcLibraryPrimitive(QString&& FileName, const char* Name, lcZipFileType ZipFileType, quint32 ZipFileIndex, bool Stud, bool StudStyle, bool SubFile) : mFileName(std::move(FileName)) { strncpy(mName, Name, sizeof(mName)-1); @@ -58,6 +58,7 @@ public: mZipFileIndex = ZipFileIndex; mState = lcPrimitiveState::NotLoaded; mStud = Stud; + mStudStyle = StudStyle; mSubFile = SubFile; } @@ -79,6 +80,7 @@ public: quint32 mZipFileIndex; lcPrimitiveState mState; bool mStud; + bool mStudStyle; bool mSubFile; lcLibraryMeshData mMeshData; }; @@ -205,6 +207,8 @@ protected: bool ReadDirectoryCacheFile(const QString& FileName, lcMemFile& CacheFile); bool WriteDirectoryCacheFile(const QString& FileName, lcMemFile& CacheFile); + static bool IsStudPrimitive(const char* FileName); + static bool IsStudStylePrimitive(const char* FileName); void UpdateStudStyleSource(); std::vector> mSources; @@ -224,4 +228,3 @@ protected: bool mHasUnofficial; bool mCancelLoading; }; - diff --git a/common/lc_meshloader.cpp b/common/lc_meshloader.cpp index 58d44d45..4ef696c1 100644 --- a/common/lc_meshloader.cpp +++ b/common/lc_meshloader.cpp @@ -1625,7 +1625,7 @@ bool lcMeshLoader::ReadMeshData(lcFile& File, const lcMatrix44& CurrentTransform else Library->GetPrimitiveFile(Primitive, FileCallback); - mMeshData.mHasStyleStud |= Primitive->mMeshData.mHasStyleStud; + mMeshData.mHasStyleStud |= Primitive->mStudStyle; } else Library->GetPieceFile(FileName, FileCallback);