Added an auto updater when game starts

This commit is contained in:
coolGi
2022-08-06 20:32:09 +09:30
parent be74011e5e
commit 6aab2d287c
6 changed files with 97 additions and 25 deletions
@@ -6,10 +6,9 @@ import com.seibel.lod.core.jar.DarkModeDetector;
import com.seibel.lod.core.jar.JarUtils;
import com.seibel.lod.core.jar.gui.BaseJFrame;
import com.seibel.lod.core.jar.gui.cusomJObject.JBox;
import com.seibel.lod.core.jar.installer.GitlabGetter;
import com.seibel.lod.core.jar.JarDependencySetup;
import com.seibel.lod.core.jar.installer.ModrinthGetter;
import com.seibel.lod.core.jar.installer.WebDownloader;
import com.seibel.lod.core.jar.JarDependencySetup;
import javax.swing.*;
import java.awt.*;
@@ -23,7 +22,7 @@ import java.util.concurrent.atomic.AtomicReference;
*
* @author coolGi
*/
// Once built it would be in core/build/libs/DistantHorizons-1.7.0a-dev-all.jar
// Once built it would be in core/build/libs/DistantHorizons-<Version>-dev-all.jar
public class JarMain {
public static final boolean isDarkTheme = DarkModeDetector.isDarkMode();
public static boolean isOffline = WebDownloader.netIsAvailable();
@@ -48,9 +47,13 @@ public class JarMain {
// JOptionPane.showMessageDialog(null, "The GUI for the standalone jar isn't made yet\nIf you want to use the mod then put it in your mods folder", "Distant Horizons", JOptionPane.WARNING_MESSAGE);
if (getOperatingSystem().equals(OperatingSystem.MACOS)) {
System.out.println("If you want the installer then please use Linux or for the time being.\nMacOS support/testing will come later on");
}
// if (getOperatingSystem().equals(OperatingSystem.MACOS)) {
// System.out.println("If you want the installer then please use Linux or Windows for the time being.\nMacOS support/testing will come later on");
// }
// Code will be changed later on to allow resizing and work better
BaseJFrame frame = new BaseJFrame(false, true);
frame.addExtraButtons(frame.getWidth(), 0, true, false);
@@ -68,6 +68,8 @@ public class Config
public static ConfigCategory advanced = new ConfigCategory.Builder().set(Advanced.class).build();
public static ConfigCategory autoUpdater = new ConfigCategory.Builder().set(AutoUpdater.class).build();
public static ConfigEntry<Boolean> optionsButton = new ConfigEntry.Builder<Boolean>()
.set(true)
.comment("Show the lod button in the options screen next to fov")
@@ -874,5 +876,21 @@ public class Config
.build();
}
}
public static class AutoUpdater
{
public static ConfigEntry<Boolean> enableAutoUpdater = new ConfigEntry.Builder<Boolean>()
.set(true)
.comment("Automatically checks for updates on game launch")
.build();
public static ConfigEntry<Boolean> promptForUpdate = new ConfigEntry.Builder<Boolean>()
.set(true)
.comment("When there is a new update available it would prompt the user whether they would like to update \n"
+ "If set to false then it would update the mod in the background without informing/annoying the user \n"
+ "So it is recommended to keep it off unless you are always sure you want the mod to be on the latest version")
.build();
}
}
}
@@ -2,6 +2,7 @@ package com.seibel.lod.core.jar;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class JarUtils {
public static final File jarFile = new File(JarUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath());
@@ -42,4 +43,46 @@ public class JarUtils {
return result.toString();
}
/**
* Checks the checksum of a file given an algorithm
*
* @param digest What algorithem to use <br>
* Eg. <br>
* MessageDigest.getInstance("MD5") <br>
* MessageDigest.getInstance("SHA-256") <br>
* @param file Location of the file
* @return Checksum
*/
// Stolen from https://howtodoinjava.com/java/java-security/sha-md5-file-checksum-hash/
public static String getFileChecksum(MessageDigest digest, File file) throws IOException {
//Get file input stream for reading the file content
FileInputStream fis = new FileInputStream(file);
//Create byte array to read data in chunks
byte[] byteArray = new byte[1024];
int bytesCount = 0;
//Read file data and update in message digest
while ((bytesCount = fis.read(byteArray)) != -1) {
digest.update(byteArray, 0, bytesCount);
};
//close the stream; We don't need it now.
fis.close();
//Get the hash's bytes
byte[] bytes = digest.digest();
//This bytes[] has bytes in decimal format;
//Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
//return complete hash
return sb.toString();
}
}
@@ -69,25 +69,21 @@ public class ModrinthGetter {
}
}
public static List<String> getReleaseIDInMcVersion(String mcVersion) {
List<String> releaseID = new ArrayList<>();
for (int i = 0; i < projectRelease.size(); i++) {
JSONObject currentRelease = (JSONObject) projectRelease.get(i);
if (((List<String>) currentRelease.get("game_versions")).contains(mcVersion))
releaseID.add(currentRelease.get("id").toString());
}
return releaseID;
public static String getLatestNameForVersion(String mcVer) {
return releaseNames.get(((List<String>) mcVerToReleaseID.get(mcVer)).get(0));
}
public static URL getDownloadFromReleaseID(String ID) {
for (int i = 0; i < projectRelease.size(); i++) {
JSONObject currentRelease = (JSONObject) projectRelease.get(i);
if (currentRelease.get("id").toString().equals(ID))
try {
return new URL(((JSONObject) ((JSONArray) currentRelease.get("files")).get(0)).get("url").toString());
} catch (Exception e) { e.printStackTrace(); }
}
return null;
public static URL getLatestDownloadForVersion(String mcVer) {
return downloadUrl.get(((List<String>) mcVerToReleaseID.get(mcVer)).get(0));
}
public static String getLatestShaForVersion(String mcVer) {
return ((JSONObject)
((JSONObject)
((JSONArray)
((JSONObject) projectRelease.get(mcVersions.indexOf(mcVer)))
.get("files"))
.get(0))
.get("hashes"))
.get("sha1")
.toString();
}
}
@@ -41,4 +41,5 @@ public interface IVersionConstants extends IBindable
boolean isVanillaRenderedChunkSquare();
String getMinecraftVersion();
}
@@ -283,6 +283,17 @@
"lod.config.client.advanced.lodOnlyMode.@tooltip":
"Due to some demand for playing without vanilla terrains, \nwe decided to add this mode for fun. \n\n§6NOTE§r: Do not report any issues when this mode is on! \n Again, this setting is only for fun, and mod \n compatibility is not guaranteed. \n",
"lod.config.client.autoUpdater":
"Auto Updater",
"lod.config.client.autoUpdater.enableAutoUpdater":
"Enable auto updater",
"lod.config.client.autoUpdater.enableAutoUpdater.@tooltip":
"Automatically updates the mod when an update is available",
"lod.config.client.autoUpdater.promptForUpdate":
"Prompt for update",
"lod.config.client.autoUpdater.promptForUpdate.@tooltip":
"If disabled then when an update is available then it would update without prompting the user",
"lod.config.enum.EHorizontalResolution.BLOCK":
"Block",
"lod.config.enum.EHorizontalResolution.TWO_BLOCKS":