arc.util.Http migration

This commit is contained in:
Anuken
2021-07-07 19:00:32 -04:00
parent 02b42c51da
commit 2ae7863cae
7 changed files with 112 additions and 174 deletions

View File

@@ -1,7 +1,6 @@
package mindustry.net; package mindustry.net;
import arc.*; import arc.*;
import arc.Net.*;
import arc.files.*; import arc.files.*;
import arc.func.*; import arc.func.*;
import arc.util.*; import arc.util.*;
@@ -62,8 +61,9 @@ public class BeControl{
/** asynchronously checks for updates. */ /** asynchronously checks for updates. */
public void checkUpdate(Boolc done){ public void checkUpdate(Boolc done){
Core.net.httpGet("https://api.github.com/repos/Anuken/MindustryBuilds/releases/latest", res -> { Http.get("https://api.github.com/repos/Anuken/MindustryBuilds/releases/latest")
if(res.getStatus() == HttpStatus.OK){ .error(e -> {}) //ignore errors
.submit(res -> {
Jval val = Jval.read(res.getResultAsString()); Jval val = Jval.read(res.getResultAsString());
int newBuild = Strings.parseInt(val.getString("tag_name", "0")); int newBuild = Strings.parseInt(val.getString("tag_name", "0"));
if(newBuild > Version.build){ if(newBuild > Version.build){
@@ -79,10 +79,7 @@ public class BeControl{
}else{ }else{
Core.app.post(() -> done.get(false)); Core.app.post(() -> done.get(false));
} }
}else{ });
Core.app.post(() -> done.get(false));
}
}, error -> {}); //ignore errors
} }
/** @return whether a new update is available */ /** @return whether a new update is available */

View File

@@ -1,7 +1,6 @@
package mindustry.net; package mindustry.net;
import arc.*; import arc.*;
import arc.Net.*;
import arc.files.*; import arc.files.*;
import arc.func.*; import arc.func.*;
import arc.struct.*; import arc.struct.*;
@@ -150,12 +149,12 @@ public class CrashSender{
Log.info("Sending crash report."); Log.info("Sending crash report.");
//post to crash report URL, exit code indicates send success //post to crash report URL, exit code indicates send success
new arc.Net().http(new HttpRequest().block(true).method(HttpMethod.POST).content(value.toJson(OutputType.json)).url(Vars.crashReportURL), r -> { Http.post(Vars.crashReportURL, value.toJson(OutputType.json)).error(t -> {
Log.info("Crash sent successfully.");
System.exit(1);
}, t -> {
Log.info("Crash report not sent."); Log.info("Crash report not sent.");
System.exit(-1); System.exit(-1);
}).block(r -> {
Log.info("Crash sent successfully.");
System.exit(1);
}); });
ret(); ret();

View File

@@ -1,7 +1,6 @@
package mindustry.ui.dialogs; package mindustry.ui.dialogs;
import arc.*; import arc.*;
import arc.Net.*;
import arc.graphics.*; import arc.graphics.*;
import arc.input.*; import arc.input.*;
import arc.math.*; import arc.math.*;
@@ -533,17 +532,11 @@ public class JoinDialog extends BaseDialog{
Log.info("Fetching community servers at @", url); Log.info("Fetching community servers at @", url);
//get servers //get servers
Core.net.httpGet(url, result -> { Http.get(url)
try{ .error(t -> Log.err("Failed to fetch community servers", t))
if(result.getStatus() != HttpStatus.OK){ .submit(result -> {
Log.warn("Failed to fetch community servers: @", result.getStatus());
return;
}
Jval val = Jval.read(result.getResultAsString()); Jval val = Jval.read(result.getResultAsString());
Core.app.post(() -> { Seq<ServerGroup> servers = new Seq<>();
try{
defaultServers.clear();
val.asArray().each(child -> { val.asArray().each(child -> {
String name = child.getString("name", ""); String name = child.getString("name", "");
String[] addresses; String[] addresses;
@@ -552,19 +545,14 @@ public class JoinDialog extends BaseDialog{
}else{ }else{
addresses = new String[]{child.getString("address", "<invalid>")}; addresses = new String[]{child.getString("address", "<invalid>")};
} }
defaultServers.add(new ServerGroup(name, addresses)); servers.add(new ServerGroup(name, addresses));
}); });
//modify default servers on main thread
Core.app.post(() -> {
defaultServers.addAll(servers);
Log.info("Fetched @ community servers.", defaultServers.size); Log.info("Fetched @ community servers.", defaultServers.size);
}catch(Throwable e){
Log.err("Failed to parse community servers.");
Log.err(e);
}
}); });
}catch(Throwable e){ });
Log.err("Failed to fetch community servers.");
Log.err(e);
}
}, Log::err);
} }
private void saveServers(){ private void saveServers(){

View File

@@ -1,7 +1,7 @@
package mindustry.ui.dialogs; package mindustry.ui.dialogs;
import arc.*; import arc.*;
import arc.Net.*; import arc.util.Http.*;
import arc.files.*; import arc.files.*;
import arc.func.*; import arc.func.*;
import arc.graphics.*; import arc.graphics.*;
@@ -104,6 +104,8 @@ public class ModsDialog extends BaseDialog{
if(Strings.getCauses(error).contains(t -> t.getMessage() != null && (t.getMessage().contains("trust anchor") || t.getMessage().contains("SSL") || t.getMessage().contains("protocol")))){ if(Strings.getCauses(error).contains(t -> t.getMessage() != null && (t.getMessage().contains("trust anchor") || t.getMessage().contains("SSL") || t.getMessage().contains("protocol")))){
ui.showErrorMessage("@feature.unsupported"); ui.showErrorMessage("@feature.unsupported");
}else if(error instanceof HttpStatusException st){
ui.showErrorMessage(Core.bundle.format("connectfail", Strings.capitalize(st.status.toString().toLowerCase())));
}else{ }else{
ui.showException(error); ui.showException(error);
} }
@@ -111,14 +113,10 @@ public class ModsDialog extends BaseDialog{
void getModList(Cons<Seq<ModListing>> listener){ void getModList(Cons<Seq<ModListing>> listener){
if(modList == null){ if(modList == null){
Core.net.httpGet("https://raw.githubusercontent.com/Anuken/MindustryMods/master/mods.json", response -> { Http.get("https://raw.githubusercontent.com/Anuken/MindustryMods/master/mods.json", response -> {
String strResult = response.getResultAsString(); String strResult = response.getResultAsString();
var status = response.getStatus();
Core.app.post(() -> { Core.app.post(() -> {
if(status != HttpStatus.OK){
ui.showErrorMessage(Core.bundle.format("connectfail", status));
}else{
try{ try{
modList = JsonIO.json.fromJson(Seq.class, ModListing.class, strResult); modList = JsonIO.json.fromJson(Seq.class, ModListing.class, strResult);
@@ -137,8 +135,6 @@ public class ModsDialog extends BaseDialog{
e.printStackTrace(); e.printStackTrace();
ui.showException(e); ui.showException(e);
} }
}
}); });
}, error -> Core.app.post(() -> modError(error))); }, error -> Core.app.post(() -> modError(error)));
}else{ }else{
@@ -424,8 +420,7 @@ public class ModsDialog extends BaseDialog{
//textures are only requested when the rendering happens; this assists with culling //textures are only requested when the rendering happens; this assists with culling
if(!textureCache.containsKey(repo)){ if(!textureCache.containsKey(repo)){
textureCache.put(repo, last = Core.atlas.find("nomap")); textureCache.put(repo, last = Core.atlas.find("nomap"));
Core.net.httpGet("https://raw.githubusercontent.com/Anuken/MindustryMods/master/icons/" + repo.replace("/", "_"), res -> { Http.get("https://raw.githubusercontent.com/Anuken/MindustryMods/master/icons/" + repo.replace("/", "_"), res -> {
if(res.getStatus() == HttpStatus.OK){
Pixmap pix = new Pixmap(res.getResult()); Pixmap pix = new Pixmap(res.getResult());
Core.app.post(() -> { Core.app.post(() -> {
try{ try{
@@ -437,7 +432,6 @@ public class ModsDialog extends BaseDialog{
Log.err(e); Log.err(e);
} }
}); });
}
}, err -> {}); }, err -> {});
} }
@@ -522,8 +516,7 @@ public class ModsDialog extends BaseDialog{
githubImportJavaMod(repo); githubImportJavaMod(repo);
}else{ }else{
ui.loadfrag.show(); ui.loadfrag.show();
Core.net.httpGet(ghApi + "/repos/" + repo, res -> { Http.get(ghApi + "/repos/" + repo, res -> {
if(checkError(res)){
var json = Jval.read(res.getResultAsString()); var json = Jval.read(res.getResultAsString());
String mainBranch = json.getString("default_branch"); String mainBranch = json.getString("default_branch");
String language = json.getString("language", "<none>"); String language = json.getString("language", "<none>");
@@ -533,8 +526,7 @@ public class ModsDialog extends BaseDialog{
if(language.equals("Java") || language.equals("Kotlin")){ if(language.equals("Java") || language.equals("Kotlin")){
githubImportJavaMod(repo); githubImportJavaMod(repo);
}else{ }else{
githubImportBranch(mainBranch, repo, this::showStatus); githubImportBranch(mainBranch, repo);
}
} }
}, this::importFail); }, this::importFail);
} }
@@ -542,8 +534,7 @@ public class ModsDialog extends BaseDialog{
private void githubImportJavaMod(String repo){ private void githubImportJavaMod(String repo){
//grab latest release //grab latest release
Core.net.httpGet(ghApi + "/repos/" + repo + "/releases/latest", res -> { Http.get(ghApi + "/repos/" + repo + "/releases/latest", res -> {
if(checkError(res)){
var json = Jval.read(res.getResultAsString()); var json = Jval.read(res.getResultAsString());
var assets = json.get("assets").asArray(); var assets = json.get("assets").asArray();
@@ -554,51 +545,23 @@ public class ModsDialog extends BaseDialog{
if(asset != null){ if(asset != null){
//grab actual file //grab actual file
var url = asset.getString("browser_download_url"); var url = asset.getString("browser_download_url");
Core.net.httpGet(url, result -> {
if(checkError(result)){ Http.get(url, result -> handleMod(repo, result), this::importFail);
handleMod(repo, result);
}
}, this::importFail);
}else{ }else{
throw new ArcRuntimeException("No JAR file found in releases. Make sure you have a valid jar file in the mod's latest Github Release."); throw new ArcRuntimeException("No JAR file found in releases. Make sure you have a valid jar file in the mod's latest Github Release.");
} }
}
}, this::importFail); }, this::importFail);
} }
private boolean checkError(HttpResponse res){ private void githubImportBranch(String branch, String repo){
if(res.getStatus() == HttpStatus.OK){ Http.get(ghApi + "/repos/" + repo + "/zipball/" + branch, loc -> {
return true;
}else{
showStatus(res.getStatus());
return false;
}
}
private void showStatus(HttpStatus status){
Core.app.post(() -> {
ui.showErrorMessage(Core.bundle.format("connectfail", Strings.capitalize(status.toString().toLowerCase())));
ui.loadfrag.hide();
});
}
private void githubImportBranch(String branch, String repo, Cons<HttpStatus> err){
Core.net.httpGet(ghApi + "/repos/" + repo + "/zipball/" + branch, loc -> {
if(loc.getStatus() == HttpStatus.OK){
if(loc.getHeader("Location") != null){ if(loc.getHeader("Location") != null){
Core.net.httpGet(loc.getHeader("Location"), result -> { Http.get(loc.getHeader("Location"), result -> {
if(result.getStatus() != HttpStatus.OK){
err.get(result.getStatus());
}else{
handleMod(repo, result); handleMod(repo, result);
}
}, this::importFail); }, this::importFail);
}else{ }else{
handleMod(repo, loc); handleMod(repo, loc);
} }
}else{
err.get(loc.getStatus());
}
}, this::importFail); }, this::importFail);
} }
} }

View File

@@ -10,4 +10,4 @@ kapt.include.compile.classpath=false
kotlin.stdlib.default.dependency=false kotlin.stdlib.default.dependency=false
#needed for android compilation #needed for android compilation
android.useAndroidX=true android.useAndroidX=true
archash=33c4976f8084fbb6fc26cfdcca2dda3442711d17 archash=3db4ede06f14022cb9e709218271e8e3df33a587

View File

@@ -1,5 +1,4 @@
import arc.*; import arc.util.*;
import arc.Net.*;
import arc.util.io.*; import arc.util.io.*;
import mindustry.*; import mindustry.*;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
@@ -14,13 +13,13 @@ public class GenericModTest{
static void grabMod(String url){ static void grabMod(String url){
//clear older mods //clear older mods
ApplicationTests.testDataFolder.deleteDirectory(); ApplicationTests.testDataFolder.deleteDirectory();
new Net().http(new HttpRequest().block(true).url(url).method(HttpMethod.GET), httpResponse -> { Http.get(url).error(Assertions::fail).block(httpResponse -> {
try{ try{
ApplicationTests.testDataFolder.child("mods").child("test_mod." + (url.endsWith("jar") ? "jar" : "zip")).writeBytes(Streams.copyBytes(httpResponse.getResultAsStream())); ApplicationTests.testDataFolder.child("mods").child("test_mod." + (url.endsWith("jar") ? "jar" : "zip")).writeBytes(Streams.copyBytes(httpResponse.getResultAsStream()));
}catch(IOException e){ }catch(IOException e){
Assertions.fail(e); Assertions.fail(e);
} }
}, Assertions::fail); });
ApplicationTests.launchApplication(false); ApplicationTests.launchApplication(false);
} }

View File

@@ -1,13 +1,9 @@
package mindustry.tools; package mindustry.tools;
import arc.*;
import arc.Net.*;
import arc.files.*; import arc.files.*;
import arc.util.*; import arc.util.*;
import arc.util.io.*; import arc.util.io.*;
import java.io.*;
/* icon font pipeline: /* icon font pipeline:
1. take set of pre-defined icons and SVGs 1. take set of pre-defined icons and SVGs
2. use Fontello API to get a font with these 2. use Fontello API to get a font with these
@@ -18,7 +14,6 @@ public class FontGenerator{
//E000 to F8FF //E000 to F8FF
public static void main(String[] args){ public static void main(String[] args){
Net net = Core.net = new Net();
Fi folder = Fi.get("core/assets-raw/fontgen/out/"); Fi folder = Fi.get("core/assets-raw/fontgen/out/");
folder.mkdirs(); folder.mkdirs();
@@ -29,13 +24,10 @@ public class FontGenerator{
Log.info("Zip..."); Log.info("Zip...");
String session = folder.child("session").readString(); String session = folder.child("session").readString();
net.http(new HttpRequest().method(HttpMethod.GET).url("https://fontello.com/" + session + "/get").block(true), result -> {
try{ Http.get("https://fontello.com/" + session + "/get").block(result -> {
Streams.copy(result.getResultAsStream(), folder.child("font.zip").write()); Streams.copy(result.getResultAsStream(), folder.child("font.zip").write());
}catch(IOException e){ });
throw new RuntimeException(e);
}
}, Log::err);
Log.info("Icon font..."); Log.info("Icon font...");