2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launcher.profiles;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.launcher.LauncherAPI;
|
|
|
|
import pro.gravit.launcher.serialize.HInput;
|
|
|
|
import pro.gravit.launcher.serialize.HOutput;
|
|
|
|
import pro.gravit.launcher.serialize.stream.StreamObject;
|
|
|
|
import pro.gravit.utils.helper.IOHelper;
|
|
|
|
import pro.gravit.utils.helper.SecurityHelper;
|
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public final class Texture extends StreamObject {
|
|
|
|
private static final SecurityHelper.DigestAlgorithm DIGEST_ALGO = SecurityHelper.DigestAlgorithm.SHA256;
|
|
|
|
|
|
|
|
// Instance
|
|
|
|
@LauncherAPI
|
|
|
|
public final String url;
|
|
|
|
@LauncherAPI
|
|
|
|
public final byte[] digest;
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public Texture(HInput input) throws IOException {
|
|
|
|
url = IOHelper.verifyURL(input.readASCII(2048));
|
|
|
|
digest = input.readByteArray(-DIGEST_ALGO.bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public Texture(String url, boolean cloak) throws IOException {
|
|
|
|
this.url = IOHelper.verifyURL(url);
|
|
|
|
|
|
|
|
// Fetch texture
|
|
|
|
byte[] texture;
|
|
|
|
try (InputStream input = IOHelper.newInput(new URL(url))) {
|
|
|
|
texture = IOHelper.read(input);
|
|
|
|
}
|
|
|
|
try (ByteArrayInputStream input = new ByteArrayInputStream(texture)) {
|
|
|
|
IOHelper.readTexture(input, cloak); // Verify texture
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get digest of texture
|
|
|
|
digest = SecurityHelper.digest(DIGEST_ALGO, new URL(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public Texture(String url, byte[] digest) {
|
|
|
|
this.url = IOHelper.verifyURL(url);
|
|
|
|
this.digest = Objects.requireNonNull(digest, "digest");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(HOutput output) throws IOException {
|
|
|
|
output.writeASCII(url, 2048);
|
|
|
|
output.writeByteArray(digest, -DIGEST_ALGO.bytes);
|
|
|
|
}
|
|
|
|
}
|