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

Commit

Permalink
Add better Version check (+ tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Dec 28, 2022
1 parent d247d83 commit 9f6bdea
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 31 deletions.
26 changes: 26 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
<maven.compiler.target>16</maven.compiler.target>
</properties>

<repositories>
<repository>
<id>flexver-repo</id>
<url>https://repo.sleeping.town/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spongepowered</groupId>
Expand Down Expand Up @@ -80,6 +87,19 @@
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>com.unascribed</groupId>
<artifactId>flexver-java</artifactId>
<version>1.0.2</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -111,6 +131,12 @@
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>com.unascribed</pattern>
<shadedPattern>ch.andre601.advancedserverlist.core.depends.flexver</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.unascribed.flexver.FlexVerComparator;
import io.leangen.geantyref.TypeToken;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -83,13 +84,12 @@ public void startUpdateChecker(){

int result = version.compare(core.getVersion());
switch(result){
case -2 -> {
logger.warn("Encountered an exception while comparing versions. Are they valid?");
logger.warn("Own version: %s; New version: %s", core.getVersion(), version.getVersionNumber());
}
case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?");
case -1 -> printUpdateBanner(version.getVersionNumber(), version.getId(), version.isRelease());
case 0 -> logger.info("No new update found. You're running the latest version!");
case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId());
case 1 -> logger.info(
"Your version (%s) is higher than the latest release (%s). Are you running a dev build?",
core.getVersion(), version.getVersionNumber()
);
}
});
}, 0L, 12L, TimeUnit.HOURS);
Expand Down Expand Up @@ -147,6 +147,7 @@ private CompletableFuture<ModrinthVersion> checkUpdate(){
}

ModrinthVersion version = list.get(0);

if(version.getVersionNumber() == null || version.getVersionNumber().isEmpty()){
logger.warn("Cannot check latest version. Received version number was null/empty.");
return null;
Expand All @@ -163,13 +164,20 @@ private CompletableFuture<ModrinthVersion> checkUpdate(){
});
}

private void printUpdateBanner(String version, String versionId){
private void printUpdateBanner(String version, String versionId, boolean isRelease){
logger.info("==================================================================");
logger.info("You are running an outdated version of AdvancedServerList!");
logger.info("");
logger.info("Your version: %s", core.getVersion());
logger.info("Modrinth version: %s", version);
logger.info("");

if(!isRelease){
logger.info("WARNING: This release is an Alpha/Beta! It may contain");
logger.info(" breaking changes!");
logger.info("");
}

logger.info("You can download the latest release from here:");
logger.info("https://modrinth.com/plugin/advancedserverlist/version/%s", versionId);
logger.info("==================================================================");
Expand All @@ -180,10 +188,13 @@ public static class ModrinthVersion{
private String id;
@SuppressWarnings("FieldMayBeFinal")
private String versionNumber;
@SuppressWarnings("FieldMayBeFinal")
private String versionType;

public ModrinthVersion(String id, String versionNumber){
public ModrinthVersion(String id, String versionNumber, String versionType){
this.id = id;
this.versionNumber = versionNumber;
this.versionType = versionType;
}

public String getId(){
Expand All @@ -194,26 +205,12 @@ public String getVersionNumber(){
return versionNumber;
}

public boolean isRelease(){
return versionType.equals("release");
}

public int compare(String version){
String[] oldParts = version.split("\\.");
String[] newParts = versionNumber.split("\\.");
int length = Math.max(oldParts.length, newParts.length);

for(int i = 0; i < length; i++){
try{
int oldPart = i < oldParts.length ? Integer.parseInt(oldParts[i]) : 0;
int newPart = i < newParts.length ? Integer.parseInt(newParts[i]) : 0;

if(oldPart < newPart)
return 1;

if(oldPart > newPart)
return -1;
}catch(NumberFormatException ex){
return -2;
}
}
return 0;
return FlexVerComparator.compare(version, versionNumber);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public static Builder resolve(ConfigurationNode node){

public Builder resolveMOTD(){
List<String> motd = resolveList(node, "motd");
System.out.println(String.join(", ", motd));
if(motd.size() <= 2){
this.motd = motd;
return this;
Expand Down Expand Up @@ -185,10 +184,8 @@ public ProfileEntry build(){

private List<String> resolveList(ConfigurationNode node, Object... path){
try{
System.out.println("Resolved list!");
return node.node(path).getList(String.class);
}catch(SerializationException ex){
System.out.println(ex.getMessage());
return Collections.emptyList();
}
}
Expand Down
Loading

0 comments on commit 9f6bdea

Please sign in to comment.