-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from vengestone-dragoon/Custom-check-suport
Custom check suport
- Loading branch information
Showing
7 changed files
with
166 additions
and
19 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
99 changes: 99 additions & 0 deletions
99
src/main/java/com/osiris/autoplug/client/tasks/updater/search/CustomCheckURL.java
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,99 @@ | ||
package com.osiris.autoplug.client.tasks.updater.search; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.osiris.autoplug.client.tasks.updater.plugins.MinecraftPlugin; | ||
import com.osiris.autoplug.client.tasks.updater.search.SearchResult; | ||
import com.osiris.autoplug.client.utils.UtilsURL; | ||
import com.osiris.jlib.json.Json; | ||
import com.osiris.jlib.logger.AL; | ||
|
||
import java.io.File; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class CustomCheckURL { | ||
|
||
public CustomCheckURL(){} | ||
|
||
private boolean isInt(String s) { | ||
try { | ||
Integer.parseInt(s); | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
public SearchResult doCustomCheck(MinecraftPlugin plugin) { | ||
|
||
String url = plugin.getCustomCheckURL(); | ||
url = new UtilsURL().clean(url); | ||
Exception exception = null; | ||
String latest = null; | ||
String type = ".jar"; | ||
String downloadUrl = null; | ||
byte code = 0; | ||
try { | ||
JsonObject release; | ||
try { | ||
release = Json.getAsJsonArray(url) | ||
.get(0).getAsJsonObject(); | ||
} catch (Exception e) { | ||
throw e; | ||
} | ||
|
||
String[] versionNaming = {"version_number", "version"}; | ||
|
||
for (String naming : versionNaming) { | ||
if (release.has(naming)) { | ||
String version = release.get(naming).getAsString().replaceAll("[^0-9.]", ""); | ||
if (!version.isEmpty()) { | ||
latest = version; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
String[] downloadNaming = {"download_url", "download", "file", "download_file"}; | ||
|
||
for (String naming : downloadNaming) { | ||
if (release.has(naming)) { | ||
String durl = release.get(naming).getAsString(); | ||
if (!durl.isEmpty()) { | ||
downloadUrl = durl; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
String[] pluginVersionComponents = plugin.getVersion().split("\\."); | ||
String[] latestVersionComponents = latest.split("\\."); | ||
|
||
for (int i = 0; i < Math.min(pluginVersionComponents.length, latestVersionComponents.length); i++) { | ||
int pluginComponent = Integer.parseInt(pluginVersionComponents[i]); | ||
int latestComponent = Integer.parseInt(latestVersionComponents[i]); | ||
|
||
if (pluginComponent < latestComponent) { | ||
// plugin.getVersion() is smaller than latest | ||
code = 1; | ||
break; | ||
} else if (pluginComponent > latestComponent) { | ||
// plugin.getVersion() is greater than latest | ||
break; | ||
} | ||
} | ||
|
||
} catch (Exception e) { | ||
exception = e; | ||
code = 2; | ||
} | ||
|
||
if (downloadUrl == null && plugin.customDownloadURL == null) | ||
code = 2; | ||
SearchResult result = new SearchResult(null, code, latest, downloadUrl, type, null, null, false); | ||
result.setException(exception); | ||
return result; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -28,4 +28,4 @@ void checkInternetAccessMethod2() throws Exception { | |
connection.connect(); | ||
connection.disconnect(); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/com/osiris/autoplug/client/tasks/updater/DoCustomUpdateCheckTest.java
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,21 @@ | ||
package com.osiris.autoplug.client.tasks.updater; | ||
|
||
import com.osiris.autoplug.client.tasks.updater.plugins.MinecraftPlugin; | ||
import com.osiris.autoplug.client.tasks.updater.search.SearchResult; | ||
import com.osiris.autoplug.client.tasks.updater.search.CustomCheckURL; | ||
import com.osiris.autoplug.client.tasks.updater.mods.ModrinthAPI; | ||
import com.osiris.autoplug.client.UtilsTest; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
|
||
class TestCustomUpdateCheck { | ||
@Test | ||
void test() { | ||
MinecraftPlugin pl = new MinecraftPlugin("./plugins/", "Chunky", "0.0.0", "pop4959", 0, 0, null); | ||
pl.customCheckURL = "https://api.modrinth.com/v2/project/chunky/version"; | ||
pl.customDownloadURL = "https://cdn.modrinth.com/data/fALzjamp/versions/dPliWter/Chunky-1.4.16.jar"; | ||
SearchResult sr = new CustomCheckURL().doCustomCheck(pl); | ||
assertTrue(1 == sr.resultCode); | ||
} | ||
} |