Skip to content

Commit

Permalink
Update Pulsar to 5803b3.
Browse files Browse the repository at this point in the history
Add configuration system.
  • Loading branch information
Sunstrike committed Jun 16, 2014
1 parent 1b41a67 commit 39c821b
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/mantle/pulsar/control/PulseManager.java
@@ -1,6 +1,5 @@
package mantle.pulsar.control;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -9,11 +8,11 @@
import org.apache.logging.log4j.Logger;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

import mantle.pulsar.internal.Configuration;
import mantle.pulsar.internal.PulseMeta;
import mantle.pulsar.pulse.IPulse;
import mantle.pulsar.pulse.Pulse;
Expand All @@ -30,11 +29,12 @@ public class PulseManager {

private final Logger log;
private final boolean useConfig;
private final String configPath;
private final String configName;

private final HashMap<IPulse, PulseMeta> pulses = new HashMap<IPulse, PulseMeta>();

private boolean blockNewRegistrations = false;
private Configuration conf = null;

/**
* Configuration-less constructor.
Expand All @@ -43,10 +43,11 @@ public class PulseManager {
*
* @param modId The parents ModID.
*/
@Deprecated
public PulseManager(String modId) {
log = LogManager.getLogger("PulseManager-" + modId);
useConfig = false;
configPath = null;
configName = null;
}

/**
Expand All @@ -60,7 +61,7 @@ public PulseManager(String modId) {
public PulseManager(String modId, String configName) {
log = LogManager.getLogger("PulseManager-" + modId);
useConfig = true;
this.configPath = Loader.instance().getConfigDir().toString() + File.pathSeparator + configName;
this.configName = configName;
}

/**
Expand Down Expand Up @@ -96,9 +97,13 @@ public void registerPulse(IPulse pulse) {
}

private boolean getEnabledFromConfig(PulseMeta meta) {
if (meta.isForced()) return true;
// TODO: Check configuration and set enabled flag as needed.
return true;
if (meta.isForced() || !useConfig) return true; // Forced or no config set.

if (conf == null) {
conf = new Configuration(configName, log);
}

return conf.isModuleEnabled(meta.getId(), meta.isEnabled());
}

private void parseAndAddProxies(IPulse pulse) {
Expand Down
111 changes: 111 additions & 0 deletions src/mantle/pulsar/internal/Configuration.java
@@ -0,0 +1,111 @@
package mantle.pulsar.internal;

import java.io.*;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import cpw.mods.fml.common.Loader;

/**
* Gson Configuration helper.
*
* @author Arkan <arkan@drakon.io>
*/
public class Configuration {

private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final String confPath;
private final Logger logger;

private Map<String, Boolean> modules;

/**
* Creates a new Configuration object.
*
* Do NOT make this the same as the overall mod configuration; it will clobber it!
*
* @param confName The config file name (without path or .json suffix)
* @param logger The logger to send debug info to.
*/
public Configuration(String confName, Logger logger) {
this.confPath = Loader.instance().getConfigDir().toString() + File.separator + confName + ".json";
this.logger = logger;
this.modules = getModulesFromJson();
}

/**
* Gets whether the given module is enabled in the config.
*
* @param id Module to lookup.
* @param defaultState Whether the module should be enabled or disabled by default (used to push new entries)
* @return Whether the module is enabled.
*/
public boolean isModuleEnabled(String id, boolean defaultState) {
Boolean enabled = modules.get(id);
if (enabled == null) {
modules.put(id, defaultState);
writeModulesToJson();
enabled = defaultState;
}
return enabled;
}

private Map<String, Boolean> getModulesFromJson() {
// Step 1: Does the file exist?
File f = new File(confPath);
if (!f.exists()) {
logger.info("Couldn't find config file; will generate a new one later.");
return new HashMap<String, Boolean>();
}

// Step 2: File exists. Let's make sure it's usable.
if (!(f.canRead() && f.canWrite())) {
throw new FileNotReadWritableException("Could not read/write Pulsar config: " + confPath);
}

// Step 3: Good enough. Read it.
try {
JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(f)));
Map<String, Boolean> m = gson.fromJson(reader, new TypeToken<HashMap<String, Boolean>>(){}.getType()); // NASTY!
if (m == null) {
throw new NullPointerException("Gson returned null.");
}
return m;
} catch (FileNotFoundException fnfe) {
throw new RuntimeException("This shouldn't be possible... " + fnfe);
} catch (Exception ex) {
logger.warn("Invalid config file. Discarding.");
return new HashMap<String, Boolean>();
}
}

private void writeModulesToJson() {
try {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(new File(confPath))));
writer.setIndent(" ");
Type t = new TypeToken<Map<String, Boolean>>(){}.getType();
gson.toJson(modules, t, writer);
writer.close();
} catch (Exception ex) {
logger.warn("Could not write config? " + confPath);
}
}

/**
* Internal exception for an unreadable/unwritable config.
*/
private static class FileNotReadWritableException extends RuntimeException {
public FileNotReadWritableException(String message) {
super(message);
}
}

}

0 comments on commit 39c821b

Please sign in to comment.