diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/gui/UpdateModScreen.java b/common/src/main/java/com/seibel/lod/common/wrappers/gui/UpdateModScreen.java index 376cbd329..cb8b5c732 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/gui/UpdateModScreen.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/gui/UpdateModScreen.java @@ -3,6 +3,7 @@ package com.seibel.lod.common.wrappers.gui; import com.mojang.blaze3d.vertex.PoseStack; import com.seibel.lod.core.ModInfo; import com.seibel.lod.core.config.Config; +import com.seibel.lod.core.jar.updater.SelfUpdater; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.GuiComponent; @@ -17,7 +18,6 @@ import net.minecraft.network.chat.Component; import java.util.*; public class UpdateModScreen extends Screen { - public static boolean modUpdated = false; private ConfigListWidget list; private Screen parent; private String newVersion; @@ -49,7 +49,7 @@ public class UpdateModScreen extends Screen { this.addBtn( new Button(this.width / 2 - 100, this.height / 2 - 20, 150, 20, translate(ModInfo.ID + ".updater.update"), (btn) -> { this.onClose(); - modUpdated = true; + SelfUpdater.deleteOldOnClose = true; }) ); this.addBtn( diff --git a/coreSubProjects b/coreSubProjects index f85d25900..2e9d118ab 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit f85d25900b8c9f7e76b22bd63668bdeb2a7d4881 +Subproject commit 2e9d118ab95a17f4c073d72e81a9351c05dcd464 diff --git a/fabric/src/main/java/com/seibel/lod/mixins/client/MixinMinecraft.java b/fabric/src/main/java/com/seibel/lod/mixins/client/MixinMinecraft.java index d996ba178..8052c9e4e 100644 --- a/fabric/src/main/java/com/seibel/lod/mixins/client/MixinMinecraft.java +++ b/fabric/src/main/java/com/seibel/lod/mixins/client/MixinMinecraft.java @@ -1,97 +1,48 @@ package com.seibel.lod.mixins.client; import com.seibel.lod.common.wrappers.gui.UpdateModScreen; -import com.seibel.lod.common.wrappers.world.ServerLevelWrapper; -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.jar.updater.SelfUpdater; import com.seibel.lod.core.wrapperInterfaces.IVersionConstants; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.TitleScreen; -import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import java.nio.file.Files; -import java.security.MessageDigest; -import java.util.Objects; - /** - * This is used to check for updates to the mod. - * * @author coolGi */ @Mixin(Minecraft.class) public class MixinMinecraft { - private static final Logger LOGGER = DhLoggerBuilder.getLogger(ServerLevelWrapper.class.getSimpleName()); - - private static boolean deleteOldOnClose = false; - - @Redirect( method = "(Lnet/minecraft/client/main/GameConfig;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;setScreen(Lnet/minecraft/client/gui/screens/Screen;)V") ) public void onOpenScreen(Minecraft instance, Screen guiScreen) { if (!Config.Client.AutoUpdater.enableAutoUpdater.get()) { // Don't do anything if the user doesn't want it - instance.setScreen(guiScreen); + instance.setScreen(guiScreen); // Sets the screen back to the vanilla screen as if nothing ever happened return; } - // Some init stuff - // We use sha1 to check the version as our versioning system is diffrent to the one on modrinth - if (!ModrinthGetter.init()) return; - String jarSha = ""; - try { jarSha = JarUtils.getFileChecksum(MessageDigest.getInstance("SHA"), JarUtils.jarFile); - } catch (Exception e) { - e.printStackTrace(); - return; - } - String mcVersion = SingletonInjector.INSTANCE.get(IVersionConstants.class).getMinecraftVersion(); - - // Check the sha's of both our stuff - if (jarSha.equals(ModrinthGetter.getLatestShaForVersion(mcVersion))) - return; - - - LOGGER.info("New version ("+ModrinthGetter.getLatestNameForVersion(mcVersion)+") of "+ModInfo.READABLE_NAME+" is available"); - if (Config.Client.AutoUpdater.promptForUpdate.get()) { - Objects.requireNonNull(Minecraft.getInstance()).setScreen(new UpdateModScreen( + if (SelfUpdater.onStart()) { + instance.setScreen(new UpdateModScreen( new TitleScreen(false), // We don't want to use the vanilla title screen as it would fade the buttons - ModrinthGetter.getLatestNameForVersion(mcVersion) - )); // Just uncommenting this to not annoy other devs for now + ModrinthGetter.getLatestNameForVersion(SingletonInjector.INSTANCE.get(IVersionConstants.class).getMinecraftVersion()) + )); } else { - // Auto-update mod - try { - LOGGER.info("Attempting to auto update "+ModInfo.READABLE_NAME); - WebDownloader.downloadAsFile(ModrinthGetter.getLatestDownloadForVersion(mcVersion), JarUtils.jarFile.getParentFile().toPath().resolve(ModInfo.NAME+"-"+ModrinthGetter.getLatestNameForVersion(mcVersion)+".jar").toFile()); - deleteOldOnClose = true; - LOGGER.info(ModInfo.READABLE_NAME+" successfully updated. It will apply on game's relaunch"); - } catch (Exception e) { - LOGGER.info("Failed to update "+ModInfo.READABLE_NAME+" to version "+ModrinthGetter.getLatestNameForVersion(mcVersion)); - e.printStackTrace(); - } + instance.setScreen(guiScreen); // Sets the screen back to the vanilla screen as if nothing ever happened } } @Inject(at = @At("HEAD"), method = "close()V") public void close(CallbackInfo ci) { - if (deleteOldOnClose || UpdateModScreen.modUpdated) { - 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(); - } - } + SelfUpdater.onClose(); } } diff --git a/forge/src/main/java/com/seibel/lod/mixins/client/MixinMinecraft.java b/forge/src/main/java/com/seibel/lod/mixins/client/MixinMinecraft.java new file mode 100644 index 000000000..8052c9e4e --- /dev/null +++ b/forge/src/main/java/com/seibel/lod/mixins/client/MixinMinecraft.java @@ -0,0 +1,48 @@ +package com.seibel.lod.mixins.client; + +import com.seibel.lod.common.wrappers.gui.UpdateModScreen; +import com.seibel.lod.core.config.Config; +import com.seibel.lod.core.dependencyInjection.SingletonInjector; +import com.seibel.lod.core.jar.installer.ModrinthGetter; +import com.seibel.lod.core.jar.updater.SelfUpdater; +import com.seibel.lod.core.wrapperInterfaces.IVersionConstants; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.client.gui.screens.TitleScreen; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +/** + * @author coolGi + */ +@Mixin(Minecraft.class) +public class MixinMinecraft +{ + @Redirect( + method = "(Lnet/minecraft/client/main/GameConfig;)V", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;setScreen(Lnet/minecraft/client/gui/screens/Screen;)V") + ) + public void onOpenScreen(Minecraft instance, Screen guiScreen) { + if (!Config.Client.AutoUpdater.enableAutoUpdater.get()) { // Don't do anything if the user doesn't want it + instance.setScreen(guiScreen); // Sets the screen back to the vanilla screen as if nothing ever happened + return; + } + + if (SelfUpdater.onStart()) { + instance.setScreen(new UpdateModScreen( + new TitleScreen(false), // We don't want to use the vanilla title screen as it would fade the buttons + ModrinthGetter.getLatestNameForVersion(SingletonInjector.INSTANCE.get(IVersionConstants.class).getMinecraftVersion()) + )); + } else { + instance.setScreen(guiScreen); // Sets the screen back to the vanilla screen as if nothing ever happened + } + } + + @Inject(at = @At("HEAD"), method = "close()V") + public void close(CallbackInfo ci) { + SelfUpdater.onClose(); + } +}