Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JNI to check VERSION file in fonts.mpq #8

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMake/platforms/android.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ set(BUILD_TESTING OFF)
# All of these will be fetched via FetchContent and linked statically.
set(DEVILUTIONX_SYSTEM_SDL2 OFF)

# JNI source directory
list(APPEND DEVILUTIONX_PLATFORM_SUBDIRECTORIES platform/android)
list(APPEND DEVILUTIONX_PLATFORM_LINK_LIBRARIES libdevilutionx_android)

# Static SDL2 on Android requires Position Independent Code.
set(SDL_STATIC_PIC ON)

Expand Down
2 changes: 1 addition & 1 deletion Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ void CheckArchivesUpToDate()
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 } != "1\n";
fontsMpqOutOfDate = string_view { version_contents.get(), size } != font_mpq_version;
}
} else {
fontsMpqOutOfDate = false;
Expand Down
2 changes: 2 additions & 0 deletions Source/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ std::optional<MpqArchive> lang_mpq;
std::optional<MpqArchive> font_mpq;
#endif

char font_mpq_version[] = "1\n";

namespace {

#ifdef UNPACKED_MPQS
Expand Down
2 changes: 2 additions & 0 deletions Source/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ extern std::optional<MpqArchive> lang_mpq;
extern std::optional<MpqArchive> devilutionx_mpq;
#endif

extern char font_mpq_version[];

inline bool HaveSpawn()
{
#ifdef UNPACKED_MPQS
Expand Down
7 changes: 7 additions & 0 deletions Source/platform/android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include(functions/devilutionx_library)
add_devilutionx_object_library(libdevilutionx_android android.cpp)

target_link_libraries(libdevilutionx_android PUBLIC
DevilutionX::SDL
SDL_audiolib::SDL_audiolib
)
49 changes: 49 additions & 0 deletions Source/platform/android/android.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "engine/assets.hpp"
#include "init.h"
#include "mpq/mpq_reader.hpp"

#include <jni.h>

namespace devilution {
namespace {

bool AreFontsOutOfDate(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;
}

} // namespace
} // namespace devilution

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);
env->ReleaseStringUTFChars(fonts_mpq, mpqPath);
return outOfDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ private boolean isMissingGameData() {

File fonts_mpq = fileManager.getFile("/fonts.mpq");
if (lang.startsWith("ko") || lang.startsWith("zh") || lang.startsWith("ja") || fonts_mpq.exists()) {
if (!fonts_mpq.exists() ||
fonts_mpq.length() == 70471463 /* v1 */ ||
fonts_mpq.length() == 53991069 /* v2 */ ||
fonts_mpq.length() == 58488019 /* v3 */) {
if (!fonts_mpq.exists() || DevilutionXSDLActivity.areFontsOutOfDate(fonts_mpq.getAbsolutePath())) {
if (!isDownloadingFonts) {
fonts_mpq.delete();
isDownloadingFonts = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,10 @@ private boolean isMissingGameData() {
return true;
if (lang.startsWith("ru") && !fileManager.hasFile("ru.mpq"))
return true;
if (lang.startsWith("ko") || lang.startsWith("zh") || lang.startsWith("ja")) {
if (!fileManager.hasFile("fonts.mpq") ||
fileManager.fileSize("fonts.mpq") == 70471463 /* v1 */ ||
fileManager.fileSize("fonts.mpq") == 53991069 /* v2 */ ||
fileManager.fileSize("fonts.mpq") == 58488019 /* v3 */) {
File fonts_mpq = fileManager.getFile("/fonts.mpq");
if (lang.startsWith("ko") || lang.startsWith("zh") || lang.startsWith("ja") || fonts_mpq.exists()) {
if (!fonts_mpq.exists() || areFontsOutOfDate(fonts_mpq.getAbsolutePath()))
return true;
}
}

return !fileManager.hasFile("diabdat.mpq") &&
Expand Down Expand Up @@ -139,4 +136,6 @@ protected String[] getLibraries() {
"devilutionx"
};
}

public static native boolean areFontsOutOfDate(String fonts_mpq);
}