Skip to content

Commit 2404b2e

Browse files
committed
clean up update checker code
1 parent b3c988c commit 2404b2e

File tree

4 files changed

+147
-132
lines changed

4 files changed

+147
-132
lines changed

src/main/java/com/falsepattern/lib/internal/impl/updates/UpdateCheckerImpl.java

Lines changed: 115 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,64 @@
4949
import java.util.concurrent.atomic.AtomicReference;
5050

5151
public final class UpdateCheckerImpl {
52+
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsyncV2(String url) {
53+
return CompletableFuture.supplyAsync(() -> {
54+
try {
55+
return fetchUpdatesV2(url);
56+
} catch (UpdateCheckException e) {
57+
throw new RuntimeException(e);
58+
}
59+
});
60+
}
61+
62+
public static List<ModUpdateInfo> fetchUpdatesV2(String url) throws UpdateCheckException {
63+
try {
64+
val modList = fetchRootListShared(url);
65+
val result = new ArrayList<ModUpdateInfo>();
66+
val installedMods = Loader.instance().getIndexedModList();
67+
for (val node : modList) {
68+
if (!node.isJsonObject()) {
69+
continue;
70+
}
71+
val obj = node.getAsJsonObject();
72+
if (!obj.has("modid")) {
73+
continue;
74+
}
75+
if (!obj.has("versions")) {
76+
continue;
77+
}
78+
val modid = obj.get("modid").getAsString();
79+
if (!installedMods.containsKey(modid)) {
80+
continue;
81+
}
82+
val mod = installedMods.get(modid);
83+
var versions = obj.get("versions");
84+
if (!versions.isJsonArray()) {
85+
if (!versions.isJsonObject()) {
86+
continue;
87+
}
88+
val ver = versions.getAsJsonObject();
89+
versions = new JsonArray();
90+
versions.getAsJsonArray().add(ver);
91+
}
92+
val parsedVersions = parseVersionSpecs(versions.getAsJsonArray()).join();
93+
val latestVersions = new ArrayList<String>();
94+
for (val version : parsedVersions) {
95+
val versionObj = version.getAsJsonObject();
96+
latestVersions.add(versionObj.get("version").getAsString());
97+
}
98+
val currentVersion = mod.getVersion();
99+
if (latestVersions.contains(currentVersion)) {
100+
continue;
101+
}
102+
val latest = parsedVersions.get(0).getAsJsonObject();
103+
result.add(new ModUpdateInfo(modid, currentVersion, latest.get("version").getAsString(), latest.get("url").getAsString()));
104+
}
105+
return result;
106+
} catch (Exception e) {
107+
throw new UpdateCheckException("Failed to check for updates!", e);
108+
}
109+
}
52110
public static List<IChatComponent> updateListToChatMessages(String initiator, List<ModUpdateInfo> updates) {
53111
if (updates == null || updates.size() == 0) {
54112
return null;
@@ -112,118 +170,6 @@ private static JsonArray fetchRootListShared(String url) {
112170
return modList;
113171
}
114172

115-
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
116-
return CompletableFuture.supplyAsync(() -> {
117-
val modList = fetchRootListShared(url);
118-
val result = new ArrayList<ModUpdateInfo>();
119-
val installedMods = Loader.instance().getIndexedModList();
120-
for (val node : modList) {
121-
if (!node.isJsonObject()) {
122-
continue;
123-
}
124-
val obj = node.getAsJsonObject();
125-
if (!obj.has("modid")) {
126-
continue;
127-
}
128-
if (!obj.has("latestVersion")) {
129-
continue;
130-
}
131-
val modid = obj.get("modid").getAsString();
132-
if (!installedMods.containsKey(modid)) {
133-
continue;
134-
}
135-
val mod = installedMods.get(modid);
136-
val latestVersionsNode = obj.get("latestVersion");
137-
List<String> latestVersions;
138-
if (latestVersionsNode.isJsonPrimitive() && (latestVersionsNode.getAsJsonPrimitive().isString())) {
139-
latestVersions = Collections.singletonList(latestVersionsNode.getAsString());
140-
} else if (latestVersionsNode.isJsonArray()) {
141-
latestVersions = new ArrayList<>();
142-
for (val version : latestVersionsNode.getAsJsonArray()) {
143-
if (!version.isJsonPrimitive() || !version.getAsJsonPrimitive().isString()) {
144-
continue;
145-
}
146-
latestVersions.add(version.getAsString());
147-
}
148-
} else {
149-
continue;
150-
}
151-
val currentVersion = mod.getVersion();
152-
if (latestVersions.contains(currentVersion)) {
153-
continue;
154-
}
155-
val updateURL = obj.has("updateURL") &&
156-
obj.get("updateURL").isJsonPrimitive() &&
157-
obj.get("updateURL").getAsJsonPrimitive().isString()
158-
? obj.get("updateURL").getAsString()
159-
: "";
160-
result.add(new ModUpdateInfo(modid, currentVersion, latestVersions.get(0), updateURL));
161-
}
162-
return result;
163-
});
164-
}
165-
166-
public static List<ModUpdateInfo> fetchUpdates(String url) throws UpdateCheckException {
167-
try {
168-
return fetchUpdatesAsync(url).join();
169-
} catch (CompletionException e) {
170-
try {
171-
throw e.getCause();
172-
} catch (UpdateCheckException e1) {
173-
throw e1;
174-
} catch (Throwable e1) {
175-
throw new UpdateCheckException("Failed to check for updates!", e1);
176-
}
177-
}
178-
}
179-
180-
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsyncV2(String url) {
181-
return CompletableFuture.supplyAsync(() -> {
182-
val modList = fetchRootListShared(url);
183-
val result = new ArrayList<ModUpdateInfo>();
184-
val installedMods = Loader.instance().getIndexedModList();
185-
for (val node : modList) {
186-
if (!node.isJsonObject()) {
187-
continue;
188-
}
189-
val obj = node.getAsJsonObject();
190-
if (!obj.has("modid")) {
191-
continue;
192-
}
193-
if (!obj.has("versions")) {
194-
continue;
195-
}
196-
val modid = obj.get("modid").getAsString();
197-
if (!installedMods.containsKey(modid)) {
198-
continue;
199-
}
200-
val mod = installedMods.get(modid);
201-
var versions = obj.get("versions");
202-
if (!versions.isJsonArray()) {
203-
if (!versions.isJsonObject()) {
204-
continue;
205-
}
206-
val ver = versions.getAsJsonObject();
207-
versions = new JsonArray();
208-
versions.getAsJsonArray().add(ver);
209-
}
210-
val parsedVersions = parseVersionSpecs(versions.getAsJsonArray()).join();
211-
val latestVersions = new ArrayList<String>();
212-
for (val version : parsedVersions) {
213-
val versionObj = version.getAsJsonObject();
214-
latestVersions.add(versionObj.get("version").getAsString());
215-
}
216-
val currentVersion = mod.getVersion();
217-
if (latestVersions.contains(currentVersion)) {
218-
continue;
219-
}
220-
val latest = parsedVersions.get(0).getAsJsonObject();
221-
result.add(new ModUpdateInfo(modid, currentVersion, latest.get("version").getAsString(), latest.get("url").getAsString()));
222-
}
223-
return result;
224-
});
225-
}
226-
227173
private static String getString(JsonObject object, String entry) {
228174
if (!object.has(entry)) {
229175
return null;
@@ -318,9 +264,63 @@ private static JsonObject parseLatestGithubVersion(String repo) throws Malformed
318264
return result;
319265
}
320266

321-
public static List<ModUpdateInfo> fetchUpdatesV2(String url) throws UpdateCheckException {
267+
//region deprecated
268+
@Deprecated
269+
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
270+
return CompletableFuture.supplyAsync(() -> {
271+
val modList = fetchRootListShared(url);
272+
val result = new ArrayList<ModUpdateInfo>();
273+
val installedMods = Loader.instance().getIndexedModList();
274+
for (val node : modList) {
275+
if (!node.isJsonObject()) {
276+
continue;
277+
}
278+
val obj = node.getAsJsonObject();
279+
if (!obj.has("modid")) {
280+
continue;
281+
}
282+
if (!obj.has("latestVersion")) {
283+
continue;
284+
}
285+
val modid = obj.get("modid").getAsString();
286+
if (!installedMods.containsKey(modid)) {
287+
continue;
288+
}
289+
val mod = installedMods.get(modid);
290+
val latestVersionsNode = obj.get("latestVersion");
291+
List<String> latestVersions;
292+
if (latestVersionsNode.isJsonPrimitive() && (latestVersionsNode.getAsJsonPrimitive().isString())) {
293+
latestVersions = Collections.singletonList(latestVersionsNode.getAsString());
294+
} else if (latestVersionsNode.isJsonArray()) {
295+
latestVersions = new ArrayList<>();
296+
for (val version : latestVersionsNode.getAsJsonArray()) {
297+
if (!version.isJsonPrimitive() || !version.getAsJsonPrimitive().isString()) {
298+
continue;
299+
}
300+
latestVersions.add(version.getAsString());
301+
}
302+
} else {
303+
continue;
304+
}
305+
val currentVersion = mod.getVersion();
306+
if (latestVersions.contains(currentVersion)) {
307+
continue;
308+
}
309+
val updateURL = obj.has("updateURL") &&
310+
obj.get("updateURL").isJsonPrimitive() &&
311+
obj.get("updateURL").getAsJsonPrimitive().isString()
312+
? obj.get("updateURL").getAsString()
313+
: "";
314+
result.add(new ModUpdateInfo(modid, currentVersion, latestVersions.get(0), updateURL));
315+
}
316+
return result;
317+
});
318+
}
319+
320+
@Deprecated
321+
public static List<ModUpdateInfo> fetchUpdates(String url) throws UpdateCheckException {
322322
try {
323-
return fetchUpdatesAsyncV2(url).join();
323+
return fetchUpdatesAsync(url).join();
324324
} catch (CompletionException e) {
325325
try {
326326
throw e.getCause();
@@ -331,4 +331,5 @@ public static List<ModUpdateInfo> fetchUpdatesV2(String url) throws UpdateCheckE
331331
}
332332
}
333333
}
334+
//endregion
334335
}

src/main/java/com/falsepattern/lib/updates/ModUpdateInfo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import lombok.NonNull;
2626
import org.apache.logging.log4j.Logger;
2727

28+
/**
29+
* Information about a new version of a mod.
30+
*/
2831
@Data
2932
@StableAPI(since = "0.8.0")
3033
public class ModUpdateInfo {

src/main/java/com/falsepattern/lib/updates/UpdateCheckException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
import com.falsepattern.lib.StableAPI;
2424

25+
/**
26+
* Exception only thrown by methods of {@link UpdateChecker}.
27+
* Please don't throw this in your own code, only catch/handle it.
28+
*/
2529
@StableAPI(since = "0.8.3")
2630
public class UpdateCheckException extends Exception {
2731
@StableAPI.Internal

src/main/java/com/falsepattern/lib/updates/UpdateChecker.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
package com.falsepattern.lib.updates;
2222

23+
import com.falsepattern.lib.DeprecationDetails;
2324
import com.falsepattern.lib.StableAPI;
2425
import com.falsepattern.lib.internal.impl.updates.UpdateCheckerImpl;
2526

@@ -30,15 +31,6 @@
3031

3132
@StableAPI(since = "0.8.0")
3233
public final class UpdateChecker {
33-
/**
34-
* DEPRECATED: Use the V2 API instead. This is kept for backwards compatibility.
35-
*/
36-
@StableAPI.Expose
37-
@Deprecated
38-
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
39-
return UpdateCheckerImpl.fetchUpdatesAsync(url);
40-
}
41-
4234
/**
4335
* Checks for updates. The URL should be a JSON file that contains a list of mods, each with a mod ID, and one or more
4436
* versions. The JSON file must have the following format:
@@ -95,15 +87,6 @@ public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsyncV2(String
9587
return UpdateCheckerImpl.fetchUpdatesAsyncV2(url);
9688
}
9789

98-
/**
99-
* DEPRECATED: Use the V2 API instead. This is kept for backwards compatibility.
100-
*/
101-
@StableAPI.Expose
102-
@Deprecated
103-
public static List<ModUpdateInfo> fetchUpdates(String url) throws UpdateCheckException {
104-
return UpdateCheckerImpl.fetchUpdates(url);
105-
}
106-
10790
/**
10891
* Same this as {@link #fetchUpdatesAsyncV2(String)}, but returns the result in a blocking fashion.
10992
*
@@ -130,4 +113,28 @@ public static List<ModUpdateInfo> fetchUpdatesV2(String url) throws UpdateCheckE
130113
public static List<IChatComponent> updateListToChatMessages(String initiator, List<ModUpdateInfo> updates) {
131114
return UpdateCheckerImpl.updateListToChatMessages(initiator, updates);
132115
}
116+
117+
//region deprecated
118+
/**
119+
* DEPRECATED: Use the V2 API instead. This is kept for backwards compatibility.
120+
*/
121+
@Deprecated
122+
@DeprecationDetails(deprecatedSince = "0.11.0",
123+
replacement = "fetchUpdatesV2")
124+
@StableAPI.Expose
125+
public static List<ModUpdateInfo> fetchUpdates(String url) throws UpdateCheckException {
126+
return UpdateCheckerImpl.fetchUpdates(url);
127+
}
128+
129+
/**
130+
* DEPRECATED: Use the V2 API instead. This is kept for backwards compatibility.
131+
*/
132+
@StableAPI.Expose
133+
@Deprecated
134+
@DeprecationDetails(deprecatedSince = "0.11.0",
135+
replacement = "fetchUpdatesAsyncV2")
136+
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
137+
return UpdateCheckerImpl.fetchUpdatesAsync(url);
138+
}
139+
//endregion
133140
}

0 commit comments

Comments
 (0)