forked from PojavLauncherTeam/PojavLauncher
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathJavaGUILauncherActivity.java
More file actions
440 lines (384 loc) · 18 KB
/
Copy pathJavaGUILauncherActivity.java
File metadata and controls
440 lines (384 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package net.kdt.pojavlaunch;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.DEFAULT_PREF;
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;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AlertDialog;
import com.kdt.LoggerView;
import net.kdt.pojavlaunch.customcontrols.keyboard.AwtCharSender;
import net.kdt.pojavlaunch.customcontrols.keyboard.TouchCharInput;
import net.kdt.pojavlaunch.multirt.MultiRTUtils;
import net.kdt.pojavlaunch.multirt.Runtime;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouchListener {
private AWTCanvasView mTextureView;
private LoggerView mLoggerView;
private TouchCharInput mTouchCharInput;
private LinearLayout mTouchPad;
private ImageView mMousePointerImageView;
private GestureDetector mGestureDetector;
private boolean mIsVirtualMouseEnabled;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_gui_launcher);
try {
File latestLogFile = new File(Tools.DIR_GAME_HOME, "latestlog.txt");
if (!latestLogFile.exists() && !latestLogFile.createNewFile())
throw new IOException("Failed to create a new log file");
Logger.begin(latestLogFile.getAbsolutePath());
}catch (IOException e) {
Tools.showError(this, e, true);
}
MainActivity.GLOBAL_CLIPBOARD = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
mTouchCharInput = findViewById(R.id.awt_touch_char);
mTouchCharInput.setCharacterSender(new AwtCharSender());
mTouchPad = findViewById(R.id.main_touchpad);
mLoggerView = findViewById(R.id.launcherLoggerView);
mMousePointerImageView = findViewById(R.id.main_mouse_pointer);
mTextureView = findViewById(R.id.installmod_surfaceview);
mGestureDetector = new GestureDetector(this, new SingleTapConfirm());
mTouchPad.setFocusable(false);
mTouchPad.setVisibility(View.GONE);
findViewById(R.id.installmod_mouse_pri).setOnTouchListener(this);
findViewById(R.id.installmod_mouse_sec).setOnTouchListener(this);
findViewById(R.id.installmod_window_moveup).setOnTouchListener(this);
findViewById(R.id.installmod_window_movedown).setOnTouchListener(this);
findViewById(R.id.installmod_window_moveleft).setOnTouchListener(this);
findViewById(R.id.installmod_window_moveright).setOnTouchListener(this);
mMousePointerImageView.post(() -> {
ViewGroup.LayoutParams params = mMousePointerImageView.getLayoutParams();
params.width = (int) (36 * LauncherPreferences.PREF_MOUSESCALE);
params.height = (int) (54 * LauncherPreferences.PREF_MOUSESCALE);
});
mTouchPad.setOnTouchListener(new View.OnTouchListener() {
float prevX = 0, prevY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
// int index = event.getActionIndex();
int action = event.getActionMasked();
float x = event.getX();
float y = event.getY();
float mouseX, mouseY;
mouseX = mMousePointerImageView.getX();
mouseY = mMousePointerImageView.getY();
if (mGestureDetector.onTouchEvent(event)) {
sendScaledMousePosition(mouseX,mouseY);
AWTInputBridge.sendMousePress(AWTInputEvent.BUTTON1_DOWN_MASK);
} else {
if (action == MotionEvent.ACTION_MOVE) { // 2
mouseX = Math.max(0, Math.min(CallbackBridge.physicalWidth, mouseX + x - prevX));
mouseY = Math.max(0, Math.min(CallbackBridge.physicalHeight, mouseY + y - prevY));
placeMouseAt(mouseX, mouseY);
sendScaledMousePosition(mouseX, mouseY);
}
}
prevY = y;
prevX = x;
return true;
}
});
mTextureView.setOnTouchListener((v, event) -> {
float x = event.getX();
float y = event.getY();
if (mGestureDetector.onTouchEvent(event)) {
sendScaledMousePosition(x + mTextureView.getX(), y);
AWTInputBridge.sendMousePress(AWTInputEvent.BUTTON1_DOWN_MASK);
return true;
}
switch (event.getActionMasked()) {
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_CANCEL: // 3
case MotionEvent.ACTION_POINTER_UP: // 6
break;
case MotionEvent.ACTION_MOVE: // 2
sendScaledMousePosition(x + mTextureView.getX(), y);
break;
}
return true;
});
try {
placeMouseAt(CallbackBridge.physicalWidth / 2f, CallbackBridge.physicalHeight / 2f);
Bundle extras = getIntent().getExtras();
if(extras == null) {
finish();
return;
}
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);
}
getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Tools.dialogForceClose(JavaGUILauncherActivity.this);
}
});
}
private void startModInstallerWithUri(Uri uri) {
try {
File cacheFile = new File(getCacheDir(), "mod-installer-temp");
InputStream contentStream = getContentResolver().openInputStream(uri);
if(contentStream == null) throw new IOException("Failed to open content stream");
try (FileOutputStream fileOutputStream = new FileOutputStream(cacheFile)) {
IOUtils.copy(contentStream, fileOutputStream);
}
contentStream.close();
startModInstaller(cacheFile, null);
}catch (IOException e) {
Tools.showError(this, e, true);
}
}
public Runtime selectRuntime(File modFile) {
int javaVersion = getJavaVersion(modFile);
if(javaVersion == -1) {
finalErrorDialog(getString(R.string.execute_jar_failed_to_read_file));
return null;
}
String nearestRuntime = MultiRTUtils.getNearestJreName(javaVersion);
if(nearestRuntime == null) {
finalErrorDialog(getString(R.string.multirt_nocompatiblert, javaVersion));
return null;
}
Runtime selectedRuntime = MultiRTUtils.forceReread(nearestRuntime);
int selectedJavaVersion = Math.max(javaVersion, selectedRuntime.javaVersion);
// Don't allow versions higher than Java 17 because our caciocavallo implementation does not allow for it
if(selectedJavaVersion > 17) {
finalErrorDialog(getString(R.string.execute_jar_incompatible_runtime, selectedJavaVersion));
return null;
}
return selectedRuntime;
}
private File findModPath(List<String> argList) {
int argsSize = argList.size();
for(int i = 0; i < argsSize; i++) {
// Look for the -jar argument
if(!argList.get(i).equals("-jar")) continue;
int pathIndex = i+1;
// Check if the supposed path is out of the argument bounds
if(pathIndex >= argsSize) return null;
// Use the path as a file
return new File(argList.get(pathIndex));
}
return null;
}
private void startModInstaller(File modFile, String javaArgs) {
new Thread(() -> {
// Maybe replace with more advanced arg parsing logic later
List<String> argList = javaArgs != null ? Arrays.asList(javaArgs.split(" ")) : null;
File selectedMod = modFile;
if(selectedMod == null && argList != null) {
// If modFile is not specified directly, try to extract the -jar argument from the javaArgs
selectedMod = findModPath(argList);
}
Runtime selectedRuntime;
if(selectedMod == null || DEFAULT_PREF.getBoolean("disable_autojre_select", false)) {
// If we are unable to find out the path to the mod or the user explicitly desires so, we use the default runtime
selectedRuntime = MultiRTUtils.forceReread(LauncherPreferences.PREF_DEFAULT_RUNTIME);
}else {
// Autoselect it properly in the other case.
selectedRuntime = selectRuntime(selectedMod);
// If the selection failed, just return. The autoselect function has already shown the dialog.
if(selectedRuntime == null) return;
}
launchJavaRuntime(selectedRuntime, modFile, argList);
}, "JREMainThread").start();
}
private void finalErrorDialog(CharSequence msg) {
runOnUiThread(()-> new AlertDialog.Builder(this)
.setTitle(R.string.global_error)
.setMessage(msg)
.setPositiveButton(android.R.string.ok, (d,w)->this.finish())
.setCancelable(false)
.show());
}
@Override
public void onResume() {
super.onResume();
final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
final View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(uiOptions);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent e) {
boolean isDown;
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: // 0
case MotionEvent.ACTION_POINTER_DOWN: // 5
isDown = true;
break;
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_CANCEL: // 3
case MotionEvent.ACTION_POINTER_UP: // 6
isDown = false;
break;
default:
return false;
}
switch (v.getId()) {
case R.id.installmod_mouse_pri:
AWTInputBridge.sendMousePress(AWTInputEvent.BUTTON1_DOWN_MASK, isDown);
break;
case R.id.installmod_mouse_sec:
AWTInputBridge.sendMousePress(AWTInputEvent.BUTTON3_DOWN_MASK, isDown);
break;
}
if(isDown) switch(v.getId()) {
case R.id.installmod_window_moveup:
AWTInputBridge.nativeMoveWindow(0, -10);
break;
case R.id.installmod_window_movedown:
AWTInputBridge.nativeMoveWindow(0, 10);
break;
case R.id.installmod_window_moveleft:
AWTInputBridge.nativeMoveWindow(-10, 0);
break;
case R.id.installmod_window_moveright:
AWTInputBridge.nativeMoveWindow(10, 0);
break;
}
return true;
}
public void placeMouseAt(float x, float y) {
mMousePointerImageView.setX(x);
mMousePointerImageView.setY(y);
}
@SuppressWarnings("SuspiciousNameCombination")
void sendScaledMousePosition(float x, float y){
// Clamp positions to the borders of the usable view, then scale them
x = androidx.core.math.MathUtils.clamp(x, mTextureView.getX(), mTextureView.getX() + mTextureView.getWidth());
y = androidx.core.math.MathUtils.clamp(y, mTextureView.getY(), mTextureView.getY() + mTextureView.getHeight());
AWTInputBridge.sendMousePos(
(int) MathUtils.map(x, mTextureView.getX(), mTextureView.getX() + mTextureView.getWidth(), 0, AWTCanvasView.AWT_CANVAS_WIDTH),
(int) MathUtils.map(y, mTextureView.getY(), mTextureView.getY() + mTextureView.getHeight(), 0, AWTCanvasView.AWT_CANVAS_HEIGHT)
);
}
public void forceClose(View v) {
Tools.dialogForceClose(this);
}
public void openLogOutput(View v) {
mLoggerView.setVisibility(View.VISIBLE);
}
public void toggleVirtualMouse(View v) {
mIsVirtualMouseEnabled = !mIsVirtualMouseEnabled;
mTouchPad.setVisibility(mIsVirtualMouseEnabled ? View.VISIBLE : View.GONE);
Toast.makeText(this,
mIsVirtualMouseEnabled ? R.string.control_mouseon : R.string.control_mouseoff,
Toast.LENGTH_SHORT).show();
}
public void launchJavaRuntime(Runtime runtime, File modFile, List<String> javaArgs) {
JREUtils.redirectAndPrintJRELog();
try {
List<String> javaArgList = new ArrayList<>();
// FIXME: Freetype is shipped inside lwjgl. We should ship it outside and use lwjgl native jars instead.
String javaLibraryPath = Tools.lwjglNativesDir + ":" +
Tools.NATIVE_LIB_DIR;
javaArgList.add("-Djava.library.path="+ javaLibraryPath);
// Enable Caciocavallo
Tools.getCacioJavaArgs(javaArgList,runtime.javaVersion == 8, this);
if(javaArgs != null) {
javaArgList.addAll(javaArgs);
}
if(modFile != null) {
javaArgList.add("-jar");
javaArgList.add(modFile.getAbsolutePath());
}
if (LauncherPreferences.PREF_JAVA_SANDBOX) {
Collections.reverse(javaArgList);
javaArgList.add("-Xbootclasspath/a:" + Tools.DIR_DATA + "/security/pro-grade.jar");
javaArgList.add("-Djava.security.manager=net.sourceforge.prograde.sm.ProGradeJSM");
javaArgList.add("-Djava.security.policy=" + Tools.DIR_DATA + "/security/java_sandbox.policy");
Collections.reverse(javaArgList);
}
Logger.appendToLog("Info: Java arguments: " + Arrays.toString(javaArgList.toArray(new String[0])));
JREUtils.launchJavaVM(this, runtime,null,javaArgList, LauncherPreferences.PREF_CUSTOM_JAVA_ARGS);
} catch (Throwable th) {
Tools.showError(this, th, true);
}
}
public void toggleKeyboard(View view) {
mTouchCharInput.switchKeyboardState();
}
public void performCopy(View view) {
AWTInputBridge.sendKey(' ', AWTInputEvent.VK_CONTROL, 1);
AWTInputBridge.sendKey(' ', AWTInputEvent.VK_C);
AWTInputBridge.sendKey(' ', AWTInputEvent.VK_CONTROL, 0);
}
public void performPaste(View view) {
AWTInputBridge.sendKey(' ', AWTInputEvent.VK_CONTROL, 1);
AWTInputBridge.sendKey(' ', AWTInputEvent.VK_V);
AWTInputBridge.sendKey(' ', AWTInputEvent.VK_CONTROL, 0);
}
public int getJavaVersion(File modFile) {
try (ZipFile zipFile = new ZipFile(modFile)){
ZipEntry manifest = zipFile.getEntry("META-INF/MANIFEST.MF");
if(manifest == null) return -1;
String manifestString = Tools.read(zipFile.getInputStream(manifest));
String mainClass = Tools.extractUntilCharacter(manifestString, "Main-Class:", '\n');
if(mainClass == null) return -1;
mainClass = mainClass.trim().replace('.', '/') + ".class";
ZipEntry mainClassFile = zipFile.getEntry(mainClass);
if(mainClassFile == null) return -1;
InputStream classStream = zipFile.getInputStream(mainClassFile);
byte[] bytesWeNeed = new byte[8];
int readCount = classStream.read(bytesWeNeed);
classStream.close();
if(readCount < 8) return -1;
ByteBuffer byteBuffer = ByteBuffer.wrap(bytesWeNeed);
if(byteBuffer.getInt() != 0xCAFEBABE) return -1;
short minorVersion = byteBuffer.getShort();
short majorVersion = byteBuffer.getShort();
Log.i("JavaGUILauncher", majorVersion+","+minorVersion);
return classVersionToJavaVersion(majorVersion);
}catch (Exception e) {
Log.e("JavaVersion", "Exception thrown", e);
return -1;
}
}
public static int classVersionToJavaVersion(int majorVersion) {
if(majorVersion < 46) return 2; // there isn't even an arm64 port of jre 1.1 (or anything before 1.8 in fact)
return majorVersion - 44;
}
}