2024-07-29 00:13:48 +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 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 > ( ) { {
2024-08-01 02:50:08 +00:00
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!
2024-07-29 00:13:48 +00:00
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 " ) ;
2024-08-01 02:50:08 +00:00
System . out . println ( " test_dir created? " + ( has_write ? " YES " : " NO " ) ) ;
2024-07-29 00:13:48 +00:00
} catch ( DIRECTORY_CREATE_EXCEPTION e ) {
2024-08-01 02:50:08 +00:00
System . out . println ( " Failed to create test directory or it already exists " ) ;
has_write = false ;
2024-07-29 00:13:48 +00:00
}
// Write to disk then read that data back
if ( has_write ) {
try {
2024-08-01 02:50:08 +00:00
has_write = conf . CreateFile ( " test_dir/test_note.txt " ) ;
has_write = conf . WriteToFile ( " test_dir/test_note.txt " , " test_write_read " , " w " ) ;
2024-07-29 00:13:48 +00:00
2024-08-01 02:50:08 +00:00
List < String > lines = conf . GetFile ( " test_dir/test_note.txt " ) ;
2024-07-29 00:13:48 +00:00
if ( lines . size ( ) = = 0 ) {
has_read = false ;
2024-08-01 02:50:08 +00:00
} else {
has_read = true ;
2024-07-29 00:13:48 +00:00
}
} catch ( Exception e ) {
2024-08-01 02:50:08 +00:00
System . out . println ( " Failed to create or write to test dir file " ) ;
2024-07-29 00:13:48 +00:00
has_read = false ;
}
}
// Delete directory if created (it's a temporary dir)
if ( has_write ) {
try {
2024-08-01 02:50:08 +00:00
has_write = conf . DeleteFile ( " test_dir/test_note.txt " ) ;
2024-07-29 00:13:48 +00:00
has_write = conf . DeleteDirectory ( " test_dir " ) ;
2024-08-01 02:50:08 +00:00
} catch ( Exception e ) {
System . out . println ( " Lost access to writing mid-way " ) ;
2024-07-29 00:13:48 +00:00
has_write = false ;
}
}
2024-08-01 02:50:08 +00:00
System . out . println ( " CHECKS DEBUG: Value of has_write: " + has_write + " . Value of has_read: " + has_read ) ;
2024-07-29 00:13:48 +00:00
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 ) ) ;
2024-08-01 02:50:08 +00:00
System . out . println ( " Creating directory " + DIRECTORY_LIST . get ( i ) + " ... " ) ;
} else {
System . out . println ( " Directory " + conf . DoesDirectoryExist ( DIRECTORY_LIST . get ( i ) ) + " already exists. Skipping... " ) ;
2024-07-29 00:13:48 +00:00
}
}
// Create necessary files
for ( Integer i = 0 ; i < FILE_LIST . size ( ) ; i + + ) {
2024-08-01 02:50:08 +00:00
if ( ! conf . DoesFileExist ( FILE_LIST . get ( i ) ) ) {
2024-07-29 00:13:48 +00:00
conf . CreateFile ( FILE_LIST . get ( i ) ) ;
2024-08-01 02:50:08 +00:00
System . out . println ( " Creating file " + FILE_LIST . get ( i ) + " ... " ) ;
} else {
System . out . println ( " File " + conf . DoesDirectoryExist ( FILE_LIST . get ( i ) ) + " already exists. Skipping... " ) ;
2024-07-29 00:13:48 +00:00
}
}
} catch ( Exception e ) {
throw new SETUP_FAILED_EXCEPTION ( ) ;
}
} else {
2024-08-01 02:50:08 +00:00
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. " ) ;
2024-07-29 00:13:48 +00:00
throw new SETUP_FAILED_EXCEPTION ( ) ;
}
2024-08-01 02:50:08 +00:00
System . out . println ( " DID SETUP COMPLETE SUCCESSFULLY? " + ( ret ? " YES " : " NO " ) ) ;
2024-07-29 00:13:48 +00:00
return ret ;
}
}