Skip to content

Commit

Permalink
Add Download Progress Manager | Remote Download JRE
Browse files Browse the repository at this point in the history
  • Loading branch information
CADIndie committed Nov 7, 2024
1 parent 5dab53b commit 2edded5
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 175 deletions.
24 changes: 23 additions & 1 deletion .run/Pojlib [build].run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,29 @@
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<ForceTestExec>false</ForceTestExec>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
<configuration default="false" name="Pojlib [build]" type="GradleRunConfiguration" factoryName="Gradle" nameIsGenerated="true">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="build" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
</component>
Binary file removed src/main/assets/JRE-22.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/assets/lwjgl/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1730783037751
1730879022018
4 changes: 2 additions & 2 deletions src/main/java/pojlib/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class API {
public static boolean finishedDownloading = true;
public static boolean ignoreInstanceName;
public static boolean customRAMValue = false;
public static double downloadStatus;
public static String currentDownload;
public static double downloadStatus = 0;
public static String currentDownload = "";
public static String profileImage;
public static String profileName;
public static String profileUUID;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/pojlib/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import java.util.stream.Collectors;

import pojlib.util.Constants;
import pojlib.util.DownloadUtils;
import pojlib.util.download.DownloadManager;
import pojlib.util.download.DownloadUtils;
import pojlib.util.GsonUtils;
import pojlib.util.Logger;

Expand Down Expand Up @@ -112,7 +113,7 @@ public static <T> T postFullUrl(String url, HashMap<String, Object> query, T bod
public static String[] getQCSupportedVersions() {
File versionsJson = new File(Constants.USER_HOME + "/supportedVersions.json");
try {
DownloadUtils.downloadFile(SUPPORTED_VERSIONS, versionsJson);
DownloadUtils.downloadFile(SUPPORTED_VERSIONS, versionsJson, new DownloadManager(1));
} catch (IOException e) {
Logger.getInstance().appendToLog("Error while grabbing supported versions!\n" + e);
}
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/pojlib/UnityPlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import pojlib.util.Constants;
import pojlib.util.FileUtil;
import pojlib.util.Logger;
import pojlib.util.download.DownloadManager;
import pojlib.util.download.DownloadUtils;

public class UnityPlayerActivity extends ActivityGroup implements IUnityPlayerLifecycleEvents, GrabListener
{
Expand Down Expand Up @@ -94,16 +96,6 @@ protected String updateUnityCommandLineArguments(String cmdLine)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mUnityPlayer.requestFocus();

File jre = new File(this.getFilesDir() + "/runtimes/JRE-22");
if (!jre.exists()) {
FileUtil.unzipArchiveFromAsset(this, "JRE-22.zip", this.getFilesDir() + "/runtimes/JRE-22");
try {
Files.copy(Paths.get(this.getApplicationInfo().nativeLibraryDir + "/libawt_xawt.so"), Paths.get(this.getFilesDir() + "/runtimes/JRE-22/lib/libawt_xawt.so"));
} catch (IOException e) {
e.printStackTrace();
}
}

updateWindowSize(this);
GLOBAL_CLIPBOARD = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

Expand Down
62 changes: 43 additions & 19 deletions src/main/java/pojlib/install/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import org.apache.commons.io.FileUtils;

import pojlib.APIHandler;
import pojlib.util.download.DownloadManager;
import pojlib.util.download.DownloadUtils;
import pojlib.util.json.MinecraftInstances;
import pojlib.util.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand All @@ -34,7 +37,7 @@ public static String installClient(VersionInfo minecraftVersionInfo, String game
for (int i = 0; i < 5; i++) {
if (i == 4) throw new RuntimeException("Client download failed after 5 retries");

if (!clientFile.exists()) DownloadUtils.downloadFile(minecraftVersionInfo.downloads.client.url, clientFile);
if (!clientFile.exists()) DownloadUtils.downloadFile(minecraftVersionInfo.downloads.client.url, clientFile, new DownloadManager(1));
if (DownloadUtils.compareSHA1(clientFile, minecraftVersionInfo.downloads.client.sha1)) return clientFile.getAbsolutePath();
}
return null;
Expand All @@ -44,7 +47,6 @@ public static String installClient(VersionInfo minecraftVersionInfo, String game
// Returns the classpath of the downloaded libraries
public static String installLibraries(VersionInfo versionInfo, String gameDir) throws IOException {
Logger.getInstance().appendToLog("Downloading Libraries for: " + versionInfo.id);

StringJoiner classpath = new StringJoiner(File.pathSeparator);
for (VersionInfo.Library library : versionInfo.libraries) {
if(library.name.contains("lwjgl")) {
Expand All @@ -63,15 +65,15 @@ public static String installLibraries(VersionInfo versionInfo, String gameDir) t
sha1 = APIHandler.getRaw(library.url + path + ".sha1");
if (!libraryFile.exists()) {
Logger.getInstance().appendToLog("Downloading: " + library.name);
DownloadUtils.downloadFile(library.url + path, libraryFile);
DownloadUtils.downloadFile(library.url + path, libraryFile, new DownloadManager(1));
}
} else {
VersionInfo.Library.Artifact artifact = library.downloads.artifact;
libraryFile = new File(gameDir + "/libraries/", artifact.path);
sha1 = artifact.sha1;
if (!libraryFile.exists()) {
Logger.getInstance().appendToLog("Downloading: " + library.name);
DownloadUtils.downloadFile(artifact.url, libraryFile);
DownloadUtils.downloadFile(artifact.url, libraryFile, new DownloadManager(1));
}
}

Expand All @@ -96,10 +98,13 @@ public static String installAssets(VersionInfo minecraftVersionInfo, String game
Logger.getInstance().appendToLog("Downloading assets");
JsonObject assets = APIHandler.getFullUrl(minecraftVersionInfo.assetIndex.url, JsonObject.class);

int totalAssets = assets.getAsJsonObject("objects").size();
DownloadManager downloadManager = new DownloadManager(totalAssets);

ThreadPoolExecutor tp = new ThreadPoolExecutor(8, 8, 100, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());

for (Map.Entry<String, JsonElement> entry : assets.getAsJsonObject("objects").entrySet()) {
AsyncDownload thread = new AsyncDownload(entry, minecraftVersionInfo, gameDir);
AsyncDownload thread = new AsyncDownload(entry, gameDir, downloadManager);
tp.execute(thread);
}

Expand All @@ -108,7 +113,21 @@ public static String installAssets(VersionInfo minecraftVersionInfo, String game
while (!tp.awaitTermination(100, TimeUnit.MILLISECONDS));
} catch (InterruptedException e) {}

DownloadUtils.downloadFile(minecraftVersionInfo.assetIndex.url, new File(gameDir + "/assets/indexes/" + minecraftVersionInfo.assets + ".json"));
File jre = new File(activity.getFilesDir() + "/runtimes/JRE-22");
String jreURL = "https://github.com/QuestCraftPlusPlus/android-openjdk-build-multiarch/releases/download/jre22-6.0.0/JRE-22.zip";
try {
if (!jre.exists()) {
File jreZip = new File(activity.getFilesDir() + "/runtimes/JRE-22.zip");
DownloadUtils.downloadFile(jreURL, jreZip, new DownloadManager(1));
FileUtil.unzipArchive(jreZip.getPath(), activity.getFilesDir() + "/runtimes/JRE-22");
Files.copy(Paths.get(activity.getApplicationInfo().nativeLibraryDir + "/libawt_xawt.so"), Paths.get(activity.getFilesDir() + "/runtimes/JRE-22/lib/libawt_xawt.so"));
jreZip.delete();
}
} catch (IOException e) {
e.printStackTrace();
}

DownloadUtils.downloadFile(minecraftVersionInfo.assetIndex.url, new File(gameDir + "/assets/indexes/" + minecraftVersionInfo.assets + ".json"), downloadManager);

FileUtils.writeByteArrayToFile(new File(instance.gameDir + "/config/sodium-options.json"), FileUtil.loadFromAssetToByte(activity, "sodium-options.json"));
FileUtils.writeByteArrayToFile(new File(instance.gameDir + "/config/vivecraft-config.properties"), FileUtil.loadFromAssetToByte(activity, "vivecraft-config.properties"));
Expand All @@ -125,42 +144,47 @@ public static String installAssets(VersionInfo minecraftVersionInfo, String game
}

public static class AsyncDownload implements Runnable {
Map.Entry<String, JsonElement> entry;
VersionInfo versionInfo;
String gameDir;
private final Map.Entry<String, JsonElement> entry;
private final String gameDir;
private final DownloadManager downloadManager;
private final String fileName;

public AsyncDownload(Map.Entry<String, JsonElement> entry, String gameDir, DownloadManager downloadManager) {
this.entry = entry;
this.gameDir = gameDir;
this.downloadManager = downloadManager;
this.fileName = entry.getKey();
}

@Override
public void run() {
VersionInfo.Asset asset = new Gson().fromJson(entry.getValue(), VersionInfo.Asset.class);
String path = asset.hash.substring(0, 2) + "/" + asset.hash;
File assetFile = new File(gameDir + "/assets/objects/", path);

for (int i = 0; i < 5; i++) {
if (i == 4) throw new RuntimeException(String.format("Asset download of %s failed after 5 retries", entry.getKey()));
if (i == 4) throw new RuntimeException(String.format("Asset download of %s failed after 5 retries", fileName));

if (!assetFile.exists()) {
Logger.getInstance().appendToLog("Downloading: " + entry.getKey());
Logger.getInstance().appendToLog("Downloading: " + fileName);
try {
DownloadUtils.downloadFile(Constants.MOJANG_RESOURCES_URL + "/" + path, assetFile);
DownloadUtils.downloadFile(Constants.MOJANG_RESOURCES_URL + "/" + path, assetFile, downloadManager);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

if (DownloadUtils.compareSHA1(assetFile, asset.hash)) {
downloadManager.fileDownloadComplete(fileName);
break;
} else {
assetFile.delete();
}
}
}

public AsyncDownload( Map.Entry<String, JsonElement> entry, VersionInfo versionInfo, String gameDir) {
this.entry = entry;
this.versionInfo = versionInfo;
this.gameDir = gameDir;
}
}


//Used for mod libraries, vanilla is handled a different (tbh better) way
private static String parseLibraryNameToPath(String libraryName) {
String[] parts = libraryName.split(":");
Expand Down
35 changes: 0 additions & 35 deletions src/main/java/pojlib/util/StreamDL.java

This file was deleted.

38 changes: 38 additions & 0 deletions src/main/java/pojlib/util/download/DownloadManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pojlib.util.download;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import pojlib.API;

public class DownloadManager {
private final Map<String, Double> downloadProgress = new ConcurrentHashMap<>();
private final int totalFiles;
private final AtomicInteger completedFiles = new AtomicInteger(0);

public DownloadManager(int totalFiles) {
this.totalFiles = totalFiles;
}

public void updateProgress(String fileName, double progress) {
downloadProgress.put(fileName, progress);
API.currentDownload = fileName;
getOverallProgress();
}

public void fileDownloadComplete(String fileName) {
completedFiles.incrementAndGet();
downloadProgress.remove(fileName);
getOverallProgress();
}

private void getOverallProgress() {
int completed = completedFiles.get();
double overallProgress = ((double) completed / totalFiles) * 100;
if (completed == totalFiles) {API.currentDownload = "Finished! Ready to start.";}
API.downloadStatus = overallProgress;
}
}


Loading

0 comments on commit 2edded5

Please sign in to comment.