-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathApolloVersionManager.java
More file actions
213 lines (181 loc) · 7.12 KB
/
Copy pathApolloVersionManager.java
File metadata and controls
213 lines (181 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* This file is part of Apollo, licensed under the MIT License.
*
* Copyright (c) 2023 Moonsworth
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.lunarclient.apollo.version;
import com.lunarclient.apollo.Apollo;
import com.lunarclient.apollo.ApolloManager;
import com.lunarclient.apollo.ApolloPlatform;
import com.lunarclient.apollo.api.request.DownloadFileRequest;
import com.lunarclient.apollo.api.request.VersionRequest;
import com.lunarclient.apollo.api.response.VersionResponse;
import com.lunarclient.apollo.option.Option;
import com.lunarclient.apollo.option.SimpleOption;
import io.leangen.geantyref.TypeToken;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.logging.Logger;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
/**
* Manages Apollo versioning.
*
* @since 1.0.0
*/
public final class ApolloVersionManager {
public static final SimpleOption<Boolean> SEND_UPDATE_MESSAGE = Option.<Boolean>builder()
.comment("Set to 'true' to send opped players available update message, otherwise 'false'.")
.node("send-updater-message").type(TypeToken.get(Boolean.class))
.defaultValue(true).build();
@Getter private VersionResponse updateAssets;
private final AtomicBoolean updated = new AtomicBoolean(false);
/**
* Constructs the {@link ApolloVersionManager}.
*
* @since 1.0.0
*/
public ApolloVersionManager() {
ApolloManager.registerOptions(ApolloVersionManager.SEND_UPDATE_MESSAGE);
}
/**
* Runs the update checker.
*
* @since 1.0.0
*/
public void checkForUpdates() {
ApolloPlatform platform = Apollo.getPlatform();
if (!platform.getOptions().get(ApolloVersionManager.SEND_UPDATE_MESSAGE)) {
return;
}
ApolloManager.getHttpManager().request(VersionRequest.builder().build())
.onSuccess(response -> {
String version = response.getVersion();
ApolloVersion currentVersion = new ApolloVersion(platform.getApolloVersion());
ApolloVersion latestVersion = new ApolloVersion(version);
if (!currentVersion.isUpdateAvailable(latestVersion)) {
return;
}
this.updateAssets = response;
Logger logger = platform.getPlatformLogger();
logger.warning(String.format("A new version of Apollo is available! Latest release: %s", version));
if (platform.getPlatform() != ApolloPlatform.Platform.MINESTOM) {
logger.warning("Please update by running \"/apollo update\" or by downloading the " +
"latest build from https://lunarclient.dev/apollo/downloads");
}
})
.onFailure(Throwable::printStackTrace);
}
/**
* Runs a force update.
*
* @param platform the platform to run the update on
* @param message the message to send to the command sender
* @since 1.0.9
*/
public void forceUpdate(ApolloPlatform.Platform platform, Consumer<Component> message) {
if (this.updated.get()) {
message.accept(Component.text(
"Apollo is already updated, please restart your server!",
NamedTextColor.RED)
);
return;
}
if (this.updateAssets == null) {
message.accept(Component.text(
"This server is already running the latest version of Apollo.",
NamedTextColor.RED)
);
return;
}
String downloadUrl = this.getPlatformUrl(platform);
if (downloadUrl == null) {
message.accept(Component.text(
"Unable to find assets to update from.",
NamedTextColor.RED)
);
return;
}
// Find the current running Apollo jar
URL url = Apollo.getPlatform().getPlugin()
.getClass()
.getProtectionDomain()
.getCodeSource()
.getLocation();
File file;
try {
file = new File(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
// Find name of the new Apollo jar
String[] urlArgs = downloadUrl.split("/");
String fileName = urlArgs[urlArgs.length - 1];
// Create a path for the downloaded Apollo jar
Path updatedJarPath = Paths.get(file.getParent() + File.separator + fileName);
DownloadFileRequest request = DownloadFileRequest.builder()
.url(downloadUrl)
.target(updatedJarPath)
.build();
ApolloManager.getHttpManager().download(request)
.onSuccess(response -> {
message.accept(Component.text(
"Successfully updated Apollo, please restart your server!",
NamedTextColor.RED)
);
// Delete old Apollo jar
file.deleteOnExit();
this.updated.set(true);
})
.onFailure(throwable -> {
message.accept(Component.text(
"Failed to update Apollo, please check your console for more information!",
NamedTextColor.RED)
);
throwable.printStackTrace();
});
}
private String getPlatformUrl(ApolloPlatform.Platform platform) {
VersionResponse.Assets assets = this.updateAssets.getAssets();
switch (platform) {
case BUKKIT: {
return assets.getBukkit();
}
case BUNGEE: {
return assets.getBungee();
}
case VELOCITY: {
return assets.getVelocity();
}
case FOLIA: {
return assets.getFolia();
}
}
return null;
}
}