android - add string map

This commit is contained in:
Emmanuel Hansen 2023-10-30 08:24:43 +00:00
parent fdb7320031
commit 71f3d22db8
10 changed files with 104 additions and 28 deletions

View File

@ -42,7 +42,23 @@ namespace LibRyujinx
private extern static JStringLocalRef createString(JEnvRef jEnv, IntPtr ch); private extern static JStringLocalRef createString(JEnvRef jEnv, IntPtr ch);
[DllImport("libryujinxjni")] [DllImport("libryujinxjni")]
private extern static void pushString(string ch); private extern static long storeString(string ch);
[DllImport("libryujinxjni")]
private extern static IntPtr getString(long id);
private static string GetStoredString(long id)
{
var pointer = getString(id);
if (pointer != IntPtr.Zero)
{
var str = Marshal.PtrToStringAnsi(pointer) ?? "";
Marshal.FreeHGlobal(pointer);
return str;
}
return "";
}
[DllImport("libryujinxjni")] [DllImport("libryujinxjni")]
internal extern static void setRenderingThread(); internal extern static void setRenderingThread();
@ -514,11 +530,11 @@ namespace LibRyujinx
} }
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_userGetOpenedUser")] [UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_userGetOpenedUser")]
public static void JniGetOpenedUser(JEnvRef jEnv, JObjectLocalRef jObj) public static JLong JniGetOpenedUser(JEnvRef jEnv, JObjectLocalRef jObj)
{ {
var userId = GetOpenedUser(); var userId = GetOpenedUser();
pushString(userId); return storeString(userId);
} }
[UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_userGetUserPicture")] [UnmanagedCallersOnly(EntryPoint = "Java_org_ryujinx_android_RyujinxNative_userGetUserPicture")]

View File

@ -29,6 +29,7 @@ add_library( # Sets the name of the library.
# Provides a relative path to your source file(s). # Provides a relative path to your source file(s).
vulkan_wrapper.cpp vulkan_wrapper.cpp
oboe.cpp oboe.cpp
string_helper.cpp
ryujinx.cpp) ryujinx.cpp)
# Searches for a specified prebuilt library and stores the path as a # Searches for a specified prebuilt library and stores the path as a

View File

@ -19,6 +19,7 @@
#include <fcntl.h> #include <fcntl.h>
#include "libraries/adrenotools/include/adrenotools/driver.h" #include "libraries/adrenotools/include/adrenotools/driver.h"
#include "native_window.h" #include "native_window.h"
#include "string_helper.h"
// A macro to pass call to Vulkan and check for return value for success // A macro to pass call to Vulkan and check for return value for success
#define CALL_VK(func) \ #define CALL_VK(func) \
@ -46,5 +47,6 @@ JavaVM* _vm = nullptr;
jobject _mainActivity = nullptr; jobject _mainActivity = nullptr;
jclass _mainActivityClass = nullptr; jclass _mainActivityClass = nullptr;
std::string _currentString = ""; std::string _currentString = "";
string_helper str_helper = string_helper();
#endif //RYUJINXNATIVE_RYUIJNX_H #endif //RYUJINXNATIVE_RYUIJNX_H

View File

@ -312,24 +312,28 @@ Java_org_ryujinx_android_NativeHelpers_getProgressInfo(JNIEnv *env, jobject thiz
return createStringFromStdString(env, progressInfo); return createStringFromStdString(env, progressInfo);
} }
extern "C"
long storeString(char* str){
return str_helper.store_cstring(str);
}
extern "C"
const char* getString(long id){
auto str = str_helper.get_stored(id);
auto cstr = (char*)::malloc(str.length() + 1);
::strcpy(cstr, str.c_str());
return cstr;
}
extern "C"
JNIEXPORT jlong JNICALL
Java_org_ryujinx_android_NativeHelpers_storeStringJava(JNIEnv *env, jobject thiz, jstring string) {
auto str = getStringPointer(env, string);
return str_helper.store_cstring(str);
}
extern "C" extern "C"
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_org_ryujinx_android_NativeHelpers_popStringJava(JNIEnv *env, jobject thiz) { Java_org_ryujinx_android_NativeHelpers_getStringJava(JNIEnv *env, jobject thiz, jlong id) {
return createStringFromStdString(env, _currentString); return createStringFromStdString(env, str_helper.get_stored(id));
}
extern "C"
JNIEXPORT void JNICALL
Java_org_ryujinx_android_NativeHelpers_pushStringJava(JNIEnv *env, jobject thiz, jstring string) {
_currentString = getStringPointer(env, string);
}
extern "C"
void pushString(char* str){
_currentString = str;
}
extern "C"
const char* popString(){
return _currentString.c_str();
} }

View File

@ -0,0 +1,24 @@
//
// Created by Emmanuel Hansen on 10/30/2023.
//
#include "string_helper.h"
long string_helper::store_cstring(const char *cstr) {
auto id = ++current_id;
_map.insert({id, cstr});
return id;
}
long string_helper::store_string(const string& str) {
auto id = ++current_id;
_map.insert({id, str});
return id;
}
string string_helper::get_stored(long id) {
auto str = _map[id];
_map.erase(id);
return str;
}

View File

@ -0,0 +1,29 @@
//
// Created by Emmanuel Hansen on 10/30/2023.
//
#ifndef RYUJINXANDROID_STRING_HELPER_H
#define RYUJINXANDROID_STRING_HELPER_H
#include <string>
#include <unordered_map>
using namespace std;
class string_helper {
public:
long store_cstring(const char * cstr);
long store_string(const string& str);
string get_stored(long id);
string_helper(){
_map = unordered_map<long,string>();
current_id = 0;
}
private:
unordered_map<long, string> _map;
long current_id;
};
#endif //RYUJINXANDROID_STRING_HELPER_H

View File

@ -28,6 +28,6 @@ class NativeHelpers {
external fun setSwapInterval(nativeWindow: Long, swapInterval: Int): Int external fun setSwapInterval(nativeWindow: Long, swapInterval: Int): Int
external fun getProgressInfo() : String external fun getProgressInfo() : String
external fun getProgressValue() : Float external fun getProgressValue() : Float
external fun pushStringJava(string: String) external fun storeStringJava(string: String) : Long
external fun popStringJava() : String external fun getStringJava(id: Long) : String
} }

View File

@ -53,7 +53,7 @@ class RyujinxNative {
external fun deviceSignalEmulationClose() external fun deviceSignalEmulationClose()
external fun deviceGetDlcTitleId(path: String, ncaPath: String) : String external fun deviceGetDlcTitleId(path: String, ncaPath: String) : String
external fun deviceGetDlcContentList(path: String, titleId: Long) : Array<String> external fun deviceGetDlcContentList(path: String, titleId: Long) : Array<String>
external fun userGetOpenedUser() external fun userGetOpenedUser() : Long
external fun userGetUserPicture(userId: String) : String external fun userGetUserPicture(userId: String) : String
external fun userSetUserPicture(userId: String, picture: String) external fun userSetUserPicture(userId: String, picture: String)
external fun userGetUserName(userId: String) : String external fun userGetUserName(userId: String) : String

View File

@ -106,8 +106,8 @@ class HomeViews {
} }
if (refreshUser.value) { if (refreshUser.value) {
native.userGetOpenedUser() val id = native.userGetOpenedUser()
user.value = NativeHelpers().popStringJava() user.value = NativeHelpers().getStringJava(id)
if (user.value.isNotEmpty()) { if (user.value.isNotEmpty()) {
val decoder = Base64.getDecoder() val decoder = Base64.getDecoder()
pic.value = decoder.decode(native.userGetUserPicture(user.value)) pic.value = decoder.decode(native.userGetUserPicture(user.value))

View File

@ -48,9 +48,9 @@ class UserViews {
fun Main(viewModel: MainViewModel? = null, navController: NavHostController? = null) { fun Main(viewModel: MainViewModel? = null, navController: NavHostController? = null) {
val ryujinxNative = RyujinxNative() val ryujinxNative = RyujinxNative()
val decoder = Base64.getDecoder() val decoder = Base64.getDecoder()
ryujinxNative.userGetOpenedUser() val id = ryujinxNative.userGetOpenedUser()
val openedUser = remember { val openedUser = remember {
mutableStateOf(NativeHelpers().popStringJava()) mutableStateOf(NativeHelpers().getStringJava(id))
} }
val openedUserPic = remember { val openedUserPic = remember {