2022-12-23 09:35:29 +00:00
|
|
|
package org.prismlauncher.fix.online;
|
2022-11-11 10:32:54 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2022-11-12 14:48:42 +00:00
|
|
|
import org.prismlauncher.utils.Base64;
|
2022-11-11 10:32:54 +00:00
|
|
|
import org.prismlauncher.utils.JsonParser;
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2022-12-23 09:35:29 +00:00
|
|
|
final class SkinFix {
|
2022-11-11 10:32:54 +00:00
|
|
|
|
2022-12-23 09:35:29 +00:00
|
|
|
static URL redirect(URL address) throws IOException {
|
2022-11-11 10:32:54 +00:00
|
|
|
String skinOwner = findSkinOwner(address);
|
|
|
|
if (skinOwner != null)
|
2022-11-20 09:16:30 +00:00
|
|
|
return convert(skinOwner, "SKIN");
|
2022-11-11 10:32:54 +00:00
|
|
|
|
|
|
|
String capeOwner = findCapeOwner(address);
|
|
|
|
if (capeOwner != null)
|
2022-11-20 09:16:30 +00:00
|
|
|
return convert(capeOwner, "CAPE");
|
2022-11-11 10:32:54 +00:00
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2022-12-23 09:35:29 +00:00
|
|
|
private static URL convert(String owner, String name) throws IOException {
|
2022-11-11 10:32:54 +00:00
|
|
|
Map<String, Object> textures = getTextures(owner);
|
|
|
|
|
|
|
|
if (textures != null) {
|
2022-11-20 09:16:30 +00:00
|
|
|
textures = (Map<String, Object>) textures.get(name);
|
2022-11-11 10:32:54 +00:00
|
|
|
if (textures == null)
|
2022-11-20 09:16:30 +00:00
|
|
|
return null;
|
2022-11-11 10:32:54 +00:00
|
|
|
|
|
|
|
return new URL((String) textures.get("url"));
|
|
|
|
}
|
|
|
|
|
2022-11-20 09:16:30 +00:00
|
|
|
return null;
|
2022-11-11 10:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Map<String, Object> getTextures(String owner) throws IOException {
|
|
|
|
try (InputStream in = new URL("https://api.mojang.com/users/profiles/minecraft/" + owner).openStream()) {
|
|
|
|
Map<String, Object> map = (Map<String, Object>) JsonParser.parse(in);
|
|
|
|
String id = (String) map.get("id");
|
|
|
|
|
|
|
|
try (InputStream profileIn = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + id)
|
|
|
|
.openStream()) {
|
|
|
|
Map<String, Object> profile = (Map<String, Object>) JsonParser.parse(profileIn);
|
|
|
|
|
|
|
|
for (Map<String, Object> property : (Iterable<Map<String, Object>>) profile.get("properties")) {
|
|
|
|
if (property.get("name").equals("textures")) {
|
|
|
|
Map<String, Object> result = (Map<String, Object>) JsonParser
|
2022-11-12 14:48:42 +00:00
|
|
|
.parse(new String(Base64.decode((String) property.get("value"))));
|
2022-11-11 10:32:54 +00:00
|
|
|
result = (Map<String, Object>) result.get("textures");
|
2022-11-20 09:16:30 +00:00
|
|
|
|
2022-11-11 10:32:54 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String findSkinOwner(URL address) {
|
|
|
|
switch (address.getHost()) {
|
|
|
|
case "www.minecraft.net":
|
2022-11-20 09:16:30 +00:00
|
|
|
return stripIfPrefixed(address.getPath(), "/skin/");
|
2022-11-11 10:32:54 +00:00
|
|
|
|
|
|
|
case "s3.amazonaws.com":
|
|
|
|
case "skins.minecraft.net":
|
2022-11-20 09:16:30 +00:00
|
|
|
return stripIfPrefixed(address.getPath(), "/MinecraftSkins/");
|
2022-11-11 10:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String findCapeOwner(URL address) {
|
|
|
|
switch (address.getHost()) {
|
|
|
|
case "www.minecraft.net":
|
2022-11-20 09:16:30 +00:00
|
|
|
return stripIfPrefixed(address.getQuery(), "user=");
|
2022-11-11 10:32:54 +00:00
|
|
|
|
|
|
|
case "s3.amazonaws.com":
|
|
|
|
case "skins.minecraft.net":
|
2022-11-20 09:16:30 +00:00
|
|
|
return stripIfPrefixed(address.getPath(), "/MinecraftCloaks/");
|
2022-11-11 10:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-11-20 09:16:30 +00:00
|
|
|
private static String stripIfPrefixed(String string, String prefix) {
|
|
|
|
if (string != null && string.startsWith(prefix)) {
|
|
|
|
string = string.substring(prefix.length());
|
2022-11-11 10:32:54 +00:00
|
|
|
|
2022-11-20 09:16:30 +00:00
|
|
|
if (string.endsWith(".png"))
|
|
|
|
string = string.substring(0, string.lastIndexOf('.'));
|
2022-11-11 10:32:54 +00:00
|
|
|
|
2022-11-20 09:16:30 +00:00
|
|
|
return string;
|
|
|
|
}
|
2022-11-11 10:32:54 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|