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

Added FCManager and support for FC serialization #3118

Merged
merged 17 commits into from
Dec 20, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ protected void setupAudio() {

@Override
protected void setupConfig() {
Config config = new Config();
Config config = new Config(context);
config.loadDefaults();
context.put(Config.class, config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.terasology.config.flexible.settings.Setting;
import org.terasology.engine.SimpleUri;

import static org.junit.Assert.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.terasology.config.flexible;

import org.terasology.config.flexible.settings.Setting;
import org.terasology.config.flexible.validators.SettingValueValidator;
import org.terasology.engine.SimpleUri;

Expand Down Expand Up @@ -79,4 +80,7 @@ public String getDescription() {
public boolean hasSubscribers() {
return isSubscribedTo;
}

@Override
public void setValueFromString(String valueString) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.terasology.config.flexible.settings.Setting;
import org.terasology.config.flexible.settings.SettingImpl;
import org.terasology.config.flexible.validators.RangedNumberValidator;
import org.terasology.engine.SimpleUri;
import org.terasology.utilities.random.FastRandom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.Test;
import org.terasology.config.Config;
import org.terasology.context.internal.ContextImpl;
import org.terasology.context.internal.MockContext;
import org.terasology.math.geom.Vector3i;
import org.terasology.registry.CoreRegistry;

Expand All @@ -34,7 +35,7 @@ public void testGetEdgeRegion() {
@Test
public void testRegionPositions() {
CoreRegistry.setContext(new ContextImpl());
CoreRegistry.put(Config.class, new Config());
CoreRegistry.put(Config.class, new Config(new MockContext()));

assertEquals(1, ChunkMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(0, 0, 0))).length);
assertEquals(1, ChunkMath.calcChunkPos(Region3i.createFromMinMax(new Vector3i(0, 0, 0), new Vector3i(31, 63, 31))).length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test;
import org.terasology.config.Config;
import org.terasology.context.internal.ContextImpl;
import org.terasology.context.internal.MockContext;
import org.terasology.registry.CoreRegistry;

import java.util.ArrayList;
Expand All @@ -37,7 +38,7 @@ public IntMathTest() {

@BeforeClass
public static void setUpClass() throws Exception {
Config config = new Config();
Config config = new Config(new MockContext());
CoreRegistry.setContext(new ContextImpl());
CoreRegistry.put(Config.class, config);
}
Expand Down
10 changes: 10 additions & 0 deletions engine/src/main/java/org/terasology/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.lwjgl.opengl.PixelFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.flexible.FlexibleConfigManager;
import org.terasology.context.Context;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.TerasologyConstants;
import org.terasology.engine.paths.PathManager;
Expand Down Expand Up @@ -70,6 +72,12 @@ public final class Config {

private RootConfig config;

private Context context;

public Config(Context context) {
this.context = context;
}

public PermissionConfig getPermission() {
return config.getPermission();
}
Expand Down Expand Up @@ -143,6 +151,8 @@ public void save() {
} catch (IOException e) {
logger.error("Failed to save config", e);
}

context.get(FlexibleConfigManager.class).saveAllConfigs();
}

public void loadDefaults() {
Expand Down
4 changes: 3 additions & 1 deletion engine/src/main/java/org/terasology/config/InputConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.terasology.config;

import org.terasology.context.internal.MockContext;

public class InputConfig {

private ControllerConfig controllers = new ControllerConfig();
Expand All @@ -34,7 +36,7 @@ public void setMouseSensitivity(float mouseSensitivity) {
}

public void reset() {
Config defaultConfig = new Config();
Config defaultConfig = new Config(new MockContext());
defaultConfig.loadDefaults();
InputConfig defaultInputConfig = defaultConfig.getInput();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
*/
package org.terasology.config.flexible;

import org.terasology.config.flexible.settings.Setting;
import org.terasology.engine.SimpleUri;

import java.io.Reader;
import java.io.Writer;
import java.util.Map;

/**
* Stores multiple {@link Setting} instances that can be retrieved using their id.
*/
Expand Down Expand Up @@ -54,4 +59,35 @@ public interface FlexibleConfig {
* Returns a boolean stating whether the config contains a {@link Setting} with the given id.
*/
boolean contains(SimpleUri id);

/**
* Returns a map of all the settings, allowing iteration of all the settings.
*
* @return A map containing all the settings, along with their id.
*/
Map<SimpleUri, Setting> getSettings();

/**
* Saves the values of all settings having non-default values, to enable persistence across sessions.
*
* All the non-default values that were not used in this session and are still "parked" are also
* saved as-is, to be used later.
*
* @param writer A writer that will serve as the destination of settings.
*/
void save(Writer writer);

/**
* Loads the values of the settings having non-default values, to enable persistence across sessions.
*
* All the non-default values are loaded and "parked", initially remaining inaccessible.
* Once a Setting object is added to the config, a corresponding non-default value is sought
* among the parked values. If one is found it is parsed and stored in the Setting object.
*
* Note that this function should be called -before- adding any settings to the FlexibleConfig.
* Otherwise any corresponding parked value will never be loaded.
*
* @param reader A reader that will serve as the source of the settings.
*/
void load(Reader reader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@
package org.terasology.config.flexible;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.flexible.settings.Setting;
import org.terasology.engine.SimpleUri;

import java.io.Reader;
import java.io.Writer;
import java.util.Map;

/**
* {@inheritDoc}
*/
public class FlexibleConfigImpl implements FlexibleConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(FlexibleConfigImpl.class);
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();

private Map<SimpleUri, Setting> settingMap;

/**
* Creates a new {@link FlexibleConfigImpl} instance.
*/
public FlexibleConfigImpl() {
this.settingMap = Maps.newHashMap();
}
private final Map<SimpleUri, Setting> settings = Maps.newHashMap();
private final Map<SimpleUri, String> temporarilyParkedSettings = Maps.newHashMap();

/**
* {@inheritDoc}
Expand All @@ -52,7 +54,12 @@ public boolean add(Setting setting) {
return false;
}

settingMap.put(id, setting);
if (temporarilyParkedSettings.containsKey(id)) {
setting.setValueFromString(temporarilyParkedSettings.remove(id));
}

settings.put(id, setting);

return true;
}

Expand All @@ -71,7 +78,7 @@ public boolean remove(SimpleUri id) {
return false;
}

settingMap.remove(id);
settings.remove(id);
return true;
}

Expand All @@ -81,14 +88,55 @@ public boolean remove(SimpleUri id) {
@Override
@SuppressWarnings("unchecked")
public <V> Setting<V> get(SimpleUri id) {
return (Setting<V>) settingMap.get(id);
return (Setting<V>) settings.get(id);
}

/**
* {@inheritDoc}
*/
@Override
public boolean contains(SimpleUri id) {
return settingMap.containsKey(id);
return settings.containsKey(id);
}

@Override
public Map<SimpleUri, Setting> getSettings() {
return settings;
}

@Override
public void save(Writer writer) {
JsonObject jsonObject = new JsonObject();

for (Map.Entry<SimpleUri, Setting> entry : settings.entrySet()) {
Setting setting = entry.getValue();
if (!setting.getValue().equals(setting.getDefaultValue())) {
jsonObject.addProperty(entry.getKey().toString(), setting.getValue().toString());
}
}

// Add any temporarily parked setting that hasn't been used in this session of the application.
for (Map.Entry<SimpleUri, String> entry : temporarilyParkedSettings.entrySet()) {
jsonObject.addProperty(entry.getKey().toString(), entry.getValue());
}

gson.toJson(jsonObject, writer);
}

@Override
public void load(Reader reader) {
try (JsonReader jsonReader = new JsonReader(reader)) {
jsonReader.beginObject();

while (jsonReader.hasNext()) {
SimpleUri id = new SimpleUri(jsonReader.nextName());
String value = jsonReader.nextString();
temporarilyParkedSettings.put(id, value);
}

jsonReader.endObject();
} catch (Exception e) {
throw new RuntimeException("Error parsing config file!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology.config.flexible;

import org.terasology.engine.SimpleUri;

/**
* Stores multiple {@link FlexibleConfig} instances that can be retrieved using their id.
* Also responsible for coordinating their serialization - to and from - disk.
*/
public interface FlexibleConfigManager {
/**
* Adds the given {@link FlexibleConfig} to this manager.
*
* @param configId A SimpleUri that effectively becomes the id of the config.
* @param config The config that is to be added.
* @throws RuntimeException if a FlexibleConfig with the given id already exists.
*/
void addConfig(SimpleUri configId, FlexibleConfig config) throws RuntimeException;

/**
* Removes the config associated with the given id, and returns it.
*
* @param configId The id of the config to remove.
* @return The config associated with the given id, or null if no config is associated with the given id.
*/
FlexibleConfig removeConfig(SimpleUri configId);

/**
* Retrieves the config associated with the given id.
*
* @param configId The id of the config to retrieve.
* @return The config associated with the given id, or null if no config is associated with the given id.
*/
FlexibleConfig getConfig(SimpleUri configId);

/**
* Iterates over all the configs added to this manager, and loads the settings stored in them.
*/
void loadAllConfigs();

/**
* Iterates over all the configs added to this manager, and saves the settings stored in them.
*/
void saveAllConfigs();
}
Loading