Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow MML config to be loaded in memory only #859

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/megameklab/com/MegaMekLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static void startup() {
} catch (IOException e) {
getLogger().warning(MegaMekLab.class, "startup()", "Could not load quirks");
}
new CConfig();
CConfig.load();
UnitUtil.loadFonts();

// Add additional themes
Expand Down
59 changes: 31 additions & 28 deletions src/megameklab/com/util/CConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,18 @@ public String shortName() {
public static final String RS_SCALE_FACTOR = "rs_scale_factor";
public static final String RS_SCALE_UNITS = "rs_scale_units";

private static Properties config;// config. player values.

// CONSTRUCTOR
public CConfig() {

if(!new File(CONFIG_DIR).exists()) {
new File(CONFIG_DIR).mkdir();
}

config = setDefaults();
// check to see if a config is present. if not, make one.
if (!(new File(CONFIG_FILE).exists()) && !(new File(CONFIG_BACKUP_FILE).exists())) {
createConfig();
}

CConfig.loadConfigFile();
}
/**
* Player configuration values.
*/
private static Properties config = getDefaults();

// METHODS
/**
* Private method that loads hardcoded defaults. These are loaded before the
* players config values, adding any new configs in their default position
* and ensuring that no config value is even missing.
*/
private Properties setDefaults() {
private static Properties getDefaults() {
Properties defaults = new Properties();

// Window Locations
Expand All @@ -165,6 +152,15 @@ private Properties setDefaults() {
return defaults;
}

/**
* Loads the MegaMekLab configuration.
*/
public static void load() {
ensureConfigFileExists();

loadConfigFile();
}

/**
* Loads the Config file.
*/
Expand Down Expand Up @@ -203,17 +199,24 @@ public static void loadConfigFile() {
ex.printStackTrace();
}
}

/**
* Creates a new Config file, and directories, if it is missing.
*/
public static void ensureConfigFileExists() {
// check to see if a config is present. if not, make one.
if (!(new File(CONFIG_FILE).exists()) && !(new File(CONFIG_BACKUP_FILE).exists())) {
if(!new File(CONFIG_DIR).exists()) {
new File(CONFIG_DIR).mkdir();
}

// Creates a new config file
public void createConfig() {
try {
FileOutputStream fos = new FileOutputStream(CONFIG_FILE);
PrintStream ps = new PrintStream(fos);

ps.close();
fos.close();
} catch (Exception ex) {
System.exit(0);
try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE);
PrintStream ps = new PrintStream(fos)) {
ps.close();
fos.close();
} catch (Exception ex) {
System.exit(0);
}
}
}

Expand Down