Advertisement
Guest User

Untitled

a guest
May 8th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. package net.minecraft.launcher.profile;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Set;
  14.  
  15. import javax.swing.SwingUtilities;
  16.  
  17. import net.minecraft.launcher.Launcher;
  18. import net.minecraft.launcher.authentication.AuthenticationDatabase;
  19. import net.minecraft.launcher.events.RefreshedProfilesListener;
  20. import net.minecraft.launcher.updater.DateTypeAdapter;
  21. import net.minecraft.launcher.updater.FileTypeAdapter;
  22. import net.minecraft.launcher.updater.LowerCaseEnumTypeAdapterFactory;
  23.  
  24. import org.apache.commons.io.FileUtils;
  25.  
  26. import com.google.gson.Gson;
  27. import com.google.gson.GsonBuilder;
  28. import com.kt.killa.MasterKlass;
  29.  
  30. public class ProfileManager
  31. {
  32. private final Launcher launcher;
  33. private final Gson gson;
  34.  
  35. private final Map<String, Profile> profiles = new HashMap<String, Profile>();
  36. private final File profileFile;
  37. private final List<RefreshedProfilesListener> refreshedProfilesListeners = Collections.synchronizedList(new ArrayList());
  38. private String selectedProfile;
  39. private AuthenticationDatabase authDatabase = new AuthenticationDatabase();
  40.  
  41. public ProfileManager(Launcher launcher)
  42. {
  43. this.launcher = launcher;
  44. this.profileFile = new File(launcher.getWorkingDirectory(), "launcher_profiles.json");
  45.  
  46. GsonBuilder builder = new GsonBuilder();
  47. builder.registerTypeAdapterFactory(new LowerCaseEnumTypeAdapterFactory());
  48. builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
  49. builder.registerTypeAdapter(File.class, new FileTypeAdapter());
  50. builder.registerTypeAdapter(AuthenticationDatabase.class, new MasterKlass());
  51. builder.setPrettyPrinting();
  52. this.gson = builder.create();
  53. }
  54.  
  55. public void addRefreshedProfilesListener(RefreshedProfilesListener listener)
  56. {
  57. this.refreshedProfilesListeners.add(listener);
  58. }
  59.  
  60. public void fireRefreshEvent()
  61. {
  62. final List<RefreshedProfilesListener> listeners = new ArrayList(this.refreshedProfilesListeners);
  63. for (Iterator<RefreshedProfilesListener> iterator = listeners.iterator(); iterator.hasNext();)
  64. {
  65. RefreshedProfilesListener listener = (RefreshedProfilesListener)iterator.next();
  66. if (!listener.shouldReceiveEventsInUIThread())
  67. {
  68. listener.onProfilesRefreshed(this);
  69. iterator.remove();
  70. }
  71. }
  72. if (!listeners.isEmpty()) {
  73. SwingUtilities.invokeLater(new Runnable()
  74. {
  75. public void run()
  76. {
  77. for (RefreshedProfilesListener listener : listeners) {
  78. listener.onProfilesRefreshed(ProfileManager.this);
  79. }
  80. }
  81. });
  82. }
  83. }
  84.  
  85. public AuthenticationDatabase getAuthDatabase()
  86. {
  87. return this.authDatabase;
  88. }
  89.  
  90. public Launcher getLauncher()
  91. {
  92. return this.launcher;
  93. }
  94.  
  95. public Map<String, Profile> getProfiles()
  96. {
  97. return this.profiles;
  98. }
  99.  
  100. public Profile getSelectedProfile()
  101. {
  102. if ((this.selectedProfile == null) || (!this.profiles.containsKey(this.selectedProfile))) {
  103. if (this.profiles.get("Default") != null)
  104. {
  105. this.selectedProfile = "Default";
  106. }
  107. else if (this.profiles.size() > 0)
  108. {
  109. this.selectedProfile = ((Profile)this.profiles.values().iterator().next()).getName();
  110. }
  111. else
  112. {
  113. this.selectedProfile = "Default";
  114. this.profiles.put("Default", new Profile(this.selectedProfile));
  115. }
  116. }
  117. Profile profile = this.profiles.get(this.selectedProfile);
  118. return profile;
  119. }
  120.  
  121. public boolean loadProfiles() throws IOException {
  122. profiles.clear();
  123. selectedProfile = null;
  124.  
  125. if(profileFile.isFile()) {
  126. RawProfileList rawProfileList = (RawProfileList) gson.fromJson(FileUtils.readFileToString(profileFile), RawProfileList.class);
  127. profiles.putAll(rawProfileList.profiles);
  128. selectedProfile = rawProfileList.selectedProfile;
  129. authDatabase = rawProfileList.authenticationDatabase;
  130. launcher.setClientToken(rawProfileList.clientToken);
  131.  
  132. fireRefreshEvent();
  133. return true;
  134. }
  135. fireRefreshEvent();
  136. return false;
  137. }
  138. public void saveProfiles() throws IOException {
  139. RawProfileList rawProfileList = new RawProfileList();
  140. rawProfileList.profiles = profiles;
  141. rawProfileList.selectedProfile = getSelectedProfile().getName();
  142. rawProfileList.clientToken = launcher.getClientToken();
  143.  
  144. FileUtils.writeStringToFile(profileFile, gson.toJson(rawProfileList));
  145. }
  146.  
  147. public void setSelectedProfile(String selectedProfile)
  148. {
  149. boolean update = !this.selectedProfile.equals(selectedProfile);
  150. this.selectedProfile = selectedProfile;
  151. if (update) {
  152. fireRefreshEvent();
  153. }
  154. }
  155.  
  156. public void trimAuthDatabase()
  157. {
  158. Set<String> uuids = new HashSet(this.authDatabase.getknownUUIDs());
  159. for (Profile profile : this.profiles.values()) {
  160. uuids.remove(profile.getPlayerUUID());
  161. }
  162. for (String uuid : uuids) {
  163. this.authDatabase.removeUUID(uuid);
  164. }
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement