Skip to content

Commit

Permalink
Clean up smdh getting
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Dec 27, 2023
1 parent 74e8f7d commit a6116c2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 52 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ if(ENABLE_VULKAN)
endif()

if(ANDROID)
set(HEADER_FILES ${HEADER_FILES} include/jni_driver.hpp)
set(ALL_SOURCES ${ALL_SOURCES} src/jni_driver.cpp)
endif()

Expand Down
3 changes: 3 additions & 0 deletions include/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <filesystem>
#include <fstream>
#include <optional>
#include <span>

#include "PICA/gpu.hpp"
#include "cheats.hpp"
Expand Down Expand Up @@ -127,4 +128,6 @@ class Emulator {

std::filesystem::path getConfigPath();
std::filesystem::path getAndroidAppPath();

std::span<u8> getSMDH();
};
8 changes: 0 additions & 8 deletions include/jni_driver.hpp

This file was deleted.

1 change: 1 addition & 0 deletions include/loader/ncch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct NCCH {
std::vector<u8> saveData;
// The cart region. Only the CXI's region matters to us. Necessary to get past region locking
std::optional<Regions> region = std::nullopt;
std::vector<u8> smdh;

// Returns true on success, false on failure
// Partition index/offset/size must have been set before this
Expand Down
16 changes: 3 additions & 13 deletions src/core/loader/ncch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
#include "loader/ncch.hpp"
#include "memory.hpp"

#ifdef __ANDROID__
#include "jni_driver.hpp"
#endif

#include <iostream>

bool NCCH::loadFromHeader(Crypto::AESEngine &aesEngine, IOFile& file, const FSInfo &info) {
Expand Down Expand Up @@ -223,11 +219,10 @@ bool NCCH::loadFromHeader(Crypto::AESEngine &aesEngine, IOFile& file, const FSIn
}
} else if (std::strcmp(name, "icon") == 0) {
// Parse icon file to extract region info and more in the future (logo, etc)
std::vector<u8> tmp;
tmp.resize(fileSize);
readFromFile(file, exeFS, tmp.data(), fileOffset + exeFSHeaderSize, fileSize);
smdh.resize(fileSize);
readFromFile(file, exeFS, smdh.data(), fileOffset + exeFSHeaderSize, fileSize);

if (!parseSMDH(tmp)) {
if (!parseSMDH(smdh)) {
printf("Failed to parse SMDH!\n");
}
}
Expand Down Expand Up @@ -259,11 +254,6 @@ bool NCCH::parseSMDH(const std::vector<u8>& smdh) {
return false;
}

// In the Android version, notify the application that we're loading an SMDH file, to extract data for the title list
#ifdef __ANDROID__
Pandroid::onSmdhLoaded(smdh);
#endif

// Bitmask showing which regions are allowed.
// https://www.3dbrew.org/wiki/SMDH#Region_Lockout
const u32 regionMasks = *(u32*)&smdh[0x2018];
Expand Down
11 changes: 11 additions & 0 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ bool Emulator::loadELF(std::ifstream& file) {
return true;
}

std::span<u8> Emulator::getSMDH() {
switch (romType) {
case ROMType::NCSD:
case ROMType::CXI:
return memory.getCXI()->smdh;
default: {
return std::span<u8>();
}
}
}

#ifdef PANDA3DS_ENABLE_DISCORD_RPC
void Emulator::updateDiscord() {
if (config.discordRpcEnabled) {
Expand Down
26 changes: 9 additions & 17 deletions src/jni_driver.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "jni_driver.hpp"

#include <EGL/egl.h>
#include <android/log.h>
#include <jni.h>
Expand All @@ -15,7 +13,6 @@ HIDService* hidService = nullptr;
RendererGL* renderer = nullptr;
bool romLoaded = false;
JavaVM* jvm = nullptr;
const char* alberClass = "com/panda3ds/pandroid/AlberDriver";

#define AlberFunction(type, name) JNIEXPORT type JNICALL Java_com_panda3ds_pandroid_AlberDriver_##name

Expand All @@ -36,20 +33,6 @@ JNIEnv* jniEnv() {
return env;
}

void Pandroid::onSmdhLoaded(const std::vector<u8>& smdh) {
JNIEnv* env = jniEnv();
int size = smdh.size();

jbyteArray result = env->NewByteArray(size);
env->SetByteArrayRegion(result, 0, size, (jbyte*)smdh.data());

auto classLoader = env->FindClass(alberClass);
auto method = env->GetStaticMethodID(classLoader, "OnSmdhLoaded", "([B)V");

env->CallStaticVoidMethod(classLoader, method, result);
env->DeleteLocalRef(result);
}

extern "C" {

AlberFunction(void, Setup)(JNIEnv* env, jobject obj) { env->GetJavaVM(&jvm); }
Expand Down Expand Up @@ -104,6 +87,15 @@ AlberFunction(void, SetCirclepadAxis)(JNIEnv* env, jobject obj, jint x, jint y)
hidService->setCirclepadX((s16)x);
hidService->setCirclepadY((s16)y);
}

AlberFunction(jbyteArray, GetSmdh)(JNIEnv* env, jobject obj) {
std::span<u8> smdh = emulator->getSMDH();

jbyteArray result = env->NewByteArray(smdh.size());
env->SetByteArrayRegion(result, 0, smdh.size(), (jbyte*)smdh.data());

return result;
}
}

#undef AlberFunction
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import android.util.Log;

import com.panda3ds.pandroid.data.SMDH;
import com.panda3ds.pandroid.data.game.GameMetadata;
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.utils.GameUtils;

public class AlberDriver {
AlberDriver() { super(); }

Expand All @@ -23,14 +18,7 @@ public class AlberDriver {
public static native void TouchScreenUp();
public static native void TouchScreenDown(int x, int y);

public static void OnSmdhLoaded(byte[] buffer) {
SMDH smdh = new SMDH(buffer);
Log.i(Constants.LOG_TAG, "Loaded rom SDMH");
Log.i(Constants.LOG_TAG, String.format("Are you playing '%s' published by '%s'", smdh.getTitle(), smdh.getPublisher()));
GameMetadata game = GameUtils.getCurrentGame();
GameUtils.removeGame(game);
GameUtils.addGame(GameMetadata.applySMDH(game, smdh));
}
public static native byte[] GetSmdh();

static { System.loadLibrary("Alber"); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import android.util.Log;
import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.utils.GameUtils;
import com.panda3ds.pandroid.view.renderer.ConsoleRenderer;
import com.panda3ds.pandroid.view.renderer.layout.ConsoleLayout;
import com.panda3ds.pandroid.view.renderer.layout.DefaultScreenLayout;
import com.panda3ds.pandroid.data.SMDH;
import com.panda3ds.pandroid.data.game.GameMetadata;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

Expand Down Expand Up @@ -76,6 +79,19 @@ public void onSurfaceCreated(GL10 unused, EGLConfig config) {

AlberDriver.Initialize();
AlberDriver.LoadRom(romPath);

// Load the SMDH
byte[] smdhData = AlberDriver.GetSmdh();
if (smdhData.length == 0) {
Log.w(Constants.LOG_TAG, "Failed to load SMDH");
} else {
SMDH smdh = new SMDH(smdhData);
Log.i(Constants.LOG_TAG, "Loaded rom SDMH");
Log.i(Constants.LOG_TAG, String.format("Are you playing '%s' published by '%s'", smdh.getTitle(), smdh.getPublisher()));
GameMetadata game = GameUtils.getCurrentGame();
GameUtils.removeGame(game);
GameUtils.addGame(GameMetadata.applySMDH(game, smdh));
}
}

public void onDrawFrame(GL10 unused) {
Expand Down

0 comments on commit a6116c2

Please sign in to comment.