Example #1
0
        internal void Save(string filePath)
        {
            UpdateRecordCount();
            string extension = string.Empty;
            //BinaryWriter bw;
            SnipStreamWrapper snipStreamWrapper = null;

            string tmpFile = filePath + ".new";

            //if (File.Exists(filePath))
            //{
            //    //bw = new BinaryWriter(File.OpenWrite(filePath + ".new"));
            //    //extension = ".new";
            //}
            //else
            //{
            //    //bw = new BinaryWriter(File.OpenWrite(filePath));
            //    fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
            //}

            //fs = new FileStream(filePath + extension, FileMode.Open, FileAccess.Write);
            //bw = new BinaryWriter(File.OpenWrite(filePath + extension));

            //bw = new BinaryWriter(File.OpenWrite(tmpFile));
            FileStream fs = new FileStream(tmpFile, FileMode.Create, FileAccess.Write, FileShare.None);

            try
            {
                ZLibWrapper.AllocateBuffers();
                snipStreamWrapper = new SnipStreamWrapper(fs);
                snipStreamWrapper.AllocateBuffers();

                this.SaveData(snipStreamWrapper);
                Name = Path.GetFileName(filePath);
                PluginPath = Path.GetDirectoryName(filePath);
            }
            finally
            {
                //bw.Close();                
                snipStreamWrapper.CloseAndDisposeFileStream();
                snipStreamWrapper.ReleaseBuffers();
                snipStreamWrapper = null;
                fs = null;
            }

            try
            {
                // ** Create Backup
                bool backupExists = true;
                int backupVersion = 0;
                string backupFolder = CreateBackupFolder(filePath);
                while (backupExists && backupVersion < 999)
                {
                    backupExists =
                        File.Exists(Path.Combine(backupFolder, Name) + string.Format(".{0,3:D3}.bak", backupVersion));
                    if (backupExists)
                    {
                        backupVersion++;
                    }
                }
                string backupFile = Path.Combine(backupFolder, Name) + string.Format(".{0,3:D3}.bak", backupVersion);
                File.Copy(tmpFile, backupFile, true);

                //if (existed)
                //{
                    ////string newFile = filePath;
                    //string backupFile = Path.Combine(backupFolder, Name) + string.Format(".{0,3:D3}.bak", backupVersion);
                    //File.Copy(tmpFile, backupFile, true);
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                    File.Move(tmpFile, filePath);
                //}
        
            }
            catch (Exception ex)
            {
                string msg = string.Format(ex.Message);
                MessageBox.Show(
                    msg,
                    TranslateUI.TranslateUiGlobalization.ResManager.GetString("Application_Title"),
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                Clipboard.SetText(msg);
            }

            var tes4 = this.Records.OfType<Record>().FirstOrDefault(x => x.Name == "TES4");
            if (tes4 != null && (tes4.Flags1 & 0x80) != 0)
            {
                if (Properties.Settings.Default.SaveStringsFiles)
                {
                    string prefix = Path.Combine(Path.Combine(Path.GetDirectoryName(filePath), "Strings"), Path.GetFileNameWithoutExtension(filePath));
                    prefix += "_" + Properties.Settings.Default.LocalizationName;
                    this.SaveStrings(prefix);
                }
            }

            StringsDirty = false;
        }