示例#1
0
 public void RefreshSongList(bool search)
 {
     FilteredBySearch = search;
     string[] directories;
     directories = Directory.GetDirectories(WIPLevels ? Settings.Instance.CustomWIPSongsFolder : Settings.Instance.CustomSongsFolder);
     songs.Clear();
     foreach (var dir in directories)
     {
         BeatSaberSong song = BeatSaberSong.GetSongFromFolder(dir);
         if (song == null)
         {
             //Get songs from subdirectories
             string[] subDirectories = Directory.GetDirectories(dir);
             foreach (var subDir in subDirectories)
             {
                 song = BeatSaberSong.GetSongFromFolder(subDir);
             }
         }
         if (song != null)
         {
             songs.Add(song);
         }
     }
     //Sort by song name, and filter by search text.
     if (FilteredBySearch)
     {
         songs = songs.Where(x => searchField.text != "" ? x.songName.AllIndexOf(searchField.text).Any() : true).ToList();
     }
     songs   = songs.OrderBy(x => x.songName).ToList();
     maxPage = Mathf.Max(0, Mathf.CeilToInt(songs.Count / (items.Length + 1)));
     SetPage(0);
 }
示例#2
0
    public void RefreshSongList()
    {
        songLocationToggleText.StringReference.TableEntryReference = WIPLevels ? "custom" : "wip";

        FilteredBySearch = !string.IsNullOrEmpty(searchField.text);
        string[] directories;
        directories = Directory.GetDirectories(WIPLevels ? Settings.Instance.CustomWIPSongsFolder : Settings.Instance.CustomSongsFolder);
        songs.Clear();
        foreach (var dir in directories)
        {
            BeatSaberSong song = BeatSaberSong.GetSongFromFolder(dir);
            if (song == null)
            {
                Debug.LogWarning($"No song at location {dir} exists! Is it in a subfolder?");

                /*
                 * Subfolder loading support has been removed for the following:
                 * A) SongCore does not natively support loading from subfolders, only through editing a config file
                 * B) OneClick no longer saves to a subfolder
                 */
                /*if (dir.ToUpper() == "CACHE") continue; //Ignore the cache folder
                 * //Get songs from subdirectories
                 * string[] subDirectories = Directory.GetDirectories(dir);
                 * foreach (var subDir in subDirectories)
                 * {
                 *  song = BeatSaberSong.GetSongFromFolder(subDir);
                 *  if (song != null) songs.Add(song);
                 * }*/
            }
            else
            {
                songs.Add(song);
            }
        }
        //Sort by song name, and filter by search text.
        songs   = songs.OrderBy(x => x.songName).ToList();
        maxPage = Mathf.Max(0, Mathf.CeilToInt((songs.Count - 1) / items.Length));
        if (FilteredBySearch)
        {
            FilterBySearch();
        }
        else
        {
            filteredSongs = songs;
            SetPage(lastVisitedPage);
        }
    }
示例#3
0
 public void RefreshSongList(bool search)
 {
     FilteredBySearch = search;
     string[] directories = new string[] { };
     if (WIPLevels) //Grabs songs from CustomWIPLevels or CustomLevels
     {
         directories = Directory.GetDirectories(Settings.Instance.CustomWIPSongsFolder);
     }
     else
     {
         directories = Directory.GetDirectories(Settings.Instance.CustomSongsFolder);
     }
     songs.Clear();
     for (int i = 0; i < directories.Length; i++)
     {
         BeatSaberSong song = BeatSaberSong.GetSongFromFolder(directories[i]);
         if (song == null)
         {   //Get songs from subdirectories
             string[] subDirectories = Directory.GetDirectories(directories[i]);
             for (int e = 0; e < subDirectories.Length; e++)
             {
                 song = BeatSaberSong.GetSongFromFolder(subDirectories[e]);
             }
         }
         if (song != null)
         {
             songs.Add(song);
         }
     }
     //Sort by song name, and filter by search text.
     if (FilteredBySearch)
     {
         songs = songs.Where(x => searchField.text != "" ? x.songName.AllIndexOf(searchField.text).Any() : true).ToList();
     }
     songs   = songs.OrderBy(x => x.songName).ToList();
     maxPage = Mathf.Max(0, Mathf.CeilToInt(songs.Count / items.Length));
     SetPage(0);
 }
    private IEnumerator GetBeatmapFromLocation(Uri uri)
    {
        // We will extract the contents of the zip to the temp directory, so we will save the zip in memory.
        DownloadHandlerBuffer downloadHandler = new DownloadHandlerBuffer();

        // Create our web request, and set our handler.
        UnityWebRequest request = UnityWebRequest.Get(uri);

        request.downloadHandler = downloadHandler;
        // Change our User-Agent so BeatSaver can download our map
        request.SetRequestHeader("User-Agent", $"{Application.productName}/{Application.version}");

        // Set progress bar state.
        PersistentUI.Instance.LevelLoadSlider.gameObject.SetActive(true);
        PersistentUI.Instance.LevelLoadSlider.value     = 0;
        PersistentUI.Instance.LevelLoadSliderLabel.text = $"Downloading file... Starting download...";

        var operation = request.SendWebRequest();

        while (!request.isDone)
        {
            // Grab Content-Length, which is the length of the downloading file, to use for progress bar.
            if (int.TryParse(request.GetResponseHeader("Content-Length"), out int length))
            {
                float progress = downloadHandler.data.Length / (float)length;
                PersistentUI.Instance.LevelLoadSlider.value = progress;
                float percent = progress * 100;
                PersistentUI.Instance.LevelLoadSliderLabel.text = $"Downloading file... {percent:F2}% complete.";
            }
            else
            {
                // Just gives the bar something to do until we get the content length.
                PersistentUI.Instance.LevelLoadSlider.value = (Mathf.Sin(Time.time) / 2) + 0.5f;
            }

            // Cancel loading if an error has occurred.
            if (request.isHttpError || request.isNetworkError)
            {
                CancelTempLoader(request.error);
                yield break;
            }
            yield return(new WaitForEndOfFrame());
        }
        // Check one more time to be safe.
        if (request.isHttpError || request.isNetworkError)
        {
            CancelTempLoader(request.error);
            yield break;
        }

        // Wahoo! We are done. Let's grab our downloaded data.
        byte[] downloaded = downloadHandler.data;

        // If the request failed, our downloaded bytes will be null. Let's check that.
        if (downloaded != null)
        {
            PersistentUI.Instance.LevelLoadSlider.value     = 1;
            PersistentUI.Instance.LevelLoadSliderLabel.text = "Extracting contents...";

            yield return(new WaitForEndOfFrame());

            try
            {
                // Slap our downloaded bytes into a memory stream and slap that into a ZipArchive.
                MemoryStream stream  = new MemoryStream(downloaded);
                ZipArchive   archive = new ZipArchive(stream, ZipArchiveMode.Read);

                // Create the directory for our song to go to.
                // Path.GetTempPath() should be compatible with Windows and UNIX.
                // See Microsoft docs on it.
                string directory = $"{Path.GetTempPath()}ChroMapper Temp Loader\\{request.GetHashCode()}";
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                // Extract our zipped file into this directory.
                archive.ExtractToDirectory(directory);

                // Dispose our downloaded bytes, we don't need them.
                downloadHandler.Dispose();

                // Try and get a BeatSaberSong out of what we've downloaded.
                BeatSaberSong song = BeatSaberSong.GetSongFromFolder(directory);
                if (song != null)
                {
                    PersistentUI.Instance.LevelLoadSliderLabel.text = "Loading song...";
                    BeatSaberSongContainer.Instance.song            = song;
                }
                else
                {
                    CancelTempLoader("Could not obtain a valid Beatmap from the downloaded content.");
                }
            }
            catch (Exception e)
            {
                // Uh oh, an error occurred.
                // Let's see if it is due to user error, or a genuine error on ChroMapper's part.
                switch (e.GetType().Name)
                {
                // InvalidDataException means that the ZipArchive cannot be created.
                // ChroMapper tried to download something that is not a zip.
                case nameof(InvalidDataException):
                    CancelTempLoader($"Downloaded content was not a valid zip.");
                    break;

                // Default case is a genuine error, let's print what it has to say.
                default:
                    CancelTempLoader($"An unknown error ({e.GetType().Name}) has occurred:\n\n{e.Message}");
                    break;
                }
            }
        }
        else
        {
            CancelTempLoader("Downloaded bytes is somehow null, yet the request was successfully completed. WTF!?");
        }
    }