mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FEATURE] Новые фитчи MirrorManager
This commit is contained in:
parent
8e1000ec41
commit
81f50a57f3
4 changed files with 101 additions and 28 deletions
|
@ -30,7 +30,8 @@ public String getUsageDescription() {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(String... args) throws Exception {
|
public void invoke(String... args) throws Exception {
|
||||||
verifyArgs(args, 2);
|
verifyArgs(args, 2);
|
||||||
Version version = Version.byName(args[0]);
|
//Version version = Version.byName(args[0]);
|
||||||
|
String versionName = args[0];
|
||||||
String dirName = IOHelper.verifyFileName(args[1]);
|
String dirName = IOHelper.verifyFileName(args[1]);
|
||||||
Path assetDir = server.updatesDir.resolve(dirName);
|
Path assetDir = server.updatesDir.resolve(dirName);
|
||||||
|
|
||||||
|
@ -40,7 +41,8 @@ public void invoke(String... args) throws Exception {
|
||||||
|
|
||||||
// Download required asset
|
// Download required asset
|
||||||
LogHelper.subInfo("Downloading asset, it may take some time");
|
LogHelper.subInfo("Downloading asset, it may take some time");
|
||||||
HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getAssetsURL(version.name), assetDir);
|
//HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getAssetsURL(version.name), assetDir);
|
||||||
|
server.mirrorManager.downloadZip(assetDir,"assets/%s.zip", versionName);
|
||||||
|
|
||||||
// Finished
|
// Finished
|
||||||
server.syncUpdatesDir(Collections.singleton(dirName));
|
server.syncUpdatesDir(Collections.singleton(dirName));
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
import pro.gravit.launcher.Launcher;
|
import pro.gravit.launcher.Launcher;
|
||||||
import pro.gravit.launcher.profiles.ClientProfile;
|
import pro.gravit.launcher.profiles.ClientProfile;
|
||||||
import pro.gravit.launcher.profiles.ClientProfile.Version;
|
import pro.gravit.launcher.profiles.ClientProfile.Version;
|
||||||
|
@ -36,7 +37,8 @@ public String getUsageDescription() {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(String... args) throws IOException, CommandException {
|
public void invoke(String... args) throws IOException, CommandException {
|
||||||
verifyArgs(args, 2);
|
verifyArgs(args, 2);
|
||||||
Version version = Version.byName(args[0]);
|
//Version version = Version.byName(args[0]);
|
||||||
|
String versionName = args[0];
|
||||||
String dirName = IOHelper.verifyFileName(args[1]);
|
String dirName = IOHelper.verifyFileName(args[1]);
|
||||||
Path clientDir = server.updatesDir.resolve(args[1]);
|
Path clientDir = server.updatesDir.resolve(args[1]);
|
||||||
|
|
||||||
|
@ -46,14 +48,19 @@ public void invoke(String... args) throws IOException, CommandException {
|
||||||
|
|
||||||
// Download required client
|
// Download required client
|
||||||
LogHelper.subInfo("Downloading client, it may take some time");
|
LogHelper.subInfo("Downloading client, it may take some time");
|
||||||
HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getClientsURL(version.name), clientDir);
|
//HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getClientsURL(version.name), clientDir);
|
||||||
|
server.mirrorManager.downloadZip(clientDir,"clients/%s.zip", versionName);
|
||||||
|
|
||||||
// Create profile file
|
// Create profile file
|
||||||
LogHelper.subInfo("Creaing profile file: '%s'", dirName);
|
LogHelper.subInfo("Creaing profile file: '%s'", dirName);
|
||||||
ClientProfile client;
|
ClientProfile client;
|
||||||
String profilePath = String.format("pro/gravit/launchserver/defaults/profile%s.cfg", version.name);
|
String profilePath = String.format("pro/gravit/launchserver/defaults/profile%s.cfg", versionName);
|
||||||
try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL(profilePath))) {
|
try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL(profilePath))) {
|
||||||
client = Launcher.gsonManager.configGson.fromJson(reader, ClientProfile.class);
|
client = Launcher.gsonManager.configGson.fromJson(reader, ClientProfile.class);
|
||||||
|
} catch (IOException e)
|
||||||
|
{
|
||||||
|
JsonElement clientJson = server.mirrorManager.jsonRequest(null, "GET", "clients/%s.json", versionName);
|
||||||
|
client = Launcher.gsonManager.configGson.fromJson(clientJson, ClientProfile.class);
|
||||||
}
|
}
|
||||||
client.setTitle(dirName);
|
client.setTitle(dirName);
|
||||||
client.setDir(dirName);
|
client.setDir(dirName);
|
||||||
|
|
|
@ -1,33 +1,36 @@
|
||||||
package pro.gravit.launchserver.manangers;
|
package pro.gravit.launchserver.manangers;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import pro.gravit.utils.HTTPRequest;
|
||||||
|
import pro.gravit.utils.HttpDownloader;
|
||||||
import pro.gravit.utils.helper.IOHelper;
|
import pro.gravit.utils.helper.IOHelper;
|
||||||
|
import pro.gravit.utils.helper.LogHelper;
|
||||||
|
|
||||||
public class MirrorManager {
|
public class MirrorManager {
|
||||||
public class Mirror {
|
public static class Mirror {
|
||||||
URL url;
|
String baseUrl;
|
||||||
String assetsURLMask;
|
|
||||||
String clientsURLMask;
|
|
||||||
boolean enabled;
|
boolean enabled;
|
||||||
|
|
||||||
Mirror(String url) {
|
Mirror(String url) {
|
||||||
assetsURLMask = url.concat("assets/%s.zip");
|
//assetsURLMask = url.concat("assets/%s.zip");
|
||||||
clientsURLMask = url.concat("clients/%s.zip");
|
//clientsURLMask = url.concat("clients/%s.zip");
|
||||||
|
baseUrl = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
private URL formatArg(String mask, String arg) throws MalformedURLException {
|
private URL formatArgs(String mask, Object... args) throws MalformedURLException {
|
||||||
return new URL(String.format(mask, IOHelper.urlEncode(arg)));
|
Object[] data = Arrays.stream(args).map(e -> IOHelper.urlEncode(e.toString())).toArray();
|
||||||
|
return new URL(baseUrl.concat(String.format(mask, data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public URL getAssetsURL(String assets) throws MalformedURLException {
|
public URL getURL(String mask, Object... args) throws MalformedURLException {
|
||||||
return formatArg(assetsURLMask, assets);
|
return formatArgs(mask, args);
|
||||||
}
|
|
||||||
|
|
||||||
public URL getClientsURL(String client) throws MalformedURLException {
|
|
||||||
return formatArg(clientsURLMask, client);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,13 +41,14 @@ public void addMirror(String mirror) {
|
||||||
Mirror m = new Mirror(mirror);
|
Mirror m = new Mirror(mirror);
|
||||||
m.enabled = true;
|
m.enabled = true;
|
||||||
if (defaultMirror == null) defaultMirror = m;
|
if (defaultMirror == null) defaultMirror = m;
|
||||||
|
list.add(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMirror(String mirror, boolean enabled) throws MalformedURLException {
|
public void addMirror(String mirror, boolean enabled) throws MalformedURLException {
|
||||||
Mirror m = new Mirror(mirror);
|
Mirror m = new Mirror(mirror);
|
||||||
m.url = new URL(mirror);
|
|
||||||
m.enabled = enabled;
|
m.enabled = enabled;
|
||||||
if (defaultMirror == null && enabled) defaultMirror = m;
|
if (defaultMirror == null && enabled) defaultMirror = m;
|
||||||
|
list.add(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Mirror getDefaultMirror() {
|
public Mirror getDefaultMirror() {
|
||||||
|
@ -66,4 +70,61 @@ public void enableMirror(int index) {
|
||||||
public int size() {
|
public int size() {
|
||||||
return list.size();
|
return list.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean downloadZip(Mirror mirror, Path path, String mask, Object... args) throws IOException
|
||||||
|
{
|
||||||
|
if(!mirror.enabled) return false;
|
||||||
|
URL url = mirror.getURL(mask, args);
|
||||||
|
LogHelper.debug("Try download %s", url.toString());
|
||||||
|
try {
|
||||||
|
HttpDownloader.downloadZip(url, path);
|
||||||
|
} catch (IOException e)
|
||||||
|
{
|
||||||
|
LogHelper.error("Download %s failed(%s: %s)", url.toString(), e.getClass().getName(), e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void downloadZip(Path path, String mask, Object... args) throws IOException
|
||||||
|
{
|
||||||
|
if(downloadZip(defaultMirror, path, mask, args))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(Mirror mirror : list)
|
||||||
|
{
|
||||||
|
if(mirror != defaultMirror)
|
||||||
|
{
|
||||||
|
if(downloadZip(mirror, path, mask, args)) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IOException(String.format("Error download %s. All mirrors return error", path.toString()));
|
||||||
|
}
|
||||||
|
public JsonElement jsonRequest(Mirror mirror, JsonElement request, String method, String mask, Object... args) throws IOException
|
||||||
|
{
|
||||||
|
if(!mirror.enabled) return null;
|
||||||
|
URL url = mirror.getURL(mask, args);
|
||||||
|
try {
|
||||||
|
return HTTPRequest.jsonRequest(request, method, url);
|
||||||
|
} catch (IOException e)
|
||||||
|
{
|
||||||
|
LogHelper.error("JsonRequest %s failed(%s: %s)", url.toString(), e.getClass().getName(), e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public JsonElement jsonRequest(JsonElement request, String method, String mask, Object... args) throws IOException
|
||||||
|
{
|
||||||
|
JsonElement result = jsonRequest(defaultMirror, request, method, mask, args);
|
||||||
|
if(result != null) return result;
|
||||||
|
for(Mirror mirror : list)
|
||||||
|
{
|
||||||
|
if(mirror != defaultMirror)
|
||||||
|
{
|
||||||
|
result = jsonRequest(mirror, request, method, mask, args);
|
||||||
|
if(result != null) return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IOException("Error jsonRequest. All mirrors return error");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,19 +39,22 @@ public static int sendCrashreport(String strurl, String data) throws IOException
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException {
|
public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException {
|
||||||
|
return jsonRequest(request, "POST", url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonElement jsonRequest(JsonElement request, String method, URL url) throws IOException {
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setDoInput(true);
|
connection.setDoInput(true);
|
||||||
connection.setDoOutput(true);
|
if(request != null) connection.setDoOutput(true);
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod(method);
|
||||||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
if(request != null) connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||||||
connection.setRequestProperty("Accept", "application/json");
|
connection.setRequestProperty("Accept", "application/json");
|
||||||
if (TIMEOUT > 0)
|
if (TIMEOUT > 0)
|
||||||
connection.setConnectTimeout(TIMEOUT);
|
connection.setConnectTimeout(TIMEOUT);
|
||||||
|
if(request != null) try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8)) {
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
|
writer.write(request.toString());
|
||||||
writer.write(request.toString());
|
writer.flush();
|
||||||
writer.flush();
|
}
|
||||||
writer.close();
|
|
||||||
|
|
||||||
InputStreamReader reader;
|
InputStreamReader reader;
|
||||||
int statusCode = connection.getResponseCode();
|
int statusCode = connection.getResponseCode();
|
||||||
|
|
Loading…
Reference in a new issue