public void HandleRefreshLibraryButtonClicked()
        {
            //throw if task running, or errored
            if (CurrentTask != null && !CurrentTask.IsCompleted)
            {
                throw new InvalidOperationException();
            }

            if (FatalErrorEncountered)
            {
                throw new InvalidOperationException();
            }

            DisableUI();

            //clear data and browse path
            XSMPState.BrowseData?.Clear();
            XSMPState.CurrentBrowsePath = new List <string>();

            //TODO also clear playlist?

            CurrentTokenSource = new CancellationTokenSource();
            CurrentTask        = refreshLibraryAsync(CurrentTokenSource.Token);
            AsyncUtils.RunWithExceptionHandling(async() => await CurrentTask);

            async Task refreshLibraryAsync(CancellationToken token)
            {
                try
                {
                    await Task.Run(() => XSMPModule.Instance.RefreshLibrary(token));
                }
                catch (Exception e)
                {
                    AsyncUtils.ThrowIfEditorStopped();

                    if (e is TaskCanceledException || e is OperationCanceledException)
                    {
                        Debug.Log("[XSMP] Async operation cancelled");
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    EnableUI();
                    SignalPaint();
                }
            }
        }
Пример #2
0
    public static void TestWeakReferencePauseLock()
    {
        AsyncUtils.RunWithExceptionHandling(async() =>
        {
            object lockObject = new object();
            WeakReference untypedWeakReference = new WeakReference(lockObject);
            LockPauseModule.PauseGame(PauseLockType.AllowMenu, untypedWeakReference);
            Debug.Log("Untyped lock added!");

            await Task.Delay(3000);
            AsyncUtils.ThrowIfEditorStopped();

            lockObject = null;
            CoreUtils.CollectGarbage(false);

            Debug.Log("Untyped lock released!");

            await Task.Yield();
            AsyncUtils.ThrowIfEditorStopped();

            LockPauseModule.ForceCleanLocks();

            Debug.Log("Locks cleaned!");

            await Task.Delay(5000);
            AsyncUtils.ThrowIfEditorStopped();

            IEnumerable typedLockObject = new string[] { "lol", "hi" };
            WeakReference <IEnumerable> typedWeakReference = new WeakReference <IEnumerable>(typedLockObject);

            LockPauseModule.PauseGame(PauseLockType.AllowMenu, typedWeakReference);
            Debug.Log("Typed lock added!");

            await Task.Delay(3000);
            AsyncUtils.ThrowIfEditorStopped();

            typedLockObject = null;
            CoreUtils.CollectGarbage(false);

            Debug.Log("Typed lock released!");

            await Task.Yield();
            AsyncUtils.ThrowIfEditorStopped();

            LockPauseModule.ForceCleanLocks();

            Debug.Log("Locks cleaned!");
        });
    }
Пример #3
0
        public static void TestScreenFader()
        {
            AsyncUtils.RunWithExceptionHandling(async() =>
            {
                ScreenFader.FadeTo(Color.white, 5.0f, true, true, true);

                await Task.Delay(6000);
                AsyncUtils.ThrowIfEditorStopped();

                SharedUtils.ChangeScene("TestScene");

                await Task.Delay(5000);
                AsyncUtils.ThrowIfEditorStopped();

                ScreenFader.FadeFrom(null, 1.0f, false, true, true);

                await Task.Delay(5000);
                AsyncUtils.ThrowIfEditorStopped();

                ScreenFader.Crossfade(Color.blue, Color.red, 5.0f, false, false, false);

                await Task.Delay(3000);
                AsyncUtils.ThrowIfEditorStopped();

                ScreenFader.ClearFade();

                await Task.Delay(5000);
                AsyncUtils.ThrowIfEditorStopped();

                ScreenFader.Crossfade(Color.blue, Color.red, 5.0f, false, false, false);

                await Task.Delay(1000);
                AsyncUtils.ThrowIfEditorStopped();

                SharedUtils.ChangeScene("TestScene");

                await Task.Delay(5000);
                AsyncUtils.ThrowIfEditorStopped();

                ScreenFader.FadeTo(Color.black, 1.0f, true, false, true);

                await Task.Delay(1500);
                AsyncUtils.ThrowIfEditorStopped();

                SharedUtils.EndGame();
            });
        }
        private async Task NavigateAsync(CancellationToken token)
        {
            try
            {
                if (XSMPState.CurrentBrowsePath.Count == 1)
                {
                    //root folders basically
                    switch (XSMPState.CurrentBrowsePath[0])
                    {
                    case "folder":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetRootFolders(token));

                        break;

                    case "album":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetAlbums(token));

                        break;

                    case "artist":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetArtists(token));

                        break;

                    case "playlist":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetPlaylists(token));

                        break;
                    }
                }
                else
                {
                    switch (XSMPState.CurrentBrowsePath[0])
                    {
                    case "folder":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetFolderContents(XSMPState.CurrentBrowsePath[1], token));

                        {
                            //break up the browse path
                            string[] pathSegments = XSMPState.CurrentBrowsePath[1].Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
                            if (pathSegments.Length > 1)
                            {
                                string lastPath = string.Join("/", new ArraySegment <string>(pathSegments, 0, pathSegments.Length - 1));
                                Debug.Log(lastPath);
                                XSMPState.BrowseData.Insert(0, new HeaderDataRow("..", new string[] { "folder", lastPath }));
                            }
                            else
                            {
                                XSMPState.BrowseData.Insert(0, new HeaderDataRow("..", new string[] { "folder" }));
                            }
                        }

                        break;

                    case "album":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetAlbumSongs(XSMPState.CurrentBrowsePath[1], token));

                        //when we add the previous browse path row we will need to be careful; examine LastBrowsePath to see how we got here
                        if (XSMPState.LastBrowsePath[0] == "artist")
                        {
                            XSMPState.BrowseData.Insert(0, new HeaderDataRow("<-Artist Albums", new string[] { "artist", XSMPState.LastBrowsePath[1] }));
                        }
                        else if (XSMPState.LastBrowsePath[0] == "album")
                        {
                            XSMPState.BrowseData.Insert(0, new HeaderDataRow("<-Albums", new string[] { "album" }));
                        }

                        break;

                    case "artist":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetArtistAlbums(XSMPState.CurrentBrowsePath[1], token));

                        XSMPState.BrowseData.Insert(0, new HeaderDataRow("<-Artists", new string[] { "artist" }));     //I think this is always correct
                        break;

                    case "playlist":
                        XSMPState.BrowseData = await Task.Run(() => XSMPModule.Instance.GetPlaylist(XSMPState.CurrentBrowsePath[1], token));

                        XSMPState.BrowseData.Insert(0, new HeaderDataRow("<-Playlists", new string[] { "playlist" }));
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                AsyncUtils.ThrowIfEditorStopped();

                if (e is TaskCanceledException || e is OperationCanceledException)
                {
                    Debug.Log("[XSMP] Navigation operation cancelled");

                    XSMPState.BrowseData.Clear();
                    //TODO clear browse path?
                }
                else
                {
                    throw; //TODO handle this instead of f*****g the UI
                }
            }

            //refresh and unlock
            EnableUI();
            SignalPaint();
        }