Skip to content

Commit

Permalink
add updater
Browse files Browse the repository at this point in the history
  • Loading branch information
wu452148993 committed Feb 2, 2021
1 parent 4bbb3d5 commit 61d6515
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/main/java/net/wurstclient/WurstClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import net.wurstclient.other_feature.OtfList;
import net.wurstclient.other_feature.OtherFeature;
import net.wurstclient.settings.SettingsFile;
//import net.wurstclient.update.WurstUpdater;
import net.wurstclient.update.WurstUpdater;
import net.wurstclient.util.json.JsonException;

public enum WurstClient
Expand Down Expand Up @@ -77,7 +77,7 @@ public enum WurstClient

private boolean enabled = true;
private static boolean guiInitialized;
//private WurstUpdater updater;
private WurstUpdater updater;
private Path wurstFolder;

private KeyBinding zoomKey;
Expand Down Expand Up @@ -136,10 +136,8 @@ public void initialize()
eventManager.add(PreMotionListener.class, rotationFaker);
eventManager.add(PostMotionListener.class, rotationFaker);

/*
updater = new WurstUpdater();
eventManager.add(UpdateListener.class, updater);
*/

Path altsFile = wurstFolder.resolve("alts.encrypted_json");
Path encFolder = createEncryptionFolder();
Expand Down Expand Up @@ -339,12 +337,12 @@ public void setEnabled(boolean enabled)
hax.panicHack.setEnabled(true);
hax.panicHack.onUpdate();
}
}
}*/

public WurstUpdater getUpdater()
{
return updater;
}*/
}

public Path getWurstFolder()
{
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/wurstclient/hud/WurstLogo.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ private String getVersionString()
String version = "v" + WurstClient.VERSION;
version += " MC" + WurstClient.MC_VERSION;

/*
if(WurstClient.INSTANCE.getUpdater().isOutdated())
version += " (outdated)";
*/

return version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public boolean isVisible()

public static enum Visibility
{
ALWAYS("Always", () -> true)/*,
ALWAYS("Always", () -> true),


ONLY_OUTDATED("Only when outdated",
() -> WURST.getUpdater().isOutdated())*/;
() -> WURST.getUpdater().isOutdated());

private final String name;
private final BooleanSupplier visible;
Expand Down
149 changes: 149 additions & 0 deletions src/main/java/net/wurstclient/update/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2014-2021 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.update;

import java.util.regex.Pattern;

public final class Version implements Comparable<Version>
{
private static final Pattern SYNTAX =
Pattern.compile("^[0-9]+\\.[0-9]+(?:\\.[0-9]+)?(?:pre[0-9]+)?$");

private final int major;
private final int minor;
private final int patch;
private final int preRelease;

public Version(String version)
{
if(!SYNTAX.asPredicate().test(version))
{
major = -1;
minor = -1;
patch = -1;
preRelease = Integer.MAX_VALUE;
return;
}

int indexOfPre = version.indexOf("pre");

String[] parts;
if(indexOfPre == -1)
{
preRelease = Integer.MAX_VALUE;
parts = version.split("\\.");

}else
{
preRelease = Integer.parseInt(version.substring(indexOfPre + 3));
parts = version.substring(0, indexOfPre).split("\\.");
}

major = Integer.parseInt(parts[0]);

minor = Integer.parseInt(parts[1]);

if(parts.length == 3)
patch = Integer.parseInt(parts[2]);
else
patch = 0;
}

@Override
public int hashCode()
{
return major << 24 | minor << 16 | patch << 8 | preRelease;
}

@Override
public boolean equals(Object obj)
{
return super.equals(obj)
|| obj instanceof Version && compareTo((Version)obj) == 0;
}

@Override
public int compareTo(Version o)
{
if(major != o.major)
return Integer.compare(major, o.major);

if(minor != o.minor)
return Integer.compare(minor, o.minor);

if(patch != o.patch)
return Integer.compare(patch, o.patch);

if(preRelease != o.preRelease)
return Integer.compare(preRelease, o.preRelease);

return 0;
}

public boolean shouldUpdateTo(Version other)
{
return isInvalid() || other.isInvalid() || isLowerThan(other);
}

public boolean isLowerThan(Version other)
{
return compareTo(other) < 0;
}

public boolean isLowerThan(String other)
{
return isLowerThan(new Version(other));
}

public boolean isHigherThan(Version other)
{
return compareTo(other) > 0;
}

public boolean isHigherThan(String other)
{
return isHigherThan(new Version(other));
}

@Override
public String toString()
{
if(isInvalid())
return "(invalid version)";

String s = major + "." + minor;

if(patch > 0)
s += "." + patch;

if(isPreRelease())
s += "pre" + preRelease;

return s;
}

public boolean isInvalid()
{
return major == -1 && minor == -1 && patch == -1;
}

public boolean isPreRelease()
{
return preRelease != Integer.MAX_VALUE;
}

public String getChangelogLink()
{
String version = major + "-" + minor;

if(isPreRelease())
version += "pre" + preRelease;

return "https://www.wurstclient.net/updates/wurst-" + version + "/";
}
}
131 changes: 131 additions & 0 deletions src/main/java/net/wurstclient/update/WurstUpdater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2014-2021 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.update;

import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.event.ClickEvent;
import net.wurstclient.WurstClient;
import net.wurstclient.events.UpdateListener;
import net.wurstclient.util.ChatUtils;
import net.wurstclient.util.json.JsonException;
import net.wurstclient.util.json.JsonUtils;
import net.wurstclient.util.json.WsonArray;
import net.wurstclient.util.json.WsonObject;

public final class WurstUpdater implements UpdateListener
{
private Thread thread;
private boolean outdated;
private ITextComponent component;

@Override
public void onUpdate()
{
if(thread == null)
{
thread = new Thread(() -> checkForUpdates(), "WurstUpdater");
thread.start();
return;
}

if(thread.isAlive())
return;

if(component != null)
ChatUtils.component(component);

WurstClient.INSTANCE.getEventManager().remove(UpdateListener.class,
this);
}

public void checkForUpdates()
{
Version currentVersion = new Version(WurstClient.VERSION);
Version latestVersion = null;

try
{
WsonArray wson = JsonUtils.parseURLToArray(
"https://api.github.com/repos/Wurst-Imperium/Wurst-MCX2/releases");

for(WsonObject release : wson.getAllObjects())
{
if(!currentVersion.isPreRelease()
&& release.getBoolean("prerelease"))
continue;

if(!containsCompatibleAsset(release.getArray("assets")))
continue;

String tagName = release.getString("tag_name");
latestVersion = new Version(tagName.substring(1));
break;
}

if(latestVersion == null)
throw new NullPointerException("Latest version is missing!");

System.out.println("[Updater] Current version: " + currentVersion);
System.out.println("[Updater] Latest version: " + latestVersion);
outdated = currentVersion.shouldUpdateTo(latestVersion);

}catch(Exception e)
{
System.err.println("[Updater] An error occurred!");
e.printStackTrace();
}

if(latestVersion == null || latestVersion.isInvalid())
{
String text = "An error occurred while checking for updates."
+ " Click \u00a7nhere\u00a7r to check manually.";
String url = "https://www.wurstclient.net/download/";
showLink(text, url);
return;
}

if(!outdated)
return;

String text = "Wurst " + latestVersion + " is now available."
+ " Click \u00a7nhere\u00a7r to download the update.";
String url = "https://www.wurstclient.net/download/";
showLink(text, url);
}

private void showLink(String text, String url)
{
component = new StringTextComponent(text);

ClickEvent event = new ClickEvent(ClickEvent.Action.OPEN_URL, url);
component.getStyle().setClickEvent(event);
}

private boolean containsCompatibleAsset(WsonArray wsonArray)
throws JsonException
{
String compatibleSuffix = "MC" + WurstClient.MC_VERSION + ".jar";

for(WsonObject asset : wsonArray.getAllObjects())
{
String assetName = asset.getString("name");
if(!assetName.endsWith(compatibleSuffix))
continue;

return true;
}

return false;
}

public boolean isOutdated()
{
return outdated;
}
}

0 comments on commit 61d6515

Please sign in to comment.