示例#1
0
 public PlaylistSet()
 {
     GameTypes = new Dictionary<string, GameType>();
     Playlists = new Playlist[48];
 }
示例#2
0
        public static void ParsePlaylists(string playlistFile)
        {
            var tokenizer = new Tokenizer(playlistFile);
            var token = "";

            var playlistData = new PlaylistSet();

            Playlist curPlaylist = null;
            GameType curGameType = null;

            var line = 0;

            do 
            {
                token = tokenizer.ReadToken();
                token = token.ToLowerInvariant();

                switch (token)
                {
                    case "version":
                        var version = tokenizer.ReadToken(false);

                        if (string.IsNullOrWhiteSpace(version))
                        {
                            Log.Write(LogLevel.Error, "Playlist error: Expected version number after version command", line);
                            break;
                        }

                        int versionNum = 0;
                        int.TryParse(version, out versionNum);

                        if (versionNum <= 0)
                        {
                            Log.Write(LogLevel.Error, "Playlist error: Invalid version number {1}", line, versionNum);
                            break;
                        }

                        playlistData.Version = versionNum;
                        break;

                    case "playlist":
                        {
                            var id = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(id))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'playlist' command needs to be followed by a f*****g number", line);
                                break;
                            }

                            int pid = -1;
                            int.TryParse(id, out pid);

                            if (pid < 0 || pid > playlistData.Playlists.Length)
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'playlist' command needs to be followed by a number between {1} and {2}", line, 0, playlistData.Playlists.Length);
                                break;
                            }

                            var playlist = new Playlist();
                            curGameType = null;
                            curPlaylist = playlist;

                            playlistData.Playlists[pid] = playlist;

                            break;
                        }
                    case "gametype":
                        {
                            var id = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(id))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'playlist' command needs to be followed by a f*****g number", line);
                                break;
                            }

                            var gametype = new GameType() { Id = id };
                            curGameType = gametype;
                            curPlaylist = null;

                            playlistData.GameTypes[id] = curGameType;

                            break;
                        }
                    case "script":
                        {
                            var script = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(script))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'script' command needs to be followed by a name", line);
                                break;
                            }

                            if (curGameType != null)
                            {
                                curGameType.Script = script;
                            }
                            else
                            {
                                Log.Write(LogLevel.Error, "Playlist error: not in a gametype during script command", line);
                                break;
                            }

                            break;
                        }
                    case "name":
                        {
                            var name = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(name))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'name' command needs to be followed by a name", line);
                                break;
                            }

                            if (curPlaylist != null)
                            {
                                curPlaylist.Name = name;
                            }
                            else if (curGameType != null)
                            {
                                curGameType.Name = name;
                            }
                            else
                            {
                                Log.Write(LogLevel.Error, "Playlist error: not in a gametype/playlist during name command", line);
                                break;
                            }

                            break;
                        }
                    case "description":
                        {
                            var description = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(description))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'description' command needs to be followed by a name", line);
                                break;
                            }

                            if (curPlaylist != null)
                            {
                                curPlaylist.Description = description;
                            }
                            else
                            {
                                Log.Write(LogLevel.Error, "Playlist error: not in a playlist during description command", line);
                                break;
                            }

                            break;
                        }
                    case "icon":
                        {
                            var icon = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(icon))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'icon' command needs to be followed by a name", line);
                                break;
                            }

                            if (curPlaylist != null)
                            {
                                curPlaylist.Image = icon;
                            }
                            else
                            {
                                Log.Write(LogLevel.Error, "Playlist error: not in a playlist during icon command", line);
                                break;
                            }

                            break;
                        }
                    case "set":
                        {
                            var varName = tokenizer.ReadToken(false);
                            var varValue = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(varName) || string.IsNullOrWhiteSpace(varValue))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'set' command needs to be followed by a variable name/value", line);
                                break;
                            }

                            if (curPlaylist != null)
                            {
                                curPlaylist.Values[varName] = varValue;
                            }
                            else if (curGameType != null)
                            {
                                curGameType.Values[varName] = varValue;
                            }
                            else
                            {
                                Values[varName] = varValue;
                            }

                            break;
                        }
                    case "rule":
                        {
                            var varName = tokenizer.ReadToken(false);
                            var varValue = tokenizer.ReadToken(false);

                            if (string.IsNullOrWhiteSpace(varName) || string.IsNullOrWhiteSpace(varValue))
                            {
                                Log.Write(LogLevel.Error, "Playlist error: 'rule' command needs to be followed by a variable name/value", line);
                                break;
                            }

                            if (curPlaylist != null)
                            {
                                curPlaylist.Rules[varName] = varValue;
                            }
                            else if (curGameType != null)
                            {
                                curGameType.Rules[varName] = varValue;
                            }
                            else
                            {
                                Rules[varName] = varValue;
                            }

                            break;
                        }
                    default:
                        {
                            if (curPlaylist != null)
                            {
                                var datas = token.Split(',');

                                if (datas.Length == 3)
                                {
                                    var entry = new PlaylistEntry();
                                    var gameType = datas[1];

                                    GameType gt;
                                    if (!playlistData.GameTypes.TryGetValue(gameType, out gt))
                                    {
                                        Log.Write(LogLevel.Error, "Playlist error: invalid gametype {1}", line, gameType);
                                        break;
                                    }

                                    int weight = 1;
                                    int.TryParse(datas[2], out weight);

                                    entry.GameType = gt;
                                    entry.Map = datas[0];
                                    entry.Weight = weight;

                                    curPlaylist.Entries.Add(entry);

                                    break;
                                }
                            }

                            if (token != "")
                            {
                                Log.Write(LogLevel.Error, "Playlist error: invalid line {1}", line, token);
                            }

                            break;
                        }
                }
            } while (token != "");

            PlaylistSet = playlistData;
        }