double choice = r.nextDouble();
try { // Super catch-all to ensure the launcher always renders
try {
// Fetch the percentage json first
String json = IOUtils.toString(new URL(Locations.masterRepo + "/FTB2/static/balance.json"));
JsonElement element = new JsonParser().parse(json);
if (element != null && element.isJsonObject()) {
JsonObject jso = element.getAsJsonObject();
if (jso != null && jso.get("minUsableLauncherVersion") != null) {
LaunchFrame.getInstance().minUsable = jso.get("minUsableLauncherVersion").getAsInt();
}
if (jso != null && jso.get("chEnabled") != null) {
Locations.chEnabled = jso.get("chEnabled").getAsBoolean();
}
if (jso != null && jso.get("repoSplitCurse") != null) {
JsonElement e = jso.get("repoSplitCurse");
Logger.logDebug("Balance Settings: " + e.getAsDouble() + " > " + choice);
if (e != null && e.getAsDouble() > choice) {
Logger.logInfo("Balance has selected Automatic:CurseCDN");
} else {
Logger.logInfo("Balance has selected Automatic:CreeperRepo");
Locations.masterRepoNoHTTP = Locations.chRepo.replaceAll("http://", "");
Locations.masterRepo = Locations.chRepo;
Locations.primaryCH = true;
downloadServers.remove("Automatic");
downloadServers.put("Automatic", Locations.masterRepoNoHTTP);
}
}
}
Benchmark.logBenchAs("DlUtils", "Download Utils Bal");
if (Locations.chEnabled) {
// Fetch servers from creeperhost using edges.json first
parseJSONtoMap(new URL(Locations.chRepo + "/edges.json"), "CH", downloadServers, false, "edges.json");
Benchmark.logBenchAs("DlUtils", "Download Utils CH");
}
// Fetch servers list from curse using edges.json second
parseJSONtoMap(new URL(Locations.curseRepo + "/edges.json"), "Curse", downloadServers, false, "edges.json");
Benchmark.logBenchAs("DlUtils", "Download Utils Curse");
} catch (IOException e) {
int i = 10;
// If fetching edges.json failed, assume new. is inaccessible
// Try alternate mirrors from the cached server list in resources
downloadServers.clear();
Logger.logInfo("Primary mirror failed, Trying alternative mirrors");
parseJSONtoMap(this.getClass().getResource("/edges.json"), "Backup", downloadServers, true, "edges.json");
}
if (downloadServers.size() == 0) {
Logger.logError("Could not find any working mirrors! If you are running a software firewall please allow the FTB Launcher permission to use the internet.");
// Fall back to new. (old system) on critical failure
downloadServers.put("Automatic", Locations.masterRepoNoHTTP);
} else if (!downloadServers.containsKey("Automatic")) {
// Use a random server from edges.json as the Automatic server
int index = (int) (Math.random() * downloadServers.size());
List<String> keys = Lists.newArrayList(downloadServers.keySet());
String defaultServer = downloadServers.get(keys.get(index));
downloadServers.put("Automatic", defaultServer);
Logger.logInfo("Selected " + keys.get(index) + " mirror for Automatic assignment");
}
} catch (Exception e) {
Logger.logError("Error while selecting server", e);
downloadServers.clear();
downloadServers.put("Automatic", Locations.masterRepoNoHTTP);
}
Locations.serversLoaded = true;
// This line absolutely must be hit, or the console will not be shown
// and the user/we will not even know why an error has occurred.
Logger.logInfo("DL ready");
String selectedMirror = Settings.getSettings().getDownloadServer();
String selectedHost = downloadServers.get(selectedMirror);
String resolvedIP = "UNKNOWN";
String resolvedHost = "UNKNOWN";
String resolvedMirror = "UNKNOWN";
try {
InetAddress ipAddress = InetAddress.getByName(selectedHost);
resolvedIP = ipAddress.getHostAddress();
} catch (UnknownHostException e) {
Logger.logError("Failed to resolve selected mirror: " + e.getMessage());
}
try {
for (String key : downloadServers.keySet()) {
if (key.equals("Automatic")) {
continue;
}
InetAddress host = InetAddress.getByName(downloadServers.get(key));
if (resolvedIP.equalsIgnoreCase(host.getHostAddress())) {
resolvedMirror = key;
resolvedHost = downloadServers.get(key);
break;
}
}
} catch (UnknownHostException e) {
Logger.logError("Failed to resolve mirror: " + e.getMessage());
}
Logger.logInfo("Using download server " + selectedMirror + ":" + resolvedMirror + " on host " + resolvedHost + " (" + resolvedIP + ")");
Benchmark.logBenchAs("DlUtils", "Download Utils Init");
}