/// <summary> /// Deserialize a string, and save it to this instance /// </summary> /// <param name="data">string to parse</param> /// <returns>returns this task object</returns> public Task Deserialize(string data) { Actions = new List <IAction>(); string[] partedData = data.Split(new[] { ":;" }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in partedData) { string[] dataFragments = s.Split(new[] { "::" }, StringSplitOptions.None); try { switch (dataFragments[0]) { case "TASK": Name = dataFragments[1]; Enabled = bool.Parse(dataFragments[2]); break; case "TRG": foreach (ITrigger t in AllTriggers) { if (!t.Name.Equals(dataFragments[1])) { continue; } ITrigger trigger = (ITrigger)Activator.CreateInstance(t.GetType()); trigger.Load(dataFragments[2]); Trigger = trigger; break; // we're done, we got the trigger. There can only be one trigger! } break; case "ACT": foreach (IAction a in AllActions) { if (!a.Name.Equals(dataFragments[1])) { continue; } IAction action = (IAction)Activator.CreateInstance(a.GetType()); action.Load(dataFragments[2]); Actions.Add(action); break; // we're done, we got the trigger. There can only be one trigger! } break; } } catch (Exception e) { Logger.Log(LogLevel.Warning, "Task", "Failed to parse task setting: " + dataFragments[0], e.Message); } } // in case 1 part wasn't loaded, fix (for UI purposes) and deactivate if (this.Trigger == null) { this.Trigger = new CurrentTimeTrigger { Parameters = "00:00:00" }; this.Enabled = false; } if (this.Actions == null) { this.Actions = new List <IAction>(); this.Enabled = false; } if (string.IsNullOrEmpty(Name)) { this.Name = "UNNAMED_" + new Random().Next(1000, 9999); this.Enabled = false; } return(this); }