4949import java .util .concurrent .atomic .AtomicReference ;
5050
5151public 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}
0 commit comments