the_big_one/src/main/java/jesse/keeblarcraft/Utils/Setup.java

163 lines
6.8 KiB
Java
Raw Normal View History

/*
*
* Setup.java
*
* Setup is a singleton object that runs on the initial mod setup to run critical checks to make sure the mod
* is allowed to do things on the users filesystem (reading conf files, writing conf files, etc). It's quite
* important for the mod to know this!
*
*/
package jesse.keeblarcraft.Utils;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.spongepowered.include.com.google.common.io.Files;
import java.util.List;
import java.util.ArrayList;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_CREATE_EXCEPTION;
import jesse.keeblarcraft.Utils.CustomExceptions.DIRECTORY_DELETE_EXCEPTION;
import jesse.keeblarcraft.Utils.CustomExceptions.SETUP_FAILED_EXCEPTION;
// Singleton class is designed to help the mod set itself up and create all the important things. It does two things:
//
// Thing 1: If on initial setup of mod for first time; creates all necessary directories and files.
// Doing this helps the mod know if it has permissions to do things, or if it shouldn't even bother!
// Some functionality is disabled if the mod can't access the hard drive in various ways.
//
// Thing 2: On any sequential startup; it checks to make sure all the directories exist & replaces missing ones.
// It will also do a state-check of the system to make sure we will have permissions to read & write.
// If we do not have these, or only partial - then functionality may be disabled for runtime performance
public final class Setup {
private static Setup static_inst;
public Setup() {
System.out.println("Running system setup and checks...");
}
// Returns the singleton object
public static Setup GetInstance() {
if (static_inst == null) {
static_inst = new Setup();
}
return static_inst;
}
/// Unit testing functions below
public static Boolean has_read = false;
public static Boolean has_write = false;
// First time setup variables
private static final List<String> DIRECTORY_LIST = new ArrayList<String>() {{
add("notes"); // Expect 1 file per player!
add("factions"); // Expect 1 file per faction!
add("story"); // Expect 1 file per story chapter!
add("commands"); // Expect 1 file per command that's configurable!
add("events"); // Expect 1 file per event that is configurable!
}};
// These will be top-level config files above the directories this mod creates
private static final List<String> FILE_LIST = new ArrayList<String>() {{
add("story/story.json"); // Big config file, determines when players can do certain things at different story levels
add("factions/factions.json"); // General configuration file for factions stuff
add("events/1events.json"); // General configuration file for story events!
add("general.json"); // The super general configuration file! (May be removed)
}};
// RunChecks()
//
// Checks if we are able to create necessary directories and run reading over the file system for the current
// directory this mod is maintained in. This will return false if any checks fail; but individual checks can
// be accessed in class variables (above)
public Boolean RunChecks() {
ConfigManager conf = new ConfigManager();
// Create directory check
try {
has_write = conf.CreateDirectory("test_dir");
System.out.println("test_dir created? " + (has_write ? "YES" : "NO"));
} catch (DIRECTORY_CREATE_EXCEPTION e) {
System.out.println("Failed to create test directory or it already exists");
has_write = false;
}
// Write to disk then read that data back
if (has_write) {
try {
has_write = conf.CreateFile("test_dir/test_note.txt");
has_write = conf.WriteToFile("test_dir/test_note.txt", "test_write_read", "w");
List<String> lines = conf.GetFile("test_dir/test_note.txt");
if (lines.size() == 0) {
has_read = false;
} else {
has_read = true;
}
} catch (Exception e) {
System.out.println("Failed to create or write to test dir file");
has_read = false;
}
}
// Delete directory if created (it's a temporary dir)
if (has_write) {
try {
has_write = conf.DeleteFile("test_dir/test_note.txt");
has_write = conf.DeleteDirectory("test_dir");
} catch (Exception e) {
System.out.println("Lost access to writing mid-way");
has_write = false;
}
}
System.out.println("CHECKS DEBUG: Value of has_write: " + has_write + ". Value of has_read: " + has_read);
return has_write && has_read;
}
// RunSetup
//
// Primary function call that will execute when mod starts up
public Boolean RunSetup() throws SETUP_FAILED_EXCEPTION {
Boolean ret = false;
// Setup can only complete if it has guarenteed we have read & write permissions
if (ret = RunChecks()) {
try {
// Create necessary directories
ConfigManager conf = new ConfigManager();
for (Integer i = 0; i < DIRECTORY_LIST.size(); i++) {
if ( ! conf.DoesDirectoryExist(DIRECTORY_LIST.get(i))) {
conf.CreateDirectory(DIRECTORY_LIST.get(i));
System.out.println("Creating directory " + DIRECTORY_LIST.get(i) + "...");
} else {
System.out.println("Directory " + conf.DoesDirectoryExist(DIRECTORY_LIST.get(i)) + " already exists. Skipping...");
}
}
// Create necessary files
for (Integer i = 0; i < FILE_LIST.size(); i++) {
if ( ! conf.DoesFileExist(FILE_LIST.get(i))) {
conf.CreateFile(FILE_LIST.get(i));
System.out.println("Creating file " + FILE_LIST.get(i) + "...");
} else {
System.out.println("File " + conf.DoesDirectoryExist(FILE_LIST.get(i)) + " already exists. Skipping...");
}
}
} catch (Exception e) {
throw new SETUP_FAILED_EXCEPTION();
}
} else {
System.out.println("RunChecks() failed in its process. This mod has deemed it does not have read or write privileges in its hosted area and will now exit.");
throw new SETUP_FAILED_EXCEPTION();
}
System.out.println("DID SETUP COMPLETE SUCCESSFULLY? " + (ret ? "YES" : "NO"));
return ret;
}
}