private void renameToolStripMenuItem_Click(object sender, EventArgs e) { var Node = GetSelectedFile(); if (Node != null) { FileStream FS = null; SaveFile F = null; try { FS = File.OpenRead(GetName(Node)); } catch (Exception ex) { Log.Write(new Exception("Unable to rename the file", ex)); Tools.E($"Unable to rename the file.\r\n{ex.Message}", "File rename"); return; } using (FS) { F = SaveFile.Open(FS); } if (F != null) { using (var Ren = new frmRename(F.SessionName, Node.Text)) { if (Ren.ShowDialog() == DialogResult.OK) { var NewName = Path.Combine(Path.GetDirectoryName(GetName(Node)), Ren.RenameFileName + ".sav"); //Show rename dialog if the file itself is renamed and the destination exists already if ( Node.Text == Ren.RenameFileName || !File.Exists(NewName) || MessageBox.Show($"There is already a file named {Ren.RenameFileName}.sav. Overwrite this file?", "Confirm overwrite", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { F.SessionName = Ren.RenameSessionName; try { FS = File.Create(NewName); } catch (Exception ex) { Log.Write(new Exception("Unable to rename the file", ex)); FS = null; Tools.E($"Unable to rename the file.\r\n{ex.Message}", "File rename"); } if (FS != null) { using (FS) { F.Export(FS); try { File.Delete(GetName(Node)); } catch (Exception ex) { Log.Write(new Exception("Unable to delete the old copy", ex)); Tools.E($"File renamed, but we are unable to delete the old copy. Please do so manually.\r\n{ex.Message}", "Unable to rename"); } InitFiles(); } } } } } } else { InvalidMessage(); } } }
private void btnImport_Click(object sender, EventArgs e) { if (OFD.ShowDialog() == DialogResult.OK) { FileStream FS; try { FS = File.OpenRead(OFD.FileName); } catch (Exception ex) { Log.Write(new Exception($"Unable to import {OFD.FileName}", ex)); Tools.E($"Unable to open the selected file.\r\n{ex.Message}", "Import error"); return; } using (FS) { var NewName = Path.GetFileName(OFD.FileName); if (NewName.ToLower().Contains(".sav")) { //Make .sav the last part of the name NewName = NewName.Substring(0, NewName.ToLower().LastIndexOf(".sav")) + ".sav"; if (NewName == ".sav") { NewName = "Import.sav"; } } else { //Add .sav extension because it's not there NewName += ".sav"; } //Make name unique int i = 1; while (File.Exists(Path.Combine(Program.SaveDirectory, NewName))) { NewName = NewName.Substring(0, NewName.Length - 4) + $"_{i++}.sav"; } //Check if selected file is actually a (somewhat) valid save game var F = SaveFile.Open(FS); if (F == null) { NewName = Path.Combine(Program.SaveDirectory, NewName); if (MessageBox.Show("This file doesn't looks like it's a save file. Import anyways?", "Incompatible file", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { //User decided to copy anyway. Decompress the file now as needed FS.Seek(0, SeekOrigin.Begin); if (Tools.IsGzFile(FS)) { Log.Write("{0}: looks compressed: {1}", GetType().Name, OFD.FileName); try { using (var GZS = new GZipStream(FS, CompressionMode.Decompress)) { //Try to manually decompress a few bytes before fully copying. //This will throw an exception for files that are not decompressable before the output file is created byte[] Data = new byte[100]; int R = GZS.Read(Data, 0, Data.Length); using (var OUT = File.Create(NewName)) { OUT.Write(Data, 0, R); GZS.CopyTo(OUT); } } InitFiles(); FeatureReport.Used(FeatureReport.Feature.RestoreBackup); } catch (Exception ex) { Log.Write(new Exception($"Unable to decompress {OFD.FileName}", ex)); Tools.E($"File looks compressed, but we are unable to decompress it.\r\n{ex.Message}", "Decompression error"); } } else { Log.Write("{0}: Importing uncompressed {1}", GetType().Name, OFD.FileName); //File is not compressed. Just copy as-is using (var OUT = File.Create(NewName)) { FS.CopyTo(OUT); InitFiles(); } } } } else { //Supply the NewName path to have a name that's not a conflict by default using (var Ren = new frmRename(F.SessionName, Path.GetFileNameWithoutExtension(NewName))) { if (Ren.ShowDialog() == DialogResult.OK) { NewName = Path.Combine(Program.SaveDirectory, Ren.RenameFileName + ".sav"); if (!File.Exists(NewName) || MessageBox.Show($"There is already a file named {Ren.RenameFileName}.sav. Overwrite this file?", "Confirm overwrite", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { F.SessionName = Ren.RenameSessionName; using (var OUT = File.Create(NewName)) { F.Export(OUT); } InitFiles(); } else { Log.Write("{0}: import destination exists: User cancelled", GetType().Name); } } } } } } }