예제 #1
0
        private void ReloadCurrentSong()
        {
            if (!_standardLevelSceneSetupData.gameplayCoreSetupData.gameplayModifiers.noFail)
            {
                return;
            }
            var reloadedLevel = LoadSong(GetCustomSongInfo(CurrentLevelPlaying.customLevel.customSongInfo.path));

            if (reloadedLevel == null)
            {
                return;
            }

            reloadedLevel.FixBPMAndGetNoteJumpMovementSpeed();
            reloadedLevel.SetAudioClip(CurrentLevelPlaying.customLevel.audioClip);

            RemoveSong(CurrentLevelPlaying.customLevel);
            CustomLevels.Add(reloadedLevel);

            CustomLevelCollectionSO.AddCustomLevel(reloadedLevel);

            var orderedList = CustomLevels.OrderBy(x => x.songName);

            CustomLevels = orderedList.ToList();

            _standardLevelSceneSetupData.__WillBeUsedInTransition();
            _standardLevelSceneSetupData.Init(
                reloadedLevel.GetDifficultyBeatmap(_standardLevelSceneSetupData.difficultyBeatmap.difficulty),
                _standardLevelSceneSetupData.gameplayCoreSetupData);

            var restartController = Resources.FindObjectsOfTypeAll <StandardLevelRestartController>().FirstOrDefault();

            if (restartController == null)
            {
                Console.WriteLine("No restart controller!");
                return;
            }

            restartController.RestartLevel();
        }
예제 #2
0
        private void RetrieveAllSongs(bool fullRefresh)
        {
            var stopwatch = new Stopwatch();
            var levelList = new List <CustomLevel>();

            if (fullRefresh)
            {
                _customLevelPool.ReturnAll();
                _beatmapDataPool.ReturnAll();
                CustomLevels.Clear();
            }

            Action job = delegate
            {
                try
                {
                    stopwatch.Start();
                    var path = Environment.CurrentDirectory;
                    path = path.Replace('\\', '/');

                    var currentHashes = new List <string>();
                    var cachedHashes  = new List <string>();
                    var cachedSongs   = new string[0];

                    if (Directory.Exists(path + "/CustomSongs/.cache"))
                    {
                        cachedSongs = Directory.GetDirectories(path + "/CustomSongs/.cache");
                    }
                    else
                    {
                        Directory.CreateDirectory(path + "/CustomSongs/.cache");
                    }


                    var songZips = Directory.GetFiles(path + "/CustomSongs")
                                   .Where(x => x.ToLower().EndsWith(".zip") || x.ToLower().EndsWith(".beat") || x.ToLower().EndsWith(".bmap")).ToArray();
                    foreach (var songZip in songZips)
                    {
                        //Check cache if zip already is extracted
                        string hash;
                        string trimmedZip = songZip;
                        trimmedZip = Utils.TrimEnd(trimmedZip, ".zip");
                        trimmedZip = Utils.TrimEnd(trimmedZip, ".beat");
                        trimmedZip = Utils.TrimEnd(trimmedZip, ".bmap");
                        if (Utils.CreateMD5FromFile(songZip, out hash))
                        {
                            using (var unzip = new Unzip(songZip))
                            {
                                try
                                {
                                    if (Directory.Exists(trimmedZip))
                                    {
                                        Log("Directory for Zip already exists, Extracting Zip to Cache instead.");
                                        cachedHashes.Add(hash);
                                        if (cachedSongs.Any(x => x.Contains(hash)))
                                        {
                                            continue;
                                        }
                                        unzip.ExtractToDirectory(path + "/CustomSongs/.cache/" + hash);
                                    }
                                    else
                                    {
                                        unzip.ExtractToDirectory(path + "/CustomSongs/" + trimmedZip.Replace(path + "/CustomSongs\\", ""));
                                        //Add hash if successfully extracted
                                        currentHashes.Add(hash);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Log("Error extracting zip " + songZip + "\n" + e, LogSeverity.Warn);
                                }
                            }
                        }
                        else
                        {
                            Log("Error reading zip " + songZip, LogSeverity.Warn);
                        }
                    }


                    var songFolders = Directory.GetDirectories(path + "/CustomSongs").ToList();
                    var songCaches  = Directory.GetDirectories(path + "/CustomSongs/.cache");

                    foreach (var songZip in songZips)
                    {
                        //Delete zip if successfully extracted
                        string hash;
                        if (Utils.CreateMD5FromFile(songZip, out hash))
                        {
                            if (currentHashes.Contains(hash))
                            {
                                Log("Zip Successfully Extracted, deleting zip.");
                                File.SetAttributes(songZip, FileAttributes.Normal);
                                File.Delete(songZip);
                            }
                        }
                    }

                    foreach (var song in songCaches)
                    {
                        var hash = Path.GetFileName(song);
                        if (!cachedHashes.Contains(hash))
                        {
                            //Old cache
                            Directory.Delete(song, true);
                        }
                    }



                    var loadedIDs = new List <string>();

                    float i = 0;
                    foreach (var song in songFolders)
                    {
                        i++;
                        var results = Directory.GetFiles(song, "info.json", SearchOption.AllDirectories);
                        if (results.Length == 0)
                        {
                            Log("Custom song folder '" + song + "' is missing info.json files!", LogSeverity.Warn);
                            continue;
                        }


                        foreach (var result in results)
                        {
                            try
                            {
                                var songPath = Path.GetDirectoryName(result).Replace('\\', '/');
                                if (!fullRefresh)
                                {
                                    if (CustomLevels.Any(x => x.customSongInfo.path == songPath))
                                    {
                                        continue;
                                    }
                                }

                                var customSongInfo = GetCustomSongInfo(songPath);

                                if (customSongInfo == null)
                                {
                                    continue;
                                }
                                var id = customSongInfo.GetIdentifier();
                                if (loadedIDs.Any(x => x == id))
                                {
                                    Log("Duplicate song found at " + customSongInfo.path, LogSeverity.Warn);
                                    continue;
                                }

                                loadedIDs.Add(id);

                                if (CustomPlatformsPresent && customSongPlatforms)
                                {
                                    if (customSongInfo.customEnvironment != null)
                                    {
                                        if (findCustomEnvironment(customSongInfo.customEnvironment) == -1)
                                        {
                                            Console.WriteLine("CustomPlatform not found: " + customSongInfo.customEnvironment);
                                            if (customSongInfo.customEnvironmentHash != null)
                                            {
                                                Console.WriteLine("Downloading with hash: " + customSongInfo.customEnvironmentHash);
                                                StartCoroutine(downloadCustomPlatform(customSongInfo.customEnvironmentHash, customSongInfo.customEnvironment));
                                            }
                                        }
                                    }
                                }

                                var i1 = i;
                                HMMainThreadDispatcher.instance.Enqueue(delegate
                                {
                                    if (_loadingCancelled)
                                    {
                                        return;
                                    }
                                    var level = LoadSong(customSongInfo);
                                    if (level != null)
                                    {
                                        levelList.Add(level);
                                    }

                                    LoadingProgress = i1 / songFolders.Count;
                                });
                            }
                            catch (Exception e)
                            {
                                Log("Failed to load song folder: " + result, LogSeverity.Warn);
                                Log(e.ToString(), LogSeverity.Warn);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log("RetrieveAllSongs failed:", LogSeverity.Error);
                    Log(e.ToString(), LogSeverity.Error);
                }
            };

            Action finish = delegate
            {
                stopwatch.Stop();
                Log("Loaded " + levelList.Count + " new songs in " + stopwatch.Elapsed.Seconds + " seconds");

                CustomLevels.AddRange(levelList);
                var orderedList = CustomLevels.OrderBy(x => x.songName);
                CustomLevels = orderedList.ToList();

                foreach (var customLevel in CustomLevels)
                {
                    CustomLevelCollectionSO.AddCustomLevel(customLevel);
                }

                AreSongsLoaded  = true;
                AreSongsLoading = false;
                LoadingProgress = 1;

                _loadingTask = null;

                if (SongsLoadedEvent != null)
                {
                    SongsLoadedEvent(this, CustomLevels);
                }
            };

            _loadingTask = new HMTask(job, finish);
            _loadingTask.Run();
        }