static bool Prefix(ref BeatmapIdentifierNetSerializable beatmapId, ref GameplayModifiers gameplayModifiers, ref float initialStartTime, MultiplayerLevelLoader __instance)
        {
            if (!beatmapId.levelID.StartsWith(CustomLevelPrefix) || SongCore.Loader.GetLevelById(beatmapId.levelID) != null)
            {
                return(true);
            }
            MultiplayerLevelLoader = __instance;
            string levelId = beatmapId?.levelID;
            BeatmapIdentifierNetSerializable bmId = beatmapId;
            GameplayModifiers modifiers           = gameplayModifiers;
            float             startTime           = initialStartTime;

            if (levelId != null && levelId.StartsWith(CustomLevelPrefix))
            {
                if (SongCore.Loader.GetLevelById(levelId) != null)
                {
                    LoadingLevelId = null;
                    return(true);
                }
                if (LoadingLevelId == null || LoadingLevelId != levelId)
                {
                    LoadingLevelId = levelId;
                    var downloadTask = Downloader.TryDownloadSong(levelId, CancellationToken.None, r =>
                    {
                        if (r)
                        {
                            //Plugin.Log?.Debug($"Triggering 'LobbyGameStateController.HandleMenuRpcManagerStartedLevel' after level download.");
                            //LobbyGameStateController_HandleMenuRpcManagerStartedLevel.LobbyGameStateController.HandleMenuRpcManagerStartedLevel(LobbyGameStateController_HandleMenuRpcManagerStartedLevel.LastUserId, bmId, modifiers, startTime);
                        }
                        else
                        {
                            Plugin.Log?.Warn($"TryDownloadSong was unsuccessful.");
                        }
                        MultiplayerLevelLoader.LoadLevel(bmId, modifiers, startTime);
                        LoadingLevelId = null;
                    });
                    return(false);
                }
            }
            LoadingLevelId = null;
            return(true);
        }
        internal void Inject(IMultiplayerSessionManager sessionManager, HostLobbySetupViewController hostViewController, MultiplayerLevelLoader levelLoader)
        {
            this.sessionManager = sessionManager;
            base.DidActivate(true, false, true);

            hostViewController.didActivateEvent += OnActivate;
        }
Esempio n. 3
0
        static bool Prefix(ref BeatmapIdentifierNetSerializable beatmapId, ref GameplayModifiers gameplayModifiers, ref float initialStartTime, MultiplayerLevelLoader __instance)
        {
            string?levelId = beatmapId.levelID;

            if (SongCore.Loader.GetLevelById(levelId) != null)
            {
                return(true);
            }
            string?hash = Utilities.Utilities.LevelIdToHash(beatmapId.levelID);

            if (hash == null)
            {
                Plugin.Log?.Info($"Could not get a hash from beatmap with LevelId {beatmapId.levelID}");
                return(true);
            }

            MultiplayerLevelLoader = __instance;
            BeatmapIdentifierNetSerializable bmId = beatmapId;
            GameplayModifiers modifiers           = gameplayModifiers;
            float             startTime           = initialStartTime;

            if (LoadingLevelId == null || LoadingLevelId != levelId)
            {
                LoadingLevelId = levelId;
                var downloadTask = Downloader.TryDownloadSong(levelId, CancellationToken.None, success =>
                {
                    try
                    {
                        if (success)
                        {
                            //Plugin.Log?.Debug($"Triggering 'LobbyGameStateController.HandleMenuRpcManagerStartedLevel' after level download.");
                            //LobbyGameStateController_HandleMenuRpcManagerStartedLevel.LobbyGameStateController.HandleMenuRpcManagerStartedLevel(LobbyGameStateController_HandleMenuRpcManagerStartedLevel.LastUserId, bmId, modifiers, startTime);
                        }
                        else
                        {
                            Plugin.Log?.Warn($"TryDownloadSong was unsuccessful.");
                        }
                        MultiplayerLevelLoader.LoadLevel(bmId, modifiers, startTime);
                    }
                    catch (Exception ex)
                    {
                        Plugin.Log?.Warn($"Error in TryDownloadSong callback: {ex.Message}");
                        Plugin.Log?.Debug(ex);
                    }
                    finally
                    {
                        LoadingLevelId = null;
                    }
                });
                return(false);
            }
            // LoadingLevelId = null;
            return(true);
        }
        static bool Prefix(ref BeatmapIdentifierNetSerializable beatmapId, ref GameplayModifiers gameplayModifiers, ref float initialStartTime, MultiplayerLevelLoader __instance)
        {
            string?levelId = beatmapId.levelID;

            if (SongCore.Loader.GetLevelById(levelId) != null)
            {
                if (LoadingLevelId != levelId)
                {
                    Plugin.Log?.Debug($"Level with ID '{levelId}' already exists."); // Don't log if LoadLevel was called when a download finished.
                }
                LoadingLevelId = null;
                return(true);
            }
            string?hash = Utilities.Utils.LevelIdToHash(beatmapId.levelID);

            if (hash == null)
            {
                Plugin.Log?.Info($"Could not get a hash from beatmap with LevelId {beatmapId.levelID}");
                LoadingLevelId = null;
                return(true);
            }
            if (Downloader.TryGetDownload(levelId, out _))
            {
                Plugin.Log?.Debug($"Download for '{levelId}' is already in progress.");
                return(false);
            }
            MultiplayerLevelLoader = __instance;
            BeatmapIdentifierNetSerializable bmId = beatmapId;
            GameplayModifiers modifiers           = gameplayModifiers;
            float             startTime           = initialStartTime;
            // TODO: Link to UI progress bar. Don't forget case where downloaded song may be switched back to a currently downloading one.
            // Probably best to have a class containing a download that the IProgress updates with the current progress.
            // Then that class can be hooked/unhooked from the UI progress bar.
            IProgress <double>?progress = null;

            //IProgress<double> progress = new Progress<double>(p =>
            //{
            //    Plugin.Log?.Debug($"Progress for '{bmId.levelID}': {p:P}");
            //});
            if (LoadingLevelId == null || LoadingLevelId != levelId)
            {
                LoadingLevelId = levelId;

                Plugin.Log?.Debug($"Attempting to download level with ID '{levelId}'...");
                Task?downloadTask = Downloader.TryDownloadSong(levelId, progress, CancellationToken.None).ContinueWith(b =>
                {
                    try
                    {
                        IPreviewBeatmapLevel?level = b.Result;
                        if (level != null)
                        {
                            Plugin.Log?.Debug($"Level with ID '{levelId}' was downloaded successfully.");
                            //Plugin.Log?.Debug($"Triggering 'LobbyGameStateController.HandleMenuRpcManagerStartedLevel' after level download.");
                            //LobbyGameStateController_HandleMenuRpcManagerStartedLevel.LobbyGameStateController.HandleMenuRpcManagerStartedLevel(LobbyGameStateController_HandleMenuRpcManagerStartedLevel.LastUserId, bmId, modifiers, startTime);
                            MultiplayerLevelLoader.LoadLevel(bmId, modifiers, startTime);
                        }
                        else
                        {
                            Plugin.Log?.Warn($"TryDownloadSong was unsuccessful.");
                        }
                    }
                    catch (Exception ex)
                    {
                        Plugin.Log?.Warn($"Error in TryDownloadSong continuation: {ex.Message}");
                        Plugin.Log?.Debug(ex);
                    }
                    finally
                    {
                        LoadingLevelId = null;
                    }
                });
                return(false);
            }
            return(true);
        }