2024-10-28 21:47:51 +00:00
/ *
*
* 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 java.util.ArrayList ;
2025-01-02 03:47:50 +00:00
import jesse.keeblarcraft.Keeblarcraft ;
2024-10-28 21:47:51 +00:00
import jesse.keeblarcraft.ConfigMgr.ConfigManager ;
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 ;
2025-01-02 03:47:50 +00:00
public Setup ( ) { }
2024-10-28 21:47:51 +00:00
// 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 > ( ) { {
2025-01-19 07:56:17 +00:00
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!
add ( " bank " ) ;
add ( " attributes " ) ;
add ( " misc " ) ;
2024-10-28 21:47:51 +00:00
} } ;
// These will be top-level config files above the directories this mod creates
private static final List < String > FILE_LIST = new ArrayList < String > ( ) { {
2025-01-19 07:56:17 +00:00
add ( " story/general_story_config.json " ) ; // Big config file, determines when players can do certain things at different story levels
add ( " factions/general_factions_config.json " ) ; // General configuration file for factions stuff
add ( " events/general_event_config.json " ) ; // General configuration file for story events!
add ( " general.json " ) ; // The super general configuration file! (May be removed)
add ( " attributes/general_attribute_config.json " ) ;
2024-10-28 21:47:51 +00:00
} } ;
// 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
2025-01-02 03:47:50 +00:00
has_write = conf . CreateDirectory ( " test_dir " ) ;
2024-10-28 21:47:51 +00:00
2025-01-02 03:47:50 +00:00
if ( ! has_write ) {
Keeblarcraft . LOGGER . error ( " Uh oh! It appears we could not successfully identify that we have write permissions. Attempt to create a directory failed. " ) ;
} else {
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 . GetFileLines ( " test_dir/test_note.txt " ) ;
if ( lines . size ( ) = = 0 ) {
2024-10-28 21:47:51 +00:00
has_read = false ;
2025-01-02 03:47:50 +00:00
} else {
has_read = true ;
2024-10-28 21:47:51 +00:00
}
2025-01-02 03:47:50 +00:00
has_write = conf . DeleteFile ( " test_dir/test_note.txt " ) ;
has_write = conf . DeleteDirectory ( " test_dir " ) ;
if ( ! has_write ) {
Keeblarcraft . LOGGER . error ( " Uh oh! It appears we lost the ability to write or delete files after making one. Something is seriously wrong! " ) ;
2024-10-28 21:47:51 +00:00
}
}
2025-01-02 03:47:50 +00:00
Keeblarcraft . LOGGER . debug ( " CHECKS DEBUG: Value of has_write: " + has_write . toString ( ) + " . Value of has_read: " + has_read . toString ( ) ) ;
// return has_write && has_read;
return true ; /// TODO: ConfigManager has been rewritten so much that the above code is out dated. Fix later return true for now
2024-10-28 21:47:51 +00:00
}
// 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 ) ) ;
2025-01-02 03:47:50 +00:00
Keeblarcraft . LOGGER . debug ( " Creating directory " + DIRECTORY_LIST . get ( i ) ) ;
2024-10-28 21:47:51 +00:00
} else {
2025-01-02 03:47:50 +00:00
Keeblarcraft . LOGGER . debug ( DIRECTORY_LIST . get ( i ) + " already exists. Skipping creation. " ) ;
2024-10-28 21:47:51 +00:00
}
}
// 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 ) ) ;
2025-01-02 03:47:50 +00:00
Keeblarcraft . LOGGER . debug ( " Creating file " + DIRECTORY_LIST . get ( i ) ) ;
2024-10-28 21:47:51 +00:00
} else {
2025-01-02 03:47:50 +00:00
Keeblarcraft . LOGGER . debug ( DIRECTORY_LIST . get ( i ) + " already exists. Skipping creation. " ) ;
2024-10-28 21:47:51 +00:00
}
}
} catch ( Exception e ) {
throw new SETUP_FAILED_EXCEPTION ( ) ;
}
} else {
2025-01-02 03:47:50 +00:00
Keeblarcraft . LOGGER . error ( " The setup phase failed in its processing and has determined it does not have necessary read or write priviliges in this hosted area and will now exit. " ) ;
2024-10-28 21:47:51 +00:00
throw new SETUP_FAILED_EXCEPTION ( ) ;
}
2025-01-02 03:47:50 +00:00
Keeblarcraft . LOGGER . debug ( " DID SETUP COMPLETE SUCCESSFULLY? " + ( ret ? " YES " : " NO " ) ) ;
2024-10-28 21:47:51 +00:00
return ret ;
}
}