public bool LoadProfile(string fileName) { if (fileName == null) { return(false); } NewProfile(); XmlDocument profileFile = new XmlDocument(); try { profileFile.Load(fileName); } catch (Exception e) { Console.WriteLine(e); Diagnostics.Log(e); throw; } XmlNodeList profileElements = profileFile.DocumentElement?.ChildNodes; if (profileElements != null) { foreach (XmlNode element in profileElements) { if (element.Name == "AssociatedProcess") { associatedProcess = element.InnerText; } else if (element.Name.Contains("ActionSequence")) { ActionSequence actionSequenceFromFile; try { actionSequenceFromFile = new ActionSequence(element, ProfileDatabase, Synthesizer); if (ActionSequences.All( actionSequence => actionSequence.Name != actionSequenceFromFile.Name)) { ActionSequences.Add(actionSequenceFromFile); } else { throw new ArgumentException( $"An action sequence with the name {actionSequenceFromFile.Name} already exists."); } } catch (Exception e) { Console.WriteLine(e); Diagnostics.Log(e); } } else if (element.Name.Contains("Trigger")) { string triggerName = element.Attributes.GetNamedItem("Name").Value; string triggerType = element.Attributes.GetNamedItem("Type").Value; string triggerValue = element.Attributes.GetNamedItem("Value").Value; string triggerComment = element.Attributes.GetNamedItem("Comment").Value; Type newTriggerType = Type.GetType($"Falcon.Radio.{triggerType}"); object triggerInstance = Activator.CreateInstance(newTriggerType, triggerName, triggerValue); Trigger triggerFromFile = (Trigger)triggerInstance; triggerFromFile.Comment = triggerComment; foreach (XmlElement triggerEvent in element.ChildNodes) { string eventType = triggerEvent.Attributes.GetNamedItem("Type").Value; string eventName = triggerEvent.Attributes.GetNamedItem("Name").Value; string eventValue = triggerEvent.Attributes.GetNamedItem("Value").Value; string eventComment = triggerEvent.Attributes.GetNamedItem("Comment").Value; if (eventType.Contains("ActionSequence")) { triggerFromFile.Add(ActionSequences.Find(actionSequence => actionSequence.Name == eventName)); } else if (eventType.Contains("Phrase")) { Type metaTriggerType = Type.GetType($"Falcon.Radio.{eventType}"); object metaTriggerInstance = Activator.CreateInstance(metaTriggerType, eventName, eventValue); Trigger newMetaTrigger = (Trigger)metaTriggerInstance; triggerFromFile.Add(newMetaTrigger); } } if (ProfileTriggers.All(trigger => trigger.Name != triggerFromFile.Name)) { ProfileTriggers.Add(triggerFromFile); } } else if (element.Name.Contains("Database") || element.Name.Contains("DB") || element.Name.Contains("Db")) { ProfileDatabase?.Load(element); } } } profileFileName = fileName; return(true); }
public bool IsEmpty() { return(ProfileTriggers == null || !ProfileTriggers.Any()); }