two step api installation

This commit is contained in:
Tungstend 2024-01-09 03:51:35 +08:00
parent 5eae48377e
commit 97c0774786
4 changed files with 106 additions and 74 deletions

View File

@ -2,6 +2,7 @@ package com.tungsten.fclcore.download;
import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
@ -34,13 +35,16 @@ public class ProcessService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String[] command = intent.getExtras().getStringArray("command");
boolean first = intent.getExtras().getBoolean("first");
String jre = first ? "jre8" : "jre17";
FCLConfig config = new FCLConfig(
getApplicationContext(),
getApplicationContext().getExternalFilesDir("log").getAbsolutePath(),
getApplicationContext().getDir("runtime", 0).getAbsolutePath() + "/java/jre8",
Environment.getExternalStorageDirectory().getAbsolutePath() + "/FCL/log",
getApplicationContext().getDir("runtime", 0).getAbsolutePath() + "/java/" + jre,
getApplicationContext().getCacheDir() + "/fclauncher",
null,
command);
command
);
startProcess(config);
return super.onStartCommand(intent, flags, startId);
}

View File

@ -157,6 +157,27 @@ public class ForgeNewInstallTask extends Task<Version> {
command.addAll(args);
runJVMProcess(processor, command, true);
for (Map.Entry<String, String> entry : outputs.entrySet()) {
Path artifact = Paths.get(entry.getKey());
if (!Files.isRegularFile(artifact))
throw new FileNotFoundException("File missing: " + artifact);
String code;
try (InputStream stream = Files.newInputStream(artifact)) {
code = DigestUtils.digestToString("SHA-1", stream);
}
if (!Objects.equals(code, entry.getValue())) {
Files.delete(artifact);
throw new ChecksumMismatchException("SHA-1", entry.getValue(), code);
}
}
}
}
private void runJVMProcess(ForgeNewInstallProfile.Processor processor, List<String> command, boolean first) throws Exception {
LOG.info("Executing external processor " + processor.getJar().toString() + ", command line: " + new CommandBuilder().addAll(command).toString());
int exitCode;
boolean listen = true;
@ -179,23 +200,11 @@ public class ForgeNewInstallTask extends Task<Version> {
server.start();
latch.await();
exitCode = Integer.parseInt((String) server.getResult());
if (exitCode != 0)
if (exitCode != 0) {
if (first) {
runJVMProcess(processor, command, false);
} else {
throw new IOException("Game processor exited abnormally with code " + exitCode);
for (Map.Entry<String, String> entry : outputs.entrySet()) {
Path artifact = Paths.get(entry.getKey());
if (!Files.isRegularFile(artifact))
throw new FileNotFoundException("File missing: " + artifact);
String code;
try (InputStream stream = Files.newInputStream(artifact)) {
code = DigestUtils.digestToString("SHA-1", stream);
}
if (!Objects.equals(code, entry.getValue())) {
Files.delete(artifact);
throw new ChecksumMismatchException("SHA-1", entry.getValue(), code);
}
}
}
}

View File

@ -158,6 +158,27 @@ public class NeoForgeOldInstallTask extends Task<Version> {
command.addAll(args);
runJVMProcess(processor, command, true);
for (Map.Entry<String, String> entry : outputs.entrySet()) {
Path artifact = Paths.get(entry.getKey());
if (!Files.isRegularFile(artifact))
throw new FileNotFoundException("File missing: " + artifact);
String code;
try (InputStream stream = Files.newInputStream(artifact)) {
code = DigestUtils.digestToString("SHA-1", stream);
}
if (!Objects.equals(code, entry.getValue())) {
Files.delete(artifact);
throw new ChecksumMismatchException("SHA-1", entry.getValue(), code);
}
}
}
}
private void runJVMProcess(ForgeNewInstallProfile.Processor processor, List<String> command, boolean first) throws Exception {
LOG.info("Executing external processor " + processor.getJar().toString() + ", command line: " + new CommandBuilder().addAll(command).toString());
int exitCode;
boolean listen = true;
@ -175,28 +196,17 @@ public class NeoForgeOldInstallTask extends Task<Version> {
Intent service = new Intent(FCLPath.CONTEXT, ProcessService.class);
Bundle bundle = new Bundle();
bundle.putStringArray("command", command.toArray(new String[0]));
bundle.putBoolean("first", first);
service.putExtras(bundle);
FCLPath.CONTEXT.startService(service);
server.start();
latch.await();
exitCode = Integer.parseInt((String) server.getResult());
if (exitCode != 0)
if (exitCode != 0) {
if (first) {
runJVMProcess(processor, command, false);
} else {
throw new IOException("Game processor exited abnormally with code " + exitCode);
for (Map.Entry<String, String> entry : outputs.entrySet()) {
Path artifact = Paths.get(entry.getKey());
if (!Files.isRegularFile(artifact))
throw new FileNotFoundException("File missing: " + artifact);
String code;
try (InputStream stream = Files.newInputStream(artifact)) {
code = DigestUtils.digestToString("SHA-1", stream);
}
if (!Objects.equals(code, entry.getValue())) {
Files.delete(artifact);
throw new ChecksumMismatchException("SHA-1", entry.getValue(), code);
}
}
}
}

View File

@ -160,29 +160,7 @@ public final class OptiFineInstallTask extends Task<Version> {
dest.toString(),
gameRepository.getLibraryFile(version, optiFineLibrary).toString()
};
int exitCode;
boolean listen = true;
while (listen) {
if (((ActivityManager) FCLPath.CONTEXT.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses().size() == 1) {
listen = false;
}
}
CountDownLatch latch = new CountDownLatch(1);
SocketServer server = new SocketServer("127.0.0.1", ProcessService.PROCESS_SERVICE_PORT, (server1, msg) -> {
server1.setResult(msg);
server1.stop();
latch.countDown();
});
Intent service = new Intent(FCLPath.CONTEXT, ProcessService.class);
Bundle bundle = new Bundle();
bundle.putStringArray("command", command);
service.putExtras(bundle);
FCLPath.CONTEXT.startService(service);
server.start();
latch.await();
exitCode = Integer.parseInt((String) server.getResult());
if (exitCode != 0)
throw new IOException("OptiFine patcher failed, command: " + new CommandBuilder().addAll(Arrays.asList(command)));
runJVMProcess(command, true);
} else {
FileUtils.copyFile(dest, gameRepository.getLibraryFile(version, optiFineLibrary).toPath());
}
@ -244,6 +222,37 @@ public final class OptiFineInstallTask extends Task<Version> {
dependencies.add(dependencyManager.checkLibraryCompletionAsync(getResult(), true));
}
private void runJVMProcess(String[] command, boolean first) throws Exception {
int exitCode;
boolean listen = true;
while (listen) {
if (((ActivityManager) FCLPath.CONTEXT.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses().size() == 1) {
listen = false;
}
}
CountDownLatch latch = new CountDownLatch(1);
SocketServer server = new SocketServer("127.0.0.1", ProcessService.PROCESS_SERVICE_PORT, (server1, msg) -> {
server1.setResult(msg);
server1.stop();
latch.countDown();
});
Intent service = new Intent(FCLPath.CONTEXT, ProcessService.class);
Bundle bundle = new Bundle();
bundle.putStringArray("command", command);
service.putExtras(bundle);
FCLPath.CONTEXT.startService(service);
server.start();
latch.await();
exitCode = Integer.parseInt((String) server.getResult());
if (exitCode != 0) {
if (first) {
runJVMProcess(command, false);
} else {
throw new IOException("OptiFine patcher failed, command: " + new CommandBuilder().addAll(Arrays.asList(command)));
}
}
}
/**
* Install OptiFine library from existing local file.
*