Last active
September 11, 2017 15:16
-
-
Save jamesrswift/240f275978227f649adbe31e43e96e14 to your computer and use it in GitHub Desktop.
ChatLibrary for S&box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace ChatLibrary | |
{ | |
public static class Command | |
{ | |
public delegate void Callback(List<string> arguments); | |
private static Dictionary<string, Callback> Commands = new Dictionary<string, Callback>(); | |
public static bool Add(string command, Callback onCommand) | |
{ | |
try | |
{ | |
Commands.Add(command, onCommand); | |
return true; | |
} | |
catch (ArgumentException) | |
{ | |
return false; | |
} | |
} | |
public static void Remove(string command) | |
{ | |
Commands.Remove(command); | |
} | |
public static Parser.Data Call(string chatText) | |
{ | |
Parser chatParser = new Parser(chatText); | |
Parser.Data CommandData = chatParser.GetCommandData(); | |
if (CommandData.validParse) | |
{ | |
Callback ChatCommand; | |
if (Commands.TryGetValue(CommandData.command, out ChatCommand)) | |
{ | |
ChatCommand(CommandData.arguments); | |
} | |
} | |
return CommandData; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
namespace ChatLibrary | |
{ | |
public class Parser | |
{ | |
public const string pattern = "('.*?'|\".*?\"|\\S+)"; | |
public struct Data | |
{ | |
public bool validParse; | |
public char prefix; | |
public string command; | |
public int argumentsCount; | |
public List<string> arguments; | |
} | |
public Data CommandData; | |
public Parser(string chatText) | |
{ | |
CommandData.validParse = false; | |
CommandData.argumentsCount = 0; | |
CommandData.arguments = new List<string>(); | |
if (chatText.Length > 1) | |
{ | |
Parse(chatText); | |
ValidateParse(); | |
} | |
} | |
public void Parse(string chatText) | |
{ | |
CommandData.prefix = chatText[0]; | |
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); | |
MatchCollection matches = rgx.Matches(chatText.Substring(1)); | |
if (matches.Count > 0) | |
{ | |
CommandData.command = matches[0].Value; | |
CommandData.argumentsCount = matches.Count - 1; | |
for ( int index = 1; index < matches.Count; index++) | |
{ | |
CommandData.arguments.Add(matches[index].Value); | |
} | |
} | |
} | |
public void ValidateParse() | |
{ | |
if (!Char.IsPunctuation(CommandData.prefix)) return; | |
if (CommandData.argumentsCount != CommandData.arguments.Count) return; | |
CommandData.validParse = true; | |
} | |
public Data GetCommandData() | |
{ | |
return CommandData; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace ChatLibrary | |
{ | |
public static class Program | |
{ | |
public static void Main() | |
{ | |
Console.WriteLine("Running test program"); | |
Command.Add("hello", (List<string> Args) => { | |
int argc = 1; | |
foreach (string argument in Args) | |
{ | |
Console.Write("Argument {0}: {1}\n", argc++, argument); | |
} | |
}); | |
Command.Call("!hello world \"and all you awesome\" guys"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment