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); } } } } } } }