Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
fix: Changelog and Updater bugfixing (#613)
Browse files Browse the repository at this point in the history
* chore: changelog and updater changes

* chore: remove debugging

* Update src/main/java/com/wynntils/webapi/profiles/UpdateProfile.java

Co-authored-by: Ryan <[email protected]>

* Update src/main/java/com/wynntils/modules/core/overlays/ui/UpdateAvailableScreen.java

Co-authored-by: Ryan <[email protected]>

* Update src/main/java/com/wynntils/modules/core/instances/MainMenuButtons.java

Co-authored-by: Ryan <[email protected]>

* refactor: newline

Co-authored-by: Ryan <[email protected]>
  • Loading branch information
DevScyu and ryanzhoudev authored Oct 22, 2022
1 parent 3dc3811 commit 7f6e390
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public List<String> getTabCompletions(MinecraftServer server, ICommandSender sen
} else if (args.length == 2) {
switch (args[0]) {
case "changelog":
return getListOfStringsMatchingLastWord(args, "major", "latest");
return getListOfStringsMatchingLastWord(args, "latest");
case "help":
return getListOfStringsMatchingLastWord(args, "1", "2");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.wynntils.core.utils.reflections.ReflectionFields;
import com.wynntils.modules.core.CoreModule;
import com.wynntils.modules.core.config.CoreDBConfig;
import com.wynntils.modules.core.enums.UpdateStream;
import com.wynntils.modules.core.instances.packet.PacketIncomingFilter;
import com.wynntils.modules.core.instances.packet.PacketOutgoingFilter;
import com.wynntils.modules.core.managers.CompassManager;
Expand Down Expand Up @@ -278,7 +277,7 @@ public void onJoinLobby(WynnClassChangeEvent e) {
}

new Thread(() -> {
List<String> changelog = WebManager.getChangelog(false);
Map<String, String> changelog = WebManager.getChangelog(false);
if (changelog == null) return;

McIf.mc().addScheduledTask(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@
package com.wynntils.modules.core.overlays.ui;

import com.wynntils.McIf;
import com.wynntils.Reference;
import com.wynntils.core.framework.rendering.ScreenRenderer;
import com.wynntils.core.framework.rendering.SmartFontRenderer;
import com.wynntils.core.framework.rendering.colors.CommonColors;
import com.wynntils.core.framework.rendering.colors.CustomColor;
import com.wynntils.core.framework.rendering.textures.Textures;
import com.wynntils.core.utils.StringUtils;
import com.wynntils.modules.core.config.CoreDBConfig;
import com.wynntils.modules.core.enums.UpdateStream;
import com.wynntils.webapi.WebManager;
import net.minecraft.client.gui.GuiScreen;
import org.lwjgl.input.Mouse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;

public class ChangelogUI extends GuiScreen {

Expand All @@ -34,27 +29,26 @@ public class ChangelogUI extends GuiScreen {
GuiScreen previousGui;

List<String> changelogContent = new ArrayList<>();
String changelogVersion = "";

int scrollbarPosition = 0;
int scrollbarSize;

boolean major;

public ChangelogUI(List<String> changelogContent) {
public ChangelogUI(Map<String, String> changelogContent) {
this(null, changelogContent);
}

public ChangelogUI(GuiScreen previousGui, List<String> changelogContent) {
public ChangelogUI(GuiScreen previousGui, Map<String, String> changelogContent) {
this.previousGui = previousGui;

this.major = major;

if (changelogContent == null || changelogContent.isEmpty()) {
changelogContent = Collections.singletonList("<Error>");
this.changelogContent.add("<Error>");
} else {
for (String rawText : changelogContent.get("changelog").split("\n"))
this.changelogContent.addAll(Arrays.asList(StringUtils.wrapText(rawText, 40)));
}

for (String rawText : changelogContent)
this.changelogContent.addAll(Arrays.asList(StringUtils.wrapText(rawText, 40)));
this.changelogVersion = changelogContent.get("version");

if (this.changelogContent.size() <= 15) scrollbarSize = 118;
else {
Expand All @@ -74,8 +68,10 @@ public static void loadChangelogAndShow(boolean forceLatest) {
* @param forceLatest {@link WebManager#getChangelog(boolean)}'s second argument (Latest or current changelog?)
*/
public static void loadChangelogAndShow(GuiScreen previousGui, boolean forceLatest) {

GuiScreen loadingScreen = new ChangelogUI(previousGui, Collections.singletonList("Loading..."));
GuiScreen loadingScreen = new ChangelogUI(previousGui, new HashMap<String, String>() {{
put("version", "Loading...");
put("changelog", "Loading...");
}});
McIf.mc().displayGuiScreen(loadingScreen);
if (McIf.mc().currentScreen != loadingScreen) {
// Changed by an event handler
Expand All @@ -86,7 +82,7 @@ public static void loadChangelogAndShow(GuiScreen previousGui, boolean forceLate
if (McIf.mc().currentScreen != loadingScreen) {
return;
}
List<String> changelog = WebManager.getChangelog(forceLatest);
Map<String, String> changelog = WebManager.getChangelog(forceLatest);
if (McIf.mc().currentScreen != loadingScreen) {
return;
}
Expand All @@ -112,7 +108,7 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
float middleX = width/2f; float middleY = height/2f;

renderer.drawRect(Textures.UIs.changelog, (int)middleX - 150, (int)middleY - 100, 0, 0, 300, 200);
renderer.drawString("Changelog " + (CoreDBConfig.INSTANCE.updateStream == UpdateStream.CUTTING_EDGE && !major ? String.format("%s-beta.%d", Reference.VERSION, Reference.BUILD_NUMBER) : "v" + Reference.VERSION), middleX - 105, middleY - 83, CommonColors.RED, SmartFontRenderer.TextAlignment.LEFT_RIGHT, SmartFontRenderer.TextShadow.NONE);
renderer.drawString("Changelog " + this.changelogVersion, middleX - 105, middleY - 83, CommonColors.RED, SmartFontRenderer.TextAlignment.LEFT_RIGHT, SmartFontRenderer.TextShadow.NONE);

// scrollbar
renderer.drawRect(SCROLL_BACKGROUND, (int)middleX + 119, (int)middleY - 80, (int)middleX + 119 + 5, (int)middleY + 40);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,83 @@
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.util.text.TextFormatting;

import java.util.ArrayList;
import java.util.List;

public class UpdateAvailableScreen extends GuiScreen {

private ServerData server;
private String text;
private String line1;
private String line2;

public UpdateAvailableScreen(ServerData server) {
this.server = server;
text = "A new update is available " + TextFormatting.YELLOW + WebManager.getUpdate().getLatestUpdate();
text += TextFormatting.GRAY + " (you are on " + TextFormatting.YELLOW + "v" + Reference.VERSION + TextFormatting.GRAY + ")";
line1 = "A new update is available " + TextFormatting.YELLOW + WebManager.getUpdate().getLatestUpdate();
if (WebManager.getUpdate().getDownloadMD5() != null) {
line1 += TextFormatting.GRAY + " (md5: " + TextFormatting.YELLOW + WebManager.getUpdate().getDownloadMD5() + TextFormatting.GRAY + ")";
}
line2 = "You are currently on " + TextFormatting.YELLOW + Reference.VERSION;
if (WebManager.getUpdate().getMd5Installed() != null) {
line2 += TextFormatting.GRAY + " (md5: " + TextFormatting.YELLOW + WebManager.getUpdate().getMd5Installed() + TextFormatting.GRAY + ")";
}
}

@Override
public void initGui() {
this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 84, 200, 20, "View changelog"));
this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 108, 98, 20, "Update now"));
this.buttonList.add(new GuiButton(2, this.width / 2 + 2, this.height / 4 + 108, 98, 20, "Update at exit"));
this.buttonList.add(new GuiButton(3, this.width / 2 - 100, this.height / 4 + 132, 98, 20, "Ignore update"));
this.buttonList.add(new GuiButton(4, this.width / 2 + 2, this.height / 4 + 132, 98, 20, "Cancel"));
int spacing = 24;
int y = this.height / 4 + 84;
// row 1
this.buttonList.add(new GuiButton(0, this.width / 2 - 100, y, 200, 20, "View changelog"));
// row 2
y += spacing;
this.buttonList.add(new GuiButton(1, this.width / 2 - 100, y, 98, 20, "Update now"));
this.buttonList.add(new GuiButton(2, this.width / 2 + 2, y, 98, 20, "Update at exit"));
// row 3
y += spacing;
this.buttonList.add(new GuiButton(3, this.width / 2 - 100, y, 98, 20, "Ignore update"));
this.buttonList.add(new GuiButton(4, this.width / 2 + 2, y, 98, 20, "Cancel"));
}

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawDefaultBackground();

int yOffset = Math.min(this.height / 2, this.height / 4 + 80 - McIf.mc().fontRenderer.FONT_HEIGHT * 2);
drawCenteredString(McIf.mc().fontRenderer, text, this.width/2, yOffset - McIf.mc().fontRenderer.FONT_HEIGHT - 2, 0xFFFFFFFF);
drawCenteredString(McIf.mc().fontRenderer, "Update now or when leaving Minecraft?", this.width/2, yOffset, 0xFFFFFFFF);
drawCenteredString(McIf.mc().fontRenderer, "(Updating now will exit Minecraft after downloading update)", this.width/2, yOffset + McIf.mc().fontRenderer.FONT_HEIGHT + 2, 0xFFFFFFFF);
List<String> lines = new ArrayList<String>() {{
add(line1);
add(line2);
add("Update now or when leaving Minecraft?");
}};

int spacing = this.fontRenderer.FONT_HEIGHT + 2; // 11
int y = this.height / 4 + (84 - spacing * lines.size());

for (String line : lines) {
drawCenteredString(this.fontRenderer, line, this.width / 2, y, 0xFFFFFF);
y += spacing;
}

// draw in the bottom left corner
drawString(fontRenderer, "Current update stream: " + TextFormatting.YELLOW + CoreDBConfig.INSTANCE.updateStream, 0, this.height - 10, 0xFFFFFFFF);

// draw gui buttons
super.drawScreen(mouseX, mouseY, partialTicks);

// Draw hover text
for (GuiButton button : buttonList) {
if (button.isMouseOver()) {
if (button.id == 0) {
drawHoveringText("View the changelog for this update", mouseX, mouseY);
} else if (button.id == 1) {
drawHoveringText("Update now and exit Minecraft", mouseX, mouseY);
} else if (button.id == 2) {
drawHoveringText("Update when you exit Minecraft", mouseX, mouseY);
} else if (button.id == 3) {
drawHoveringText("Ignore this update", mouseX, mouseY);
} else if (button.id == 4) {
drawHoveringText("Cancel", mouseX, mouseY);
}
}
}
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/wynntils/webapi/WebManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ public void run() {
*
* @return an ArrayList of ChangelogProfile's
*/
public static List<String> getChangelog(boolean forceLatest) {
public static Map<String, String> getChangelog(boolean forceLatest) {
if (apiUrls == null) return null;

String version = "v" + Reference.VERSION;
Expand All @@ -839,7 +839,10 @@ public static List<String> getChangelog(boolean forceLatest) {

JsonObject main = new JsonParser().parse(IOUtils.toString(st.getInputStream(), StandardCharsets.UTF_8)).getAsJsonObject();

return Arrays.asList(main.get("changelog").getAsString().split("\n"));
return new HashMap<String, String>() {{
put("version", main.get("version").getAsString());
put("changelog", main.get("changelog").getAsString());
}};
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/com/wynntils/webapi/profiles/UpdateProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ public class UpdateProfile {
boolean updateCheckFailed = false;
String latestUpdate = Reference.VERSION;
String downloadUrl = null;
String downloadMD5 = null;
String md5Installed = null;

public UpdateProfile() {
new Thread(() -> {
try {
MD5Verification md5Installed = new MD5Verification(ModCore.jarFile);
HashMap<String, String> updateData = WebManager.getUpdateData(CoreDBConfig.INSTANCE.updateStream);
latestUpdate = updateData.get("version").replace("v", "");
downloadUrl = updateData.get("url");
downloadMD5 = updateData.get("md5");

if (!md5Installed.equals(updateData.get("md5"))) {
md5Installed = new MD5Verification(ModCore.jarFile).getMd5();
if (md5Installed == null) {
updateCheckFailed = true;
return;
}

if (!md5Installed.equals(downloadMD5)) {
Reference.LOGGER.info("Update found for version " + latestUpdate + " (" + downloadMD5 + ") Current version: " + Reference.VERSION + " (" + md5Installed + ")");
hasUpdate = true;
latestUpdate = updateData.get("version");
downloadUrl = updateData.get("url");
UpdateOverlay.reset();
}

Expand Down Expand Up @@ -63,6 +72,14 @@ public String getDownloadUrl() {
return downloadUrl;
}

public String getDownloadMD5() {
return downloadMD5;
}

public String getMd5Installed() {
return md5Installed;
}

public boolean updateCheckFailed() {
return updateCheckFailed;
}
Expand Down

0 comments on commit 7f6e390

Please sign in to comment.