Beispiel #1
0
        public void BackupCheck()
        {
            if (!enabled || !UserSettings.BackupEnabled)
            {
                return;
            }
            if (input.IsUserIdleFor(UserSettings.BackupIdleTime) || input.IsBackupDelayedFor(UserSettings.BackupMaxTime))
            {
                lock (multiBoard)
                {
                    if (multiBoard.SelectedBoard == null ||
                        multiBoard.SelectedBoard.Mouse == null ||
                        multiBoard.SelectedBoard.Mouse.State != MouseState.Selection ||
                        multiBoard.SelectedBoard.Mouse.BoundItems.Count > 0)
                    {
                        return;
                    }
                }
                input.OnBackup();

                // We really don't want to hang on IO while multiBoard is locked, so we queue it
                // This also prevents from having to lock both BackupManager and MultiBoard, which might deadlock
                Dictionary <string, string> ioQueue = new Dictionary <string, string>();

                // We also don't want to serialize while locked, since not all of the serialization process requires locking
                Dictionary <string, SerializationManager> serQueue = new Dictionary <string, SerializationManager>();

                // BackupManager serves as the backup file access lock, to avoid conflicts when writing files
                lock (this)
                {
                    lock (multiBoard)
                    {
                        if (multiBoard.UserObjects.Dirty)
                        {
                            multiBoard.UserObjects.Dirty = false;
                            ioQueue.Add(userObjsFileName, multiBoard.UserObjects.SerializedForm);
                        }
                        foreach (Board board in multiBoard.Boards)
                        {
                            if (board.Dirty)
                            {
                                board.Dirty = false;
                                serQueue.Add(board.UniqueID.ToString() + ".ham", board.SerializationManager);
                            }
                        }
                    }

                    // Execute the serialization queue
                    if (serQueue.Count > 0)
                    {
                        foreach (KeyValuePair <string, SerializationManager> serReq in serQueue)
                        {
                            ioQueue.Add(serReq.Key, serReq.Value.SerializeBoard(false));
                        }
                    }


                    // Execute the IO queue
                    if (ioQueue.Count > 0)
                    {
                        string basePath = GetBasePath();
                        if (!Directory.Exists(basePath))
                        {
                            Directory.CreateDirectory(basePath);
                        }
                        foreach (KeyValuePair <string, string> ioRequest in ioQueue)
                        {
                            File.WriteAllText(Path.Combine(basePath, ioRequest.Key), ioRequest.Value);
                        }
                    }
                }
            }
        }