Files
MCCustomSkinLoader/Common/source/customskinloader/loader/LegacyLoader.java
T

118 lines
5.3 KiB
Java
Raw Normal View History

2016-02-27 15:22:35 +08:00
package customskinloader.loader;
2016-07-31 22:00:46 +08:00
import java.io.File;
2019-10-03 15:30:46 +08:00
import com.mojang.util.UUIDTypeAdapter;
2016-09-10 21:00:53 +08:00
import org.apache.commons.lang3.StringUtils;
2016-07-31 22:00:46 +08:00
import com.mojang.authlib.GameProfile;
2016-02-27 15:22:35 +08:00
import customskinloader.CustomSkinLoader;
import customskinloader.config.SkinSiteProfile;
2016-09-24 19:08:01 +08:00
import customskinloader.profile.ModelManager0;
2016-07-31 22:00:46 +08:00
import customskinloader.profile.UserProfile;
2016-11-06 12:00:36 +08:00
import customskinloader.utils.HttpRequestUtil;
import customskinloader.utils.HttpRequestUtil.HttpRequest;
import customskinloader.utils.HttpRequestUtil.HttpResponce;
2016-07-31 22:00:46 +08:00
import customskinloader.utils.HttpTextureUtil;
2016-02-27 16:37:05 +08:00
import customskinloader.utils.HttpUtil0;
2016-02-27 15:22:35 +08:00
2019-10-03 15:30:46 +08:00
import javax.annotation.Nonnull;
2016-08-21 11:26:00 +08:00
public class LegacyLoader implements ProfileLoader.IProfileLoader {
2019-10-03 15:30:46 +08:00
public static final String USERNAME_PLACEHOLDER = "{USERNAME}";
public static final String UUID_PLACEHOLDER = "{UUID}";
2018-02-12 12:20:28 +08:00
@Override
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception {
2019-10-03 15:30:46 +08:00
String username = gameProfile.getName();
UserProfile profile = new UserProfile();
if (StringUtils.isNoneEmpty(ssp.skin)) {
String skin = expandURL(ssp.skin, username);
if (HttpUtil0.isLocal(ssp.skin)) {
File skinFile = new File(CustomSkinLoader.DATA_DIR, skin);
if (skinFile.exists() && skinFile.isFile())
profile.skinUrl = HttpTextureUtil.getLocalLegacyFakeUrl(skin, HttpTextureUtil.getHash(skin, skinFile.length(), skinFile.lastModified()));
} else {
HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG != null && ssp.checkPNG).setLoadContent(false).setCacheTime(90));
if (responce.success)
profile.skinUrl = HttpTextureUtil.getLegacyFakeUrl(skin);
2018-02-12 12:20:28 +08:00
}
2019-10-03 15:30:46 +08:00
profile.model = profile.hasSkinUrl() ? ssp.model : null;
2018-02-12 12:20:28 +08:00
}
2019-10-03 15:30:46 +08:00
if (StringUtils.isNoneEmpty(ssp.cape)) {
String cape = expandURL(ssp.cape, username);
if (HttpUtil0.isLocal(ssp.cape)) {
File capeFile = new File(CustomSkinLoader.DATA_DIR, cape);
if (capeFile.exists() && capeFile.isFile())
profile.capeUrl = HttpTextureUtil.getLocalLegacyFakeUrl(cape, HttpTextureUtil.getHash(cape, capeFile.length(), capeFile.lastModified()));
} else {
HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequest(cape).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG != null && ssp.checkPNG).setLoadContent(false).setCacheTime(90));
if (responce.success)
profile.capeUrl = HttpTextureUtil.getLegacyFakeUrl(cape);
2018-02-12 12:20:28 +08:00
}
}
2019-10-03 15:30:46 +08:00
if (ModelManager0.isElytraSupported() && StringUtils.isNoneEmpty(ssp.elytra)) {
String elytra = expandURL(ssp.elytra, username);
if (HttpUtil0.isLocal(ssp.elytra)) {
File elytraFile = new File(CustomSkinLoader.DATA_DIR, elytra);
if (elytraFile.exists() && elytraFile.isFile())
profile.elytraUrl = HttpTextureUtil.getLocalLegacyFakeUrl(elytra, HttpTextureUtil.getHash(elytra, elytraFile.length(), elytraFile.lastModified()));
} else {
HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequest(elytra).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG != null && ssp.checkPNG).setLoadContent(false).setCacheTime(90));
if (responce.success)
profile.elytraUrl = HttpTextureUtil.getLegacyFakeUrl(elytra);
2018-02-12 12:20:28 +08:00
}
}
2019-10-03 15:30:46 +08:00
if (profile.isEmpty()) {
2018-02-12 12:20:28 +08:00
CustomSkinLoader.logger.info("Both skin and cape not found.");
return null;
}
return profile;
}
2016-08-21 11:26:00 +08:00
2018-02-12 12:20:28 +08:00
@Override
public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) {
2018-05-18 21:06:28 +08:00
return (!StringUtils.isNoneEmpty(ssp0.skin) || ssp0.skin.equalsIgnoreCase(ssp1.skin)) || (!StringUtils.isNoneEmpty(ssp0.cape) || ssp0.cape.equalsIgnoreCase(ssp1.cape));
2018-02-12 12:20:28 +08:00
}
2019-10-03 15:30:46 +08:00
2018-02-12 12:20:28 +08:00
@Override
public String getName() {
return "Legacy";
}
2016-09-24 20:47:08 +08:00
2018-02-12 12:20:28 +08:00
@Override
2019-09-18 22:15:58 +08:00
public void init(SkinSiteProfile ssp) {
2019-10-03 15:30:46 +08:00
if (HttpUtil0.isLocal(ssp.skin))
2018-02-12 12:20:28 +08:00
initFolder(ssp.skin);
2019-10-03 15:30:46 +08:00
if (HttpUtil0.isLocal(ssp.cape))
2018-02-12 12:20:28 +08:00
initFolder(ssp.cape);
2019-10-03 15:30:46 +08:00
if (HttpUtil0.isLocal(ssp.elytra))
2018-02-12 12:20:28 +08:00
initFolder(ssp.elytra);
}
2019-10-03 15:30:46 +08:00
2018-06-25 00:34:32 +08:00
@SuppressWarnings("ResultOfMethodCallIgnored")
2019-10-03 15:30:46 +08:00
private void initFolder(String target) {
String file = target.replace(USERNAME_PLACEHOLDER, "init");
File folder = new File(CustomSkinLoader.DATA_DIR, file).getParentFile();
if (folder != null && !folder.exists())
2018-02-12 12:20:28 +08:00
folder.mkdirs();
}
2019-10-03 15:30:46 +08:00
private String expandURL(String url, String username) {
String t = url.replace(USERNAME_PLACEHOLDER, username);
if (t.contains(UUID_PLACEHOLDER))
t = t.replace(UUID_PLACEHOLDER, getMojangUUID(username));
return t;
}
@Nonnull
private String getMojangUUID(String username) {
GameProfile profile = MojangAPILoader.loadGameProfile(MojangAPILoader.getMojangApiRoot(), username);
if (profile == null) {
CustomSkinLoader.logger.info("UUID for %s not found.", username);
return "{ABORT}";
}
return UUIDTypeAdapter.fromUUID(profile.getId());
}
2016-02-27 15:22:35 +08:00
}