218 lines
8.8 KiB
Java
218 lines
8.8 KiB
Java
package net.kdt.pojavlaunch;
|
|
|
|
import java.io.*;
|
|
import java.util.Arrays;
|
|
|
|
import org.lwjgl.glfw.CallbackBridge;
|
|
|
|
import net.kdt.pojavlaunch.prefs.*;
|
|
import net.kdt.pojavlaunch.uikit.*;
|
|
import net.kdt.pojavlaunch.utils.*;
|
|
import net.kdt.pojavlaunch.value.*;
|
|
|
|
public class PLaunchApp {
|
|
private static float currProgress;
|
|
|
|
public static JMinecraftVersionList mVersionList;
|
|
public static MinecraftAccount mAccount;
|
|
public static JMinecraftVersionList.Version mVersion;
|
|
|
|
public static void main(String[] args) throws Throwable {
|
|
// User might remove the minecraft folder, this can cause crashes, safety re-create it
|
|
try {
|
|
File mcDir = new File("/var/mobile/Documents/minecraft");
|
|
mcDir.mkdirs();
|
|
if (!new File(mcDir, "config_ver.txt").exists()) {
|
|
Tools.write(mcDir.getAbsolutePath() + "/config_ver.txt", "1.16.5");
|
|
}
|
|
} catch (Throwable th) {
|
|
th.printStackTrace();
|
|
}
|
|
|
|
if (args[0].startsWith("/Applications/")) {
|
|
System.out.println("We are on java now! Starting UI...");
|
|
UIKit.launchUI(args);
|
|
} else {
|
|
// Test on cmdline
|
|
launchMinecraft();
|
|
}
|
|
|
|
LauncherPreferences.loadPreferences();
|
|
}
|
|
|
|
public static void installMinecraft() {
|
|
new Thread(() -> {
|
|
|
|
currProgress = 0;
|
|
float maxProgress = 0;
|
|
|
|
UIKit.updateProgressSafe(0, "Finding a version");
|
|
String mcver = "1.16.5";
|
|
|
|
try {
|
|
mcver = Tools.read(Tools.DIR_GAME_NEW + "/config_ver.txt");
|
|
} catch (IOException e) {
|
|
UIKit.updateProgressSafe(0, "config_ver.txt not found, defaulting to Minecraft 1.16.5");
|
|
}
|
|
|
|
UIKit.updateProgressSafe(0, "Selected Minecraft version: " + mcver);
|
|
|
|
try {
|
|
Thread.sleep(1000);
|
|
} catch (InterruptedException e) {}
|
|
|
|
// dummy account
|
|
MinecraftAccount acc = new MinecraftAccount();
|
|
acc.selectedVersion = mcver;
|
|
|
|
new File(Tools.DIR_HOME_VERSION + "/" + mcver).mkdirs();
|
|
|
|
JMinecraftVersionList.Version verInfo = null;
|
|
|
|
try {
|
|
final String downVName = "/" + mcver + "/" + mcver;
|
|
String minecraftMainJar = Tools.DIR_HOME_VERSION + downVName + ".jar";
|
|
String verJsonDir = Tools.DIR_HOME_VERSION + downVName + ".json";
|
|
|
|
JAssets assets = null;
|
|
|
|
UIKit.updateProgressSafe(0, "Downloading version list");
|
|
mVersionList = Tools.GLOBAL_GSON.fromJson(DownloadUtils.downloadString("https://launchermeta.mojang.com/mc/game/version_manifest.json"), JMinecraftVersionList.class);
|
|
|
|
verInfo = findVersion(mcver);
|
|
if (verInfo.url != null && !new File(verJsonDir).exists()) {
|
|
UIKit.updateProgressSafe(0, "Downloading " + mcver + ".json");
|
|
Tools.downloadFile(verInfo.url, verJsonDir);
|
|
}
|
|
|
|
verInfo = Tools.getVersionInfo(mcver);
|
|
try {
|
|
UIKit.updateProgressSafe(0, "Downloading " + mcver + ".json assets info");
|
|
assets = downloadIndex(verInfo.assets, new File(Tools.ASSETS_PATH, "indexes/" + verInfo.assets + ".json"));
|
|
} catch (IOException e) {
|
|
UIKit.updateProgressSafe(0, "Error: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
|
|
maxProgress = verInfo.libraries.length + 1 + (assets == null ? 0 : assets.objects.size());
|
|
|
|
File outLib;
|
|
String libPathURL;
|
|
|
|
for (final DependentLibrary libItem : verInfo.libraries) {
|
|
if (
|
|
libItem.name.startsWith("net.java.jinput") ||
|
|
libItem.name.startsWith("org.lwjgl")
|
|
) { // Black list
|
|
currProgress++;
|
|
UIKit.updateProgressSafe(currProgress / maxProgress, "Ignored " + libItem.name);
|
|
// Thread.sleep(100);
|
|
} else {
|
|
String[] libInfo = libItem.name.split(":");
|
|
String libArtifact = Tools.artifactToPath(libInfo[0], libInfo[1], libInfo[2]);
|
|
outLib = new File(Tools.DIR_HOME_LIBRARY + "/" + libArtifact);
|
|
outLib.getParentFile().mkdirs();
|
|
|
|
if (!outLib.exists()) {
|
|
currProgress++;
|
|
UIKit.updateProgressSafe(currProgress / maxProgress, "Downloading " + libItem.name);
|
|
|
|
boolean skipIfFailed = false;
|
|
|
|
if (libItem.downloads == null || libItem.downloads.artifact == null) {
|
|
MinecraftLibraryArtifact artifact = new MinecraftLibraryArtifact();
|
|
artifact.url = (libItem.url == null ? "https://libraries.minecraft.net/" : libItem.url) + libArtifact;
|
|
libItem.downloads = new DependentLibrary.LibraryDownloads(artifact);
|
|
|
|
skipIfFailed = true;
|
|
}
|
|
try {
|
|
libPathURL = libItem.downloads.artifact.url;
|
|
Tools.downloadFile(libPathURL, outLib.getAbsolutePath());
|
|
} catch (Throwable th) {
|
|
th.printStackTrace();
|
|
UIKit.updateProgressSafe(currProgress / maxProgress, "Download failed");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
currProgress++;
|
|
UIKit.updateProgressSafe(currProgress / maxProgress, "Downloading " + mcver + ".jar");
|
|
File minecraftMainFile = new File(minecraftMainJar);
|
|
if (!minecraftMainFile.exists() || minecraftMainFile.length() == 0l) {
|
|
Tools.downloadFile(verInfo.downloads.values().toArray(new MinecraftClientInfo[0])[0].url, minecraftMainJar);
|
|
}
|
|
|
|
if (assets != null) {
|
|
downloadAssets(assets, verInfo.assets, assets.map_to_resources ? new File(Tools.OBSOLETE_RESOURCES_PATH) : new File(Tools.ASSETS_PATH));
|
|
}
|
|
|
|
// TODO download assets
|
|
|
|
} catch (IOException e) {
|
|
UIKit.updateProgressSafe(currProgress / maxProgress, "Download error, skipping");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
mAccount = acc;
|
|
mVersion = verInfo;
|
|
|
|
UIKit.launchMinecraftSurface();
|
|
|
|
}).start();
|
|
}
|
|
|
|
public void downloadAssets(JAssets assets, String assetsVersion, File outputDir) throws IOException, Throwable {
|
|
File hasDownloadedFile = new File(outputDir, "downloaded/" + assetsVersion + ".downloaded");
|
|
if (!hasDownloadedFile.exists()) {
|
|
System.out.println("Assets begin time: " + System.currentTimeMillis());
|
|
Map<String, JAssetInfo> assetsObjects = assets.objects;
|
|
mActivity.mLaunchProgress.setMax(assetsObjects.size());
|
|
zeroProgress();
|
|
File objectsDir = new File(outputDir, "objects");
|
|
int downloadedSs = 0;
|
|
for (JAssetInfo asset : assetsObjects.values()) {
|
|
if (!mActivity.mIsAssetsProcessing) {
|
|
return;
|
|
}
|
|
|
|
currProgress++;
|
|
UIKit.updateProgressSafe(currProgress / maxProgress, "Downloading " + assetsObjects.keySet().toArray(new String[0])[downloadedSs]));
|
|
if(!assets.map_to_resources) downloadAsset(asset, objectsDir);
|
|
else downloadAssetMapped(asset,(assetsObjects.keySet().toArray(new String[0])[downloadedSs]),outputDir);
|
|
downloadedSs++;
|
|
}
|
|
hasDownloadedFile.getParentFile().mkdirs();
|
|
hasDownloadedFile.createNewFile();
|
|
System.out.println("Assets end time: " + System.currentTimeMillis());
|
|
}
|
|
}
|
|
|
|
// Called from SurfaceViewController
|
|
public static void launchMinecraft() {
|
|
System.out.println("Saving GLES context");
|
|
JREUtils.saveGLContext();
|
|
|
|
System.out.println("Launching Minecraft " + mVersion.id);
|
|
try {
|
|
Tools.launchMinecraft(mAccount, mVersion);
|
|
} catch (Throwable th) {
|
|
throw new RuntimeException(th);
|
|
}
|
|
}
|
|
|
|
private static JMinecraftVersionList.Version findVersion(String version) {
|
|
if (mVersionList != null) {
|
|
for (JMinecraftVersionList.Version valueVer: mVersionList.versions) {
|
|
if (valueVer.id.equals(version)) {
|
|
return valueVer;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Custom version, inherits from base.
|
|
return Tools.getVersionInfo(version);
|
|
}
|
|
}
|