From d25cfcbade5b664aa6c20868ebd55bdc1ce76da1 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Sun, 30 Jul 2017 14:55:37 -0500 Subject: [PATCH] Store a settings record in the user class. Serialize it for ease of loading / saving. Use this settings class for all storing / retrieving stuff. Works awesome. --- EduNetworkBuilder/NB.cs | 38 ++++++++++++++++++++++++++++- EduNetworkBuilder/NBSettings.cs | 14 ++--------- EduNetworkBuilder/NetworkBuilder.cs | 2 +- EduNetworkBuilder/PersonClass.cs | 31 +++++++---------------- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/EduNetworkBuilder/NB.cs b/EduNetworkBuilder/NB.cs index 1c873ad..929be3e 100644 --- a/EduNetworkBuilder/NB.cs +++ b/EduNetworkBuilder/NB.cs @@ -12,7 +12,8 @@ using System.Drawing; using System.Media; using System.Reflection; using System.Text.RegularExpressions; - +using System.Xml.Serialization; +using System.IO; namespace EduNetworkBuilder { @@ -367,6 +368,11 @@ namespace EduNetworkBuilder public static NBSettings GetSettings() { + //Try getting the user settings + PersonClass User = GetUser(); + if (User != null) return User.UserSettings; + + //If no user, get the settings for the program. BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"]; if (myWin != null) { @@ -580,6 +586,36 @@ namespace EduNetworkBuilder } } + /// + /// Serialize any serializable class to an XML string. + /// + /// The type of object + /// The object to serialize + /// A string containing the serialized object in XML + public static string SerializeObject(T toSerialize) + { + XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); + + using (StringWriter textWriter = new StringWriter()) + { + xmlSerializer.Serialize(textWriter, toSerialize); + return textWriter.ToString(); + } + } + + /// + /// Deserialize (oposite of SerializeObject) an object from an xml string. + /// + /// The type of object to deserialize + /// the xml string containing the object + /// An object of the specified type + public static T Deserialize(string toDeserialize) + { + XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); + StringReader textReader = new StringReader(toDeserialize); + return (T)xmlSerializer.Deserialize(textReader); + } + public static List GetPuzzleTags() { BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"]; diff --git a/EduNetworkBuilder/NBSettings.cs b/EduNetworkBuilder/NBSettings.cs index 3ca2d9f..9c9dc90 100644 --- a/EduNetworkBuilder/NBSettings.cs +++ b/EduNetworkBuilder/NBSettings.cs @@ -108,12 +108,7 @@ namespace EduNetworkBuilder } public void MarkAsDone(string PuzzleName) - { - PersonClass PC = NB.GetUser(); - if(PC != null) - { - PC.MarkAsDone(PuzzleName); //Mark it on the user's personal settings. - } + { //we just mark it in the settings class if (!ScoreList.Contains(PuzzleName)) { @@ -123,12 +118,7 @@ namespace EduNetworkBuilder } public bool CheckIfDone(string PuzzleName) - { - PersonClass PC = NB.GetUser(); - if (PC != null) - { - return PC.CheckIfDone(PuzzleName); //Mark it on the user's personal settings. - } + { //we just mark it in the settings class if (ScoreList.Contains(PuzzleName)) return true; diff --git a/EduNetworkBuilder/NetworkBuilder.cs b/EduNetworkBuilder/NetworkBuilder.cs index 9d33827..15dcb19 100644 --- a/EduNetworkBuilder/NetworkBuilder.cs +++ b/EduNetworkBuilder/NetworkBuilder.cs @@ -535,7 +535,7 @@ namespace EduNetworkBuilder { if ("Level_" + pi.Level.ToString() == LevelName) { - if (!OurSettings.ScoreList.Contains(pi.PuzzleName)) + if (!OurSettings.CheckIfDone(pi.PuzzleName)) { return true; //We have one puzzle in the level which has not been solved } diff --git a/EduNetworkBuilder/PersonClass.cs b/EduNetworkBuilder/PersonClass.cs index ced51b4..8933c37 100644 --- a/EduNetworkBuilder/PersonClass.cs +++ b/EduNetworkBuilder/PersonClass.cs @@ -39,10 +39,13 @@ namespace EduNetworkBuilder /// /// EverCompletedPuzzles is a list of puzzles that they have completed once. /// - List EverCompletedPuzzles = new List(); public List Projects = new List(); + public NBSettings UserSettings = new NBSettings(); + + + private PersonClass() { } @@ -121,8 +124,8 @@ namespace EduNetworkBuilder case "password": Passwords.Add(Individual.InnerText); break; - case "scorelist": - EverCompletedPuzzles.Add(Individual.InnerText); + case "settings": + UserSettings = NB.Deserialize(Individual.InnerText); break; case "project": case "schoolwork": @@ -171,10 +174,9 @@ namespace EduNetworkBuilder writer.WriteElementString("PasswordHint", PasswordHint); writer.WriteElementString("AltPassword", AltPassword); writer.WriteElementString("IsAdmin", isAdmin.ToString()); - foreach(string one in EverCompletedPuzzles) - { - writer.WriteElementString("ScoreList", one); - } + string settingsstring = NB.SerializeObject(UserSettings); + writer.WriteElementString("Settings", settingsstring); + foreach(string One in Passwords) writer.WriteElementString("Password", One); //Save all the devices @@ -186,20 +188,5 @@ namespace EduNetworkBuilder } #endregion - public void MarkAsDone(string PuzzleName) - { - if (!EverCompletedPuzzles.Contains(PuzzleName)) - { - EverCompletedPuzzles.Add(PuzzleName); - } - } - - public bool CheckIfDone(string PuzzleName) - { - if (EverCompletedPuzzles.Contains(PuzzleName)) - return true; - else - return false; - } } }