Skip to content

Commit

Permalink
Merge pull request #185 from ManuelHu/feature/set
Browse files Browse the repository at this point in the history
Settings erweitert
tomwendel authored Nov 24, 2016
2 parents 17b2c1c + 3b8028c commit 811b2d1
Showing 8 changed files with 155 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ public OptionsOptionControl(ScreenComponent manager, OptionsScreen optionsScreen

Checkbox disablePersistence = new Checkbox(manager)
{
Checked = bool.Parse(settings.Get<string>("DisablePersistence")),
Checked = settings.Get("DisablePersistence", false),
HookBrush = new TextureBrush(manager.Game.Assets.LoadTexture(typeof(ScreenComponent), "iconCheck_brown"), TextureBrushMode.Stretch),
};
disablePersistence.CheckedChanged += (state) => SetPersistence(state);
@@ -110,7 +110,7 @@ public OptionsOptionControl(ScreenComponent manager, OptionsScreen optionsScreen

Checkbox enableFullscreen = new Checkbox(manager)
{
Checked = bool.Parse(settings.Get<string>("EnableFullscreen")),
Checked = settings.Get<bool>("EnableFullscreen"),
HookBrush = new TextureBrush(manager.Game.Assets.LoadTexture(typeof(ScreenComponent), "iconCheck_brown"), TextureBrushMode.Stretch),
};
enableFullscreen.CheckedChanged += (state) => SetFullscreen(state);
10 changes: 3 additions & 7 deletions OctoAwesome/OctoAwesome.Client/OctoGame.cs
Original file line number Diff line number Diff line change
@@ -53,15 +53,11 @@ public OctoGame()

//TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 15);

int width = 1080, height = 720;
if (Settings.KeyExists("Width"))
width = Settings.Get<int>("Width");
if (Settings.KeyExists("Height"))
height = Settings.Get<int>("Height");
int width = Settings.Get("Width", 1080);
int height = Settings.Get("Height", 720);
Window.ClientSize = new Size(width, height);

if (Settings.KeyExists("EnableFullscreen") && Settings.Get<bool>("EnableFullscreen"))
Window.Fullscreen = true;
Window.Fullscreen = Settings.Get("EnableFullscreen", false);

if (Settings.KeyExists("Viewrange"))
{
3 changes: 1 addition & 2 deletions OctoAwesome/OctoAwesome.Client/Screens/LoadScreen.cs
Original file line number Diff line number Diff line change
@@ -135,8 +135,7 @@ public LoadScreen(ScreenComponent manager) : base(manager)
if (levelList.Items.Count >= 1)
levelList.SelectedItem = levelList.Items[0];

if (settings.KeyExists("LastUniverse") && settings.Get<string>("LastUniverse") != null
&& settings.Get<string>("LastUniverse") != "")
if (settings.Get<string>("LastUniverse") != null)
{
var lastlevel = levelList.Items.FirstOrDefault(u => u.Id == Guid.Parse(settings.Get<string>("LastUniverse")));
if (lastlevel != null)
2 changes: 1 addition & 1 deletion OctoAwesome/OctoAwesome.Runtime/ResourceManager.cs
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ private ResourceManager()

planets = new Dictionary<int, IPlanet>();

bool.TryParse(Settings.Get<string>("DisablePersistence"), out disablePersistence);
disablePersistence = Settings.Get("DisablePersistence", false);
}

/// <summary>
2 changes: 1 addition & 1 deletion OctoAwesome/OctoAwesome.Tests/OctoAwesome.Tests.csproj
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7E2C82CC-CCC7-4837-9662-9850092BDC3B}</ProjectGuid>
<ProjectGuid>{0468B67C-E076-4B19-833C-382513606A5A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OctoAwesome.Tests</RootNamespace>
164 changes: 90 additions & 74 deletions OctoAwesome/OctoAwesome.Tests/SettingsManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,90 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace OctoAwesome.Tests
{

public class SettingsManagerTests
{
[Fact]
public void ReadWrite()
{
Settings settings = new Settings(true);

string[] testArray = new string[] {"foo", "bar"};
settings.Set("foo", testArray);

string[] newArray = settings.GetArray<string>("foo");

Assert.True(testArray.SequenceEqual(newArray));


int[] testArrayInt = new int[] { 3,5,333,456,3457};
settings.Set("fooInt", testArrayInt);

int[] newArrayInt = settings.GetArray<int>("fooInt");

Assert.True(testArray.SequenceEqual(newArray));


bool[] testArrayBool = new bool[] { true, false};
settings.Set("fooBool", testArrayBool);

bool[] newArrayBool = settings.GetArray<bool>("fooBool");

Assert.True(testArray.SequenceEqual(newArray));


String inputString = "randomStringWith§$%&/()=Charakters";
settings.Set("inputString", inputString);


Assert.Equal(inputString, settings.Get<string>("inputString"));


int inputInt = new Random().Next();
settings.Set("inputInt", inputInt);

Assert.Equal(inputInt, settings.Get<int>("inputInt"));


bool inputBool = true;
settings.Set("inputBool", inputBool);

Assert.Equal(inputBool, settings.Get<bool>("inputBool"));





}

[Fact]
public void NullTest()
{

Settings settings = new Settings(true);

int test = settings.Get<int>("foobarnotset");
Assert.Equal(0,test);
}
}
}
using System;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace OctoAwesome.Tests
{
public class SettingsManagerTests
{
[Fact]
public void ReadWrite()
{
Settings settings = new Settings(true);

string[] testArray = new string[] { "foo", "bar" };
settings.Set("foo", testArray);

string[] newArray = settings.GetArray<string>("foo");

Assert.True(testArray.SequenceEqual(newArray));


int[] testArrayInt = new int[] { 3, 5, 333, 456, 3457 };
settings.Set("fooInt", testArrayInt);

int[] newArrayInt = settings.GetArray<int>("fooInt");

Assert.True(testArray.SequenceEqual(newArray));


bool[] testArrayBool = new bool[] { true, false };
settings.Set("fooBool", testArrayBool);

bool[] newArrayBool = settings.GetArray<bool>("fooBool");

Assert.True(testArray.SequenceEqual(newArray));


String inputString = "randomStringWith§$%&/()=Charakters";
settings.Set("inputString", inputString);


Assert.Equal(inputString, settings.Get<string>("inputString"));


int inputInt = new Random().Next();
settings.Set("inputInt", inputInt);

Assert.Equal(inputInt, settings.Get<int>("inputInt"));


bool inputBool = true;
settings.Set("inputBool", inputBool);

Assert.Equal(inputBool, settings.Get<bool>("inputBool"));
}

[Fact]
public void UnsetTest()
{
Settings settings = new Settings(true);

int testInt = settings.Get<int>("foobarnotset");
Assert.Equal(0, testInt);

string testString = settings.Get<string>("foobarnotset");
Assert.Equal(null, testString);

int testIntDefault = settings.Get("foobarnotset", 42);
Assert.Equal(42, testIntDefault);

string testStringDefault = settings.Get("foobarnotset", "ABC");
Assert.Equal("ABC", testStringDefault);
}

[Fact]
public void DeleteTest()
{
Settings settings = new Settings(true);

settings.Set("test", 1);
int test1 = settings.Get<int>("test");
Assert.Equal(1, test1);

settings.Delete("test");
int test2 = settings.Get<int>("test");
Assert.Equal(0, test2);
}
}
}
17 changes: 17 additions & 0 deletions OctoAwesome/OctoAwesome/ISettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace OctoAwesome
{
/// <summary>
/// Interface zur Verwaltung der Anwendungseinstellungen
/// </summary>
public interface ISettings
{
/// <summary>
@@ -9,6 +12,14 @@ public interface ISettings
/// <returns>Der Wert der Einstellung.</returns>
T Get<T>(string key);

/// <summary>
/// Gibt den Wert einer Einstellung zurück.
/// </summary>
/// <param name="key">Der Schlüssel der Einstellung.</param>
/// <param name="defaultValue">Default-Wert, der zurückgegeben wird, wenn der key nicht vorhanden ist.</param>
/// <returns>Der Wert der Einstellung oder der Default-Wert.</returns>
T Get<T>(string key, T defaultValue);

/// <summary>
/// Gibt das Array einer Einstellung zurück
/// </summary>
@@ -65,5 +76,11 @@ public interface ISettings
/// <param name="key">Der Schlüssel der Einstellung.</param>
/// <param name="values">Der Wert der Einstellung.</param>
void Set(string key, bool[] values);

/// <summary>
/// Löscht eine Eigenschaft aus den Einstellungen
/// </summary>
/// <param name="key">Der Schlüssel der Einstellung</param>
void Delete(string key);
}
}
70 changes: 40 additions & 30 deletions OctoAwesome/OctoAwesome/Settings.cs
Original file line number Diff line number Diff line change
@@ -13,17 +13,18 @@ namespace OctoAwesome
/// </summary>
public class Settings : ISettings
{
private Configuration _config;

/// <summary>
/// Bei UnitTests ist Assembly.GetEntryAssembly null, Gründe dazu gibts auf StackOverflow.
/// Um Schmerzen zu vermeiden wurde eine Variable eingeführt, die unabhängig testet.
/// Erzeugt eine neue Instanz der Klasse Settings, die auf die Konfigurationsdatei der aktuell laufenden Anwendung zugreift.
/// </summary>

private Configuration _config;
/// <param name="debug">Bei UnitTests ist Assembly.GetEntryAssembly null, Gründe dazu gibts auf StackOverflow.
/// Um Schmerzen zu vermeiden wurde eine Variable eingeführt, die unabhängig testet.</param>
internal Settings(bool debug)
{
if (debug)
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap {ExeConfigFilename = "EXECONFIG_PATH"};
ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "EXECONFIG_PATH" };
_config = ConfigurationManager.OpenMappedExeConfiguration(map,
ConfigurationUserLevel.None);
}
@@ -32,6 +33,10 @@ internal Settings(bool debug)
_config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
}
}

/// <summary>
/// Erzeugt eine neue Instanz der Klasse Settings, die auf die Konfigurationsdatei der aktuell laufenden Anwendung zugreift.
/// </summary>
public Settings()
{
_config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
@@ -44,14 +49,23 @@ public Settings()
/// <returns>Der Wert der Einstellung.</returns>
public T Get<T>(string key)
{
// var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);

return Get<T>(key, default(T));
}

/// <summary>
/// Gibt den Wert einer Einstellung zurück.
/// </summary>
/// <param name="key">Der Schlüssel der Einstellung.</param>
/// <param name="defaultValue">Default-Wert, der zurückgegeben wird, wenn der key nicht vorhanden ist.</param>
/// <returns>Der Wert der Einstellung oder der Default-Wert.</returns>
public T Get<T>(string key, T defaultValue)
{
var settingElement = _config.AppSettings.Settings[key];
if (settingElement == null)
return default(T);
return defaultValue;
var valueConfig = settingElement.Value;

return (T) Convert.ChangeType(valueConfig, typeof(T));
return (T)Convert.ChangeType(valueConfig, typeof(T));
}

/// <summary>
@@ -74,8 +88,6 @@ public T[] GetArray<T>(string key)
/// <returns></returns>
public bool KeyExists(string key)
{


return _config.AppSettings.Settings.AllKeys.Contains(key);
}

@@ -85,9 +97,7 @@ public bool KeyExists(string key)
/// <param name="key">Der Schlüssel der Einstellung.</param>
/// <param name="value">Der Wert der Einstellung.</param>
public void Set(string key, string value)
{


{
if (_config.AppSettings.Settings.AllKeys.Contains(key))
_config.AppSettings.Settings[key].Value = value;
else
@@ -122,12 +132,10 @@ public void Set(string key, bool value)
/// <param name="values">Der Wert der Einstellung.</param>
public void Set(string key, string[] values)
{
/* Wir bauen das Array in eine Art serialisierten String um.
* Wenn eine Zeichenkette, die wir aus den Einstellugen lesen mit einer
* eckigen Klammer anfängt, ist es ein Array.
* [value1, value2, value3]
*
*/
// Wir bauen das Array in eine Art serialisierten String um.
// Wenn eine Zeichenkette, die wir aus den Einstellugen lesen mit einer
// eckigen Klammer anfängt, ist es ein Array.
// [value1, value2, value3]

string writeString = "[" + String.Join(",", values) + "]";
Set(key, writeString);
@@ -142,9 +150,7 @@ public void Set(string key, int[] values)
{
string[] strValues = new string[values.Length];
for (int i = 0; i < values.Length; i++)
{
strValues[i] = Convert.ToString(values[i]);
}
Set(key, strValues);
}

@@ -157,9 +163,7 @@ public void Set(string key, bool[] values)
{
string[] stringValues = new string[values.Length];
for (int i = 0; i < values.Length; i++)
{
stringValues[i] = Convert.ToString(values[i]);
}
Set(key, stringValues);
}

@@ -169,18 +173,24 @@ private T[] DeserializeArray<T>(string arrayString)
// Wir müssten, um beide Klammern zu entfernen, - 3 rechnen. Ich lasse die letzte Klammer stellvertretend für das Komma, was folgen würde, stehen.
// Das wird in der for-Schleife auseinander gepflückt.


arrayString = arrayString.Substring(1, arrayString.Length - 2 /*- 1*/);

string[] partsString = arrayString.Split(',');
T[] tArray = new T[partsString.Length];
for (int i = 0; i < partsString.Length; i++)
{
tArray[i] = (T) Convert.ChangeType(partsString[i], typeof(T));
}

tArray[i] = (T)Convert.ChangeType(partsString[i], typeof(T));

return tArray;
}
}

/// <summary>
/// Löscht eine Eigenschaft aus den Einstellungen
/// </summary>
/// <param name="key">Der Schlüssel der Einstellung</param>
public void Delete(string key)
{
_config.AppSettings.Settings.Remove(key);
_config.Save(ConfigurationSaveMode.Modified, false);
}
}
}

0 comments on commit 811b2d1

Please sign in to comment.