private void cmdSaveChanges_Click(object sender, EventArgs e) { var vars = new List<VariableData>(); //Check for custom variables that will be used to increment/decrement data if(txtReply.Text != null) { foreach(var dat in txtReply.Text.Split(' ')) { var potentialVar = GetStringBetween(dat, "{", "}"); if(potentialVar != null && potentialVar.StartsWith("$")) { if(potentialVar.EndsWith("++") || potentialVar.EndsWith("--")) { //This is a variable, add it to the list vars.Add(new VariableData { Name = potentialVar.Remove(potentialVar.Length - 2, 2), Value = 0, Increment = potentialVar.Contains("++") }); } } } } var cmd = new CommandData { Name = txtCommand.Text, Description = txtDescription.Text, Enabled = chkEnabled.Checked, Privileges = txtPrivileges.Text.Split(' '), CustomReply = txtReply.Text, SendViaChat = chkSendViaChat.Checked, Variables = vars }; CommandManager.AddCommand(cmd); //CommandManager.SaveCommands(); CommandManager.UpdateCommandList(); Close(); }
private void ProcessCommandData(CommandData command) { if(command != null) { var messageToSend = command.CustomReply; foreach(var variable in command.Variables) { var replaceStr = "{" + variable.Name + (variable.Increment ? "++" : "--") + "}"; if(variable.Increment) { ++variable.Value; } else { --variable.Value; } messageToSend = messageToSend.Replace(replaceStr, variable.Value.ToString()); } //Use the command data in order to generate a custom reply channelConnection.send_message(messageToSend); } CommandManager.SaveCommand(command); CommandManager.AddCommand(command); }
/// <summary> /// Adds a new command to the list /// </summary> /// <param name="command"></param> public static void AddCommand(CommandData command) { var id = commands.FindIndex(f => f.Name == command.Name); if(commands.Count > 0 && id != -1) { commands.RemoveAt(id); } commands.Add(command); }
/// <summary> /// Saves a single command into the XML version /// </summary> /// <param name="command"></param> public static void SaveCommand(CommandData command) { foreach(var cmd in commands) { if(!File.Exists(commandsFile)) { var doc = new XDocument(new XElement("Commands")); var data = GetNewCommandXml(cmd); doc.Root?.Add(data); doc.Save(commandsFile); } else { var doc = XDocument.Load(commandsFile); var found = false; foreach(var el in doc.Elements("Commands").Elements()) { var cmdName = el.Attribute("name"); if(cmdName != null) { if(cmdName.Value == command.Name) { el.ReplaceWith(GetNewCommandXml(command)); found = true; break; } } } if(!found) { doc.Root?.Add(GetNewCommandXml(cmd)); } doc.Save(commandsFile); } } }
/// <summary> /// Get the command in XML format - used for persistent storage /// </summary> /// <param name="command"></param> /// <returns></returns> private static XElement GetNewCommandXml(CommandData command) { var tempStr = new StringBuilder(); if(command.Variables != null) { foreach(var itm in command.Variables) { tempStr.Append(itm.Name + ":" + (itm.Increment ? "1" : "0") + ":" + itm.Value + ";"); } } else { tempStr.Append(""); } if(command.CustomReply == null) { command.CustomReply = ""; } return new XElement("command", new XAttribute("name", command.Name), new XAttribute("enabled", command.Enabled), new XAttribute("description", command.Description), new XAttribute("privileges", string.Join(PRIVILEGE_SPLIT_STRING, command.Privileges)), new XAttribute("custom_reply", command.CustomReply), new XAttribute("sendViaChat", command.SendViaChat), new XAttribute("variables", tempStr) ); }