Exemplo n.º 1
0
        private TLCGenDefaultsModel DeserializeDefaultsFile(string filename)
        {
            var defaults = new TLCGenDefaultsModel();
            var doc      = new XmlDocument();
            var reader   =
                XmlReader.Create(
                    filename,
                    new XmlReaderSettings()
            {
                IgnoreComments = true
            });

            doc.Load(reader);
            reader.Dispose();
            var defs = doc.DocumentElement?.SelectSingleNode("Defaults");

            if (defs == null)
            {
                return(null);
            }
            foreach (XmlNode def in defs.ChildNodes)
            {
                var x = def.SelectSingleNode("DataType");
                var t = x.InnerText;
                                #warning this corrects old files; should be removed at a point in the future (@24-1-2018)
                if (t.EndsWith(",TLCGen.Model"))
                {
                    t = t.Replace(",TLCGen.Model", ",TLCGen.Dependencies");
                }
                var type  = Type.GetType(t);
                var xRoot = new XmlRootAttribute
                {
                    ElementName = "Data",
                    IsNullable  = true
                };
                if (type == null)
                {
                    continue;
                }
                var ser = new XmlSerializer(type, xRoot);
                // http://stackoverflow.com/questions/1563473/xmlnode-to-objects
                var o    = ser.Deserialize(new XmlNodeReader(def.SelectSingleNode("Data")));
                var item = new TLCGenDefaultModel
                {
                    DefaultName = def.SelectSingleNode("DefaultName")?.InnerText,
                    DataType    = def.SelectSingleNode("DataType")?.InnerText,
                    Category    = def.SelectSingleNode("Category")?.InnerText
                };
                var n1 = def.SelectSingleNode("Selector1");
                if (n1 != null)
                {
                    item.Selector1 = n1.InnerText;
                }
                var n2 = def.SelectSingleNode("Selector2");
                if (n2 != null)
                {
                    item.Selector2 = n2.InnerText;
                }
                item.Data = o;
                defaults.Defaults.Add(item);
            }
            return(defaults);
        }
Exemplo n.º 2
0
        public void LoadSettings()
        {
            var appdatpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var setpath    = Path.Combine(appdatpath, @"TLCGen\Defaults\");

            if (!Directory.Exists(setpath))
            {
                Directory.CreateDirectory(setpath);
            }
            var    setfile    = Path.Combine(setpath, @"settings.xml");
            string defsetfile = null;

            if (!File.Exists(setfile))
            {
                setfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings\\tlcgendefaultdefaults.xml");
            }
            else
            {
                defsetfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings\\tlcgendefaultdefaults.xml");
            }
            if (!File.Exists(setfile))
            {
                MessageBox.Show("Could not find defaults for default settings. None loaded.", "Error loading defaults");
                Defaults = new TLCGenDefaultsModel();
                return;
            }
            try
            {
                Defaults = DeserializeDefaultsFile(setfile);
                if (defsetfile != null && File.Exists(defsetfile))
                {
                    var message         = "";
                    var message2        = "";
                    var defaultDefaults = DeserializeDefaultsFile(defsetfile);
                    foreach (var d in defaultDefaults.Defaults)
                    {
                        bool found = false;
                        foreach (var d2 in Defaults.Defaults)
                        {
                            if (d.DataType == d2.DataType &&
                                d.Category == d2.Category &&
                                d.Selector1 == d2.Selector1 &&
                                d.Selector2 == d2.Selector2)
                            {
                                found = true;
                            }
                        }
                        if (!found)
                        {
                            message += d.DefaultName + "; ";
                            Defaults.Defaults.Add(d);
                        }
                    }
                    var remDs = new List <TLCGenDefaultModel>();
                    foreach (var d in Defaults.Defaults)
                    {
                        bool found = false;
                        foreach (var d2 in defaultDefaults.Defaults)
                        {
                            if (d.DataType == d2.DataType &&
                                d.Category == d2.Category &&
                                d.Selector1 == d2.Selector1 &&
                                d.Selector2 == d2.Selector2)
                            {
                                found = true;
                            }
                        }
                        if (!found)
                        {
                            message2 += d.DefaultName + "; ";
                            remDs.Add(d);
                        }
                    }
                    foreach (var d in remDs)
                    {
                        Defaults.Defaults.Remove(d);
                    }
                    if (message.Length > 0 || message2.Length > 0)
                    {
                        var s = "";
                        if (message.Length > 0)
                        {
                            s += "Defaults added: " + message;
                        }
                        if (message2.Length > 0)
                        {
                            if (message.Length > 0)
                            {
                                s += Environment.NewLine;
                            }
                            s += "Defaults removed: " + message2;
                        }
                        MessageBox.Show(s, "Defaults updated");
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("An error occured while loading the defaults:\n " + e.ToString() + "\nPlease report this.", "Error while loading defaults");
            }
        }