Files

107 lines
3.4 KiB
Java
Raw Permalink Normal View History

2016-07-31 22:00:46 +08:00
package customskinloader.utils;
import java.io.File;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import customskinloader.CustomSkinLoader;
public class HttpTextureUtil {
2022-07-28 10:59:12 +08:00
public static class HttpTextureInfo {
public String url = "";
2018-02-12 12:20:28 +08:00
public File cacheFile;
public String hash;
}
2022-07-28 10:59:12 +08:00
private static final String
LEGACY_MARK = "(LEGACY)",
LOCAL_MARK = "(LOCAL)",
LOCAL_LEGACY_MARK = "(LOCAL_LEGACY)",
BASE64_MARK = "(BASE64)";
2018-02-12 12:20:28 +08:00
public static File defaultCacheDir;
2022-07-28 10:59:12 +08:00
2018-06-25 00:34:32 +08:00
@SuppressWarnings("ResultOfMethodCallIgnored")
2022-07-28 10:59:12 +08:00
public static void cleanCacheDir() {
if (defaultCacheDir != null) {
2018-02-12 12:20:28 +08:00
defaultCacheDir.delete();
defaultCacheDir.mkdirs();
}
}
2022-07-28 10:59:12 +08:00
public static File getCacheDir() {
return defaultCacheDir == null ? new File(MinecraftUtil.getMinecraftDataDir(), "assets/skins") : defaultCacheDir;
2018-02-12 12:20:28 +08:00
}
2022-07-28 10:59:12 +08:00
public static HttpTextureInfo toHttpTextureInfo(String fakeUrl) {
HttpTextureInfo info = new HttpTextureInfo();
if (fakeUrl.startsWith("http")) {
info.url = fakeUrl;
info.hash = FilenameUtils.getBaseName(fakeUrl);
info.cacheFile = getCacheFile(info.hash);
2016-07-31 22:00:46 +08:00
return info;
2018-02-12 12:20:28 +08:00
}
2022-07-28 10:59:12 +08:00
if (fakeUrl.startsWith(LOCAL_LEGACY_MARK)) {
fakeUrl = fakeUrl.replace(LOCAL_LEGACY_MARK, "");
String[] t = fakeUrl.split(",", 2);
if (t.length != 2)
2018-02-12 12:20:28 +08:00
return info;
2022-07-28 10:59:12 +08:00
info.cacheFile = new File(CustomSkinLoader.DATA_DIR, t[1]);
info.hash = t[0];
2018-02-12 12:20:28 +08:00
return info;
}
2022-07-28 10:59:12 +08:00
if (fakeUrl.startsWith(LOCAL_MARK)) {
fakeUrl = fakeUrl.replace(LOCAL_MARK, "");
info.cacheFile = new File(CustomSkinLoader.DATA_DIR, fakeUrl);
info.hash = FilenameUtils.getBaseName(fakeUrl);
2018-02-12 12:20:28 +08:00
return info;
}
2022-07-28 10:59:12 +08:00
if (fakeUrl.startsWith(LEGACY_MARK)) {
fakeUrl = fakeUrl.replace(LEGACY_MARK, "");
info.url = fakeUrl;
info.hash = DigestUtils.sha1Hex(info.url);
info.cacheFile = HttpRequestUtil.getCacheFile(info.hash);
return info;
}
//Base64 fake url format: `(BASE64)${hash}`
if (fakeUrl.startsWith(BASE64_MARK)) {
fakeUrl = fakeUrl.replace(BASE64_MARK, "");
info.hash = fakeUrl;
2022-07-29 02:15:19 +08:00
info.cacheFile = getCacheFile(info.hash);
2018-02-12 12:20:28 +08:00
return info;
}
return info;
}
2018-07-12 15:18:13 +08:00
2022-07-28 10:59:12 +08:00
public static String getLegacyFakeUrl(String url) {
return LEGACY_MARK + url;
2018-02-12 12:20:28 +08:00
}
2022-07-28 10:59:12 +08:00
public static String getLocalFakeUrl(String path) {
return LOCAL_MARK + path;
2018-02-12 12:20:28 +08:00
}
2022-07-28 10:59:12 +08:00
public static String getLocalLegacyFakeUrl(String path, String hash) {
return LOCAL_LEGACY_MARK + hash + "," + path;
2018-02-12 12:20:28 +08:00
}
2022-07-28 10:59:12 +08:00
public static String getBase64FakeUrl(String hash) {
return BASE64_MARK + hash;
}
public static String getHash(String url, long size, long lastModified) {
return DigestUtils.sha1Hex(size + url + lastModified);
}
2022-07-29 02:15:19 +08:00
public static String getHash(byte[] bytes) {
return DigestUtils.sha256Hex(bytes);
2022-07-28 10:59:12 +08:00
}
public static File getCacheFile(String hash) {
return getCacheFile(defaultCacheDir, hash);
2018-02-12 12:20:28 +08:00
}
2022-07-28 10:59:12 +08:00
public static File getCacheFile(File cacheDir, String hash) {
return new File(new File(cacheDir, hash.length() > 2 ? hash.substring(0, 2) : "xx"), hash);
2018-02-12 12:20:28 +08:00
}
2016-07-31 22:00:46 +08:00
}