Compare commits

...
1 Commits
Author SHA1 Message Date
artdeell 003503e988 Remote Java VM implementation: the beginning 2022-12-30 22:21:13 +03:00
6 changed files with 107 additions and 5 deletions

No files matched your search

@@ -106,6 +106,7 @@
<service android:name=".services.ProgressService"/>
<service android:name=".services.GameService" android:process=":game"/>
<service android:name=".services.remotevm.RemoteVMService" android:process=":remotevm"/>
</application>
@@ -0,0 +1,12 @@
// IRemoteVMLogCallback.aidl
package net.kdt.pojavlaunch.services;
// Declare any non-default types here with import statements
interface IRemoteVMLogCallback {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void getLogLine(String line);
}
@@ -0,0 +1,13 @@
// IRemoteVMProcess.aidl
package net.kdt.pojavlaunch.services;
// Declare any non-default types here with import statements
import net.kdt.pojavlaunch.services.IRemoteVMLogCallback;
interface IRemoteVMProcess {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void runJVM(IRemoteVMLogCallback logCallback, in String[] commandLine);
}
@@ -0,0 +1,50 @@
package net.kdt.pojavlaunch.services.remotevm;
import android.content.Context;
import android.os.Process;
import android.os.RemoteException;
import net.kdt.pojavlaunch.Logger;
import net.kdt.pojavlaunch.multirt.MultiRTUtils;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.services.IRemoteVMLogCallback;
import net.kdt.pojavlaunch.services.IRemoteVMProcess;
import net.kdt.pojavlaunch.utils.JREUtils;
import java.util.Arrays;
public class RemoteVMBinder extends IRemoteVMProcess.Stub implements Logger.eventLogListener{
private final Context mContext;
private IRemoteVMLogCallback mCallback;
RemoteVMBinder(Context context) {
mContext = context;
}
@Override
public void runJVM(IRemoteVMLogCallback logCallback, String[] commandLine) throws RemoteException {
try {
this.mCallback = logCallback;
MultiRTUtils.setRuntimeNamed(LauncherPreferences.PREF_DEFAULT_RUNTIME);
Logger.getInstance().setLogListener(this);
JREUtils.launchJavaVM(mContext, Arrays.asList(commandLine));
}catch (Throwable th) {
RemoteException exception = new RemoteException("We fucked up");
exception.initCause(th);
throw exception;
}
suicide();
}
@Override
public void onEventLogged(String text) {
try {
if (mCallback != null) mCallback.getLogLine(text);
}catch (Exception e) {
e.printStackTrace();
suicide();
}
}
private void suicide() {
Process.killProcess(Process.myPid());
}
}
@@ -0,0 +1,23 @@
package net.kdt.pojavlaunch.services.remotevm;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
import net.kdt.pojavlaunch.multirt.MultiRTUtils;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
public class RemoteVMService extends Service {
private final RemoteVMBinder mBinder = new RemoteVMBinder(this);
@Override
public void onCreate() {
LauncherPreferences.loadPreferences(this);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
@@ -9,6 +9,8 @@ import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
import android.app.*;
import android.content.*;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.system.*;
import android.util.*;
import android.widget.Toast;
@@ -186,7 +188,7 @@ public class JREUtils {
LD_LIBRARY_PATH = ldLibraryPath.toString();
}
public static void setJavaEnvironment(Activity activity) throws Throwable {
public static void setJavaEnvironment(Context activity) throws Throwable {
Map<String, String> envMap = new ArrayMap<>();
envMap.put("POJAV_NATIVEDIR", NATIVE_LIB_DIR);
envMap.put("JAVA_HOME", Tools.DIR_HOME_JRE);
@@ -274,7 +276,8 @@ public class JREUtils {
// return ldLibraryPath;
}
public static int launchJavaVM(final Activity activity,final List<String> JVMArgs) throws Throwable {
public static int launchJavaVM(final Context activity,final List<String> JVMArgs) throws Throwable {
final Handler handler = new Handler(Looper.getMainLooper());
JREUtils.relocateLibPath();
// For debugging only!
/*
@@ -304,18 +307,18 @@ public class JREUtils {
if(LOCAL_RENDERER != null) userArgs.add("-Dorg.lwjgl.opengl.libname=" + graphicsLib);
userArgs.addAll(JVMArgs);
activity.runOnUiThread(() -> Toast.makeText(activity, activity.getString(R.string.autoram_info_msg,LauncherPreferences.PREF_RAM_ALLOCATION), Toast.LENGTH_SHORT).show());
handler.post(() -> Toast.makeText(activity, activity.getString(R.string.autoram_info_msg,LauncherPreferences.PREF_RAM_ALLOCATION), Toast.LENGTH_SHORT).show());
System.out.println(JVMArgs);
initJavaRuntime();
setupExitTrap(activity.getApplication());
setupExitTrap(activity);
chdir(Tools.DIR_GAME_NEW);
userArgs.add(0,"java"); //argv[0] is the program name according to C standard.
final int exitCode = VMLauncher.launchJVM(userArgs.toArray(new String[0]));
Logger.getInstance().appendToLog("Java Exit code: " + exitCode);
if (exitCode != 0) {
activity.runOnUiThread(() -> {
handler.post(() -> {
AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
dialog.setMessage(activity.getString(R.string.mcn_exit_title, exitCode));