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!");
        }
Esempio 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!");
        }
Esempio 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!");
            }
        }