Browse Source

De-duplicate extra fonts version check code

Co-authored-by: Stephen C. Wills <swills@gridprotectionalliance.org>
pull/6375/head
Gleb Mazovetskiy 3 years ago committed by Anders Jenbo
parent
commit
9d96eea86b
  1. 15
      Source/diablo.cpp
  2. 44
      Source/init.cpp
  3. 17
      Source/init.h
  4. 6
      Source/platform/android/CMakeLists.txt
  5. 28
      Source/platform/android/android.cpp

15
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<char[]> 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"));

44
Source/init.cpp

@ -74,10 +74,10 @@ std::optional<MpqArchive> lang_mpq;
std::optional<MpqArchive> font_mpq;
#endif
char font_mpq_version[] = "1\n";
namespace {
constexpr char ExtraFontsVersion[] = "1\n";
#ifdef UNPACKED_MPQS
std::optional<std::string> FindUnpackedMpqData(const std::vector<std::string> &paths, string_view mpqName)
{
@ -171,8 +171,48 @@ std::vector<std::string> 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<char[]> 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) {

17
Source/init.h

@ -47,8 +47,6 @@ extern std::optional<MpqArchive> lang_mpq;
extern std::optional<MpqArchive> 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();

6
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)

28
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<MpqArchive> 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<char[]> 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;
}

Loading…
Cancel
Save