public DeckFile(string path) { cardLines = new List <MainLine> (); parserState state = parserState.init; using (Stream s = new FileStream(path, FileMode.Open)) { using (StreamReader sr = new StreamReader(s)) { while (!sr.EndOfStream) { string tmp = sr.ReadLine(); if (tmp.StartsWith("[")) { switch (tmp.ToLower()) { case "[shop]": state = parserState.shop; continue; case "[metadata]": state = parserState.metadata; continue; case "[main]": state = parserState.main; continue; case "[sideboard]": state = parserState.sideboard; continue; default: state = parserState.init; continue; } } switch (state) { case parserState.shop: break; case parserState.metadata: string[] tokens = tmp.Split(new char[] { '=' }); switch (tokens[0].ToLower()) { case "name": Name = tokens[1]; break; case "description": Description = tokens[1]; break; case "set": Set = tokens[1]; break; case "Image": Image = tokens[1]; break; case "deck type": DeckType = tokens[1]; break; default: break; } break; case parserState.main: if (string.IsNullOrEmpty(tmp)) { continue; } MainLine l = new MainLine(); string strCount = tmp.Split(new char[] { ' ' }) [0]; l.count = int.Parse(strCount); string strTmp = tmp.Substring(strCount.Length).Trim(); string[] ts = strTmp.Split(new char[] { '|' }); l.name = ts [0]; l.code = ""; if (ts.Length > 1) { l.code = ts [1]; } cardLines.Add(l); break; case parserState.sideboard: break; default: break; } } } } }
/// <summary> /// Builds an IrcMessage from a raw string /// </summary> /// <param name="raw">Raw IRC message</param> /// <returns>IrcMessage object</returns> public static IrcMessage FromRawMessage(string raw) { var tagDict = new Dictionary <string, string>(); parserState state = parserState.STATE_NONE; int[] starts = new int[] { 0, 0, 0, 0, 0, 0 }; int[] lens = new int[] { 0, 0, 0, 0, 0, 0 }; for (int i = 0; i < raw.Length; ++i) { lens[(int)state] = i - starts[(int)state] - 1; if (state == parserState.STATE_NONE && raw[i] == '@') { state = parserState.STATE_V3; starts[(int)state] = ++i; int start = i; string key = null; for (; i < raw.Length; ++i) { if (raw[i] == '=') { key = raw.Substring(start, i - start); start = i + 1; } else if (raw[i] == ';') { if (key == null) { tagDict[raw.Substring(start, i - start)] = "1"; } else { tagDict[key] = raw.Substring(start, i - start); } start = i + 1; } else if (raw[i] == ' ') { if (key == null) { tagDict[raw.Substring(start, i - start)] = "1"; } else { tagDict[key] = raw.Substring(start, i - start); } break; } } } else if (state < parserState.STATE_PREFIX && raw[i] == ':') { state = parserState.STATE_PREFIX; starts[(int)state] = ++i; } else if (state < parserState.STATE_COMMAND) { state = parserState.STATE_COMMAND; starts[(int)state] = i; } else if (state < parserState.STATE_TRAILING && raw[i] == ':') { state = parserState.STATE_TRAILING; starts[(int)state] = ++i; break; } else if (state == parserState.STATE_COMMAND) { state = parserState.STATE_PARAM; starts[(int)state] = i; } while (i < raw.Length && raw[i] != ' ') { ++i; } } lens[(int)state] = raw.Length - starts[(int)state]; string cmd = raw.Substring(starts[(int)parserState.STATE_COMMAND], lens[(int)parserState.STATE_COMMAND]); IrcCommand command = IrcCommand.Unknown; switch (cmd) { case "PRIVMSG": command = IrcCommand.PrivMsg; break; case "NOTICE": command = IrcCommand.Notice; break; case "PING": command = IrcCommand.Ping; break; case "PONG": command = IrcCommand.Pong; break; case "HOSTTARGET": command = IrcCommand.HostTarget; break; case "CLEARCHAT": command = IrcCommand.ClearChat; break; case "USERSTATE": command = IrcCommand.UserState; break; case "GLOBALUSERSTATE": command = IrcCommand.GlobalUserState; break; case "NICK": command = IrcCommand.Nick; break; case "JOIN": command = IrcCommand.Join; break; case "PART": command = IrcCommand.Part; break; case "PASS": command = IrcCommand.Pass; break; case "CAP": command = IrcCommand.Cap; break; case "001": command = IrcCommand.RPL_001; break; case "002": command = IrcCommand.RPL_002; break; case "003": command = IrcCommand.RPL_003; break; case "004": command = IrcCommand.RPL_004; break; case "353": command = IrcCommand.RPL_353; break; case "366": command = IrcCommand.RPL_366; break; case "372": command = IrcCommand.RPL_372; break; case "375": command = IrcCommand.RPL_375; break; case "376": command = IrcCommand.RPL_376; break; case "WHISPER": command = IrcCommand.Whisper; break; case "SERVERCHANGE": command = IrcCommand.ServerChange; break; case "RECONNECT": command = IrcCommand.Reconnect; break; case "ROOMSTATE": command = IrcCommand.RoomState; break; case "USERNOTICE": command = IrcCommand.UserNotice; break; case "MODE": command = IrcCommand.Mode; break; case "CLEARMSG": command = IrcCommand.ClearMsg; break; default: command = IrcCommand.Unknown; break; } string parameters = raw.Substring(starts[(int)parserState.STATE_PARAM], lens[(int)parserState.STATE_PARAM]); string message = raw.Substring(starts[(int)parserState.STATE_TRAILING], lens[(int)parserState.STATE_TRAILING]); string hostmask = raw.Substring(starts[(int)parserState.STATE_PREFIX], lens[(int)parserState.STATE_PREFIX]); return(new IrcMessage(command, new string[] { parameters, message }, hostmask, tagDict)); }
public static void LoadEditionsDatabase() { string[] editions = Directory.GetFiles(editionsPath, "*.txt"); foreach (string f in editions) { Edition e = new Edition(); parserState state = parserState.init; using (Stream s = new FileStream(f, FileMode.Open)) { using (StreamReader sr = new StreamReader(s)) { while (!sr.EndOfStream) { string tmp = sr.ReadLine(); switch (state) { case parserState.init: if (tmp == "[metadata]") { state = parserState.metadata; } break; case parserState.metadata: if (tmp == "[cards]") { state = parserState.cards; continue; } if (string.IsNullOrEmpty(tmp)) { continue; } string[] tokens = tmp.Split(new char[] { '=' }); switch (tokens[0].ToLower()) { case "code": e.Code = tokens[1]; break; case "name": e.Name = tokens[1]; break; case "code2": e.Code2 = tokens[1]; break; case "type": e.Type = (EditionTypes)Enum.Parse(typeof(EditionTypes), tokens[1], true); break; case "foil": e.Foil = tokens[1]; break; case "date": e.Date = tokens[1]; break; case "boostercover": case "boostercovers": case "booster": case "alias": case "foilalwaysincommonslot": case "foilchanceinbooster": break; case "border": e.Border = (MagicColors)Enum.Parse(typeof(MagicColors), tokens[1], true); break; default: break; } break; case parserState.cards: MagicCardEdition me = new MagicCardEdition(); char rarityToken = tmp[0]; switch (rarityToken) { case 'C': me.Rarity = Rarities.Common; break; case 'U': me.Rarity = Rarities.Uncommon; break; case 'R': me.Rarity = Rarities.Rare; break; case 'M': me.Rarity = Rarities.Mythic; break; case 'L': me.Rarity = Rarities.BasicLand; break; case 'T': me.Rarity = Rarities.Token; break; case 'S': me.Rarity = Rarities.Special; break; default: break; } me.Name = tmp.Substring(2); e.Cards.Add(me); break; default: break; } } } } EditionsDB.Add(e); } }