This commit is contained in:
James Seibel
2023-10-07 18:50:59 -05:00
8 changed files with 119 additions and 32 deletions
@@ -0,0 +1,44 @@
package com.seibel.distanthorizons.coreapi.util.jar;
import java.io.File;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;
/**
* Deletes the first file in the arguments when a lock is lifted from it (for Windows) <br>
* DON'T MOVE: If this class is moved, then the updater will no longer work on Windows
*
* @author coolgi
*/
public class DeleteOnUnlock
{
/**
* @param args Takes whatever the first argument is, treats it as a file, and deletes it once a process lock is lifted from it
*/
public static void main(String[] args)
{
File file = new File(args[0]);
try
{
for (int i = 0; i < 600; i++) // As it rests for around 0.1 second each loop, this should last a minute
{
if (file.renameTo(file)) // If it is able to be renamed, then it is unlocked and can be deleted
{
Files.delete(file.toPath());
break;
}
TimeUnit.MILLISECONDS.sleep(100);
}
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException("Deletion failed");
}
// If it isn't deleted by the end, crash
if (Files.exists(file.toPath()))
throw new RuntimeException("File was not able to be deleted");
}
}
@@ -34,7 +34,13 @@ import java.util.Objects;
*/
public class JarUtils
{
public static final File jarFile = new File(JarUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath());
public static File jarFile = null;
static {
try {
jarFile = new File(JarUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI()); // Always safe
} catch (Exception e) { e.printStackTrace(); }
}
/**
@@ -95,7 +101,7 @@ public class JarUtils
/**
* Checks the checksum of a file given an algorithm
*
* @param digest What algorithem to use <br>
* @param digest What algorithm to use <br>
* Eg. <br>
* MessageDigest.getInstance("MD5") <br>
* MessageDigest.getInstance("SHA-256") <br>
@@ -22,6 +22,7 @@ package com.seibel.distanthorizons.core.jar.updater;
import com.seibel.distanthorizons.api.enums.config.EUpdateBranch;
import com.seibel.distanthorizons.core.jar.JarUtils;
import com.seibel.distanthorizons.core.jar.ModGitInfo;
import com.seibel.distanthorizons.core.jar.Platform;
import com.seibel.distanthorizons.core.jar.installer.GitlabGetter;
import com.seibel.distanthorizons.coreapi.ModInfo;
import com.seibel.distanthorizons.core.config.Config;
@@ -37,9 +38,11 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -128,9 +131,21 @@ public class SelfUpdater
return false;
com.electronwill.nightconfig.core.Config pipeline = GitlabGetter.INSTANCE.projectPipelines.get(0);
if (!pipeline.get("ref").equals(ModGitInfo.Git_Main_Branch))
{
//LOGGER.warn("Latest pipeline was found for branch ["+ pipeline.get("ref") +"], but we are on branch ["+ ModGitInfo.Git_Main_Branch +"].");
return false;
}
if (!pipeline.get("status").equals("success"))
{
LOGGER.warn("Pipeline for branch ["+ ModGitInfo.Git_Main_Branch +"], commit ["+ pipeline.get("id") +"], has either failed to build, or still building.");
return false;
}
if (!GitlabGetter.INSTANCE.getDownloads(pipeline.get("id")).containsKey(mcVersion))
{
LOGGER.warn("Minecraft version ["+ mcVersion +"] is not findable on Gitlab, findable versions are ["+ GitlabGetter.INSTANCE.getDownloads(pipeline.get("id")).keySet().toArray().toString() +"]");
LOGGER.warn("Minecraft version ["+ mcVersion +"] is not findable on Gitlab, findable versions are ["+ GitlabGetter.INSTANCE.getDownloads(pipeline.get("id")).keySet().toArray().toString() +"].");
return false;
}
@@ -151,35 +166,8 @@ public class SelfUpdater
return true;
}
/**
* Should be called when the game is closed.
* This is ued to delete the previous file if it is required at the end.
*/
public static void onClose()
{
if (deleteOldOnClose)
{
try
{
Files.move(newFileLocation.toPath(), JarUtils.jarFile.getParentFile().toPath().resolve(newFileLocation.getName()));
Files.delete(newFileLocation.getParentFile().toPath());
}
catch (Exception e)
{
LOGGER.warn("Failed to move updated fire from [" + newFileLocation.getAbsolutePath() + "] to [" + JarUtils.jarFile.getParentFile().getAbsolutePath() + "], please move it manually");
e.printStackTrace();
}
try
{
Files.delete(JarUtils.jarFile.toPath());
}
catch (Exception e)
{
LOGGER.warn("Failed to delete previous " + ModInfo.READABLE_NAME + " file, please delete it manually at [" + JarUtils.jarFile + "]");
e.printStackTrace();
}
}
}
public static boolean updateMod()
{
@@ -295,4 +283,53 @@ public class SelfUpdater
return false;
}
}
/**
* Should be called when the game is closed.
* This is ued to delete the previous file if it is required at the end.
*/
public static void onClose()
{
if (deleteOldOnClose)
{
try
{
Files.move(newFileLocation.toPath(), JarUtils.jarFile.getParentFile().toPath().resolve(newFileLocation.getName()));
Files.delete(newFileLocation.getParentFile().toPath());
}
catch (Exception e)
{
LOGGER.warn("Failed to move updated fire from [" + newFileLocation.getAbsolutePath() + "] to [" + JarUtils.jarFile.getParentFile().getAbsolutePath() + "], please move it manually");
e.printStackTrace();
}
try
{
if (Platform.get() != Platform.WINDOWS)
{
Files.delete(JarUtils.jarFile.toPath());
}
else
{
/* // If we want the user to delete it manually
System.setProperty("java.awt.headless", "false"); // Required to make it work
JOptionPane.showMessageDialog(null, "As you are on Windows, DH can not update fully by itself\nPlease delete ["+ JarUtils.jarFile.getAbsolutePath() +"] manually\nClick OK once ready.", ModInfo.READABLE_NAME, JOptionPane.INFORMATION_MESSAGE);
Runtime.getRuntime().exec("explorer.exe /select," + JarUtils.jarFile.getAbsolutePath());
*/
// Execute the new jar, to delete the old jar once it detects the lock has been lifted
Runtime.getRuntime().exec("java -cp "+ newFileLocation.getAbsolutePath() +" com.seibel.distanthorizons.coreapi.util.jar.DeleteOnUnlock "+ JarUtils.jarFile.getAbsolutePath());
}
}
catch (Exception e)
{
LOGGER.warn("Failed to delete previous " + ModInfo.READABLE_NAME + " file, please delete it manually at [" + JarUtils.jarFile + "]");
e.printStackTrace();
}
}
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 694 B

After

Width:  |  Height:  |  Size: 573 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 494 B

After

Width:  |  Height:  |  Size: 299 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 299 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 573 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB