private static void DownloadSongs()
        {
            Settings.Instance.Server.Downloaded.GetDirectories().AsParallel().ForAll(dir => dir.Delete(true));

            Settings.Instance.AvailableSongs.Songs.ToList().AsParallel().ForAll(id =>
            {
                DownloadSongByID(id);
            });

            Logger.Instance.Log("All songs downloaded!");

            List <CustomSongInfo> _songs = SongLoader.RetrieveAllSongs();

            _songs.AsParallel().ForAll(song =>
            {
                ProcessSong(song);
            });

            if (Settings.Instance.AvailableSongs.SongOrder == Settings.SongOrder.List)
            {
                CustomSongInfo[] buffer = new CustomSongInfo[availableSongs.Count];
                availableSongs.CopyTo(buffer);
                availableSongs.Clear();

                foreach (string id in Settings.Instance.AvailableSongs.Songs)
                {
                    try
                    {
                        availableSongs.Add(buffer.First(x => x.beatSaverId == id));
                    }
                    catch (Exception)
                    {
                        Logger.Instance.Warning($"Can't find song with ID {id}");
                    }
                }

                var notSortedSongs = buffer.Except(availableSongs).ToList();
                if (notSortedSongs.Count > 0)
                {
                    Logger.Instance.Warning($"{notSortedSongs.Count} songs not sorted!");
                    availableSongs.AddRange(notSortedSongs);
                }
            }


            Logger.Instance.Log("Done!");
        }
Ejemplo n.º 2
0
        private static void DownloadSongs()
        {
            Settings.Instance.Server.Downloaded.GetDirectories().AsParallel().ForAll(dir => dir.Delete(true));

            Settings.Instance.AvailableSongs.Songs.AsParallel().ForAll(id => {
                var zipPath = Path.Combine(Settings.Instance.Server.Downloads.FullName, $"{id}.zip");
                Thread.Sleep(25);
                using (var client = new WebClient()) {
                    client.Headers.Add("user-agent",
                                       $"BeatSaverMultiplayerServer-{Assembly.GetEntryAssembly().GetName().Version}");
                    if (Settings.Instance.Server.Downloads.GetFiles().All(o => o.Name != $"{id}.zip"))
                    {
                        Logger.Instance.Log($"Downloading {id}.zip");
                        client.DownloadFile($"https://beatsaver.com/dl.php?id={id}", zipPath);
                    }
                }

                ZipArchive zip = null;
                try {
                    zip = ZipFile.OpenRead(zipPath);
                }
                catch (Exception ex) {
                    Logger.Instance.Exception(ex.Message);
                }

                var songName = zip?.Entries[0].FullName.Split('/')[0];
                try {
                    zip?.ExtractToDirectory(Settings.Instance.Server.Downloaded.FullName);
                    try {
                        zip?.Dispose();
                    }
                    catch (IOException ex) {
                        Logger.Instance.Exception($"Failed to remove Zip [{id}]");
                    }
                }
                catch (IOException ex) {
                    Logger.Instance.Exception($"Folder [{songName}] exists. Continuing.");
                    try {
                        zip?.Dispose();
                    }
                    catch (IOException) {
                        Logger.Instance.Exception($"Failed to remove Zip [{id}]");
                    }
                }
            });

            Logger.Instance.Log("All songs downloaded!");

            List <CustomSongInfo> _songs = SongLoader.RetrieveAllSongs();

            _songs.AsParallel().ForAll(song => {
                Logger.Instance.Log($"Processing {song.songName} {song.songSubName}");
                using (NVorbis.VorbisReader vorbis =
                           new NVorbis.VorbisReader($"{song.path}/{song.difficultyLevels[0].audioPath}")) {
                    song.duration = vorbis.TotalTime;
                }

                availableSongs.Add(song);
            });

            Logger.Instance.Log("Done!");
        }
Ejemplo n.º 3
0
        private static void DownloadSongs()
        {
            if (!Directory.Exists("AvailableSongs"))
            {
                Directory.CreateDirectory("AvailableSongs");
            }

            using (var client = new WebClient())
            {
                client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
                                               "(compatible; MSIE 6.0; Windows NT 5.1; " +
                                               ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                foreach (string dir in Directory.GetDirectories("AvailableSongs/"))
                {
                    Directory.Delete(dir, true);
                }

                foreach (int id in availableSongsIDs)
                {
                    if (!File.Exists("AvailableSongs/" + id + ".zip"))
                    {
                        Console.WriteLine("Downloading " + id + ".zip");
                        client.DownloadFile("https://beatsaver.com/dl.php?id=" + id, "AvailableSongs/" + id + ".zip");

                        FastZip zip = new FastZip();
                        Console.WriteLine("Extracting " + id + ".zip...");
                        zip.ExtractZip("AvailableSongs/" + id + ".zip", "AvailableSongs", null);
                    }
                    else
                    {
                        string downloadedSongPath = "";

                        using (var zf = new ZipFile("AvailableSongs/" + id + ".zip"))
                        {
                            foreach (ZipEntry ze in zf)
                            {
                                if (ze.IsFile)
                                {
                                    if (string.IsNullOrEmpty(downloadedSongPath) && ze.Name.IndexOf('/') != -1)
                                    {
                                        downloadedSongPath = "AvailableSongs/" + ze.Name.Substring(0, ze.Name.IndexOf('/'));
                                    }
                                }
                                else if (ze.IsDirectory)
                                {
                                    downloadedSongPath = "AvailableSongs/" + ze.Name;
                                }
                            }
                        }

                        if (downloadedSongPath.Contains("/autosaves"))
                        {
                            downloadedSongPath = downloadedSongPath.Replace("/autosaves", "");
                        }

                        if (!Directory.Exists(downloadedSongPath))
                        {
                            FastZip zip = new FastZip();
                            Console.WriteLine("Extracting " + id + ".zip...");
                            zip.ExtractZip("AvailableSongs/" + id + ".zip", "AvailableSongs", null);
                        }
                    }
                }

                Console.WriteLine("All songs downloaded!");

                List <CustomSongInfo> _songs = SongLoader.RetrieveAllSongs();

                foreach (CustomSongInfo song in _songs)
                {
                    Console.WriteLine("Processing " + song.songName + " " + song.songSubName);
                    using (NVorbis.VorbisReader vorbis = new NVorbis.VorbisReader(song.path + "/" + song.difficultyLevels[0].audioPath))
                    {
                        song.duration = vorbis.TotalTime;
                    }

                    availableSongs.Add(song);
                }

                Console.WriteLine("Done!");
            }
        }
        void ProcessCommand(string comName, string[] comArgs, bool exitAfterPrint)
        {
            string s = string.Empty;

            switch (comName.ToLower())
            {
            case "help":
                foreach (var com in new[] { "help", "quit", "clients", "blacklist [add/remove] [playerID/IP]", "whitelist [enable/disable/add/remove] [playerID/IP]", "songs [add/remove/start/list] [songID on BeatSaver]" })
                {
                    s += $"{Environment.NewLine}> {com}";
                }

                Logger.Instance.Log($"Commands:{s}", exitAfterPrint);
                break;

            case "version":
                Logger.Instance.Log($"{Assembly.GetEntryAssembly().GetName().Version}", exitAfterPrint);
                break;

            case "quit":
                Environment.Exit(0);
                return;

            case "clients":
                if (!exitAfterPrint)
                {
                    foreach (var t in clients)
                    {
                        var client = t.playerInfo;
                        if (t.playerInfo == null)
                        {
                            s +=
                                $"{Environment.NewLine}[{t.state}] NOT AVAILABLE @ {((IPEndPoint)t._client.Client.RemoteEndPoint).Address}";
                        }
                        else
                        {
                            s +=
                                $"{Environment.NewLine}[{t.state}] {client.playerName} @ {((IPEndPoint)t._client.Client.RemoteEndPoint).Address}";
                        }
                    }

                    if (s == String.Empty)
                    {
                        s = " No Clients";
                    }
                    Logger.Instance.Log($"Connected Clients:{s}", exitAfterPrint);
                }
                break;

            case "blacklist":
            {
                if (comArgs.Length == 2 && !comArgs[1].IsNullOrEmpty())
                {
                    switch (comArgs[0])
                    {
                    case "add":
                    {
                        if (!Settings.Instance.Access.Blacklist.Contains(comArgs[1]))
                        {
                            clients.Where(y => y.clientIP == comArgs[1] || y.playerId.ToString() == comArgs[1]).AsParallel().ForAll(z => z.KickClient());
                            Settings.Instance.Access.Blacklist.Add(comArgs[1]);
                            Logger.Instance.Log($"Successfully banned {comArgs[1]}", exitAfterPrint);
                            Settings.Instance.Save();
                        }
                        else
                        {
                            Logger.Instance.Log($"{comArgs[1]} is already blacklisted", exitAfterPrint);
                        }
                    }
                    break;

                    case "remove":
                    {
                        if (Settings.Instance.Access.Blacklist.Remove(comArgs[1]))
                        {
                            Logger.Instance.Log($"Successfully unbanned {comArgs[1]}", exitAfterPrint);
                            Settings.Instance.Save();
                        }
                        else
                        {
                            Logger.Instance.Warning($"{comArgs[1]} is not banned", exitAfterPrint);
                        }
                    }
                    break;

                    default:
                    {
                        Logger.Instance.Warning($"Command usage: blacklist [add/remove] [playerID/IP]", exitAfterPrint);
                    }
                    break;
                    }
                }
                else
                {
                    Logger.Instance.Warning($"Command usage: blacklist [add/remove] [playerID/IP]", exitAfterPrint);
                }
            }
            break;

            case "whitelist":
            {
                if (comArgs.Length >= 1)
                {
                    switch (comArgs[0])
                    {
                    case "enable":
                    {
                        Settings.Instance.Access.WhitelistEnabled = true;
                        Logger.Instance.Log($"Whitelist enabled", exitAfterPrint);
                        Settings.Instance.Save();
                    }
                    break;

                    case "disable":
                    {
                        Settings.Instance.Access.WhitelistEnabled = false;
                        Logger.Instance.Log($"Whitelist disabled", exitAfterPrint);
                        Settings.Instance.Save();
                    }
                    break;

                    case "add":
                    {
                        if (comArgs.Length == 2 && !comArgs[1].IsNullOrEmpty())
                        {
                            if (!Settings.Instance.Access.Whitelist.Contains(comArgs[1]))
                            {
                                Settings.Instance.Access.Whitelist.Add(comArgs[1]);
                                Settings.Instance.Save();
                                Logger.Instance.Log($"Successfully whitelisted {comArgs[1]}", exitAfterPrint);
                            }
                            else
                            {
                                Logger.Instance.Log($"{comArgs[1]} is already whitelisted", exitAfterPrint);
                            }
                        }
                        else
                        {
                            Logger.Instance.Warning($"Command usage: whitelist [enable/disable/add/remove] [playerID/IP]", exitAfterPrint);
                        }
                    }
                    break;

                    case "remove":
                    {
                        if (comArgs.Length == 2 && !comArgs[1].IsNullOrEmpty())
                        {
                            clients.Where(y => y.clientIP == comArgs[1] || y.playerId.ToString() == comArgs[1]).AsParallel().ForAll(z => z.KickClient());
                            if (Settings.Instance.Access.Whitelist.Remove(comArgs[1]))
                            {
                                Logger.Instance.Log($"Successfully removed {comArgs[1]} from whitelist", exitAfterPrint);
                                Settings.Instance.Save();
                            }
                            else
                            {
                                Logger.Instance.Warning($"{comArgs[1]} is not whitelisted", exitAfterPrint);
                            }
                        }
                        else
                        {
                            Logger.Instance.Warning($"Command usage: whitelist [enable/disable/add/remove] [playerID/IP]", exitAfterPrint);
                        }
                    }
                    break;

                    default:
                    {
                        Logger.Instance.Warning($"Command usage: whitelist [enable/disable/add/remove] [playerID/IP]", exitAfterPrint);
                    }
                    break;
                    }
                }
                else
                {
                    Logger.Instance.Warning($"Command usage: whitelist [enable/disable/add/remove] [playerID/IP]", exitAfterPrint);
                }
            }
            break;

            case "songs":
            {
                if (comArgs.Length >= 1)
                {
                    switch (comArgs[0])
                    {
                    case "add":
                    {
                        string songId = comArgs[1];
                        if (comArgs.Length == 2 && !comArgs[1].IsNullOrEmpty())
                        {
                            if (!Settings.Instance.AvailableSongs.Songs.Contains(songId))
                            {
                                List <string> songs = new List <string>(Settings.Instance.AvailableSongs.Songs);
                                songs.Add(songId);
                                Settings.Instance.AvailableSongs.Songs = songs.ToArray();
                                Settings.Instance.Save();
                                string path = DownloadSongByID(songId);

                                ProcessSong(SongLoader.GetCustomSongInfo(path));

                                Logger.Instance.Log($"Successfully added {comArgs[1]} to the song list", exitAfterPrint);

                                SendToAllClients(new ServerCommand(
                                                     ServerCommandType.DownloadSongs,
                                                     _songs: availableSongs.Select(y => y.levelId).ToArray()), wss);
                            }
                            else
                            {
                                Logger.Instance.Log($"{comArgs[1]} is already on the song list", exitAfterPrint);
                            }
                        }
                        else
                        {
                            Logger.Instance.Warning($"Command usage: songs [add/remove/start/list] [songID on BeatSaver]", exitAfterPrint);
                        }
                    }
                    break;

                    case "remove":
                    {
                        string songId = comArgs[1];
                        if (comArgs.Length == 2 && !comArgs[1].IsNullOrEmpty())
                        {
                            clients.Where(y => y.clientIP == comArgs[1] || y.playerId.ToString() == comArgs[1]).AsParallel().ForAll(z => z.KickClient());

                            List <string> songs = new List <string>(Settings.Instance.AvailableSongs.Songs);

                            if (songs.Remove(songId))
                            {
                                Settings.Instance.AvailableSongs.Songs = songs.ToArray();
                                Settings.Instance.Save();
                                availableSongs.RemoveAll(y => y.beatSaverId == songId);
                                Logger.Instance.Log($"Successfully removed {comArgs[1]} from the song list", exitAfterPrint);
                            }
                            else
                            {
                                Logger.Instance.Warning($"{comArgs[1]} is not in the song list", exitAfterPrint);
                            }
                        }
                        else
                        {
                            Logger.Instance.Warning($"Command usage: songs [add/remove/start/list] [songID on BeatSaver]", exitAfterPrint);
                        }
                    }
                    break;

                    case "list":
                    {
                        if (exitAfterPrint)
                        {
                            foreach (var com in Settings.Instance.AvailableSongs.Songs)
                            {
                                s += $"{Environment.NewLine}> {com}";
                            }
                        }
                        else
                        {
                            foreach (var com in availableSongs)
                            {
                                s += $"{Environment.NewLine}> {com.beatSaverId}: {com.authorName} - {com.songName} {com.songSubName}";
                            }
                        }

                        Logger.Instance.Log($"Songs:{s}", exitAfterPrint);
                    }; break;

                    case "start":
                    {
                        if (!exitAfterPrint && serverState != ServerState.Playing && currentSongIndex == -1)
                        {
                            string songId = comArgs[1];
                            if (comArgs.Length == 2 && !comArgs[1].IsNullOrEmpty())
                            {
                                currentSongIndex = availableSongs.FindIndex(x => x.beatSaverId == songId);
                                SendToAllClients(new ServerCommand(
                                                     ServerCommandType.SetSelectedSong,
                                                     _difficulty: GetPreferredDifficulty(availableSongs[currentSongIndex])), wss);
                                Logger.Instance.Log($"Next song is {availableSongs[currentSongIndex].songName} {availableSongs[currentSongIndex].songSubName}");
                            }
                            else
                            {
                                Logger.Instance.Warning($"Command usage: songs [add/remove/start/list] [songID on BeatSaver]", exitAfterPrint);
                            }
                        }
                    }; break;

                    default:
                    {
                        Logger.Instance.Warning($"Command usage: songs [add/remove/start/list] [songID on BeatSaver]", exitAfterPrint);
                    }
                    break;
                    }
                }
                else
                {
                    Logger.Instance.Warning($"Command usage: songs [add/remove/start/list] [songID on BeatSaver]", exitAfterPrint);
                }
            }; break;

            case "crash":
                throw new Exception("DebugException");
            }

            if (exitAfterPrint)
            {
                Environment.Exit(0);
            }
        }