This commit is contained in:
James Seibel
2022-10-31 22:16:16 -05:00
6 changed files with 121 additions and 25 deletions
+6 -3
View File
@@ -17,8 +17,11 @@ It should be automatically included when pulling the full mod.
XZ for Java (data compression)\
https://tukaani.org/xz/java.html
Toml for Java (config handling)\
Json & Toml for Java (config handling)\
https://github.com/TheElectronWill/night-config
Json for Java (config handling)\
https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
SVG Salamander for SVG's\
https://github.com/blackears/svgSalamander
FlatLaf for theming (Tempory to test stuff)\
https://www.formdev.com/flatlaf/
@@ -873,7 +873,7 @@ public class Config
public static class AutoUpdater
{
public static ConfigEntry<Boolean> enableAutoUpdater = new ConfigEntry.Builder<Boolean>()
.set(false) // Keep this as false for development
.set(false) // Keep this as false for development but should be set to true when released
.comment("Automatically checks for updates on game launch")
.build();
@@ -16,7 +16,7 @@ import java.util.*;
public class ModrinthGetter {
public static final String ModrinthAPI = "https://api.modrinth.com/v2/project/";
public static final String projectID = "distanthorizons";
public static Config projectRelease = Config.inMemory();
public static ArrayList<Config> projectRelease;
public static List<String> releaseID = new ArrayList<>(); // This list contains the release ID's
public static List<String> mcVersions = new ArrayList<>(); // List of available Minecraft versions in the mod
@@ -36,18 +36,24 @@ public class ModrinthGetter {
public static boolean init() {
try {
JsonFormat.fancyInstance().createParser().parse(WebDownloader.downloadAsString(new URL(ModrinthAPI+projectID+"/version")), projectRelease, ParsingMode.REPLACE);
projectRelease = JsonFormat.fancyInstance().createParser().parse("{\"E\":" + WebDownloader.downloadAsString(new URL(ModrinthAPI+projectID+"/version")) + "}").get("E");
for (int i = 0; i < projectRelease.size(); i++) {
Config currentRelease = (Config) projectRelease.get(String.valueOf(i));
for (Config currentRelease: projectRelease) {
String workingID = currentRelease.get("id").toString();
releaseID.add(workingID);
releaseNames.put(workingID, currentRelease.get("name").toString().replaceAll(" - 1\\..*", ""));
changeLogs.put(workingID, currentRelease.get("changelog").toString());
try {
downloadUrl.put(workingID, new URL(currentRelease.get("files.0.url").toString()));
downloadUrl.put(workingID,
new URL(
((Config)
((ArrayList) currentRelease.get("files"))
.get(0))
.get("url")
.toString()
));
} catch (Exception e) { e.printStackTrace(); }
// Get all the mc versions this mod is available for
@@ -71,21 +77,17 @@ public class ModrinthGetter {
}
public static String getLatestNameForVersion(String mcVer) {
return releaseNames.get(((List<String>) mcVerToReleaseID.get(mcVer)).get(0));
return releaseNames.get(mcVerToReleaseID.get(mcVer).get(0));
}
public static URL getLatestDownloadForVersion(String mcVer) {
return downloadUrl.get(((List<String>) mcVerToReleaseID.get(mcVer)).get(0));
return downloadUrl.get(mcVerToReleaseID.get(mcVer).get(0));
}
public static String getLatestShaForVersion(String mcVer) {
return projectRelease.get(mcVersions.indexOf(mcVer) + ".files.0.hashes.sha1").toString();
// return ((JSONObject)
// ((JSONObject)
// ((JSONArray)
// ((JSONObject) projectRelease.get(mcVersions.indexOf(mcVer)))
// .get("files"))
// .get(0))
// .get("hashes"))
// .get("sha1")
// .toString();
return ((Config)
((ArrayList) projectRelease.get(
mcVersions.indexOf(mcVer)
).get("files")).get(0))
.get("hashes.sha1")
.toString();
}
}
@@ -0,0 +1,91 @@
package com.seibel.lod.core.jar.updater;
import com.seibel.lod.core.ModInfo;
import com.seibel.lod.core.config.Config;
import com.seibel.lod.core.dependencyInjection.SingletonInjector;
import com.seibel.lod.core.jar.JarUtils;
import com.seibel.lod.core.jar.installer.ModrinthGetter;
import com.seibel.lod.core.jar.installer.WebDownloader;
import com.seibel.lod.core.logging.DhLoggerBuilder;
import com.seibel.lod.core.wrapperInterfaces.IVersionConstants;
import org.apache.logging.log4j.Logger;
import java.nio.file.Files;
import java.security.MessageDigest;
/**
* Used to update the mod automatically
*
* @author coolGi
*/
public class SelfUpdater {
private static final Logger LOGGER = DhLoggerBuilder.getLogger(SelfUpdater.class.getSimpleName());
/** As we cannot delete(or replace) the jar while the mod is running, we just have this to delete it once the game closes */
public static boolean deleteOldOnClose = false;
/**
* Should be called on the game starting.
* (After the config has been initialised)
* @return Whether it should open the update ui
*/
public static boolean onStart() {
// Some init stuff
// We use sha1 to check the version as our versioning system is different to the one on modrinth
if (!ModrinthGetter.init()) return false;
String jarSha = "";
try { jarSha = JarUtils.getFileChecksum(MessageDigest.getInstance("SHA"), JarUtils.jarFile);
} catch (Exception e) {
e.printStackTrace();
return false;
}
String mcVersion = SingletonInjector.INSTANCE.get(IVersionConstants.class).getMinecraftVersion();
// Check the sha's of both our stuff
if (jarSha.equals(ModrinthGetter.getLatestShaForVersion(mcVersion)))
return false;
LOGGER.info("New version ("+ModrinthGetter.getLatestNameForVersion(mcVersion)+") of "+ ModInfo.READABLE_NAME+" is available");
if (!Config.Client.AutoUpdater.promptForUpdate.get()) {
// Auto-update mod
updateMod(mcVersion);
return false;
} // else
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.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() {
return updateMod(
SingletonInjector.INSTANCE.get(IVersionConstants.class).getMinecraftVersion()
);
}
public static boolean updateMod(String minecraftVersion) {
try {
LOGGER.info("Attempting to auto update " + ModInfo.READABLE_NAME);
WebDownloader.downloadAsFile(ModrinthGetter.getLatestDownloadForVersion(minecraftVersion), JarUtils.jarFile.getParentFile().toPath().resolve(ModInfo.NAME + "-" + ModrinthGetter.getLatestNameForVersion(minecraftVersion) + ".jar").toFile());
deleteOldOnClose = true;
LOGGER.info(ModInfo.READABLE_NAME + " successfully updated. It will apply on game's relaunch");
return true;
} catch (Exception e) {
LOGGER.info("Failed to update "+ModInfo.READABLE_NAME+" to version "+ModrinthGetter.getLatestNameForVersion(minecraftVersion));
e.printStackTrace();
return false;
}
}
}
@@ -7,9 +7,9 @@
"lod.updater.title":
"Distant Horizons auto updater",
"lod.updater.text1":
"There is a new update for Distant Horizons available",
"§lUpdate for Distant Horizons available!",
"lod.updater.text2":
"Would you like to automatically update from version %s to %s",
"§fWould you like to automatically update from version %s§f to %s§f?",
"lod.updater.later":
"Later",
"lod.updater.never":
@@ -18,9 +18,9 @@
"Update",
"lod.updater.update.@tooltip":
"Updates the mod this 1 time\n(Updates when game closed)",
"lod.updater.silentUpdate":
"lod.updater.silent":
"Always silent update",
"lod.updater.silentUpdate.@tooltip":
"lod.updater.silent.@tooltip":
"Every time an update is available, it would update\n(§6WARNING§r: It wont prompt the user on an update, tough it would keep the mod up to date)",
Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 KiB

After

Width:  |  Height:  |  Size: 277 KiB