|
1 | 1 | /* |
| 2 | + * UpdateLib - A simple update checking library for Minecraft Plugins. |
2 | 3 | * Copyright (c) 2021 Joshua Sing <joshua@hypera.dev> |
3 | 4 | * |
4 | | - * Permission to use, copy, modify, and distribute this software for any |
5 | | - * purpose with or without fee is hereby granted, provided that the above |
6 | | - * copyright notice and this permission notice appear in all copies. |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
7 | 11 | * |
8 | | - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | | - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | | - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | | - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | | - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | | - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | | - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
15 | 22 | */ |
16 | 23 |
|
17 | 24 | package dev.hypera.updatelib; |
18 | 25 |
|
19 | | -import dev.hypera.updatelib.annotations.RunAsync; |
20 | | -import dev.hypera.updatelib.checkers.UpdateChecker; |
21 | | -import dev.hypera.updatelib.data.CheckData; |
| 26 | +import dev.hypera.updatelib.comparators.IVersionComparator; |
| 27 | +import dev.hypera.updatelib.exceptions.UpdateLibException; |
22 | 28 | import dev.hypera.updatelib.objects.UpdateStatus; |
23 | | - |
| 29 | +import dev.hypera.updatelib.objects.enums.Status; |
| 30 | +import dev.hypera.updatelib.resolvers.IVersionResolver; |
| 31 | +import java.time.Instant; |
24 | 32 | import java.util.Timer; |
25 | 33 | import java.util.TimerTask; |
| 34 | +import java.util.concurrent.CompletableFuture; |
26 | 35 | import java.util.function.Consumer; |
| 36 | +import org.jetbrains.annotations.ApiStatus.Internal; |
27 | 37 |
|
| 38 | +/** |
| 39 | + * UpdateLib main class |
| 40 | + * |
| 41 | + * @author Joshua Sing <joshua@hypera.dev> |
| 42 | + */ |
28 | 43 | public class UpdateLib { |
29 | 44 |
|
30 | | - private final static String VERSION = "3.1.2"; // Current UpdateLib version. |
| 45 | + private static final String VERSION = "4.0.0"; |
31 | 46 |
|
32 | 47 | private final long resourceId; |
33 | 48 | private final String currentVersion; |
34 | | - private final int connectionTimeout; |
35 | | - |
36 | | - private final UpdateChecker updateChecker; |
37 | | - |
38 | | - private final Consumer<UpdateStatus> completeAction; |
39 | | - private final Consumer<Throwable> errorHandler; |
| 49 | + private final int timeout; |
| 50 | + private final IVersionResolver versionResolver; |
| 51 | + private final IVersionComparator versionComparator; |
| 52 | + private final Consumer<UpdateStatus> statusHandler; |
40 | 53 |
|
41 | | - private UpdateStatus lastStatus = null; |
| 54 | + private UpdateStatus lastStatus = UpdateStatus.DEFAULT; |
42 | 55 | private long lastCheck = 0L; |
43 | 56 |
|
44 | | - /** |
45 | | - * UpdateLib Constructor - Used internally by UpdateLib. |
46 | | - * |
47 | | - * @param resourceId Resource ID. |
48 | | - * @param currentVersion Current version of the Resource. |
49 | | - * @param repeatingChecksEnabled If UpdateLib should check for updates periodically. |
50 | | - * @param interval How often UpdateLib should check for updates. |
51 | | - * @param connectionTimeout The amount of milliseconds UpdateLib should wait before timing out on requests. |
52 | | - * @param updateChecker {@link UpdateChecker} to be used by UpdateLib to check for updates. |
53 | | - * @param completeAction Action to run after UpdateLib has checked for an update. |
54 | | - * @param errorHandler Consumer to run if UpdateLib encounters an exception. |
55 | | - */ |
56 | | - protected UpdateLib(long resourceId, String currentVersion, boolean repeatingChecksEnabled, long interval, int connectionTimeout, UpdateChecker updateChecker, Consumer<UpdateStatus> completeAction, Consumer<Throwable> errorHandler) { |
| 57 | + @Internal |
| 58 | + protected UpdateLib(long resourceId, String currentVersion, int timeout, boolean repeatingChecks, long interval, IVersionResolver versionResolver, IVersionComparator versionComparator, Consumer<UpdateStatus> statusHandler) { |
57 | 59 | this.resourceId = resourceId; |
58 | 60 | this.currentVersion = currentVersion; |
59 | | - this.connectionTimeout = connectionTimeout; |
60 | | - this.updateChecker = updateChecker; |
61 | | - this.completeAction = completeAction; |
62 | | - this.errorHandler = errorHandler; |
| 61 | + this.timeout = timeout; |
| 62 | + this.versionResolver = versionResolver; |
| 63 | + this.versionComparator = versionComparator; |
| 64 | + this.statusHandler = statusHandler; |
63 | 65 |
|
64 | | - Thread thread = new Thread(this::checkNow); |
65 | | - thread.setName("UpdateLib-" + thread.getId()); |
66 | | - thread.start(); |
| 66 | + check(); |
67 | 67 |
|
68 | | - if(repeatingChecksEnabled) { |
| 68 | + if (repeatingChecks) { |
69 | 69 | new Timer().schedule(new TimerTask() { |
70 | 70 | @Override |
71 | 71 | public void run() { |
72 | | - checkNow(); |
| 72 | + check(); |
73 | 73 | } |
74 | 74 | }, interval); |
75 | 75 | } |
76 | 76 | } |
77 | 77 |
|
| 78 | + /** |
| 79 | + * Create a new {@link UpdateLibBuilder} instance. |
| 80 | + * @return New {@link UpdateLibBuilder} instance. |
| 81 | + */ |
| 82 | + public static UpdateLibBuilder builder() { |
| 83 | + return UpdateLibBuilder.create(); |
| 84 | + } |
78 | 85 |
|
79 | 86 | /** |
80 | | - * Start an update check. It's recommended to only run this asynchronously as it may take time to fetch data from |
81 | | - * the API. |
82 | | - * |
83 | | - * @return Response - Instance of {@link UpdateStatus} |
84 | | - * @since 2.0.0-SNAPSHOT |
| 87 | + * Get the current version of UpdateLib. |
| 88 | + * @return Current UpdateLib version. |
85 | 89 | */ |
86 | | - @RunAsync |
87 | | - public UpdateStatus checkNow() { |
88 | | - try { |
89 | | - lastStatus = updateChecker.check(new CheckData(resourceId, currentVersion, connectionTimeout)); |
90 | | - lastCheck = System.currentTimeMillis(); |
91 | | - if(null != completeAction) |
92 | | - completeAction.accept(lastStatus); |
93 | | - } catch (Exception ex) { |
94 | | - if(null == errorHandler) |
95 | | - ex.printStackTrace(); |
96 | | - else |
97 | | - errorHandler.accept(ex); |
98 | | - } |
| 90 | + public static String getVersion() { |
| 91 | + return VERSION; |
| 92 | + } |
99 | 93 |
|
100 | | - return lastStatus; |
| 94 | + /** |
| 95 | + * Checks for an update. |
| 96 | + * @return {@link CompletableFuture<UpdateStatus>} |
| 97 | + */ |
| 98 | + public CompletableFuture<UpdateStatus> check() { |
| 99 | + return CompletableFuture.supplyAsync(() -> { |
| 100 | + try { |
| 101 | + String distributedVersion = versionResolver.getVersion(this, resourceId); |
| 102 | + Status comparison = versionComparator.compareVersions(currentVersion, distributedVersion); |
| 103 | + |
| 104 | + lastStatus = new UpdateStatus(currentVersion, distributedVersion, comparison); |
| 105 | + lastCheck = Instant.now().toEpochMilli(); |
| 106 | + |
| 107 | + statusHandler.accept(lastStatus); |
| 108 | + return lastStatus; |
| 109 | + } catch (UpdateLibException ex) { |
| 110 | + throw new IllegalStateException(ex); |
| 111 | + } |
| 112 | + }); |
101 | 113 | } |
102 | 114 |
|
103 | 115 | /** |
104 | | - * Get last stored {@link UpdateStatus}. If UpdateLib hasn't checked for an update or the last check failed, this |
105 | | - * will return null. |
106 | | - * |
107 | | - * @return Last stored {@link UpdateStatus} |
108 | | - * @since 3.0.0-SNAPSHOT |
| 116 | + * Get the last update status. |
| 117 | + * @return Last update status. |
109 | 118 | */ |
110 | 119 | public UpdateStatus getLastStatus() { |
111 | 120 | return lastStatus; |
112 | 121 | } |
113 | 122 |
|
114 | 123 | /** |
115 | | - * Get the last time UpdateLib checked for updates. |
116 | | - * |
117 | | - * @return Last check time in milliseconds. |
118 | | - * @since 2.0.0-SNAPSHOT |
| 124 | + * Get the last time UpdateLib checked for an update. |
| 125 | + * @return Last check time. |
119 | 126 | */ |
120 | 127 | public long getLastCheck() { |
121 | 128 | return lastCheck; |
122 | 129 | } |
123 | 130 |
|
124 | 131 | /** |
125 | | - * Get the current version of UpdateLib. |
126 | | - * |
127 | | - * @return Current version of UpdateLib. |
128 | | - * @since 2.1.0-SNAPSHOT |
| 132 | + * Get the http connection timeout. |
| 133 | + * @return Connection timeout. |
129 | 134 | */ |
130 | | - public static String getVersion() { |
131 | | - return VERSION; |
| 135 | + @Internal |
| 136 | + public int getTimeout() { |
| 137 | + return timeout; |
132 | 138 | } |
133 | 139 |
|
134 | 140 | } |
0 commit comments