Skip to content

Commit

Permalink
Ref[javagui]: changes to the Java GUI installer
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Dec 11, 2023
1 parent 8da6924 commit c6a6c54
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.kdt.pojavlaunch;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.ClipboardManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
Expand All @@ -24,9 +26,11 @@
import net.kdt.pojavlaunch.utils.JREUtils;
import net.kdt.pojavlaunch.utils.MathUtils;

import org.apache.commons.io.IOUtils;
import org.lwjgl.glfw.CallbackBridge;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -47,7 +51,7 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc
private ImageView mMousePointerImageView;
private GestureDetector mGestureDetector;

private boolean mSkipDetectMod, mIsVirtualMouseEnabled;
private boolean mIsVirtualMouseEnabled;

@SuppressLint("ClickableViewAccessibility")
@Override
Expand Down Expand Up @@ -147,40 +151,23 @@ public boolean onTouch(View v, MotionEvent event) {
try {

placeMouseAt(CallbackBridge.physicalWidth / 2f, CallbackBridge.physicalHeight / 2f);

final File modFile = (File) getIntent().getExtras().getSerializable("modFile");
final String javaArgs = getIntent().getExtras().getString("javaArgs");
String jreName = LauncherPreferences.PREF_DEFAULT_RUNTIME;
if(modFile != null) {
int javaVersion = getJavaVersion(modFile);
if(javaVersion != -1) {
String autoselectRuntime = MultiRTUtils.getNearestJreName(javaVersion);
if (autoselectRuntime != null) jreName = autoselectRuntime;
}
}
final Runtime runtime = MultiRTUtils.forceReread(jreName);

mSkipDetectMod = getIntent().getExtras().getBoolean("skipDetectMod", false);
if(getIntent().getExtras().getBoolean("openLogOutput", false)) openLogOutput(null);
if (mSkipDetectMod) {
new Thread(() -> launchJavaRuntime(runtime, modFile, javaArgs), "JREMainThread").start();
Bundle extras = getIntent().getExtras();
if(extras == null) {
finish();
return;
}

// No skip detection
openLogOutput(null);
new Thread(() -> {
try {
// Due to time, the code here became, like, actually useless
// So it was removed
// Tbh this whole class needs a refactor...
doCustomInstall(runtime, modFile, javaArgs);
} catch (Throwable e) {
Logger.appendToLog("Install failed:");
Logger.appendToLog(Log.getStackTraceString(e));
Tools.showError(JavaGUILauncherActivity.this, e);
}
}, "Installer").start();
final String javaArgs = extras.getString("javaArgs");
final Uri resourceUri = (Uri) extras.getParcelable("modUri");
if(extras.getBoolean("openLogOutput", false)) openLogOutput(null);
if (javaArgs != null) {
startModInstaller(null, javaArgs);
}else if(resourceUri != null) {
ProgressDialog barrierDialog = Tools.getWaitingDialog(this, R.string.multirt_progress_caching);
PojavApplication.sExecutorService.execute(()->{
startModInstallerWithUri(resourceUri);
runOnUiThread(barrierDialog::dismiss);
});
}
} catch (Throwable th) {
Tools.showError(this, th, true);
}
Expand All @@ -194,6 +181,39 @@ public void handleOnBackPressed() {
});
}

private void startModInstallerWithUri(Uri uri) {
try {
File cacheFile = new File(getCacheDir(), "mod-installer-temp");
InputStream contentStream = getContentResolver().openInputStream(uri);
try (FileOutputStream fileOutputStream = new FileOutputStream(cacheFile)) {
IOUtils.copy(contentStream, fileOutputStream);
}
contentStream.close();
startModInstaller(cacheFile, null);
}catch (IOException e) {
Tools.showError(this, e, true);
}
}

private void startModInstaller(File modFile, String javaArgs) {
new Thread(() -> {
Runtime runtime = pickJreForMod(modFile);
launchJavaRuntime(runtime, modFile, javaArgs);
}, "JREMainThread").start();
}

private Runtime pickJreForMod(File modFile) {
String jreName = LauncherPreferences.PREF_DEFAULT_RUNTIME;
if(modFile != null) {
int javaVersion = getJavaVersion(modFile);
if(javaVersion != -1) {
String autoselectRuntime = MultiRTUtils.getNearestJreName(javaVersion);
if (autoselectRuntime != null) jreName = autoselectRuntime;
}
}
return MultiRTUtils.forceReread(jreName);
}

@Override
public void onResume() {
super.onResume();
Expand Down Expand Up @@ -313,13 +333,6 @@ public void launchJavaRuntime(Runtime runtime, File modFile, String javaArgs) {
}
}



private void doCustomInstall(Runtime runtime, File modFile, String javaArgs) {
mSkipDetectMod = true;
launchJavaRuntime(runtime, modFile, javaArgs);
}

public void toggleKeyboard(View view) {
mTouchCharInput.switchKeyboardState();
}
Expand Down
34 changes: 8 additions & 26 deletions app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -980,48 +980,30 @@ public static void installMod(Activity activity, boolean customJavaArgs) {
.setView(editText)
.setPositiveButton(android.R.string.ok, (di, i) -> {
Intent intent = new Intent(activity, JavaGUILauncherActivity.class);
intent.putExtra("skipDetectMod", true);
intent.putExtra("javaArgs", editText.getText().toString());
activity.startActivity(intent);
});
builder.show();
}

/** Display and return a progress dialog, instructing to wait */
private static ProgressDialog getWaitingDialog(Context ctx){
public static ProgressDialog getWaitingDialog(Context ctx, int message){
final ProgressDialog barrier = new ProgressDialog(ctx);
barrier.setMessage(ctx.getString(R.string.global_waiting));
barrier.setMessage(ctx.getString(message));
barrier.setProgressStyle(ProgressDialog.STYLE_SPINNER);
barrier.setCancelable(false);
barrier.show();

return barrier;
}

/** Copy the mod file, and launch the mod installer activity */
/** Launch the mod installer activity. The Uri must be from our own content provider or
* from ACTION_OPEN_DOCUMENT
*/
public static void launchModInstaller(Activity activity, @NonNull Uri uri){
final ProgressDialog alertDialog = getWaitingDialog(activity);

alertDialog.setMessage(activity.getString(R.string.multirt_progress_caching));
sExecutorService.execute(() -> {
try {
final String name = getFileName(activity, uri);
final File modInstallerFile = new File(Tools.DIR_CACHE, name);
FileOutputStream fos = new FileOutputStream(modInstallerFile);
InputStream input = activity.getContentResolver().openInputStream(uri);
IOUtils.copy(input, fos);
input.close();
fos.close();
activity.runOnUiThread(() -> {
alertDialog.dismiss();
Intent intent = new Intent(activity, JavaGUILauncherActivity.class);
intent.putExtra("modFile", modInstallerFile);
activity.startActivity(intent);
});
}catch(IOException e) {
Tools.showError(activity, e);
}
});
Intent intent = new Intent(activity, JavaGUILauncherActivity.class);
intent.putExtra("modUri", uri);
activity.startActivity(intent);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ public static void addAutoInstallArgs(Intent intent, File modInstallerJar, boole
intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar"
+ (createProfile ? "=NPS" : "") + // No Profile Suppression
" -jar "+modInstallerJar.getAbsolutePath());
intent.putExtra("skipDetectMod", true);
}
public static void addAutoInstallArgs(Intent intent, File modInstallerJar, String modpackFixupId) {
intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar"
+ "=\"" + modpackFixupId +"\"" +
" -jar "+modInstallerJar.getAbsolutePath());
intent.putExtra("skipDetectMod", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static void addAutoInstallArgs(Intent intent, File modInstallerJar) {
intent.putExtra("javaArgs", "-javaagent:"+ Tools.DIR_DATA+"/forge_installer/forge_installer.jar"
+ "=OFNPS" +// No Profile Suppression
" -jar "+modInstallerJar.getAbsolutePath());
intent.putExtra("skipDetectMod", true);
}

public static class OptiFineVersions {
Expand Down

0 comments on commit c6a6c54

Please sign in to comment.