Introduce JniString as C++ wrapper over jstring

We used to manually call JNI UTF-8 string allocation and deallocation functions when utilizing a `jstring` but this could be erroneous and is just inconvenient. All of this has now been consolidated into an class `JniString` which is a wrapper around `std::string` and creates a copy of the contents of the `jstring` in its constructor and passes them into the `std::string` constructor.
This commit is contained in:
PixelyIon 2021-11-09 21:18:47 +05:30
parent 79ceb2cf23
commit fb476567ff
4 changed files with 23 additions and 11 deletions

View File

@ -76,8 +76,8 @@ extern "C" JNIEXPORT void Java_emu_skyline_EmulationActivity_executeApplication(
auto settings{std::make_shared<skyline::Settings>(preferenceFd)};
close(preferenceFd);
auto appFilesPath{env->GetStringUTFChars(appFilesPathJstring, nullptr)};
auto logger{std::make_shared<skyline::Logger>(std::string(appFilesPath) + "skyline.log", settings->logLevel)};
skyline::JniString appFilesPath(env, appFilesPathJstring);
auto logger{std::make_shared<skyline::Logger>(appFilesPath + "skyline.log", settings->logLevel)};
auto start{std::chrono::steady_clock::now()};
@ -92,7 +92,7 @@ extern "C" JNIEXPORT void Java_emu_skyline_EmulationActivity_executeApplication(
jvmManager,
logger,
settings,
std::string(appFilesPath),
appFilesPath,
GetTimeZoneName(),
static_cast<skyline::language::SystemLanguage>(systemLanguage),
std::make_shared<skyline::vfs::AndroidAssetFileSystem>(AAssetManager_fromJava(env, assetManager))
@ -102,11 +102,8 @@ extern "C" JNIEXPORT void Java_emu_skyline_EmulationActivity_executeApplication(
AudioWeak = os->state.audio;
InputWeak = os->state.input;
jvmManager->InitializeControllers();
env->ReleaseStringUTFChars(appFilesPathJstring, appFilesPath);
auto romUri{env->GetStringUTFChars(romUriJstring, nullptr)};
logger->InfoNoPrefix("Launching ROM {}", romUri);
env->ReleaseStringUTFChars(romUriJstring, romUri);
logger->InfoNoPrefix("Launching ROM {}", skyline::JniString(env, romUriJstring));
os->Execute(romFd, static_cast<skyline::loader::RomFormat>(romType));
} catch (std::exception &e) {

View File

@ -14,10 +14,7 @@
extern "C" JNIEXPORT jint JNICALL Java_emu_skyline_loader_RomFile_populate(JNIEnv *env, jobject thiz, jint jformat, jint fd, jstring appFilesPathJstring, jint systemLanguage) {
skyline::loader::RomFormat format{static_cast<skyline::loader::RomFormat>(jformat)};
auto appFilesPath{env->GetStringUTFChars(appFilesPathJstring, nullptr)};
auto keyStore{std::make_shared<skyline::crypto::KeyStore>(appFilesPath)};
env->ReleaseStringUTFChars(appFilesPathJstring, appFilesPath);
auto keyStore{std::make_shared<skyline::crypto::KeyStore>(skyline::JniString(env, appFilesPathJstring))};
std::unique_ptr<skyline::loader::Loader> loader;
try {
auto backing{std::make_shared<skyline::vfs::OsBacking>(fd)};

View File

@ -4,6 +4,13 @@
#include "jvm.h"
namespace skyline {
std::string JniString::GetJString(JNIEnv *env, jstring jString) {
auto utf{env->GetStringUTFChars(jString, nullptr)};
std::string string{utf};
env->ReleaseStringUTFChars(jString, utf);
return string;
}
/*
* @brief A thread-local wrapper over JNIEnv and JavaVM which automatically handles attaching and detaching threads
*/

View File

@ -7,6 +7,17 @@
#include <jni.h>
namespace skyline {
/**
* @brief A wrapper over std::string that supports construction using a JNI jstring
*/
class JniString : public std::string {
private:
static std::string GetJString(JNIEnv *env, jstring jString);
public:
JniString(JNIEnv *env, jstring jString) : std::string(GetJString(env, jString)) {}
};
/**
* @brief The JvmManager class is used to simplify transactions with the Java component
*/