Versatile Java game engine with pluggable backends (this was used in Rogue, I think)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
5.9 KiB

10 years ago
package mightypork.gamecore.core.config;
import java.util.HashMap;
import java.util.Map;
import mightypork.gamecore.core.events.ShutdownRequest;
import mightypork.gamecore.core.events.ShutdownRequestListener;
10 years ago
import mightypork.gamecore.input.Key;
import mightypork.gamecore.input.KeyStroke;
import mightypork.utils.config.propmgr.Property;
import mightypork.utils.config.propmgr.PropertyManager;
import mightypork.utils.config.propmgr.PropertyStore;
import mightypork.utils.config.propmgr.store.PropertyFile;
import mightypork.utils.files.WorkDir;
10 years ago
import mightypork.utils.logging.Log;
/**
* Settings repository.
*
10 years ago
* @author Ondřej Hruška (MightyPork)
*/
public class Config implements ShutdownRequestListener {
/** Array of configs registered for the app */
10 years ago
protected static Map<String, Config> configs = new HashMap<>();
private final Map<String, KeyStrokeProperty> strokes = new HashMap<>();
private final PropertyManager propertyManager;
10 years ago
/**
* Get a config from the static map, by given alias
*
10 years ago
* @param alias alias
* @return the config
*/
public static Config forAlias(String alias)
{
final Config c = configs.get(alias);
10 years ago
if (c == null) {
throw new IllegalArgumentException("There is no config with alias \"" + alias + "\"");
}
10 years ago
return c;
}
10 years ago
/**
* Register a config by alias.
*
10 years ago
* @param alias config alias
* @param config the config
*/
public static void register(String alias, Config config)
{
if (configs.get(alias) != null) {
throw new IllegalArgumentException("The alias \"" + alias + "\" is already used.");
}
10 years ago
configs.put(alias, config);
}
10 years ago
/**
* Initialize property manager for a file
*
10 years ago
* @param file config file, relative to workdir
* @param headComment file comment
*/
public Config(String file, String headComment)
{
10 years ago
this(new PropertyFile(WorkDir.getFile(file), headComment));
}
10 years ago
/**
* Initialize property manager for a given store
*
10 years ago
* @param store property store backing the property manager
*/
public Config(PropertyStore store)
{
10 years ago
propertyManager = new PropertyManager(store);
}
10 years ago
/**
* Add a keystroke property
*
10 years ago
* @param key key in config file
* @param defval default value (keystroke datastring)
* @param comment optional comment, can be null
*/
public void addKeyStroke(String key, String defval, String comment)
{
final KeyStrokeProperty kprop = new KeyStrokeProperty(prefixKeyStroke(key), KeyStroke.createFromString(defval), comment);
strokes.put(prefixKeyStroke(key), kprop);
propertyManager.addProperty(kprop);
}
10 years ago
/**
* Add a boolean property (flag)
*
10 years ago
* @param key key in config file
* @param defval default value
* @param comment optional comment, can be null
*/
public void addBoolean(String key, boolean defval, String comment)
{
propertyManager.addBoolean(key, defval, comment);
}
10 years ago
/**
* Add an integer property
*
10 years ago
* @param key key in config file
* @param defval default value
* @param comment optional comment, can be null
*/
public void addInteger(String key, int defval, String comment)
{
propertyManager.addInteger(key, defval, comment);
}
10 years ago
/**
* Add a double property
*
10 years ago
* @param key key in config file
* @param defval default value
* @param comment optional comment, can be null
*/
public void addDouble(String key, double defval, String comment)
{
propertyManager.addDouble(key, defval, comment);
}
10 years ago
/**
* Add a string property
*
10 years ago
* @param key key in config file
* @param defval default value
* @param comment optional comment, can be null
*/
public void addString(String key, String defval, String comment)
{
propertyManager.addString(key, defval, comment);
}
10 years ago
/**
* Add an arbitrary property (can be custom type)
*
10 years ago
* @param prop the property to add
*/
public <T> void addProperty(Property<T> prop)
{
propertyManager.addProperty(prop);
}
10 years ago
/**
* Load config from file
*/
public void load()
{
propertyManager.load();
}
10 years ago
/**
* Save config to file
*/
public void save()
{
Log.f3("Saving config.");
propertyManager.save();
}
10 years ago
/**
* Get an option for key
*
* @param key config key
* @return option values
10 years ago
*/
public <T> T getValue(String key)
{
try {
if (propertyManager.getProperty(key) == null) {
throw new IllegalArgumentException("No such property: " + key);
}
10 years ago
return propertyManager.getValue(key);
} catch (final ClassCastException cce) {
throw new RuntimeException("Property of incompatible type: " + key);
}
}
10 years ago
/**
* Set option to a value. Call the save() method to make the change
* permanent.
*
10 years ago
* @param key option key
* @param value value to set
*/
public <T> void setValue(String key, T value)
{
if (propertyManager.getProperty(key) == null) {
throw new IllegalArgumentException("No such property: " + key);
}
10 years ago
propertyManager.setValue(key, value);
}
10 years ago
/**
* Add "key." before the given config file key
*
10 years ago
* @param cfgKey config key
* @return key. + cfgKey
*/
private String prefixKeyStroke(String cfgKey)
{
return "key." + cfgKey;
}
10 years ago
/**
* Get keystroke for name
*
10 years ago
* @param cfgKey stroke identifier in config file
* @return the stroke
*/
public KeyStroke getKeyStroke(String cfgKey)
{
final KeyStrokeProperty kp = strokes.get(prefixKeyStroke(cfgKey));
if (kp == null) {
throw new IllegalArgumentException("No such stroke: " + cfgKey);
}
10 years ago
return kp.getValue();
}
10 years ago
/**
* Set a keystroke for name
*
10 years ago
* @param cfgKey stroke identifier in config file
* @param key stroke key
* @param mod stroke modifiers
*/
public void setKeyStroke(String cfgKey, Key key, int mod)
{
final KeyStrokeProperty kp = strokes.get(prefixKeyStroke(cfgKey));
if (kp == null) {
throw new IllegalArgumentException("No such stroke: " + cfgKey);
}
10 years ago
kp.getValue().setTo(key, mod);
}
@Override
public void onShutdownRequested(ShutdownRequest event)
{
save(); // save changes done to the config
}
10 years ago
}