Skip to content

Commit 685bc21

Browse files
committed
convert old storage type to the new one
1 parent 5e6d358 commit 685bc21

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

src/main/java/lol/hyper/customlauncher/Main.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ public static void main(String[] args) throws IOException {
5858
logger.info("Creating base accounts file...");
5959
}
6060

61-
JSONObject optionsFile = JSONManager.readJSONObject(JSONManager.configFile);
61+
JSONObject optionsFile = new JSONObject(JSONManager.readFile(JSONManager.configFile));
62+
63+
// automatically convert the old format to the new one
64+
char firstChar = JSONManager.readFile(JSONManager.accountsFile).charAt(0);
65+
if (firstChar == '{') {
66+
JSONManager.convertToNewFormat();
67+
Main.logger.info("Converting account storage to JSONArray format.");
68+
}
6269

6370
// check the config installation path
6471
// if it's not valid, use default

src/main/java/lol/hyper/customlauncher/accounts/JSONManager.java

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.crypto.spec.SecretKeySpec;
2727
import java.io.*;
2828
import java.nio.charset.StandardCharsets;
29+
import java.nio.file.Files;
2930
import java.nio.file.Path;
3031
import java.nio.file.Paths;
3132
import java.security.MessageDigest;
@@ -42,49 +43,18 @@ public class JSONManager {
4243
private static SecretKeySpec secretKey;
4344

4445
/**
45-
* Read JSONArray from file.
46+
* Read contents of a file.
4647
*
47-
* @return JSONArray with JSON data.
48+
* @return Contents of a file.
4849
*/
49-
public static JSONArray readJSONArray(File file) {
50-
JSONArray object = null;
50+
public static String readFile(File file) {
51+
byte[] encoded = new byte[0];
5152
try {
52-
BufferedReader br = new BufferedReader(new FileReader(file));
53-
StringBuilder sb = new StringBuilder();
54-
String line = br.readLine();
55-
while (line != null) {
56-
sb.append(line);
57-
line = br.readLine();
58-
}
59-
object = new JSONArray(sb.toString());
60-
br.close();
61-
} catch (Exception e) {
62-
Main.logger.error("Unable to read file!", e);
63-
}
64-
return object;
65-
}
66-
67-
/**
68-
* Read JSONObject from file.
69-
*
70-
* @return JSONObject with JSON data.
71-
*/
72-
public static JSONObject readJSONObject(File file) {
73-
JSONObject object = null;
74-
try {
75-
BufferedReader br = new BufferedReader(new FileReader(file));
76-
StringBuilder sb = new StringBuilder();
77-
String line = br.readLine();
78-
while (line != null) {
79-
sb.append(line);
80-
line = br.readLine();
81-
}
82-
object = new JSONObject(sb.toString());
83-
br.close();
84-
} catch (Exception e) {
85-
Main.logger.error("Unable to read file!", e);
53+
encoded = Files.readAllBytes(file.toPath());
54+
} catch (IOException e) {
55+
Main.logger.error(e);
8656
}
87-
return object;
57+
return new String(encoded, StandardCharsets.UTF_8);
8858
}
8959

9060
/**
@@ -109,7 +79,7 @@ public static void writeFile(Object json, File file) {
10979
*/
11080
public static List<Account> getAccounts() {
11181
List<Account> accounts = new ArrayList<>();
112-
JSONArray accountsJSON = readJSONArray(accountsFile);
82+
JSONArray accountsJSON = new JSONArray(readFile(accountsFile));
11383
for (int i = 0; i < accountsJSON.length(); i++) {
11484
JSONObject currentAccount = accountsJSON.getJSONObject(i);
11585
String username = (String) currentAccount.get("username");
@@ -126,7 +96,7 @@ public static List<Account> getAccounts() {
12696
* @param encryptedPassword Account encrypted password.
12797
*/
12898
public static void addNewAccount(String username, String encryptedPassword) {
129-
JSONArray accountsJSON = readJSONArray(accountsFile);
99+
JSONArray accountsJSON = new JSONArray(readFile(accountsFile));
130100
JSONObject newAccount = new JSONObject();
131101
newAccount.put("username", username);
132102
newAccount.put("password", encryptedPassword);
@@ -140,13 +110,13 @@ public static void addNewAccount(String username, String encryptedPassword) {
140110
* @param account Account to delete.
141111
*/
142112
public static void deleteAccount(Account account) {
143-
JSONArray accountsJSON = readJSONArray(accountsFile);
113+
JSONArray accountsJSON = new JSONArray(readFile(accountsFile));
144114
accountsJSON.remove(getAccountIndex(account));
145115
writeFile(accountsJSON, accountsFile);
146116
}
147117

148118
public static int getAccountIndex(Account account) {
149-
JSONArray accountsJSON = readJSONArray(accountsFile);
119+
JSONArray accountsJSON = new JSONArray(readFile(accountsFile));
150120
String username = account.getUsername();
151121
for (int i = 0; i < accountsJSON.length(); i++) {
152122
JSONObject currentAccount = accountsJSON.getJSONObject(i);
@@ -217,7 +187,7 @@ public static String decrypt(String strToDecrypt, String secret) {
217187
* @param remove Should we remove this entry or add this entry?
218188
*/
219189
public static void editConfig(String key, Object value, boolean remove) {
220-
JSONObject config = readJSONObject(configFile);
190+
JSONObject config = new JSONObject(readFile(configFile));
221191
if (remove) {
222192
config.remove(key);
223193
} else {
@@ -232,7 +202,22 @@ public static void editConfig(String key, Object value, boolean remove) {
232202
* @return Yes/no if we should.
233203
*/
234204
public static boolean shouldWeUpdate() {
235-
JSONObject config = readJSONObject(configFile);
205+
JSONObject config = new JSONObject(readFile(configFile));
236206
return config.getBoolean("autoCheckTTRUpdates");
237207
}
208+
209+
public static void convertToNewFormat() {
210+
JSONObject oldFile = new JSONObject(readFile(accountsFile));
211+
JSONArray newFile = new JSONArray();
212+
Iterator<String> keys = oldFile.keys();
213+
214+
while(keys.hasNext()) {
215+
String key = keys.next();
216+
if (oldFile.get(key) instanceof JSONObject) {
217+
JSONObject temp = (JSONObject) oldFile.get(key);
218+
newFile.put(temp);
219+
}
220+
}
221+
writeFile(newFile, accountsFile);
222+
}
238223
}

src/main/java/lol/hyper/customlauncher/accounts/windows/MainWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public MainWindow(String title) {
106106
public void mouseClicked(MouseEvent evt) {
107107
JList list = (JList) evt.getSource();
108108
if (evt.getClickCount() == 2) {
109-
JSONObject options = JSONManager.readJSONObject(JSONManager.configFile);
109+
JSONObject options = new JSONObject(JSONManager.readFile(JSONManager.configFile));
110110
if (!Paths.get(options.getString("ttrInstallLocation"))
111111
.toFile()
112112
.exists()) {

0 commit comments

Comments
 (0)