Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.util.CommonColors;
import net.minecraft.util.Util;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpRequest;
import java.time.Duration;
import java.util.*;
import java.util.function.Function;

import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -151,7 +149,7 @@ public void uploadSkin(SkinUpload upload) throws IOException, AuthenticationExce
MoreHttpResponses.execute(FileTypes.multiPart()
.field("type", fileUpload.type().getParameterizedName())
.field("file", fileUpload.file())
.field("meta", new Gson().toJson(addChecksum(fileUpload.metadata(), fileUpload.file().toUri())))
.field("meta", new Gson().toJson(fileUpload.metadata()))
.build(builder.newHttpBuilder(buildBackendUri("textures"))::PUT)
.build())
.requireOk();
Expand All @@ -160,23 +158,14 @@ public void uploadSkin(SkinUpload upload) throws IOException, AuthenticationExce
.POST(FileTypes.json(Map.of(
"type", uriUpload.type().getParameterizedName(),
"file", uriUpload.uri().toString(),
"meta", addChecksum(uriUpload.metadata(), uriUpload.uri())
"meta", uriUpload.metadata()
)))
.header(FileTypes.HEADER_CONTENT_TYPE, FileTypes.APPLICATION_JSON)
.build())
.requireOk();
});
}

private Map<String, String> addChecksum(Map<String, String> metadata, URI uri) throws IOException {
if (metadata.containsKey("checksum")) {
return metadata;
}
metadata = new HashMap<>(metadata);
metadata.put("checksum", URIUtil.getChecksum(uri));
return Map.copyOf(metadata);
}

@Override
public void authorize(Session session) throws IOException, AuthenticationException {
if (accessToken != null) {
Expand All @@ -203,19 +192,10 @@ public Optional<SkinServerProfile<?>> loadProfile(Session session) throws IOExce
return doAuthorizedRequest(session, (builder) -> MoreHttpResponses.execute(builder.newHttpBuilder(buildBackendUri("history"))
.GET()
.build()).accept(r -> r.json(Textures.class, "Server sent invalid profile response")).map(p -> {
Function<SkinType, List<Texture>> textures = Util.memoize(type -> {
Set<String> visited = new HashSet<>();
return p.textures().getOrDefault(type, List.of())
.stream()
.filter(texture -> texture.metadata().containsKey("checksum"))
.sorted(Comparator.comparing(t -> -t.startTime))
.filter(texture -> visited.add(texture.metadata().get("checksum") + texture.getModel()))
.toList();
});
return new SkinServerProfile<Texture>() {
@Override
public List<Texture> getSkins(SkinType type) {
return textures.apply(type);
return p.textures().getOrDefault(type, List.of());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ String toURIComponent() {
}
}

static String getChecksum(URI uri) throws IOException {
return Hashing.farmHashFingerprint64().hashBytes(getBytes(uri)).toString();
}

static byte[] getBytes(URI uri) throws IOException {
if ("file".equalsIgnoreCase(uri.getScheme())) {
return Files.readAllBytes(Path.of(uri));
Expand Down
Loading