Skip to content

Commit

Permalink
Check DevilutionX version
Browse files Browse the repository at this point in the history
This is similar to how we handle extra fonts
  • Loading branch information
glebm committed Aug 14, 2024
1 parent 8e57785 commit 7d5bef7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ end_of_line = lf

[Dockerfile]
end_of_line = lf

[ASSETS_VERSION]
end_of_line = lf
1 change: 1 addition & 0 deletions CMake/Assets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if (Gettext_FOUND)
endif()

set(devilutionx_assets
ASSETS_VERSION
arena/church.dun
arena/circle_of_death.dun
arena/hell.dun
Expand Down
2 changes: 1 addition & 1 deletion Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ void CheckArchivesUpToDate()
#ifdef UNPACKED_MPQS
const bool devilutionxMpqOutOfDate = false;
#else
const bool devilutionxMpqOutOfDate = devilutionx_mpq && (!devilutionx_mpq->HasFile("data\\charbg.clx") || devilutionx_mpq->HasFile("fonts\\12-00.bin"));
const bool devilutionxMpqOutOfDate = IsDevilutionXMpqOutOfDate();
#endif
const bool fontsMpqOutOfDate = AreExtraFontsOutOfDate();

Expand Down
37 changes: 30 additions & 7 deletions Source/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ std::optional<MpqArchive> font_mpq;

namespace {

constexpr char DevilutionXMpqVersion[] = "1\n";
constexpr char ExtraFontsVersion[] = "1\n";

#ifdef UNPACKED_MPQS
Expand Down Expand Up @@ -174,22 +175,44 @@ std::vector<std::string> GetMPQSearchPaths()
return paths;
}

bool CheckExtraFontsVersion(AssetRef &&ref)
bool AssetContentsEq(AssetRef &&ref, std::string_view expected)
{
const size_t size = ref.size();
AssetHandle handle = OpenAsset(std::move(ref), false);
if (!handle.ok())
return true;
if (!handle.ok()) return false;
std::unique_ptr<char[]> contents { new char[size] };
if (!handle.read(contents.get(), size)) return false;
return std::string_view { contents.get(), size } == expected;
}

std::unique_ptr<char[]> version_contents { new char[size] };
if (!handle.read(version_contents.get(), size))
return true;
bool CheckDevilutionXMpqVersion(AssetRef &&ref)
{
return !AssetContentsEq(std::move(ref), DevilutionXMpqVersion);
}

return std::string_view { version_contents.get(), size } != ExtraFontsVersion;
bool CheckExtraFontsVersion(AssetRef &&ref)
{
return !AssetContentsEq(std::move(ref), ExtraFontsVersion);
}

} // namespace

#ifndef UNPACKED_MPQS
bool IsDevilutionXMpqOutOfDate(MpqArchive &archive)
{
const char filename[] = "ASSETS_VERSION";
const MpqFileHash fileHash = CalculateMpqFileHash(filename);
uint32_t fileNumber;
if (!archive.GetFileNumber(fileHash, fileNumber))
return true;
AssetRef ref;
ref.archive = &archive;
ref.fileNumber = fileNumber;
ref.filename = filename;
return CheckDevilutionXMpqVersion(std::move(ref));
}
#endif

#ifdef UNPACKED_MPQS
bool AreExtraFontsOutOfDate(const std::string &path)
{
Expand Down
13 changes: 13 additions & 0 deletions Source/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ inline bool AreExtraFontsOutOfDate()
#endif
}

#ifndef UNPACKED_MPQS
bool IsDevilutionXMpqOutOfDate(MpqArchive &archive);
#endif

inline bool IsDevilutionXMpqOutOfDate()
{
#ifdef UNPACKED_MPQS
return false;
#else
return !devilutionx_mpq.has_value() || IsDevilutionXMpqOutOfDate(*devilutionx_mpq);
#endif
}

void init_cleanup();
void LoadCoreArchives();
void LoadLanguageArchive();
Expand Down
1 change: 1 addition & 0 deletions assets/ASSETS_VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1

2 comments on commit 7d5bef7

@bubio
Copy link
Contributor

@bubio bubio commented on 7d5bef7 Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glebm
This change prevents the game from launching on macOS.
The following may be correct.

inline bool IsDevilutionXMpqOutOfDate()
{
#ifdef UNPACKED_MPQS
	return false;
#else // !UNPACKED_MPQS
#if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__3DS__) && !defined(__SWITCH__)
	return !devilutionx_mpq.has_value() || IsDevilutionXMpqOutOfDate(*devilutionx_mpq);
#endif
	return false;
#endif
}

@glebm
Copy link
Collaborator Author

@glebm glebm commented on 7d5bef7 Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bubio Thanks, sent a fix in #7346

Please sign in to comment.