Exemplo n.º 1
0
        private static void DeleteFilesAsync(string[] paths, Action callback)
        {
            if (backgroundWorker == null)
            {
                backgroundWorker = InitializeBackgroundWorker();

                backgroundWorker.DoWork += (sender, args) =>
                {
                    DeleteFiles(paths);
                };

                backgroundWorker.RunWorkerCompleted += (sender, args) =>
                {
                    if (callback != null)
                    {
                        AsyncActionHelper.RunInGameThread(() =>
                        {
                            callback();
                        });
                    }

                    backgroundWorker = null;
                };

                backgroundWorker.RunWorkerAsync();
            }
            else if (backgroundWorker != null && backgroundWorker.IsBusy)
            {
                EnqueueAction(() =>
                {
                    DeleteFilesAsync(paths, callback);
                });
            }
        }
Exemplo n.º 2
0
        private static void ReadFileAsync <T>(string path, Action <T> callback)
        {
            if (backgroundWorker == null)
            {
                backgroundWorker = InitializeBackgroundWorker();

                backgroundWorker.DoWork += (sender, args) =>
                {
                    args.Result = ReadAndDeserialize <T>(path);
                };

                backgroundWorker.RunWorkerCompleted += (sender, args) =>
                {
                    var result = (T)args.Result;

                    AsyncActionHelper.RunInGameThread(() =>
                    {
                        callback(result);
                    });

                    backgroundWorker = null;
                };

                backgroundWorker.RunWorkerAsync();
            }
            else if (backgroundWorker != null && backgroundWorker.IsBusy)
            {
                EnqueueAction(() =>
                {
                    ReadFileAsync <T>(path, callback);
                });
            }
        }
Exemplo n.º 3
0
        private static void ReadFilesAsync <T>(string rootPath, string searchPattern, Action <List <T> > callback)
        {
            if (backgroundWorker == null)
            {
                backgroundWorker = InitializeBackgroundWorker();

                backgroundWorker.DoWork += (sender, args) =>
                {
                    args.Result = ReadAndDeserializeMultiple <T>(rootPath, searchPattern);
                };

                backgroundWorker.RunWorkerCompleted += (sender, args) =>
                {
                    var result = (List <T>)args.Result;

                    AsyncActionHelper.RunInGameThread(() =>
                    {
                        callback(result);
                    });

                    backgroundWorker = null;
                };

                backgroundWorker.RunWorkerAsync();
            }
            else if (backgroundWorker != null && backgroundWorker.IsBusy)
            {
                EnqueueAction(() =>
                {
                    ReadFilesAsync(rootPath, searchPattern, callback);
                });
            }
        }
Exemplo n.º 4
0
        private static void SaveDataAsync(object data, string path, Action callback)
        {
            if (backgroundWorker == null)
            {
                backgroundWorker = InitializeBackgroundWorker();

                backgroundWorker.DoWork += (sender, args) =>
                {
                    SerializeAndWrite(data, path);
                };

                backgroundWorker.RunWorkerCompleted += (sender, args) =>
                {
                    if (callback != null)
                    {
                        AsyncActionHelper.RunInGameThread(() =>
                        {
                            callback();
                        });
                    }

                    backgroundWorker = null;
                };

                backgroundWorker.RunWorkerAsync();
            }
            else if (backgroundWorker != null && backgroundWorker.IsBusy)
            {
                EnqueueAction(() =>
                {
                    SaveDataAsync(data, path, callback);
                });
            }
        }
Exemplo n.º 5
0
        private static BackgroundWorker InitializeBackgroundWorker()
        {
            var newBackgroundWorker = new BackgroundWorker();

            newBackgroundWorker.RunWorkerCompleted += (sender, args) =>
            {
                if (args.Error != null)
                {
                    AsyncActionHelper.RunInGameThread(() =>
                    {
                        throw args.Error;
                    });
                }

                if (backgroundWorkQueue != null && backgroundWorkQueue.Count > 0)
                {
                    backgroundWorkQueue.Dequeue().Invoke();
                }
            };

            return(newBackgroundWorker);
        }
Exemplo n.º 6
0
        private static void SaveGameAsync(SavedGame savedGame, string gameDataPath, string metadataPath,
                                          Action callback)
        {
            if (backgroundWorker == null)
            {
                backgroundWorker = InitializeBackgroundWorker();

                backgroundWorker.DoWork += (sender, args) =>
                {
                    SerializeAndWrite(savedGame.gameSpecificData, gameDataPath);
                    SerializeAndWrite(savedGame.metadata, metadataPath);
                };

                backgroundWorker.RunWorkerCompleted += (sender, args) =>
                {
                    if (callback != null)
                    {
                        AsyncActionHelper.RunInGameThread(() =>
                        {
                            callback();
                        });
                    }

                    backgroundWorker = null;
                };

                backgroundWorker.RunWorkerAsync();
            }
            else if (backgroundWorker != null && backgroundWorker.IsBusy)
            {
                EnqueueAction(() =>
                {
                    SaveGameAsync(savedGame, gameDataPath, metadataPath, callback);
                });
            }
        }