Skip to content

Commit

Permalink
fix read temp file in priority for updated config
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Andres committed Jul 13, 2021
1 parent 8a2c797 commit 1cc4bf0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
Binary file modified out/artifacts/DragonBallArena_jar/DragonBallArena.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ScoreData {
public static void init(String file) throws JSONException {
Console.debug("file: " + file);
dataScore = new ArrayList<>();
configs = new JSONObject(FilesTools.readFile(file));
configs = new JSONObject(FilesTools.readTempFile(file));
ScoreData.file = file;

JSONArray array = configs.getJSONArray("scores");
Expand Down Expand Up @@ -85,7 +85,7 @@ public static boolean setAvailableScore(String pseudo, String score) {
} catch (JSONException e) {
e.printStackTrace();
}
FilesTools.writeInFile(file, configs.toString());
FilesTools.writeInTempFile(file, configs.toString());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class InputData {

public static void init(String file) throws JSONException {
availableInput = new LinkedHashMap<>();
configs = new JSONObject(FilesTools.readFile(file));
configs = new JSONObject(FilesTools.readTempFile(file));
InputData.file = file;

Iterator iterator = configs.keys();
Expand Down Expand Up @@ -62,7 +62,7 @@ public static boolean setAvailableInput(EInput type, String value){
} catch (JSONException e) {
e.printStackTrace();
}
FilesTools.writeInFile(file, configs.toString());
FilesTools.writeInTempFile(file, configs.toString());
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class ComboAvailableList {
private ComboAvailableList() {
this.playerCombos = new HashMap<>();
try {
JSONObject configs = new JSONObject(FilesTools.readFile(ConfigPath.comboAvailableList));
JSONObject configs = new JSONObject(FilesTools.readTempFile(ConfigPath.comboAvailableList));

Iterator iterator = configs.keys();
while (iterator.hasNext()) {
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/com/andres_k/utils/tools/FilesTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

public class FilesTools {

public static boolean validFile(String path) {
File file = new File(path);

return (file.exists() && !file.isDirectory());
}

public static String readInput(InputStream inputStream) {
Scanner scan = new Scanner(inputStream).useDelimiter("\\A");
Expand All @@ -32,15 +37,26 @@ public static String readFile(String fileName) {
}
}

public static String readTempFile(String fileName) {
String tempFileName = GameInfo.get().getGamePathTMP() + "/" + fileName;
File tempFile = new File(tempFileName);

if (!tempFile.exists()) {
ClassLoader classLoader = FilesTools.class.getClassLoader();
return FilesTools.readInput(classLoader.getResourceAsStream(fileName));
} else {
return FilesTools.parseFile(tempFile);
}
}

public static String parseFile(File file) {
StringBuilder content = new StringBuilder("");
StringBuilder content = new StringBuilder();

try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
content.append(line).append("\n");
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -55,6 +71,22 @@ private static void createParents(String fileName) {
}

public static void writeInFile(String fileName, String value) {
File file = new File(fileName);

if (!file.exists())
createParents(fileName);
try {
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(value);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void writeInTempFile(String fileName, String value) {
fileName = GameInfo.get().getGamePathTMP() + "/" + fileName;
File file = new File(fileName);
if (!file.exists())
Expand All @@ -69,4 +101,11 @@ public static void writeInFile(String fileName, String value) {
e.printStackTrace();
}
}

public static void createFolder(String folderName) {
folderName = folderName.substring(0, folderName.lastIndexOf("/"));
File file = new File(folderName);

file.mkdirs();
}
}

0 comments on commit 1cc4bf0

Please sign in to comment.