Files

126 lines
5.2 KiB
Java
Raw Permalink Normal View History

2016-07-31 22:00:46 +08:00
package customskinloader.profile;
import java.io.File;
2021-09-24 11:19:06 +08:00
import java.util.Deque;
2020-03-26 10:22:20 +08:00
import java.util.Map;
2021-09-24 11:19:06 +08:00
import java.util.Optional;
2020-03-24 11:46:11 +08:00
import java.util.concurrent.ConcurrentHashMap;
2021-09-24 11:19:06 +08:00
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;
2016-07-31 22:00:46 +08:00
2021-09-24 11:19:06 +08:00
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
2016-07-31 22:00:46 +08:00
import customskinloader.CustomSkinLoader;
import customskinloader.utils.TimeUtil;
2021-09-24 11:19:06 +08:00
import org.apache.commons.io.FileUtils;
2016-07-31 22:00:46 +08:00
public class ProfileCache {
2018-02-12 12:20:28 +08:00
public static File PROFILE_CACHE_DIR=new File(CustomSkinLoader.DATA_DIR,"ProfileCache");
2020-03-26 10:22:20 +08:00
private Map<String, CachedProfile> cachedProfiles = new ConcurrentHashMap<>();
private Map<String, UserProfile> localProfiles = new ConcurrentHashMap<>();
2021-09-24 11:19:06 +08:00
private Map<String, Deque<Consumer<Map<MinecraftProfileTexture.Type, MinecraftProfileTexture>>>> profileLoaders = new ConcurrentHashMap<>();
2018-02-12 12:20:28 +08:00
2018-06-25 00:34:32 +08:00
@SuppressWarnings("ResultOfMethodCallIgnored")
2018-02-12 12:20:28 +08:00
public ProfileCache(){
if(!PROFILE_CACHE_DIR.exists())
PROFILE_CACHE_DIR.mkdir();
}
public boolean isExist(String username){
return cachedProfiles.containsKey(username.toLowerCase());
}
public boolean isLoading(String username) {
CachedProfile cp=cachedProfiles.get(username.toLowerCase());
return cp != null && cp.loading;
}
2018-02-12 12:20:28 +08:00
public boolean isReady(String username){
CachedProfile cp=cachedProfiles.get(username.toLowerCase());
2018-06-25 00:34:32 +08:00
return cp != null && (cp.loading || cp.expiryTime > TimeUtil.getCurrentUnixTimestamp());
2018-02-12 12:20:28 +08:00
}
public boolean isExpired(String username){
CachedProfile cp=cachedProfiles.get(username.toLowerCase());
2018-06-25 00:34:32 +08:00
return cp == null || (cp.expiryTime <= TimeUtil.getCurrentUnixTimestamp());
2018-02-12 12:20:28 +08:00
}
public UserProfile getProfile(String username){
return getCachedProfile(username).profile;
}
public long getExpiry(String username){
return getCachedProfile(username).expiryTime;
}
public UserProfile getLocalProfile(String username){
if(localProfiles.containsKey(username.toLowerCase()))
return localProfiles.get(username.toLowerCase());
return loadLocalProfile(username);
}
2021-09-24 11:19:06 +08:00
public Optional<Consumer<Map<MinecraftProfileTexture.Type, MinecraftProfileTexture>>> getLastLoader(String username) {
Deque<Consumer<Map<MinecraftProfileTexture.Type, MinecraftProfileTexture>>> deque = this.profileLoaders.get(username);
if (deque != null) {
return Optional.ofNullable(deque.pollLast());
}
return Optional.empty();
}
2018-02-12 12:20:28 +08:00
public void setLoading(String username,boolean loading){
getCachedProfile(username).loading=loading;
}
public void updateCache(String username,UserProfile profile){
updateCache(username,profile,CustomSkinLoader.config.enableLocalProfileCache);
}
public void updateCache(String username,UserProfile profile,boolean saveLocalProfile){
CachedProfile cp=getCachedProfile(username);
cp.profile=profile;
2020-08-16 21:55:06 +08:00
cp.expiryTime=TimeUtil.getUnixTimestamp(CustomSkinLoader.config.cacheExpiry);
2018-02-12 12:20:28 +08:00
if(!saveLocalProfile)
return;
saveLocalProfile(username,profile);
}
2021-09-24 11:19:06 +08:00
public void putLoader(String username, Consumer<Map<MinecraftProfileTexture.Type, MinecraftProfileTexture>> loader) {
this.profileLoaders.putIfAbsent(username, new ConcurrentLinkedDeque<>());
this.profileLoaders.get(username).offerLast(loader);
}
2018-02-12 12:20:28 +08:00
private CachedProfile getCachedProfile(String username){
CachedProfile cp=cachedProfiles.get(username.toLowerCase());
if(cp!=null)
return cp;
cp=new CachedProfile();
cachedProfiles.put(username.toLowerCase(), cp);
return cp;
}
private UserProfile loadLocalProfile(String username){
File localProfile=new File(PROFILE_CACHE_DIR,username.toLowerCase()+".json");
if(!localProfile.exists()){
localProfiles.put(username.toLowerCase(), null);
}
try{
2018-06-25 00:34:32 +08:00
String json = FileUtils.readFileToString(localProfile, "UTF-8");
2018-02-12 12:20:28 +08:00
UserProfile profile=CustomSkinLoader.GSON.fromJson(json, UserProfile.class);
localProfiles.put(username.toLowerCase(), profile);
CustomSkinLoader.logger.info("Successfully load LocalProfile.");
return profile;
}catch(Exception e){
CustomSkinLoader.logger.info("Failed to load LocalProfile.("+e.toString()+")");
localProfiles.put(username.toLowerCase(), null);
}
return null;
}
2018-06-25 00:34:32 +08:00
@SuppressWarnings("ResultOfMethodCallIgnored")
private void saveLocalProfile(String username, UserProfile profile){
2018-02-12 12:20:28 +08:00
String json=CustomSkinLoader.GSON.toJson(profile);
File localProfile=new File(PROFILE_CACHE_DIR,username+".json");
if(localProfile.exists())
localProfile.delete();
try {
2018-06-25 00:34:32 +08:00
FileUtils.write(localProfile, json, "UTF-8");
2018-02-12 12:20:28 +08:00
CustomSkinLoader.logger.info("Successfully save LocalProfile.");
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to save LocalProfile.("+e.toString()+")");
}
}
2016-07-31 22:00:46 +08:00
}
class CachedProfile{
2018-02-12 12:20:28 +08:00
public UserProfile profile;
public long expiryTime=0;
public boolean loading=false;
2021-07-25 15:48:50 +08:00
}