public static T GetAdditionalData <T>(string key) { if (!AdditionalSettings.ContainsKey(key)) { return(default(T)); } var additional = AdditionalSettings[key]; try { return(GenericStructure.Deserialise <T>(additional)); } catch { return(default(T)); // Deserialisation failure } }
public void TestMapObjectSerialisation() { // compare the *'s: // brush -> GS -> string* -> GS -> brush -> GS -> string* var brush = new BlockBrush(); var b = brush.Create(new IDGenerator(), new Box(Coordinate.Zero, Coordinate.One * 100), null, 0); var serialised = GenericStructure.Serialise(b); var toString = serialised.ToString(); var parsed = GenericStructure.Parse(new StringReader(toString)); var deserialised = GenericStructure.Deserialise <List <MapObject> >(parsed.First()); var reserialised = GenericStructure.Serialise(deserialised); Assert.AreEqual(serialised.ToString(), reserialised.ToString()); }
public void TestPrimitiveSerialisation() { Assert.AreEqual(null, GenericStructure.Deserialise <object>(GenericStructure.Serialise(null))); Assert.AreEqual(new DateTime(2014, 01, 01), GenericStructure.Deserialise <DateTime>(GenericStructure.Serialise(new DateTime(2014, 01, 01)))); Assert.AreEqual(10, GenericStructure.Deserialise <int>(GenericStructure.Serialise(10))); Assert.AreEqual(10m, GenericStructure.Deserialise <decimal>(GenericStructure.Serialise(10m))); Assert.AreEqual(false, GenericStructure.Deserialise <bool>(GenericStructure.Serialise(false))); Assert.AreEqual("12345", GenericStructure.Deserialise <string>(GenericStructure.Serialise("12345"))); Assert.AreEqual(new Coordinate(123, 456, 789.0005m), GenericStructure.Deserialise <Coordinate>(GenericStructure.Serialise(new Coordinate(123, 456, 789.0005m)))); var dsBox = GenericStructure.Deserialise <Box>(GenericStructure.Serialise(new Box(Coordinate.Zero, Coordinate.One))); Assert.AreEqual(Coordinate.Zero, dsBox.Start); Assert.AreEqual(Coordinate.One, dsBox.End); Assert.AreEqual(Color.FromArgb(255, 255, 0, 0), GenericStructure.Deserialise <Color>(GenericStructure.Serialise(Color.FromArgb(255, 255, 0, 0)))); Assert.AreEqual(new Plane(Coordinate.UnitZ, 1), GenericStructure.Deserialise <Plane>(GenericStructure.Serialise(new Plane(Coordinate.UnitZ, 1)))); }
public static void Read() { Builds.Clear(); Games.Clear(); RecentFiles.Clear(); Settings.Clear(); Hotkeys.Clear(); AdditionalSettings.Clear(); FavouriteTextureFolders.Clear(); var root = ReadSettingsFile(); if (root == null) { return; } var settings = root.Children.FirstOrDefault(x => x.Name == "Settings"); if (settings != null) { foreach (var key in settings.GetPropertyKeys()) { Settings.Add(new Setting { Key = key, Value = settings[key] }); } } var recents = root.Children.FirstOrDefault(x => x.Name == "RecentFiles"); if (recents != null) { foreach (var key in recents.GetPropertyKeys()) { int i; if (int.TryParse(key, out i)) { RecentFiles.Add(new RecentFile { Location = recents[key], Order = i }); } } } var games = root.Children.Where(x => x.Name == "Game"); foreach (var game in games) { var g = new Game(); g.Read(game); Games.Add(g); } var builds = root.Children.Where(x => x.Name == "Build"); foreach (var build in builds) { var b = new Build(); b.Read(build); Builds.Add(b); } var hotkeys = root.Children.FirstOrDefault(x => x.Name == "Hotkeys"); if (hotkeys != null) { foreach (var key in hotkeys.GetPropertyKeys()) { var spl = key.Split(':'); Hotkeys.Add(new Hotkey { ID = spl[0], HotkeyString = hotkeys[key] }); } } Serialise.DeserialiseSettings(Settings.ToDictionary(x => x.Key, x => x.Value)); Sledge.Settings.Hotkeys.SetupHotkeys(Hotkeys); var additionalSettings = root.Children.FirstOrDefault(x => x.Name == "AdditionalSettings"); if (additionalSettings != null) { foreach (var child in additionalSettings.Children) { if (child.Children.Count > 0) { AdditionalSettings.Add(child.Name, child.Children[0]); } } } var favTextures = root.Children.FirstOrDefault(x => x.Name == "FavouriteTextures"); if (favTextures != null && favTextures.Children.Any()) { try { var ft = GenericStructure.Deserialise <List <FavouriteTextureFolder> >(favTextures.Children[0]); if (ft != null) { FavouriteTextureFolders.AddRange(ft); } FixFavouriteNames(FavouriteTextureFolders); } catch { // Nope } } if (!File.Exists(SettingsFile)) { Write(); } }
public void TestObjectSerialisation() { var thing0 = new SerialisationThing(0); var thing1 = new SerialisationThing(1); var thing3 = new SerialisationThing(3); Assert.AreEqual(GenericStructure.Serialise(thing0).ToString(), GenericStructure.Serialise(GenericStructure.Deserialise <SerialisationThing>(GenericStructure.Serialise(thing0))).ToString()); Assert.AreEqual(GenericStructure.Serialise(thing1).ToString(), GenericStructure.Serialise(GenericStructure.Deserialise <SerialisationThing>(GenericStructure.Serialise(thing1))).ToString()); Assert.AreEqual(GenericStructure.Serialise(thing3).ToString(), GenericStructure.Serialise(GenericStructure.Deserialise <SerialisationThing>(GenericStructure.Serialise(thing3))).ToString()); }