Fixed (hopefully) updater on Windows
This commit is contained in:
@@ -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 UpdateJarRun
|
||||
{
|
||||
/**
|
||||
* @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.canWrite())
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -167,45 +167,7 @@ public class SelfUpdater
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
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()
|
||||
{
|
||||
@@ -321,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.UpdateJarRun "+ 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user