Browse Source

Use JNI to check VERSION file in fonts.mpq

pull/6375/head
staphen 3 years ago committed by Anders Jenbo
parent
commit
22fdeaf2f0
  1. 4
      CMake/platforms/android.cmake
  2. 2
      Source/diablo.cpp
  3. 2
      Source/init.cpp
  4. 2
      Source/init.h
  5. 7
      Source/platform/android/CMakeLists.txt
  6. 49
      Source/platform/android/android.cpp
  7. 5
      android-project/app/src/main/java/org/diasurgical/devilutionx/DataActivity.java
  8. 11
      android-project/app/src/main/java/org/diasurgical/devilutionx/DevilutionXSDLActivity.java

4
CMake/platforms/android.cmake

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

2
Source/diablo.cpp

@ -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;

2
Source/init.cpp

@ -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

2
Source/init.h

@ -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

7
Source/platform/android/CMakeLists.txt

@ -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
Source/platform/android/android.cpp

@ -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;
}
}

5
android-project/app/src/main/java/org/diasurgical/devilutionx/DataActivity.java

@ -103,10 +103,7 @@ public class DataActivity extends Activity {
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;

11
android-project/app/src/main/java/org/diasurgical/devilutionx/DevilutionXSDLActivity.java

@ -78,13 +78,10 @@ public class DevilutionXSDLActivity extends SDLActivity {
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") &&
@ -139,4 +136,6 @@ public class DevilutionXSDLActivity extends SDLActivity {
"devilutionx"
};
}
public static native boolean areFontsOutOfDate(String fonts_mpq);
}

Loading…
Cancel
Save