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 b9bcf6d commit 12b5d20
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Binary file modified out/artifacts/JumppyJumper_jar/JumppyJumper.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ScoreData {

public static void init(String file) throws JSONException {
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 @@ -84,7 +84,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 @@ -60,7 +60,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 @@ -11,6 +11,12 @@

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 @@ -31,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 @@ -54,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 @@ -68,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 12b5d20

Please sign in to comment.