2020-06-19 22:18:33 +02:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
|
|
|
2020-09-14 15:53:40 +02:00
|
|
|
#include "skyline/crypto/key_store.h"
|
|
|
|
#include "skyline/vfs/nca.h"
|
2020-06-19 22:18:33 +02:00
|
|
|
#include "skyline/vfs/os_backing.h"
|
|
|
|
#include "skyline/loader/nro.h"
|
2020-06-25 17:29:35 +02:00
|
|
|
#include "skyline/loader/nso.h"
|
2020-06-29 20:23:33 +02:00
|
|
|
#include "skyline/loader/nca.h"
|
2021-02-27 23:11:09 +01:00
|
|
|
#include "skyline/loader/xci.h"
|
2020-06-29 22:19:32 +02:00
|
|
|
#include "skyline/loader/nsp.h"
|
2020-06-19 22:18:33 +02:00
|
|
|
#include "skyline/jvm.h"
|
|
|
|
|
2021-08-16 21:52:36 +02:00
|
|
|
extern "C" JNIEXPORT jint JNICALL Java_emu_skyline_loader_RomFile_populate(JNIEnv *env, jobject thiz, jint jformat, jint fd, jstring appFilesPathJstring, jint systemLanguage) {
|
2020-09-14 15:53:40 +02:00
|
|
|
skyline::loader::RomFormat format{static_cast<skyline::loader::RomFormat>(jformat)};
|
2020-06-19 22:18:33 +02:00
|
|
|
|
2021-11-09 16:48:47 +01:00
|
|
|
auto keyStore{std::make_shared<skyline::crypto::KeyStore>(skyline::JniString(env, appFilesPathJstring))};
|
2020-09-14 15:53:40 +02:00
|
|
|
std::unique_ptr<skyline::loader::Loader> loader;
|
2020-06-19 22:18:33 +02:00
|
|
|
try {
|
2020-09-14 15:53:40 +02:00
|
|
|
auto backing{std::make_shared<skyline::vfs::OsBacking>(fd)};
|
2020-06-19 22:18:33 +02:00
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case skyline::loader::RomFormat::NRO:
|
2020-09-14 15:53:40 +02:00
|
|
|
loader = std::make_unique<skyline::loader::NroLoader>(backing);
|
|
|
|
break;
|
2020-06-25 17:29:35 +02:00
|
|
|
case skyline::loader::RomFormat::NSO:
|
2020-09-14 15:53:40 +02:00
|
|
|
loader = std::make_unique<skyline::loader::NsoLoader>(backing);
|
|
|
|
break;
|
2020-06-29 20:23:33 +02:00
|
|
|
case skyline::loader::RomFormat::NCA:
|
2020-09-14 15:53:40 +02:00
|
|
|
loader = std::make_unique<skyline::loader::NcaLoader>(backing, keyStore);
|
|
|
|
break;
|
2021-02-27 23:11:09 +01:00
|
|
|
case skyline::loader::RomFormat::XCI:
|
|
|
|
loader = std::make_unique<skyline::loader::XciLoader>(backing, keyStore);
|
|
|
|
break;
|
2020-06-29 22:19:32 +02:00
|
|
|
case skyline::loader::RomFormat::NSP:
|
2020-09-14 15:53:40 +02:00
|
|
|
loader = std::make_unique<skyline::loader::NspLoader>(backing, keyStore);
|
|
|
|
break;
|
2020-06-19 22:18:33 +02:00
|
|
|
default:
|
2020-09-14 15:53:40 +02:00
|
|
|
return static_cast<jint>(skyline::loader::LoaderResult::ParsingError);
|
2020-06-19 22:18:33 +02:00
|
|
|
}
|
2020-09-14 15:53:40 +02:00
|
|
|
} catch (const skyline::loader::loader_exception &e) {
|
|
|
|
return static_cast<jint>(e.error);
|
2020-06-19 22:18:33 +02:00
|
|
|
} catch (const std::exception &e) {
|
2020-09-14 15:53:40 +02:00
|
|
|
return static_cast<jint>(skyline::loader::LoaderResult::ParsingError);
|
2020-06-19 22:18:33 +02:00
|
|
|
}
|
|
|
|
|
2020-09-14 15:53:40 +02:00
|
|
|
jclass clazz{env->GetObjectClass(thiz)};
|
|
|
|
jfieldID applicationNameField{env->GetFieldID(clazz, "applicationName", "Ljava/lang/String;")};
|
|
|
|
jfieldID applicationAuthorField{env->GetFieldID(clazz, "applicationAuthor", "Ljava/lang/String;")};
|
|
|
|
jfieldID rawIconField{env->GetFieldID(clazz, "rawIcon", "[B")};
|
2020-06-19 22:18:33 +02:00
|
|
|
|
2020-09-14 15:53:40 +02:00
|
|
|
if (loader->nacp) {
|
2021-09-27 18:24:07 +02:00
|
|
|
auto language{skyline::language::GetApplicationLanguage(static_cast<skyline::language::SystemLanguage>(systemLanguage))};
|
2021-08-29 15:27:31 +02:00
|
|
|
if (((1 << static_cast<skyline::u32>(language)) & loader->nacp->supportedTitleLanguages) == 0)
|
2021-08-16 21:52:36 +02:00
|
|
|
language = loader->nacp->GetFirstSupportedTitleLanguage();
|
|
|
|
|
|
|
|
env->SetObjectField(thiz, applicationNameField, env->NewStringUTF(loader->nacp->GetApplicationName(language).c_str()));
|
|
|
|
env->SetObjectField(thiz, applicationAuthorField, env->NewStringUTF(loader->nacp->GetApplicationPublisher(language).c_str()));
|
2020-06-19 22:18:33 +02:00
|
|
|
|
2021-09-23 11:36:10 +02:00
|
|
|
auto icon{loader->GetIcon(language)};
|
2021-10-24 21:45:29 +02:00
|
|
|
jbyteArray iconByteArray{env->NewByteArray(static_cast<jsize>(icon.size()))};
|
|
|
|
env->SetByteArrayRegion(iconByteArray, 0, static_cast<jsize>(icon.size()), reinterpret_cast<const jbyte *>(icon.data()));
|
2020-09-14 15:53:40 +02:00
|
|
|
env->SetObjectField(thiz, rawIconField, iconByteArray);
|
|
|
|
}
|
2020-06-19 22:18:33 +02:00
|
|
|
|
2020-09-14 15:53:40 +02:00
|
|
|
return static_cast<jint>(skyline::loader::LoaderResult::Success);
|
2020-06-29 20:23:33 +02:00
|
|
|
}
|