Skip to content

Commit

Permalink
Use schem repository default branch
Browse files Browse the repository at this point in the history
  • Loading branch information
sbxte committed Jan 10, 2024
1 parent eb1aa16 commit 2a5a080
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions core/src/mindustry/client/ui/SchematicBrowserDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import arc.scene.ui.layout.*;
import arc.struct.*;
import arc.util.*;
import arc.util.serialization.*;
import mindustry.client.*;
import mindustry.client.communication.*;
import mindustry.client.navigation.*;
Expand Down Expand Up @@ -508,7 +509,7 @@ void loadRepositories(){
}
} catch (Throwable e) {
Log.err("Error parsing schematic " + link + " " + f.name(), e);
ui.showErrorMessage(Core.bundle.format("client.schematic.browser.fail.parse", link, f.name()));
// ui.showErrorMessage(Core.bundle.format("client.schematic.browser.fail.parse", link, f.name())); // FINISHME: Find a better way to do this, currently it spams the screen with error messages
}
});
schems.sort();
Expand All @@ -521,26 +522,33 @@ void fetch(Seq<String> repos){
Log.debug("Fetching schematics from repos: @", repos);
ui.showInfoFade("@client.schematic.browser.fetching", 2f);
for (String link : repos){
Http.get(ghApi + "/repos/" + link + "/zipball/main", res -> handleRedirect(link, res), e -> Core.app.post(() -> {
Log.info("Schematic repository " + link + " could not be reached. " + e);
ui.showErrorMessage(Core.bundle.format("client.schematic.browser.fail.fetch", link));
}));
Http.get(ghApi + "/repos/" + link, res -> handleBranch(link, res), e -> handleFetchError(link, e));
}
}

void handleRedirect(String link, Http.HttpResponse res){
if (res.getHeader("Location") != null) {
Http.get(res.getHeader("Location"), r -> handleRepo(link, r), e -> Core.app.post(() -> {
Log.info("Schematic repository " + link + " could not be reached. " + e);
ui.showErrorMessage(Core.bundle.format("client.schematic.browser.fail.fetch", link));
}));
} else handleRepo(link, res);
void handleFetchError(String link, Throwable e){
Core.app.post(() -> {
Log.err("Schematic repository " + link + " could not be reached. " + e);
ui.showErrorMessage(Core.bundle.format("client.schematic.browser.fail.fetch", link));
});
}

void handleBranch(String link, Http.HttpResponse response){
var json = new JsonReader().parse(response.getResultAsString());
var branch = json.getString("default_branch");
Http.get(ghApi + "/repos/" + link + "/zipball/" + branch, res -> handleRedirect(link, res), e -> handleFetchError(link, e));
}

void handleRedirect(String link, Http.HttpResponse response){
if (response.getHeader("Location") != null) {
Http.get(response.getHeader("Location"), r -> handleRepo(link, r), e -> handleFetchError(link, e));
} else handleRepo(link, response);
}

void handleRepo(String link, Http.HttpResponse res){
void handleRepo(String link, Http.HttpResponse response){
String fileName = link.replace("/","") + ".zip";
Fi filePath = schematicRepoDirectory.child(fileName);
filePath.writeBytes(res.getResult());
filePath.writeBytes(response.getResult());
Core.app.post(() ->{
unfetchedRepositories.remove(link);
unloadedRepositories.add(link);
Expand Down

0 comments on commit 2a5a080

Please sign in to comment.