void SaveAsync(string newSaveName, string sessionPath, MyWorldInfo copyFrom)
            {
                // Try a simple path, then a random if it already exists
                var newSessionPath = MyLocalCache.GetSessionSavesPath(newSaveName, false, false);

                while (Directory.Exists(newSessionPath))
                {
                    newSessionPath = MyLocalCache.GetSessionSavesPath(newSaveName + MyUtils.GetRandomInt(int.MaxValue).ToString("########"), false, false);
                }
                Directory.CreateDirectory(newSessionPath);
                MyUtils.CopyDirectory(sessionPath, newSessionPath);
                ulong sizeInBytes;
                var   checkpoint = MyLocalCache.LoadCheckpoint(newSessionPath, out sizeInBytes);

                Debug.Assert(checkpoint != null);
                checkpoint.SessionName = copyFrom.SessionName;
                checkpoint.WorkshopId  = null;
                MyLocalCache.SaveCheckpoint(checkpoint, newSessionPath);
                MyLocalCache.SaveLastLoadedTime(newSessionPath, DateTime.Now);
            }
示例#2
0
        public bool Save()
        {
            bool success = true;

            using (m_savingLock.AcquireExclusiveUsing())
            {
                MySandboxGame.Log.WriteLine("Session snapshot save - START");
                using (var indent = MySandboxGame.Log.IndentUsing(LoggingOptions.NONE))
                {
                    Debug.Assert(!string.IsNullOrWhiteSpace(TargetDir), "TargetDir should always be correctly set!");

                    Directory.CreateDirectory(TargetDir);

                    MySandboxGame.Log.WriteLine("Checking file access for files in target dir.");
                    if (!CheckAccessToFiles())
                    {
                        return(false);
                    }

                    var saveAbsPath = SavingDir;
                    if (Directory.Exists(saveAbsPath))
                    {
                        Directory.Delete(saveAbsPath, true);
                    }
                    Directory.CreateDirectory(saveAbsPath);

                    try
                    {
                        ulong sectorSizeInBytes     = 0;
                        ulong checkpointSizeInBytes = 0;
                        ulong voxelSizeInBytes      = 0;
                        success = MyLocalCache.SaveSector(SectorSnapshot, SavingDir, Vector3I.Zero, out sectorSizeInBytes) &&
                                  MyLocalCache.SaveCheckpoint(CheckpointSnapshot, SavingDir, out checkpointSizeInBytes) &&
                                  MyLocalCache.SaveLastLoadedTime(TargetDir, DateTime.Now);
                        if (success)
                        {
                            foreach (var entry in CompressedVoxelSnapshots)
                            {
                                voxelSizeInBytes += (ulong)entry.Value.Length;
                                success           = success && SaveVoxelSnapshot(entry.Key, entry.Value);
                            }
                        }
                        if (success && Sync.IsServer)
                        {
                            success = MyLocalCache.SaveLastSessionInfo(TargetDir);
                        }

                        if (success)
                        {
                            SavedSizeInBytes = sectorSizeInBytes + checkpointSizeInBytes + voxelSizeInBytes;
                        }
                    }
                    catch (Exception ex)
                    {
                        MySandboxGame.Log.WriteLine("There was an error while saving snapshot.");
                        MySandboxGame.Log.WriteLine(ex);
                        ReportFileError(ex);
                        success = false;
                    }

                    if (success)
                    {
                        HashSet <string> saveFiles = new HashSet <string>();
                        foreach (var filepath in Directory.GetFiles(saveAbsPath))
                        {
                            string filename = Path.GetFileName(filepath);

                            var targetFile = Path.Combine(TargetDir, filename);
                            if (File.Exists(targetFile))
                            {
                                File.Delete(targetFile);
                            }

                            File.Move(filepath, targetFile);
                            saveFiles.Add(filename);
                        }

                        // Clean leftovers from previous saves
                        foreach (var filepath in Directory.GetFiles(TargetDir))
                        {
                            string filename = Path.GetFileName(filepath);
                            if (saveFiles.Contains(filename) || filename == MyTextConstants.SESSION_THUMB_NAME_AND_EXTENSION)
                            {
                                continue;
                            }

                            File.Delete(filepath);
                        }

                        Directory.Delete(saveAbsPath);

                        //Backup Saves Functionality
                        Backup();
                    }
                    else
                    {
                        // We don't delete previous save, just the new one.
                        if (Directory.Exists(saveAbsPath))
                        {
                            Directory.Delete(saveAbsPath, true);
                        }
                    }
                }
                MySandboxGame.Log.WriteLine("Session snapshot save - END");
            }
            return(success);
        }