[FEATURE] Performance: Skin/Cloak local load

This commit is contained in:
Gravita 2021-07-16 18:14:18 +07:00
parent fecc14010d
commit b0538abe63
3 changed files with 39 additions and 5 deletions

View file

@ -7,12 +7,17 @@
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID; import java.util.UUID;
public final class RequestTextureProvider extends TextureProvider { public final class RequestTextureProvider extends TextureProvider {
// Instance // Instance
private String skinURL; public String skinURL;
private String cloakURL; public String cloakURL;
public String skinLocalPath;
public String cloakLocalPath;
public RequestTextureProvider() { public RequestTextureProvider() {
} }
@ -30,6 +35,14 @@ private static Texture getTexture(String url, boolean cloak) throws IOException
} }
} }
private static Texture getTexture(String url, Path local, boolean cloak) throws IOException {
try {
return new Texture(url, local, cloak);
} catch (FileNotFoundException ignored) {
return null; // Simply not found
}
}
private static String getTextureURL(String url, UUID uuid, String username, String client) { private static String getTextureURL(String url, UUID uuid, String username, String client) {
return CommonHelper.replace(url, "username", IOHelper.urlEncode(username), return CommonHelper.replace(url, "username", IOHelper.urlEncode(username),
"uuid", IOHelper.urlEncode(uuid.toString()), "hash", IOHelper.urlEncode(Launcher.toHash(uuid)), "uuid", IOHelper.urlEncode(uuid.toString()), "hash", IOHelper.urlEncode(Launcher.toHash(uuid)),
@ -43,11 +56,21 @@ public void close() {
@Override @Override
public Texture getCloakTexture(UUID uuid, String username, String client) throws IOException { public Texture getCloakTexture(UUID uuid, String username, String client) throws IOException {
return getTexture(getTextureURL(cloakURL, uuid, username, client), true); String textureUrl = getTextureURL(cloakURL, uuid, username, client);
if (cloakLocalPath == null) {
return getTexture(textureUrl, true);
} else {
return getTexture(textureUrl, Paths.get(cloakLocalPath), true);
}
} }
@Override @Override
public Texture getSkinTexture(UUID uuid, String username, String client) throws IOException { public Texture getSkinTexture(UUID uuid, String username, String client) throws IOException {
return getTexture(getTextureURL(skinURL, uuid, username, client), false); String textureUrl = getTextureURL(skinURL, uuid, username, client);
if (skinLocalPath == null) {
return getTexture(textureUrl, false);
} else {
return getTexture(textureUrl, Paths.get(skinLocalPath), false);
}
} }
} }

View file

@ -17,6 +17,7 @@ public final class PlayerProfile extends StreamObject {
public final Texture skin, cloak; public final Texture skin, cloak;
@Deprecated
public PlayerProfile(HInput input) throws IOException { public PlayerProfile(HInput input) throws IOException {
uuid = input.readUUID(); uuid = input.readUUID();
username = VerifyHelper.verifyUsername(input.readString(64)); username = VerifyHelper.verifyUsername(input.readString(64));
@ -41,6 +42,7 @@ public static UUID offlineUUID(String username) {
} }
@Override @Override
@Deprecated
public void write(HOutput output) throws IOException { public void write(HOutput output) throws IOException {
output.writeUUID(uuid); output.writeUUID(uuid);
output.writeString(username, 64); output.writeString(username, 64);

View file

@ -10,6 +10,7 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.Path;
import java.util.Objects; import java.util.Objects;
public final class Texture extends StreamObject { public final class Texture extends StreamObject {
@ -22,12 +23,12 @@ public final class Texture extends StreamObject {
public final byte[] digest; public final byte[] digest;
@Deprecated
public Texture(HInput input) throws IOException { public Texture(HInput input) throws IOException {
url = IOHelper.verifyURL(input.readASCII(2048)); url = IOHelper.verifyURL(input.readASCII(2048));
digest = input.readByteArray(-DIGEST_ALGO.bytes); digest = input.readByteArray(-DIGEST_ALGO.bytes);
} }
public Texture(String url, boolean cloak) throws IOException { public Texture(String url, boolean cloak) throws IOException {
this.url = IOHelper.verifyURL(url); this.url = IOHelper.verifyURL(url);
@ -44,6 +45,14 @@ public Texture(String url, boolean cloak) throws IOException {
digest = SecurityHelper.digest(DIGEST_ALGO, new URL(url)); digest = SecurityHelper.digest(DIGEST_ALGO, new URL(url));
} }
public Texture(String url, Path local, boolean cloak) throws IOException {
this.url = IOHelper.verifyURL(url);
try (InputStream input = IOHelper.newInput(local)) {
IOHelper.readTexture(input, cloak); // Verify texture
}
this.digest = Objects.requireNonNull(SecurityHelper.digest(DIGEST_ALGO, local), "digest");
}
public Texture(String url, byte[] digest) { public Texture(String url, byte[] digest) {
this.url = IOHelper.verifyURL(url); this.url = IOHelper.verifyURL(url);