From 9d96eea86b5e6305e71f8f0a9dbea4b5f3ad35c4 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 16 Jul 2023 07:54:37 +0900 Subject: [PATCH] De-duplicate extra fonts version check code Co-authored-by: Stephen C. Wills --- Source/diablo.cpp | 15 +-------- Source/init.cpp | 44 ++++++++++++++++++++++++-- Source/init.h | 17 ++++++++-- Source/platform/android/CMakeLists.txt | 6 +--- Source/platform/android/android.cpp | 28 ++-------------- 5 files changed, 62 insertions(+), 48 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 1358f0dd5..48e33ce8a 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -1094,23 +1094,10 @@ void CheckArchivesUpToDate() { #ifdef UNPACKED_MPQS const bool devilutionxMpqOutOfDate = false; - const bool haveFonts { font_data_path }; #else const bool devilutionxMpqOutOfDate = devilutionx_mpq && (!devilutionx_mpq->HasFile("data\\charbg.clx") || devilutionx_mpq->HasFile("fonts\\12-00.bin")); - const bool haveFonts { font_mpq }; #endif - bool fontsMpqOutOfDate = true; - if (haveFonts) { - size_t size; - AssetHandle handle = OpenAsset("fonts\\VERSION", size); - if (handle.ok()) { - std::unique_ptr version_contents { new char[size] }; - handle.read(version_contents.get(), size); - fontsMpqOutOfDate = string_view { version_contents.get(), size } != font_mpq_version; - } - } else { - fontsMpqOutOfDate = false; - } + const bool fontsMpqOutOfDate = AreExtraFontsOutOfDate(); if (devilutionxMpqOutOfDate && fontsMpqOutOfDate) { app_fatal(_("Please update devilutionx.mpq and fonts.mpq to the latest version")); diff --git a/Source/init.cpp b/Source/init.cpp index 911ecca69..a069d71c8 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -74,10 +74,10 @@ std::optional lang_mpq; std::optional font_mpq; #endif -char font_mpq_version[] = "1\n"; - namespace { +constexpr char ExtraFontsVersion[] = "1\n"; + #ifdef UNPACKED_MPQS std::optional FindUnpackedMpqData(const std::vector &paths, string_view mpqName) { @@ -171,8 +171,48 @@ std::vector GetMPQSearchPaths() return paths; } +bool CheckExtraFontsVersion(AssetRef &&ref) +{ + const size_t size = ref.size(); + AssetHandle handle = OpenAsset(std::move(ref), false); + if (!handle.ok()) + return true; + + std::unique_ptr version_contents { new char[size] }; + if (!handle.read(version_contents.get(), size)) + return true; + + return string_view { version_contents.get(), size } != ExtraFontsVersion; +} + } // namespace +#ifdef UNPACKED_MPQS +bool AreExtraFontsOutOfDate(const std::string &path) +{ + const std::string versionPath = path + "fonts" DIRECTORY_SEPARATOR_STR "VERSION"; + if (versionPath.size() + 1 > AssetRef::PathBufSize) + app_fatal("Path too long"); + AssetRef ref; + *BufCopy(ref.path, versionPath) = '\0'; + return CheckExtraFontsVersion(std::move(ref)); +} +#else +bool AreExtraFontsOutOfDate(MpqArchive &archive) +{ + const char filename[] = "fonts\\VERSION"; + const MpqArchive::FileHash fileHash = MpqArchive::CalculateFileHash(filename); + uint32_t fileNumber; + if (!archive.GetFileNumber(fileHash, fileNumber)) + return true; + AssetRef ref; + ref.archive = &archive; + ref.fileNumber = fileNumber; + ref.filename = filename; + return CheckExtraFontsVersion(std::move(ref)); +} +#endif + void init_cleanup() { if (gbIsMultiplayer && gbRunGame) { diff --git a/Source/init.h b/Source/init.h index bce3d2e96..2edcd387e 100644 --- a/Source/init.h +++ b/Source/init.h @@ -47,8 +47,6 @@ extern std::optional lang_mpq; extern std::optional devilutionx_mpq; #endif -extern char font_mpq_version[]; - inline bool HaveSpawn() { #ifdef UNPACKED_MPQS @@ -85,6 +83,21 @@ inline bool HaveExtraFonts() #endif } +#ifdef UNPACKED_MPQS +bool AreExtraFontsOutOfDate(const std::string &path); +#else +bool AreExtraFontsOutOfDate(MpqArchive &archive); +#endif + +inline bool AreExtraFontsOutOfDate() +{ +#ifdef UNPACKED_MPQS + return font_data_path && AreExtraFontsOutOfDate(*font_data_path); +#else + return font_mpq && AreExtraFontsOutOfDate(*font_mpq); +#endif +} + void init_cleanup(); void LoadCoreArchives(); void LoadLanguageArchive(); diff --git a/Source/platform/android/CMakeLists.txt b/Source/platform/android/CMakeLists.txt index 4f3e10b9d..66d091cab 100644 --- a/Source/platform/android/CMakeLists.txt +++ b/Source/platform/android/CMakeLists.txt @@ -1,7 +1,3 @@ include(functions/devilutionx_library) add_devilutionx_object_library(libdevilutionx_android android.cpp) - -target_link_libraries(libdevilutionx_android PUBLIC - DevilutionX::SDL - SDL_audiolib::SDL_audiolib -) +target_link_libraries(libdevilutionx_android PUBLIC DevilutionX::SDL) diff --git a/Source/platform/android/android.cpp b/Source/platform/android/android.cpp index 5ad2b3956..c0cbfab7c 100644 --- a/Source/platform/android/android.cpp +++ b/Source/platform/android/android.cpp @@ -1,4 +1,3 @@ -#include "engine/assets.hpp" #include "init.h" #include "mpq/mpq_reader.hpp" @@ -7,32 +6,11 @@ namespace devilution { namespace { -bool AreFontsOutOfDate(const char *mpqPath) +bool AreExtraFontsOutOfDateForMpqPath(const char *mpqPath) { int32_t error = 0; std::optional archive = MpqArchive::Open(mpqPath, error); - if (error != 0 || !archive) - return false; - - const char filename[] = "fonts\\VERSION"; - const MpqArchive::FileHash fileHash = MpqArchive::CalculateFileHash(filename); - uint32_t fileNumber; - if (!archive->GetFileNumber(fileHash, fileNumber)) - return true; - - AssetRef ref; - ref.archive = &*archive; - ref.fileNumber = fileNumber; - ref.filename = filename; - - const size_t size = ref.size(); - AssetHandle handle = OpenAsset(std::move(ref), false); - if (!handle.ok()) - return true; - - std::unique_ptr version_contents { new char[size] }; - handle.read(version_contents.get(), size); - return string_view { version_contents.get(), size } != font_mpq_version; + return error == 0 && archive && AreExtraFontsOutOfDate(*archive); } } // namespace @@ -42,7 +20,7 @@ extern "C" { JNIEXPORT jboolean JNICALL Java_org_diasurgical_devilutionx_DevilutionXSDLActivity_areFontsOutOfDate(JNIEnv *env, jclass cls, jstring fonts_mpq) { const char *mpqPath = env->GetStringUTFChars(fonts_mpq, nullptr); - bool outOfDate = devilution::AreFontsOutOfDate(mpqPath); + bool outOfDate = devilution::AreExtraFontsOutOfDateForMpqPath(mpqPath); env->ReleaseStringUTFChars(fonts_mpq, mpqPath); return outOfDate; }