private static ScriptInfo ParseConfig(string text) { var si = new ScriptInfo(); var lines = text.Split(new[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries); var mode = ParseMode.None; var players = new List<PlayerInfo>(); int lastTeam = -1; foreach (var s in lines) { string line = s.Trim(); if (line.StartsWith("[player", StringComparison.InvariantCultureIgnoreCase)) { mode = ParseMode.Player; var m = Regex.Match(line, "\\[player([0-9]+)", RegexOptions.IgnoreCase); if (m.Success) { int id = int.Parse(m.Groups[1].Value); var pi = new PlayerInfo(id); players.Add(pi); } else throw new ApplicationException("Error parsing " + scriptName); } else if (line.StartsWith("[team", StringComparison.InvariantCultureIgnoreCase)) { mode = ParseMode.Team; var m = Regex.Match(line, "\\[team([0-9]+)", RegexOptions.IgnoreCase); if (m.Success) lastTeam = int.Parse(m.Groups[1].Value); else throw new ApplicationException("Error parsing " + scriptName); } else { var m = Regex.Match(line, "([^=]+)=([^;]+)"); if (m.Success) { string var = m.Groups[1].Value; string val = m.Groups[2].Value; if (mode == ParseMode.Player) { if (String.Compare(var, "team", true) == 0) players[players.Count - 1].Team = int.Parse(val); else if (String.Compare(var, "spectator", true) == 0) players[players.Count - 1].IsSpectator = (val == "1"); else if (String.Compare(var, "name", true) == 0) { players[players.Count - 1].Name = val; if (players[players.Count - 1].Id == 0) si.Host = val; } } else if (mode == ParseMode.Team) if (String.Compare(var, "allyteam", true) == 0) for (int i = 0; i < players.Count; ++i) if (players[i].Team == lastTeam) players[i].Ally = int.Parse(val); } } } foreach (var pi in players) { if (!pi.IsSpectator) { var pp = new ScriptInfo.PlayerPair(pi.Ally, pi.Name); si.Players.Add(pp); } } return si; }
private static ScriptInfo ParseConfig(string text) { var si = new ScriptInfo(); var lines = text.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var mode = ParseMode.None; var players = new List <PlayerInfo>(); int lastTeam = -1; foreach (var s in lines) { string line = s.Trim(); if (line.StartsWith("[player", StringComparison.InvariantCultureIgnoreCase)) { mode = ParseMode.Player; var m = Regex.Match(line, "\\[player([0-9]+)", RegexOptions.IgnoreCase); if (m.Success) { int id = int.Parse(m.Groups[1].Value); var pi = new PlayerInfo(id); players.Add(pi); } else { throw new ApplicationException("Error parsing " + scriptName); } } else if (line.StartsWith("[team", StringComparison.InvariantCultureIgnoreCase)) { mode = ParseMode.Team; var m = Regex.Match(line, "\\[team([0-9]+)", RegexOptions.IgnoreCase); if (m.Success) { lastTeam = int.Parse(m.Groups[1].Value); } else { throw new ApplicationException("Error parsing " + scriptName); } } else { var m = Regex.Match(line, "([^=]+)=([^;]+)"); if (m.Success) { string var = m.Groups[1].Value; string val = m.Groups[2].Value; if (mode == ParseMode.Player) { if (String.Compare(var, "team", true) == 0) { players[players.Count - 1].Team = int.Parse(val); } else if (String.Compare(var, "spectator", true) == 0) { players[players.Count - 1].IsSpectator = (val == "1"); } else if (String.Compare(var, "name", true) == 0) { players[players.Count - 1].Name = val; if (players[players.Count - 1].Id == 0) { si.Host = val; } } } else if (mode == ParseMode.Team) { if (String.Compare(var, "allyteam", true) == 0) { for (int i = 0; i < players.Count; ++i) { if (players[i].Team == lastTeam) { players[i].Ally = int.Parse(val); } } } } } } } foreach (var pi in players) { if (!pi.IsSpectator) { var pp = new ScriptInfo.PlayerPair(pi.Ally, pi.Name); si.Players.Add(pp); } } return(si); }