2024-07-24 01:21:47 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* ConfigManager
|
|
|
|
*
|
|
|
|
* This class is the central configuration file manager for all other classes and acts as a utility class for other classes to use.
|
|
|
|
* It is typical to define this class as a general object (not static instance) for each class in single-threaded actions. If you need
|
|
|
|
* a special function or action from this class; it is recommended to add it to this class and not make your own.
|
|
|
|
*
|
2024-07-29 00:13:48 +00:00
|
|
|
*
|
2024-07-24 01:21:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package jesse.keeblarcraft.ConfigMgr;
|
|
|
|
|
2024-07-29 00:13:48 +00:00
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import com.google.common.base.Charsets;
|
|
|
|
import com.google.common.io.Files;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonIOException;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
// Import all custom exceptions
|
|
|
|
import jesse.keeblarcraft.Utils.CustomExceptions.*;
|
|
|
|
|
2024-07-24 01:21:47 +00:00
|
|
|
public class ConfigManager {
|
|
|
|
|
2024-07-29 00:13:48 +00:00
|
|
|
// Pedantic empty constructor
|
|
|
|
public ConfigManager() {}
|
|
|
|
|
|
|
|
// CreateFile
|
|
|
|
//
|
|
|
|
// Returns true if file is created, will return false if file cannot be created or already exists
|
|
|
|
public Boolean CreateFile(String fileName) throws FILE_CREATE_EXCEPTION {
|
|
|
|
Boolean ret = false;
|
|
|
|
File file = new File(fileName);
|
|
|
|
|
|
|
|
// Check 1: Does the file already exist?
|
|
|
|
ret = !file.exists();
|
|
|
|
|
|
|
|
// Check 2: If the file does not exist, attempt to create it
|
|
|
|
if (ret == false) {
|
|
|
|
try {
|
|
|
|
ret = file.createNewFile();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// The file could not be created
|
|
|
|
throw new FILE_CREATE_EXCEPTION();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile
|
|
|
|
//
|
|
|
|
// Returns true if file is deleted, false if could not be deleted or does not exist
|
|
|
|
public Boolean DeleteFile(String fileName) throws FILE_DELETE_EXCEPTION {
|
|
|
|
Boolean ret = false;
|
|
|
|
File file = new File(fileName);
|
|
|
|
|
|
|
|
// Step 1: Does file exist?
|
|
|
|
ret = file.exists();
|
|
|
|
|
|
|
|
// Step 2: If file exists, attempt to delete
|
|
|
|
if (ret == true) {
|
|
|
|
try {
|
|
|
|
ret = file.delete();
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
throw new FILE_DELETE_EXCEPTION();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToFile
|
|
|
|
//
|
|
|
|
// Will write or append to file (valid modes: "w" or "a") if file is available. Returns false if not
|
|
|
|
public Boolean WriteToFile(String fileName, String data, String mode) {
|
|
|
|
Boolean ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WriteToJsonFile
|
|
|
|
//
|
|
|
|
// Will write to or append to a json file. It will search if the key exists first & update the field;
|
|
|
|
// or add a new entry. It should be noted that json objects *can* be buried inside each other. It is
|
|
|
|
// considered best (and only) practice to call the "GetJsonStringFromFile" function first from this
|
|
|
|
// class and simply iterate to what you would need and then update the entire entry alongside the
|
|
|
|
// top-level key.
|
|
|
|
//
|
|
|
|
// NOTE: THIS DOES NOT SAFE UPDATE THE KEY OBJECT. PRE-EXISTING DATA WILL BE DELETED FOREVER
|
|
|
|
public void WriteToJsonFile(String fileName, String key, Object data, String mode) throws FILE_WRITE_EXCEPTION {
|
|
|
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
|
try {
|
|
|
|
gson.toJson(data, new FileWriter(fileName));
|
|
|
|
} catch (JsonIOException | IOException e) {
|
|
|
|
throw new FILE_WRITE_EXCEPTION();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetJsonStringFromFile
|
|
|
|
//
|
|
|
|
// Retrieves a json formatted string from the file based on key. Returns empty string if not found
|
|
|
|
public String GetJsonStringFromFile(String fileName, String key) {
|
|
|
|
String ret = "";
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean DoesFileExist(String fileName) {
|
|
|
|
Boolean ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean DoesDirectoryExist(String dirName) {
|
|
|
|
Boolean ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean CreateDirectory(String dirName) throws DIRECTORY_CREATE_EXCEPTION {
|
|
|
|
Boolean ret = false;
|
|
|
|
|
|
|
|
File dir = new File(dirName);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (dir.exists()) {
|
|
|
|
ret = dir.mkdirs();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new DIRECTORY_CREATE_EXCEPTION();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean DeleteDirectory(String dirName) throws DIRECTORY_DELETE_EXCEPTION {
|
|
|
|
Boolean ret = false;
|
|
|
|
|
|
|
|
File dir = new File(dirName);
|
|
|
|
|
|
|
|
try {
|
|
|
|
ret = dir.delete();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new DIRECTORY_DELETE_EXCEPTION();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddToKey
|
|
|
|
//
|
|
|
|
// Adds new text to a key if found inside the config
|
|
|
|
public String AddToKey(String key, String newInfo) {
|
|
|
|
String ret = "";
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFile
|
|
|
|
//
|
|
|
|
// Returns a file as an arraylist of all the lines in the file. Generally only used for testing
|
|
|
|
//
|
|
|
|
// NOTE: Returns UTF-8 Encoding of file
|
|
|
|
public List<String> GetFile(String fileName) {
|
|
|
|
List<String> ret = new ArrayList<String>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Files.readLines(new File(fileName), Charsets.UTF_8);
|
|
|
|
} catch (IOException e) {
|
|
|
|
ret.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2024-07-24 01:21:47 +00:00
|
|
|
}
|