improve json saving to file

This commit is contained in:
OMGeeky
2025-06-24 21:50:52 +02:00
parent 1cbcd5b661
commit a3ffecfd23
7 changed files with 1644 additions and 1657 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,7 @@
package com.gpl.rpg.atcontentstudio.model;
import com.gpl.rpg.atcontentstudio.Notification;
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import org.json.simple.JSONObject;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.parser.JSONParser;
import java.io.*;
@@ -108,22 +107,8 @@ public class WorkspaceSettings {
}
json.put(VERSION_KEY, SETTINGS_VERSION);
StringWriter writer = new JsonPrettyWriter();
try {
JSONObject.writeJSONString(json, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
try {
FileWriter w = new FileWriter(file);
w.write(toWrite);
w.close();
Notification.addSuccess("Workspace settings saved.");
} catch (IOException e) {
Notification.addError("Error while saving workspace settings : " + e.getMessage());
e.printStackTrace();
}
String toWrite = FileUtils.toJsonString(json);
FileUtils.writeStringToFile(toWrite, file, "Workspace settings");
}
public void resetDefault() {

View File

@@ -5,6 +5,7 @@ import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.JSONArray;
import javax.swing.tree.TreeNode;
@@ -157,26 +158,13 @@ public class GameDataCategory<E extends JSONElement> extends ArrayList<E> implem
return;
}
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(dataToSave, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
try {
FileWriter w = new FileWriter(jsonFile);
w.write(toWrite);
w.close();
String toWrite = FileUtils.toJsonString(dataToSave);
if(FileUtils.writeStringToFile(toWrite, jsonFile, "JSON file '"+jsonFile.getAbsolutePath()+"'")){
for (E element : this) {
element.state = GameDataElement.State.saved;
}
Notification.addSuccess("Json file " + jsonFile.getAbsolutePath() + " saved.");
} catch (IOException e) {
Notification.addError("Error while writing json file " + jsonFile.getAbsolutePath() + " : " + e.getMessage());
e.printStackTrace();
}
}

View File

@@ -4,6 +4,7 @@ import com.gpl.rpg.atcontentstudio.Notification;
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
import com.gpl.rpg.atcontentstudio.model.SaveEvent;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@@ -73,16 +74,10 @@ public abstract class JSONElement extends GameDataElement {
public abstract Map toJson();
public String toJsonString() {
StringWriter writer = new JsonPrettyWriter();
try {
JSONObject.writeJSONString(this.toJson(), writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
return writer.toString();
Map json = this.toJson();
return FileUtils.toJsonString(json);
}
@Override
public GameDataSet getDataSet() {
if (parent == null) {

View File

@@ -145,16 +145,12 @@ public class ResourcesCompactor {
private Minify jsonMinifier = new Minify();
private void writeJson(List<Map> dataToSave, File target) {
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(dataToSave, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
String toWrite = FileUtils.toJsonString(dataToSave);
toWrite = jsonMinifier.minify(toWrite);
FileUtils.writeStringToFile(toWrite, target, null);
try {
FileWriter w = new FileWriter(target);
w.write(jsonMinifier.minify(toWrite));
w.write(toWrite);
w.close();
} catch (IOException e) {
e.printStackTrace();

View File

@@ -6,6 +6,7 @@ import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@@ -155,24 +156,12 @@ public class WriterModeDataSet implements ProjectTreeNode, Serializable {
return;
}
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(dataToSave, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
try {
FileWriter w = new FileWriter(writerFile);
w.write(toWrite);
w.close();
String toWrite = FileUtils.toJsonString(dataToSave);
if(FileUtils.writeStringToFile(toWrite, writerFile, "Json file " + writerFile.getAbsolutePath())) {
for (WriterModeData element : writerModeDataList) {
element.state = GameDataElement.State.saved;
}
Notification.addSuccess("Json file " + writerFile.getAbsolutePath() + " saved.");
} catch (IOException e) {
Notification.addError("Error while writing json file " + writerFile.getAbsolutePath() + " : " + e.getMessage());
e.printStackTrace();
}
}

View File

@@ -1,219 +1,266 @@
package com.gpl.rpg.atcontentstudio.utils;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
public static void deleteDir(File dir) {
if (dir.exists()) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
deleteDir(f);
} else {
f.delete();
}
}
dir.delete();
}
}
public static void copyFile(File sourceLocation, File targetLocation) {
try {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
try {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
}
}
private static final int BUFFER = 2048;
public static void writeToZip(File folder, File target) {
try {
FileOutputStream dest = new FileOutputStream(target);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
zipDir(folder, "", out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* cp sourceFolder/* targetFolder/
*
* @param sourceFolder
* @param targetFolder
*/
public static void copyOver(File sourceFolder, File targetFolder) {
if (!sourceFolder.isDirectory() || !targetFolder.isDirectory()) return;
for (File f : sourceFolder.listFiles()) {
if (Files.isSymbolicLink(f.toPath())) {
//Skip symlinks
continue;
} else if (f.isDirectory()) {
File dest = new File(targetFolder, f.getName());
if (!dest.exists()) {
dest.mkdir();
}
copyOver(f, dest);
} else {
copyFile(f, new File(targetFolder, f.getName()));
}
}
}
private static void zipDir(File dir, String prefix, ZipOutputStream zos) {
if (prefix != "") {
prefix = prefix + File.separator;
}
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
zipDir(f, prefix + f.getName(), zos);
} else {
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedInputStream origin = new BufferedInputStream(fis, BUFFER);
ZipEntry entry = new ZipEntry(prefix + f.getName());
try {
zos.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = origin.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count);
zos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
origin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static boolean makeSymlink(File targetFile, File linkFile) {
Path target = Paths.get(targetFile.getAbsolutePath());
Path link = Paths.get(linkFile.getAbsolutePath());
if (!Files.exists(link)) {
try {
Files.createSymbolicLink(link, target);
} catch (Exception e) {
System.err.println("Failed to create symbolic link to target \"" + targetFile.getAbsolutePath() + "\" as \"" + linkFile.getAbsolutePath() + "\" the java.nio way:");
e.printStackTrace();
switch (DesktopIntegration.detectedOS) {
case Windows:
System.err.println("Trying the Windows way with mklink");
try {
Runtime.getRuntime().exec(
"cmd.exe /C mklink " + (targetFile.isDirectory() ? "/J " : "") + "\"" + linkFile.getAbsolutePath() + "\" \"" + targetFile.getAbsolutePath() + "\"");
} catch (IOException e1) {
e1.printStackTrace();
}
if (!linkFile.exists()) {
System.err.println("Attempting UAC elevation through VBS script.");
runWithUac("cmd.exe /C mklink " + (targetFile.isDirectory() ? "/J " : "") + "\"" + linkFile.getAbsolutePath() + "\" \"" + targetFile.getAbsolutePath() + "\"", 3, linkFile);
}
break;
case MacOS:
case NIX:
case Other:
System.err.println("Trying the unix way with ln -s");
try {
Runtime.getRuntime().exec("ln -s " + targetFile.getAbsolutePath() + " " + linkFile.getAbsolutePath());
} catch (IOException e1) {
e1.printStackTrace();
}
break;
default:
System.out.println("Unrecognized OS. Please contact ATCS dev.");
break;
}
}
}
if (!Files.exists(link)) {
System.err.println("Failed to create link \"" + linkFile.getAbsolutePath() + "\" targetting \"" + targetFile.getAbsolutePath() + "\"");
System.err.println("You can try running ATCS with administrative privileges once, or create the symbolic link manually.");
}
return true;
}
public static File backupFile(File f) {
try {
Path returned = Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(f.getAbsolutePath() + ".bak"), StandardCopyOption.REPLACE_EXISTING);
return returned.toFile();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static final String uacBatName = "ATCS_elevateWithUac.bat";
public static void runWithUac(String command, int tries, File checkExists) {
File tmpFolder = new File(System.getProperty("java.io.tmpdir"));
File batFile = new File(tmpFolder, uacBatName);
batFile.deleteOnExit();
FileWriter writer;
try {
writer = new FileWriter(batFile, false);
writer.write(
"@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
+ "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
+ "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
+ "@cscript %temp%\\sudo.tmp.vbs\r\n"
+ "del /f %temp%\\sudo.tmp.vbs\r\n");
writer.close();
while (!checkExists.exists() && tries-- > 0) {
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", batFile.getAbsolutePath() + " " + command});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.gpl.rpg.atcontentstudio.utils;
import com.gpl.rpg.atcontentstudio.Notification;
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
public static String toJsonString(Map json) {
StringWriter writer = new JsonPrettyWriter();
try {
JSONObject.writeJSONString(json, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
return writer.toString();
}
public static String toJsonString(List json) {
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(json, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
return writer.toString();
}
public static boolean writeStringToFile(String toWrite, File file, String type) {
return writeStringToFile(toWrite, file, type, true);
}
public static boolean writeStringToFile(String toWrite, File file, String type, boolean notifyOnSuccess) {
try {
FileWriter w = new FileWriter(file);
w.write(toWrite);
w.close();
if(type != null) {
Notification.addSuccess(type + " saved.");
}
return true;
} catch (IOException e) {
if(type != null) {
Notification.addError("Error while saving " + type + " : " + e.getMessage());
}
e.printStackTrace();
return false;
}
}
public static void deleteDir(File dir) {
if (dir.exists()) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
deleteDir(f);
} else {
f.delete();
}
}
dir.delete();
}
}
public static void copyFile(File sourceLocation, File targetLocation) {
try {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
try {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
}
}
private static final int BUFFER = 2048;
public static void writeToZip(File folder, File target) {
try {
FileOutputStream dest = new FileOutputStream(target);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
zipDir(folder, "", out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* cp sourceFolder/* targetFolder/
*
* @param sourceFolder
* @param targetFolder
*/
public static void copyOver(File sourceFolder, File targetFolder) {
if (!sourceFolder.isDirectory() || !targetFolder.isDirectory()) return;
for (File f : sourceFolder.listFiles()) {
if (Files.isSymbolicLink(f.toPath())) {
//Skip symlinks
continue;
} else if (f.isDirectory()) {
File dest = new File(targetFolder, f.getName());
if (!dest.exists()) {
dest.mkdir();
}
copyOver(f, dest);
} else {
copyFile(f, new File(targetFolder, f.getName()));
}
}
}
private static void zipDir(File dir, String prefix, ZipOutputStream zos) {
if (prefix != "") {
prefix = prefix + File.separator;
}
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
zipDir(f, prefix + f.getName(), zos);
} else {
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedInputStream origin = new BufferedInputStream(fis, BUFFER);
ZipEntry entry = new ZipEntry(prefix + f.getName());
try {
zos.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = origin.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count);
zos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
origin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static boolean makeSymlink(File targetFile, File linkFile) {
Path target = Paths.get(targetFile.getAbsolutePath());
Path link = Paths.get(linkFile.getAbsolutePath());
if (!Files.exists(link)) {
try {
Files.createSymbolicLink(link, target);
} catch (Exception e) {
System.err.println("Failed to create symbolic link to target \"" + targetFile.getAbsolutePath() + "\" as \"" + linkFile.getAbsolutePath() + "\" the java.nio way:");
e.printStackTrace();
switch (DesktopIntegration.detectedOS) {
case Windows:
System.err.println("Trying the Windows way with mklink");
try {
Runtime.getRuntime().exec(
"cmd.exe /C mklink " + (targetFile.isDirectory() ? "/J " : "") + "\"" + linkFile.getAbsolutePath() + "\" \"" + targetFile.getAbsolutePath() + "\"");
} catch (IOException e1) {
e1.printStackTrace();
}
if (!linkFile.exists()) {
System.err.println("Attempting UAC elevation through VBS script.");
runWithUac("cmd.exe /C mklink " + (targetFile.isDirectory() ? "/J " : "") + "\"" + linkFile.getAbsolutePath() + "\" \"" + targetFile.getAbsolutePath() + "\"", 3, linkFile);
}
break;
case MacOS:
case NIX:
case Other:
System.err.println("Trying the unix way with ln -s");
try {
Runtime.getRuntime().exec("ln -s " + targetFile.getAbsolutePath() + " " + linkFile.getAbsolutePath());
} catch (IOException e1) {
e1.printStackTrace();
}
break;
default:
System.out.println("Unrecognized OS. Please contact ATCS dev.");
break;
}
}
}
if (!Files.exists(link)) {
System.err.println("Failed to create link \"" + linkFile.getAbsolutePath() + "\" targetting \"" + targetFile.getAbsolutePath() + "\"");
System.err.println("You can try running ATCS with administrative privileges once, or create the symbolic link manually.");
}
return true;
}
public static File backupFile(File f) {
try {
Path returned = Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(f.getAbsolutePath() + ".bak"), StandardCopyOption.REPLACE_EXISTING);
return returned.toFile();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static final String uacBatName = "ATCS_elevateWithUac.bat";
public static void runWithUac(String command, int tries, File checkExists) {
File tmpFolder = new File(System.getProperty("java.io.tmpdir"));
File batFile = new File(tmpFolder, uacBatName);
batFile.deleteOnExit();
FileWriter writer;
try {
writer = new FileWriter(batFile, false);
writer.write(
"@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
+ "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
+ "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
+ "@cscript %temp%\\sudo.tmp.vbs\r\n"
+ "del /f %temp%\\sudo.tmp.vbs\r\n");
writer.close();
while (!checkExists.exists() && tries-- > 0) {
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", batFile.getAbsolutePath() + " " + command});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}