Files

166 lines
5.2 KiB
Java
Raw Permalink Normal View History

2016-08-09 17:23:27 +08:00
package customskinloader.loader;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
2016-08-09 17:23:27 +08:00
import com.mojang.authlib.GameProfile;
import customskinloader.CustomSkinLoader;
import customskinloader.config.SkinSiteProfile;
2021-04-24 10:33:18 +08:00
import customskinloader.plugin.ICustomSkinLoaderPlugin;
2016-08-09 17:23:27 +08:00
import customskinloader.profile.UserProfile;
2016-11-06 12:00:36 +08:00
import customskinloader.utils.HttpRequestUtil;
2016-08-09 17:23:27 +08:00
import customskinloader.utils.HttpUtil0;
2021-04-24 10:33:18 +08:00
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
2016-08-09 17:23:27 +08:00
2021-04-24 10:33:18 +08:00
public class JsonAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.IProfileLoader {
2018-02-12 12:20:28 +08:00
public interface IJsonAPI {
List<IDefaultProfile> getDefaultProfiles(JsonAPILoader loader);
2018-06-25 00:34:32 +08:00
String toJsonUrl(String root, String username);
2023-01-11 22:06:58 +08:00
default String getPayload(SkinSiteProfile ssp) {
return null;
}
/**
* get JSON API payload, which will make a POST request
*
* @param ssp skin site profile
* @param username username
* @return JSON payload
* @since 14.16
*/
default String getPayload(SkinSiteProfile ssp, String username) {
return getPayload(ssp);
}
2018-06-25 00:34:32 +08:00
UserProfile toUserProfile(String root, String json, boolean local);
2018-06-25 00:34:32 +08:00
String getName();
2018-02-12 12:20:28 +08:00
}
public static class ErrorProfile {
2018-02-12 12:20:28 +08:00
public int errno;
public String msg;
}
2021-04-24 10:33:18 +08:00
private final IJsonAPI jsonAPI;
public JsonAPILoader(IJsonAPI jsonAPI) {
2021-04-24 10:33:18 +08:00
this.jsonAPI = jsonAPI;
2018-02-12 12:20:28 +08:00
}
2021-04-24 10:33:18 +08:00
// === ICustomSkinLoaderPlugin ===
@Override
public ProfileLoader.IProfileLoader getProfileLoader() {
return this;
}
@Override
public List<IDefaultProfile> getDefaultProfiles() {
return this.jsonAPI.getDefaultProfiles(this);
2021-04-24 10:33:18 +08:00
}
public abstract static class DefaultProfile implements ICustomSkinLoaderPlugin.IDefaultProfile {
protected final JsonAPILoader loader;
public DefaultProfile(JsonAPILoader loader) {
this.loader = loader;
}
@Override
public void updateSkinSiteProfile(SkinSiteProfile ssp) {
ssp.type = this.loader.getName();
ssp.root = this.getRoot();
}
public abstract String getRoot();
2021-04-24 10:33:18 +08:00
}
// === IProfileLoader ===
2018-02-12 12:20:28 +08:00
@Override
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception {
String username = gameProfile.getName();
2021-04-24 10:33:18 +08:00
if (StringUtils.isEmpty(ssp.root)) {
2018-02-12 12:20:28 +08:00
CustomSkinLoader.logger.info("Root not defined.");
return null;
}
2018-07-09 19:53:35 +08:00
boolean local = HttpUtil0.isLocal(ssp.root);
2021-04-24 10:33:18 +08:00
String jsonUrl = this.jsonAPI.toJsonUrl(ssp.root, username);
//Some API like MinecraftCapesAPI won't load profile at sometime
if (jsonUrl == null) {
CustomSkinLoader.logger.info("Profile url not found.");
return null;
}
2018-02-12 12:20:28 +08:00
String json;
if (local) {
2018-05-05 23:07:20 +08:00
File jsonFile = new File(CustomSkinLoader.DATA_DIR, jsonUrl);
if (!jsonFile.exists()) {
2018-02-12 12:20:28 +08:00
CustomSkinLoader.logger.info("Profile File not found.");
return null;
}
json = IOUtils.toString(Files.newInputStream(jsonFile.toPath()), StandardCharsets.UTF_8);
} else {
2023-01-11 22:06:58 +08:00
try {
HttpRequestUtil.HttpResponce responce = HttpRequestUtil
.makeHttpRequest(new HttpRequestUtil.HttpRequest(jsonUrl).setCacheTime(90)
.setUserAgent(ssp.userAgent).setPayload(this.jsonAPI.getPayload(ssp, username)));
json = responce.content;
} catch (ProfileNotFoundException ignored) {
CustomSkinLoader.logger.info("Profile not found.");
return null;
}
2018-02-12 12:20:28 +08:00
}
if (json == null || json.equals("")) {
2018-02-12 12:20:28 +08:00
CustomSkinLoader.logger.info("Profile not found.");
return null;
}
ErrorProfile profile = CustomSkinLoader.GSON.fromJson(json, ErrorProfile.class);
if (profile.errno != 0) {
CustomSkinLoader.logger.info("Error " + profile.errno + ": " + profile.msg);
2018-02-12 12:20:28 +08:00
return null;
}
UserProfile p = this.jsonAPI.toUserProfile(ssp.root, json, local);
if (p == null || p.isEmpty()) {
2018-02-12 12:20:28 +08:00
CustomSkinLoader.logger.info("Both skin and cape not found.");
return null;
} else {
2018-02-12 12:20:28 +08:00
return p;
}
}
2018-02-12 12:20:28 +08:00
@Override
public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) {
2018-06-25 00:34:32 +08:00
return !StringUtils.isNoneEmpty(ssp0.root) || ssp0.root.equalsIgnoreCase(ssp1.root);
2018-02-12 12:20:28 +08:00
}
2018-02-12 12:20:28 +08:00
@Override
public String getName() {
2021-04-24 10:33:18 +08:00
return this.jsonAPI.getName();
2018-02-12 12:20:28 +08:00
}
2018-02-12 12:20:28 +08:00
@Override
2019-09-18 22:15:58 +08:00
public void init(SkinSiteProfile ssp) {
if (HttpUtil0.isLocal(ssp.root)) {
File f = new File(ssp.root);
if (!f.exists())
//noinspection ResultOfMethodCallIgnored
2018-02-12 12:20:28 +08:00
f.mkdirs();
}
}
2023-01-11 22:06:58 +08:00
/**
* When getting payload, this exception can end skin loading
* @since 14.16
*/
public static class ProfileNotFoundException extends RuntimeException {
}
2016-08-09 17:23:27 +08:00
}