-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bbb3d5
commit 61d6515
Showing
5 changed files
with
286 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "/"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |