Skip to content

Commit 8e22693

Browse files
authored
Merge pull request #227 from AngelAuraMC/feat/misc
feat: Download JREs from github
2 parents 32d987c + 5ce3158 commit 8e22693

9 files changed

Lines changed: 210 additions & 47 deletions

File tree

.github/workflows/android.yml

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,35 @@ jobs:
3838
branch: buildjre8
3939
name: jre8-pojav
4040

41-
- name: Get JRE17
42-
uses: dawidd6/action-download-artifact@v9
43-
with:
44-
workflow: build.yml
45-
path: app_pojavlauncher/src/main/assets/components/jre-new
46-
workflow_conclusion: completed
47-
repo: AngelAuraMC/angelauramc-openjdk-build
48-
branch: buildjre17-21
49-
name: jre17-pojav
50-
51-
- name: Get JRE21
52-
uses: dawidd6/action-download-artifact@v9
53-
with:
54-
workflow: build.yml
55-
path: app_pojavlauncher/src/main/assets/components/jre-21
56-
workflow_conclusion: completed
57-
repo: AngelAuraMC/angelauramc-openjdk-build
58-
branch: buildjre17-21
59-
name: jre21-pojav
60-
61-
- name: Get JRE25
62-
uses: dawidd6/action-download-artifact@v9
63-
with:
64-
workflow: build.yml
65-
path: app_pojavlauncher/src/main/assets/components/jre-25
66-
workflow_conclusion: completed
67-
repo: FCL-Team/Android-OpenJDK-Build
68-
branch: Build_JRE_25
69-
name: jre25-multiarch
41+
# - name: Get JRE17
42+
# uses: dawidd6/action-download-artifact@v9
43+
# with:
44+
# workflow: build.yml
45+
# path: app_pojavlauncher/src/main/assets/components/jre-new
46+
# workflow_conclusion: completed
47+
# repo: AngelAuraMC/angelauramc-openjdk-build
48+
# branch: buildjre17-21
49+
# name: jre17-pojav
50+
#
51+
# - name: Get JRE21
52+
# uses: dawidd6/action-download-artifact@v9
53+
# with:
54+
# workflow: build.yml
55+
# path: app_pojavlauncher/src/main/assets/components/jre-21
56+
# workflow_conclusion: completed
57+
# repo: AngelAuraMC/angelauramc-openjdk-build
58+
# branch: buildjre17-21
59+
# name: jre21-pojav
60+
#
61+
# - name: Get JRE25
62+
# uses: dawidd6/action-download-artifact@v9
63+
# with:
64+
# workflow: build.yml
65+
# path: app_pojavlauncher/src/main/assets/components/jre-25
66+
# workflow_conclusion: completed
67+
# repo: FCL-Team/Android-OpenJDK-Build
68+
# branch: Build_JRE_25
69+
# name: jre25-multiarch
7070

7171
- uses: gradle/actions/setup-gradle@v4
7272
with:

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/NewJREUtil.java

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package net.kdt.pojavlaunch;
22

33
import static net.kdt.pojavlaunch.Architecture.archAsString;
4+
import static net.kdt.pojavlaunch.Architecture.getDeviceArchitecture;
5+
import static net.kdt.pojavlaunch.Tools.NATIVE_LIB_DIR;
6+
import static net.kdt.pojavlaunch.Tools.isOnline;
47

58
import android.app.Activity;
9+
import android.content.Context;
610
import android.content.res.AssetManager;
711
import android.util.Log;
812

13+
import com.kdt.mcgui.ProgressLayout;
14+
915
import net.kdt.pojavlaunch.multirt.MultiRTUtils;
1016
import net.kdt.pojavlaunch.multirt.Runtime;
17+
import net.kdt.pojavlaunch.progresskeeper.DownloaderProgressWrapper;
18+
import net.kdt.pojavlaunch.utils.DownloadUtils;
1119
import net.kdt.pojavlaunch.utils.MathUtils;
1220
import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;
1321
import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile;
1422

23+
import java.io.File;
24+
import java.io.FileInputStream;
1525
import java.io.IOException;
1626
import java.util.Arrays;
1727
import java.util.List;
@@ -56,7 +66,7 @@ private static InternalRuntime getInternalRuntime(Runtime runtime) {
5666
}
5767

5868
private static MathUtils.RankedValue<Runtime> getNearestInstalledRuntime(int targetVersion) {
59-
List<Runtime> runtimes = MultiRTUtils.getRuntimes();
69+
List<Runtime> runtimes = MultiRTUtils.getInstalledRuntimes();
6070
return MathUtils.findNearestPositive(targetVersion, runtimes, (runtime)->runtime.javaVersion);
6171
}
6272

@@ -84,7 +94,7 @@ public static boolean installNewJreIfNeeded(Activity activity, JMinecraftVersion
8494
// Check whether the selection is an internal runtime
8595
InternalRuntime internalRuntime = getInternalRuntime(runtime);
8696
// If it is, check if updates are available from the APK file
87-
if(internalRuntime != null) {
97+
if (internalRuntime != null) {
8898
// Not calling showRuntimeFail on failure here because we did, technically, find the compatible runtime
8999
return checkInternalRuntime(assetManager, internalRuntime);
90100
}
@@ -100,6 +110,18 @@ public static boolean installNewJreIfNeeded(Activity activity, JMinecraftVersion
100110
nearestInternalRuntime, nearestInstalledRuntime, (value)->value.rank
101111
);
102112

113+
// Check if the selected runtime actually exists in the APK, else download it
114+
// If it isn't InternalRuntime then it wasn't in the apk in the first place!
115+
if (selectedRankedRuntime.value instanceof InternalRuntime)
116+
if (!checkInternalRuntime(assetManager, (InternalRuntime) selectedRankedRuntime.value)) {
117+
if (nearestInstalledRuntime == null) // If this was non-null then it would be a valid runtime and we can leave it be
118+
tryDownloadRuntime(activity, gameRequiredVersion);
119+
// This means the internal runtime didn't extract so let's use installed instead
120+
// This also refreshes it so after the runtime download, it can find the new runtime
121+
selectedRankedRuntime = getNearestInstalledRuntime(gameRequiredVersion);
122+
}
123+
124+
103125
// No possible selections
104126
if(selectedRankedRuntime == null) {
105127
showRuntimeFail(activity, versionInfo);
@@ -141,6 +163,47 @@ private static void showRuntimeFail(Activity activity, JMinecraftVersionList.Ver
141163
activity.getString(R.string.multirt_nocompatiblert, verInfo.javaVersion.majorVersion));
142164
}
143165

166+
public static boolean isJavaVersionAvailableForDownload(int version) {
167+
for (ExternalRuntime javaVersion : ExternalRuntime.values()) {
168+
if (javaVersion.majorVersion == version) {
169+
return true;
170+
}
171+
}
172+
return false;
173+
}
174+
175+
private static String getJreSource(int javaVersion, String arch){
176+
return String.format("https://github.com/AngelAuraMC/angelauramc-openjdk-build/releases/download/download_jre%1$s/jre%1$s-android-%2$s.tar.xz", javaVersion, arch);
177+
}
178+
/**
179+
* @return whether installation was successful or not
180+
*/
181+
private static void tryDownloadRuntime(Context activity, int javaVersion){
182+
if (!isOnline(activity)) throw new RuntimeException(activity.getString(R.string.multirt_no_internet));
183+
String arch = archAsString(getDeviceArchitecture());
184+
// Checks for using this method
185+
if (!isJavaVersionAvailableForDownload(javaVersion)) throw new RuntimeException("This is not an available JRE version");
186+
if ((getDeviceArchitecture() == Architecture.ARCH_X86 && javaVersion >= 21)) throw new RuntimeException("x86 is not supported on Java"+javaVersion);
187+
try {
188+
File outputFile = new File(Tools.DIR_CACHE, String.format("jre%s-android-%s.tar.xz", javaVersion, arch));
189+
DownloaderProgressWrapper monitor = new DownloaderProgressWrapper(R.string.newdl_downloading_jre_runtime,
190+
ProgressLayout.UNPACK_RUNTIME);
191+
monitor.extraString = Integer.toString(javaVersion);
192+
DownloadUtils.downloadFileMonitored(
193+
getJreSource(javaVersion, arch),
194+
outputFile,
195+
null,
196+
monitor
197+
);
198+
String jreName = "External-" + javaVersion;
199+
MultiRTUtils.installRuntimeNamed(NATIVE_LIB_DIR, new FileInputStream(outputFile), jreName);
200+
MultiRTUtils.postPrepare(jreName);
201+
outputFile.delete();
202+
} catch (IOException e) {
203+
throw new RuntimeException("Failed to download Java "+javaVersion+" for "+arch, e);
204+
}
205+
}
206+
144207
private enum InternalRuntime {
145208
JRE_17(17, "Internal-17", "components/jre-new"),
146209
JRE_21(21, "Internal-21", "components/jre-21"),
@@ -155,4 +218,24 @@ private enum InternalRuntime {
155218
}
156219
}
157220

221+
public enum ExternalRuntime {
222+
JRE_8(8, "External-8"),
223+
JRE_17(17, "External-17"),
224+
JRE_21(21, "External-21"),
225+
JRE_25(25, "External-25");
226+
public final int majorVersion;
227+
public final String name;
228+
public final String downloadLink;
229+
public boolean isDownloading = false;
230+
231+
ExternalRuntime(int majorVersion, String name) {
232+
this.majorVersion = majorVersion;
233+
this.name = name;
234+
this.downloadLink = getJreSource(majorVersion, archAsString(getDeviceArchitecture()));
235+
}
236+
public void downloadRuntime(Context activity){
237+
tryDownloadRuntime(activity, majorVersion);
238+
}
239+
}
240+
158241
}

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/fragments/ProfileEditorFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.util.ArrayList;
4343
import java.util.Arrays;
4444
import java.util.List;
45-
import java.util.Objects;
4645

4746
public class ProfileEditorFragment extends Fragment implements CropperUtils.CropperListener{
4847
public static final String TAG = "ProfileEditorFragment";
@@ -173,7 +172,7 @@ private void loadValues(@NonNull String profile, @NonNull Context context){
173172
);
174173

175174
// Runtime spinner
176-
List<Runtime> runtimes = MultiRTUtils.getRuntimes();
175+
List<Runtime> runtimes = MultiRTUtils.getInstalledRuntimes();
177176
int jvmIndex = runtimes.indexOf(new Runtime("<Default>"));
178177
if (mTempProfile.javaDir != null) {
179178
String selectedRuntime = mTempProfile.javaDir.substring(Tools.LAUNCHERPROFILES_RTPREFIX.length());

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/multirt/MultiRTConfigDialog.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ public void refresh() {
2727
if(adapter != null) adapter.notifyDataSetChanged();
2828
}
2929

30+
public MultiRTConfigDialog get() {
31+
return this;
32+
}
33+
3034
/** Build the dialog behavior and style */
3135
public void prepare(Context activity, ActivityResultLauncher<Object> installJvmLauncher) {
3236
mDialogView = new RecyclerView(activity);
3337
mDialogView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
3438
RTRecyclerViewAdapter adapter = new RTRecyclerViewAdapter();
39+
adapter.setDialog(get());
3540
mDialogView.setAdapter(adapter);
3641

3742
mDialog = new AlertDialog.Builder(activity)

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/multirt/MultiRTUtils.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.kdt.pojavlaunch.multirt;
22

3+
import static net.kdt.pojavlaunch.Architecture.getDeviceArchitecture;
34
import static net.kdt.pojavlaunch.Tools.NATIVE_LIB_DIR;
45
import static org.apache.commons.io.FileUtils.listFiles;
56

@@ -8,6 +9,8 @@
89

910
import com.kdt.mcgui.ProgressLayout;
1011

12+
import net.kdt.pojavlaunch.Architecture;
13+
import net.kdt.pojavlaunch.NewJREUtil.ExternalRuntime;
1114
import net.kdt.pojavlaunch.R;
1215
import net.kdt.pojavlaunch.Tools;
1316
import net.kdt.pojavlaunch.utils.MathUtils;
@@ -36,7 +39,7 @@ public class MultiRTUtils {
3639
private static final String JAVA_VERSION_STR = "JAVA_VERSION=\"";
3740
private static final String OS_ARCH_STR = "OS_ARCH=\"";
3841

39-
public static List<Runtime> getRuntimes() {
42+
public static List<Runtime> getInstalledRuntimes() {
4043
if(!RUNTIME_FOLDER.exists() && !RUNTIME_FOLDER.mkdirs()) {
4144
throw new RuntimeException("Failed to create runtime directory");
4245
}
@@ -51,16 +54,33 @@ public static List<Runtime> getRuntimes() {
5154
return runtimes;
5255
}
5356

57+
/**
58+
*
59+
* @return Java versions which are not installed but are present in {@link ExternalRuntime}
60+
*/
61+
public static List<ExternalRuntime> getRuntimesToDownload() {
62+
List<ExternalRuntime> runtimesToDownload = new ArrayList<>();
63+
ExternalRuntime[] downloadableRuntimes = ExternalRuntime.values();
64+
for (ExternalRuntime downloadableruntime : downloadableRuntimes) {
65+
if(getExactJreName(downloadableruntime.majorVersion) == null){
66+
// x86 isn't supported anymore for JRE25
67+
if (!(getDeviceArchitecture() == Architecture.ARCH_X86 && downloadableruntime.majorVersion >= 21))
68+
runtimesToDownload.add(downloadableruntime);
69+
}
70+
}
71+
return runtimesToDownload;
72+
}
73+
5474
public static String getExactJreName(int majorVersion) {
55-
List<Runtime> runtimes = getRuntimes();
75+
List<Runtime> runtimes = getInstalledRuntimes();
5676
for(Runtime r : runtimes)
5777
if(r.javaVersion == majorVersion)return r.name;
5878

5979
return null;
6080
}
6181

6282
public static String getNearestJreName(int majorVersion) {
63-
List<Runtime> runtimes = getRuntimes();
83+
List<Runtime> runtimes = getInstalledRuntimes();
6484
MathUtils.RankedValue<Runtime> nearestRankedRuntime = MathUtils.findNearestPositive(majorVersion, runtimes, (runtime)->runtime.javaVersion);
6585
if(nearestRankedRuntime == null) return null;
6686
Runtime nearestRuntime = nearestRankedRuntime.value;

0 commit comments

Comments
 (0)