Exemple #1
0
        // Initializes for an existing map
        internal bool SaveMap(string newfilepathname, SavePurpose purpose)
        {
            General.WriteLogLine("Saving map to " + newfilepathname);

            // Suspend data resources
            data.Suspend();

            try
            {
                // Backup existing file, if any
                if (File.Exists(newfilepathname))
                {
                    if (File.Exists(newfilepathname + ".backup3"))
                    {
                        File.Delete(newfilepathname + ".backup3");
                    }
                    if (File.Exists(newfilepathname + ".backup2"))
                    {
                        File.Move(newfilepathname + ".backup2", newfilepathname + ".backup3");
                    }
                    if (File.Exists(newfilepathname + ".backup1"))
                    {
                        File.Move(newfilepathname + ".backup1", newfilepathname + ".backup2");
                    }
                    File.Copy(newfilepathname, newfilepathname + ".backup1");
                }

                // Kill the target file if it is different from source file
                if (newfilepathname != filepathname)
                {
                    // Kill target file
                    if (File.Exists(newfilepathname))
                    {
                        File.Delete(newfilepathname);
                    }

                    // Kill .dbs settings file
                    string settingsfile = newfilepathname.Substring(0, newfilepathname.Length - 4) + ".dbs";
                    if (File.Exists(settingsfile))
                    {
                        File.Delete(settingsfile);
                    }
                }

                //mxd. Save map
                using (FileStream stream = File.OpenWrite(newfilepathname))
                {
                    io.Write(map, stream);
                }
            }
            catch (IOException)
            {
                General.ShowErrorMessage("IO Error while writing target file: " + newfilepathname + ". Please make sure the location is accessible and not in use by another program.", MessageBoxButtons.OK);
                data.Resume();
                General.WriteLogLine("Map saving failed");
                return(false);
            }
            catch (UnauthorizedAccessException)
            {
                General.ShowErrorMessage("Error while accessing target file: " + newfilepathname + ". Please make sure the location is accessible and not in use by another program.", MessageBoxButtons.OK);
                data.Resume();
                General.WriteLogLine("Map saving failed");
                return(false);
            }

            // Resume data resources
            data.Resume();

            // Not saved for testing purpose?
            if (purpose != SavePurpose.Testing)
            {
                // Saved in a different file?
                if (newfilepathname != filepathname)
                {
                    // Keep new filename
                    filepathname = newfilepathname;
                    filename     = Path.GetFileName(filepathname);
                    filepath     = Path.GetDirectoryName(filepathname);

                    // Reload resources
                    ReloadResources();
                }

                try
                {
                    // Open or create the map settings
                    string settingsfile = newfilepathname.Substring(0, newfilepathname.Length - 4) + ".dbs";
                    options.WriteConfiguration(settingsfile);
                }
                catch (Exception e)
                {
                    // Warning only
                    General.ErrorLogger.Add(ErrorType.Warning, "Could not write the map settings configuration file. " + e.GetType().Name + ": " + e.Message);
                }

                // Changes saved
                changed = false;
            }

            // Success!
            General.WriteLogLine("Map saving done");
            General.MainWindow.UpdateTitle();             //mxd
            return(true);
        }