public void TestAutoFinish()
        {
            var listener = new TaskListener();

            Assert.IsFalse(listener.IsAutoFinish);
            Assert.IsFalse(listener.IsFinished);
            listener.SetFinished();
            Assert.IsTrue(listener.IsFinished);

            listener = new TaskListener();
            listener.IsAutoFinish = true;
            Assert.IsFalse(listener.IsFinished);
            Assert.IsTrue(listener.IsAutoFinish);
            listener.SetFinished();
            Assert.IsTrue(listener.IsFinished);

            listener = new TaskListener();
            var sub = listener.CreateSubListener();

            sub.SetFinished();
            Assert.IsFalse(listener.IsFinished);
            var sub2 = listener.CreateSubListener();
            var sub3 = listener.CreateSubListener();

            listener.IsAutoFinish = true;
            Assert.IsFalse(listener.IsFinished);
            sub2.SetFinished();
            Assert.IsFalse(listener.IsFinished);
            sub3.SetFinished();
            Assert.IsTrue(listener.IsFinished);
        }
Beispiel #2
0
        public override Task Reload(TaskListener listener = null)
        {
            return(Task.Run(async() =>
            {
                // Perform internal reloading routine.
                await base.Reload(listener?.CreateSubListener());

                // Retrieve processor count.

                // Load all mapsets from the storage.
                var rawMapsets = GetAll().ToList();
                var threadedLoader = new ThreadedLoader <Mapset, Mapset>(ProcessMapsetLoad);
                var results = await threadedLoader.StartLoad(
                    8,
                    rawMapsets,
                    listener: listener?.CreateSubListener <Mapset[]>()
                    );

                // Add results to the mapsets list.
                lock (mapsets)
                {
                    mapsets.Clear();
                    for (int i = 0; i < results.Length; i++)
                    {
                        if (results[i] != null)
                        {
                            mapsets.Add(results[i]);
                        }
                    }
                }
                listener?.SetFinished();
            }));
        }
        public void TestSubListener()
        {
            var listener = new TaskListener();

            Assert.AreEqual(0, listener.SubListenerCount);

            var sub = listener.CreateSubListener();

            Assert.AreEqual(1, listener.SubListenerCount);

            var sub2 = listener.CreateSubListener();

            Assert.AreEqual(2, listener.SubListenerCount);
        }
        public void TestTotalProgress()
        {
            var   listener = new TaskListener();
            float progress = 0f;

            listener.OnProgress += (p) => progress = p;

            listener.SetProgress(0.4f);
            Assert.AreEqual(0.4f, listener.TotalProgress, Delta);
            Assert.AreEqual(0.4f, progress, Delta);
            Assert.AreEqual(0.4f, listener.Progress, Delta);

            var   sub         = listener.CreateSubListener();
            float subProgress = 0f;

            sub.OnProgress += (p) => subProgress = p;
            Assert.AreEqual(0.2f, listener.TotalProgress, Delta);
            Assert.AreEqual(0.2f, progress, Delta);
            Assert.AreEqual(0.4f, listener.Progress, Delta);
            Assert.AreEqual(0f, subProgress, Delta);
            Assert.AreEqual(0f, sub.Progress, Delta);

            sub.SetProgress(1f);
            Assert.AreEqual(0.7f, listener.TotalProgress, Delta);
            Assert.AreEqual(0.7f, progress, Delta);
            Assert.AreEqual(0.4f, listener.Progress, Delta);
            Assert.AreEqual(1f, subProgress, Delta);
            Assert.AreEqual(1f, sub.Progress, Delta);
        }
        public void TestHasOwnProgress()
        {
            var listener = new TaskListener();

            Assert.IsTrue(listener.HasOwnProgress);

            listener.SetProgress(0.5f);
            Assert.AreEqual(0.5f, listener.Progress, Delta);
            Assert.AreEqual(0.5f, listener.TotalProgress, Delta);

            var subListener = listener.CreateSubListener();

            Assert.AreEqual(0.5f, listener.Progress, Delta);
            Assert.AreEqual(0f, subListener.Progress, Delta);
            Assert.AreEqual(0.25f, listener.TotalProgress, Delta);

            listener.HasOwnProgress = false;
            Assert.IsFalse(listener.HasOwnProgress);
            Assert.AreEqual(0.5f, listener.Progress, Delta);
            Assert.AreEqual(0f, subListener.Progress, Delta);
            Assert.AreEqual(0f, listener.TotalProgress, Delta);

            listener.SetProgress(1f);
            subListener.SetProgress(0.25f);
            Assert.AreEqual(1f, listener.Progress, Delta);
            Assert.AreEqual(0.25f, subListener.Progress, Delta);
            Assert.AreEqual(0.25f, listener.TotalProgress, Delta);
        }
        public Task Reload(TaskListener listener = null)
        {
            return(Task.Run(async() =>
            {
                if (listener != null)
                {
                    listener.HasOwnProgress = false;
                }

                // Wait for store reloading.
                await store.Reload(listener?.CreateSubListener());

                // Run on the main thread
                UnityThread.DispatchUnattended(() =>
                {
                    // Refill the mapset list
                    allMapsets.Clear();
                    allMapsets.AddRange(store.Mapsets);

                    // TODO: Process for a case where the previously selected map no longer exists.

                    // Fill the displayed mapsets list using last search term.
                    Search(lastSearch);
                    // Finished
                    listener?.SetFinished();
                    return null;
                });
            }));
        }
 public virtual Task Reload(TaskListener listener = null)
 {
     return(Task.Run(() =>
     {
         InitModules(true);
         LoadOrphanedData(listener?.CreateSubListener());
         listener.SetFinished();
     }));
 }
 public Task <IMapset> Import(FileInfo file, TaskListener <IMapset> listener = null)
 {
     return(Task.Run <IMapset>(async() =>
     {
         try
         {
             // Start importing the file
             Mapset mapset = await store.Import(file, listener: listener?.CreateSubListener <Mapset>());
             if (mapset != null)
             {
                 // Mapset must be fully loaded.
                 Mapset loadedMapset = store.LoadData(mapset);
                 if (loadedMapset != null)
                 {
                     // Dispatch mapset imported event on main thread.
                     UnityThread.Dispatch(() =>
                     {
                         // Add to all mapsets
                         allMapsets.AddOrReplace(loadedMapset);
                         // Reapply filter
                         Search(lastSearch);
                         OnImportMapset?.Invoke(loadedMapset);
                         return null;
                     });
                 }
                 else
                 {
                     notificationBox?.Add(new Notification()
                     {
                         Message = $"Failed to load imported mapset ({mapset.Metadata.Artist} - {mapset.Metadata.Title})",
                         Type = NotificationType.Error
                     });
                 }
             }
             else
             {
                 notificationBox?.Add(new Notification()
                 {
                     Message = $"Failed to import mapset at ({file.FullName})",
                     Type = NotificationType.Error
                 });
             }
             listener?.SetFinished(mapset);
             return mapset;
         }
         catch (Exception e)
         {
             Logger.LogError($"Error while importing mapset: {e.Message}\n{e.StackTrace}");
             listener?.SetFinished();
             return null;
         }
     }));
 }
        void ITask <T> .StartTask(TaskListener <T> listener)
        {
            TaskListener <IWebRequest> newListener = null;

            if (listener != null)
            {
                listener.HasOwnProgress = false;
                newListener             = listener.CreateSubListener <IWebRequest>();
                newListener.OnFinished += (req) => listener.SetFinished(Output);
            }
            Request(newListener);
        }
Beispiel #10
0
        /// <summary>
        /// Records the specified play record under the current player.
        /// </summary>
        public Task <IRecord> RecordScore(IScoreProcessor scoreProcessor, int playTime, TaskListener <IRecord> listener = null)
        {
            return(Task.Run <IRecord>(async() =>
            {
                try
                {
                    if (scoreProcessor == null || scoreProcessor.JudgeCount <= 0)
                    {
                        listener?.SetFinished();
                        return null;
                    }

                    // Retrieve user and user stats.
                    var currentMap = currentParameter.Map;
                    var user = UserManager.CurrentUser.Value;
                    var userStats = user.GetStatistics(currentMap.PlayableMode);

                    // Record the play result to records database and user statistics.
                    Record newRecord = new Record(currentMap, user, scoreProcessor, playTime);
                    lastRecord = newRecord;
                    // Retrieve old records for the map and user.
                    var records = await RecordStore.GetTopRecords(currentMap, user, limit: null, listener: listener?.CreateSubListener <List <IRecord> >());

                    // Save as cleared play.
                    if (scoreProcessor.IsFinished)
                    {
                        RecordStore.SaveRecord(newRecord);

                        var bestRecord = records == null || records.Count == 0 ? null : records[0];
                        userStats.RecordPlay(newRecord, bestRecord);
                    }
                    // Save as failed play.
                    else
                    {
                        userStats.RecordIncompletePlay(newRecord);
                    }
                    listener?.SetFinished(newRecord);
                    return newRecord;
                }
                catch (Exception e)
                {
                    Logger.LogError($"Error while recording score: {e.Message}\n{e.StackTrace}");
                    listener?.SetFinished();
                    return null;
                }
            }));
        }
        public async Task <T> Import(FileInfo archive, bool deleteOnImport = true, TaskListener <T> listener = null)
        {
            if (archive == null)
            {
                throw new ArgumentNullException(nameof(archive));
            }
            if (!archive.Exists)
            {
                throw new FileNotFoundException($"File at ({archive.FullName}) does not exist!");
            }

            // Retrieve the compressed file representation of the archive.
            var compressed = CompressedHelper.GetCompressed(archive);

            if (compressed == null)
            {
                throw new NotImportableException(archive, GetType());
            }

            // Start extraction of archive.
            var extractedDir = await compressed.Uncompress(GetTempExtractDir(archive), listener?.CreateSubListener <DirectoryInfo>());

            if (!extractedDir.Exists)
            {
                throw new NotImportableException(archive, GetType());
            }

            // Parse the data at temporary extraction destination.
            var data = ParseData(extractedDir);

            // Failed to parse.
            if (data == null)
            {
                listener?.SetFinished();
                return(default(T));
            }

            // Calculate hash code.
            data.CalculateHash();

            // Check whether this data already exists using hash check.
            bool isNewData = false;

            if (ContainsHash(data.HashCode, out T existingData))
            {
                // Replace existing data.
                PostProcessData(data, existingData.Id);
            }
            else
            {
                // Allocate a new Id.
                PostProcessData(data, Guid.NewGuid());
                isNewData = true;
            }

            // Move the extracted data under management of the storage.
            storage.Move(data.Id.ToString(), extractedDir);
            // Replace or add the data to database.
            database.Edit().Write(data).Commit();

            // Delete archive
            if (deleteOnImport)
            {
                archive.Delete();
            }

            // Report finished.
            listener?.SetValue(data);
            listener?.SetFinished();
            if (isNewData)
            {
                OnNewData?.Invoke(data);
            }
            return(data);
        }