From 0e34866345748c19e2b64d0027fe6bdb39512f0e Mon Sep 17 00:00:00 2001 From: artdeell Date: Sun, 17 Dec 2023 15:41:20 +0300 Subject: [PATCH] Fix[mcdl]: copy the last inherited client JAR into the version folder --- .../main/java/net/kdt/pojavlaunch/Tools.java | 20 +++--- .../tasks/NewMinecraftDownloader.java | 62 ++++++++++++++----- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java index 2fff72563..9a612d7cf 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -420,7 +420,7 @@ public final class Tools { return libInfos[0].replaceAll("\\.", "/") + "/" + libInfos[1] + "/" + libInfos[2] + "/" + libInfos[1] + "-" + libInfos[2] + ".jar"; } - public static String getPatchedFile(String version) { + public static String getClientClasspath(String version) { return DIR_HOME_VERSION + "/" + version + "/" + version + ".jar"; } @@ -441,26 +441,26 @@ public final class Tools { } private final static boolean isClientFirst = false; - public static String generateLaunchClassPath(JMinecraftVersionList.Version info,String actualname) { - StringBuilder libStr = new StringBuilder(); //versnDir + "/" + version + "/" + version + ".jar:"; + public static String generateLaunchClassPath(JMinecraftVersionList.Version info, String actualname) { + StringBuilder finalClasspath = new StringBuilder(); //versnDir + "/" + version + "/" + version + ".jar:"; String[] classpath = generateLibClasspath(info); if (isClientFirst) { - libStr.append(getPatchedFile(actualname)); + finalClasspath.append(getClientClasspath(actualname)); } - for (String perJar : classpath) { - if (!new File(perJar).exists()) { - Log.d(APP_NAME, "Ignored non-exists file: " + perJar); + for (String jarFile : classpath) { + if (!FileUtils.exists(jarFile)) { + Log.d(APP_NAME, "Ignored non-exists file: " + jarFile); continue; } - libStr.append((isClientFirst ? ":" : "")).append(perJar).append(!isClientFirst ? ":" : ""); + finalClasspath.append((isClientFirst ? ":" : "")).append(jarFile).append(!isClientFirst ? ":" : ""); } if (!isClientFirst) { - libStr.append(getPatchedFile(actualname)); + finalClasspath.append(getClientClasspath(actualname)); } - return libStr.toString(); + return finalClasspath.toString(); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/NewMinecraftDownloader.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/NewMinecraftDownloader.java index 02791d323..4c6fc4978 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/NewMinecraftDownloader.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/NewMinecraftDownloader.java @@ -38,11 +38,13 @@ import java.util.concurrent.atomic.AtomicReference; public class NewMinecraftDownloader { public static final String MINECRAFT_RES = "https://resources.download.minecraft.net/"; - private AtomicReference mThrownDownloaderException; + private AtomicReference mDownloaderThreadException; private ArrayList mScheduledDownloadTasks; private AtomicLong mDownloadFileCounter; private AtomicLong mDownloadSizeCounter; private long mDownloadFileCount; + private File mSourceJarFile; // The source client JAR picked during the inheritance process + private File mTargetJarFile; // The destination client JAR to which the source will be copied to. private static final ThreadLocal sThreadLocalDownloadBuffer = new ThreadLocal<>(); @@ -79,10 +81,11 @@ public class NewMinecraftDownloader { // work to keep the launcher alive. We will replace this line when we will start downloading stuff. ProgressLayout.setProgress(ProgressLayout.DOWNLOAD_MINECRAFT, 0, R.string.newdl_starting); + mTargetJarFile = createGameJarPath(versionName); mScheduledDownloadTasks = new ArrayList<>(); mDownloadFileCounter = new AtomicLong(0); mDownloadSizeCounter = new AtomicLong(0); - mThrownDownloaderException = new AtomicReference<>(null); + mDownloaderThreadException = new AtomicReference<>(null); if(!downloadAndProcessMetadata(activity, verInfo, versionName)) { throw new RuntimeException(activity.getString(R.string.exception_failed_to_unpack_jre17)); @@ -99,7 +102,7 @@ public class NewMinecraftDownloader { downloaderPool.shutdown(); try { - while (mThrownDownloaderException.get() == null && + while (mDownloaderThreadException.get() == null && !downloaderPool.awaitTermination(33, TimeUnit.MILLISECONDS)) { long dlFileCounter = mDownloadFileCounter.get(); int progress = (int)((dlFileCounter * 100L) / mDownloadFileCount); @@ -107,8 +110,12 @@ public class NewMinecraftDownloader { R.string.newdl_downloading_game_files, dlFileCounter, mDownloadFileCount, (double)mDownloadSizeCounter.get() / (1024d * 1024d)); } - Exception thrownException = mThrownDownloaderException.get(); - if(thrownException != null) throw thrownException; + Exception thrownException = mDownloaderThreadException.get(); + if(thrownException != null) { + throw thrownException; + } else { + ensureJarFileCopy(); + } }catch (InterruptedException e) { // Interrupted while waiting, which means that the download was cancelled. // Kill all downloading threads immediately, and ignore any exceptions thrown by them @@ -124,6 +131,20 @@ public class NewMinecraftDownloader { return new File(Tools.DIR_HOME_VERSION, versionId + File.separator + versionId + ".jar"); } + /** + * Ensure that there is a copy of the client JAR file in the version folder, if a copy is + * needed. + * @throws IOException if the copy fails + */ + private void ensureJarFileCopy() throws IOException { + if(mSourceJarFile == null) return; + if(mSourceJarFile.equals(mTargetJarFile)) return; + if(mTargetJarFile.exists()) return; + FileUtils.ensureParentDirectory(mTargetJarFile); + Log.i("NewMCDownloader", "Copying " + mSourceJarFile.getName() + " to "+mTargetJarFile.getAbsolutePath()); + org.apache.commons.io.FileUtils.copyFile(mSourceJarFile, mTargetJarFile, false); + } + private File downloadGameJson(JMinecraftVersionList.Version verInfo) throws IOException, MirrorTamperedException { File targetFile = createGameJsonPath(verInfo.id); if(verInfo.sha1 == null && targetFile.canRead() && targetFile.isFile()) @@ -179,7 +200,7 @@ public class NewMinecraftDownloader { if(versionJsonFile.canRead()) { verInfo = Tools.GLOBAL_GSON.fromJson(Tools.read(versionJsonFile), JMinecraftVersionList.Version.class); } else { - throw new IOException("Unable to read Version JSON for version "+versionName); + throw new IOException("Unable to read Version JSON for version " + versionName); } if(activity != null && !JRE17Util.installNewJreIfNeeded(activity, verInfo)){ @@ -191,16 +212,7 @@ public class NewMinecraftDownloader { MinecraftClientInfo minecraftClientInfo = getClientInfo(verInfo); - if(minecraftClientInfo != null) { - growDownloadList(1); - scheduleDownload(createGameJarPath(verInfo.id), - DownloadMirror.DOWNLOAD_CLASS_LIBRARIES, - minecraftClientInfo.url, - minecraftClientInfo.sha1, - minecraftClientInfo.size, - false - ); - } + if(minecraftClientInfo != null) scheduleGameJarDownload(minecraftClientInfo, versionName); if(verInfo.libraries != null) scheduleLibraryDownloads(verInfo.libraries); @@ -304,6 +316,22 @@ public class NewMinecraftDownloader { false); } + private void scheduleGameJarDownload(MinecraftClientInfo minecraftClientInfo, String versionName) { + File clientJar = createGameJarPath(versionName); + String clientSha1 = LauncherPreferences.PREF_CHECK_LIBRARY_SHA ? + minecraftClientInfo.sha1 : null; + growDownloadList(1); + scheduleDownload(clientJar, + DownloadMirror.DOWNLOAD_CLASS_LIBRARIES, + minecraftClientInfo.url, + clientSha1, + minecraftClientInfo.size, + false + ); + // Store the path of the JAR to copy it into our new version folder later. + mSourceJarFile = clientJar; + } + private static byte[] getLocalBuffer() { byte[] tlb = sThreadLocalDownloadBuffer.get(); if(tlb != null) return tlb; @@ -336,7 +364,7 @@ public class NewMinecraftDownloader { try { runCatching(); }catch (Exception e) { - mThrownDownloaderException.set(e); + mDownloaderThreadException.set(e); } }