コード例 #1
0
        // Initializes for an existing map
        internal bool InitializeOpenMap(string filepathname, MapOptions options)
        {
            // Apply settings
            this.filename     = Path.GetFileName(filepathname);
            this.filepathname = filepathname;
            this.filepath     = Path.GetDirectoryName(filepathname);
            this.changed      = false;
            this.options      = options;

            General.WriteLogLine("Opening map \"" + this.filename + "\" with configuration \"" + options.ConfigFile + "\"");

            // Initiate graphics
            General.WriteLogLine("Initializing graphics device...");
            graphics = new D3DDevice(General.MainWindow.Display);
            if (!graphics.Initialize())
            {
                return(false);
            }

            // Create renderers
            renderer2d = new Renderer2D(graphics);
            renderer3d = new Renderer3D(graphics);

            // Load game configuration
            General.WriteLogLine("Loading game configuration...");
            configinfo = General.GetConfigurationInfo(options.ConfigFile);
            config     = new GameConfiguration(General.LoadGameConfiguration(options.ConfigFile));
            configinfo.ApplyDefaults(config);
            General.Editing.UpdateCurrentEditModes();

            // Create map data
            bool maprestored = false;             //mxd

            map = new MapSet();

            string mapname = Path.GetFileNameWithoutExtension(filepathname);

            if (!string.IsNullOrEmpty(mapname))
            {
                string hash       = MurmurHash2.Hash(mapname + File.GetLastWriteTime(filepathname)).ToString();
                string backuppath = Path.Combine(General.MapRestorePath, mapname + "." + hash + ".restore");

                // Backup exists and it's newer than the map itself?
                if (File.Exists(backuppath) && File.GetLastWriteTime(backuppath) > File.GetLastWriteTime(filepathname))
                {
                    if (General.ShowWarningMessage("Looks like your previous editing session has gone terribly wrong." + Environment.NewLine
                                                   + "Would you like to restore the map from the backup?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        General.WriteLogLine("Initializing map format interface " + config.FormatInterface + "...");
                        io = MapSetIO.Create(config.FormatInterface, this);
                        General.WriteLogLine("Restoring map from \"" + backuppath + "\"...");

#if DEBUG
                        // Restore map
                        map.Deserialize(new MemoryStream(File.ReadAllBytes(backuppath)));
#else
                        try
                        {
                            // Restore map
                            map.Deserialize(new MemoryStream(File.ReadAllBytes(backuppath)));

                            // Delete the backup
                            File.Delete(backuppath);
                        }
                        catch (Exception e)
                        {
                            General.ErrorLogger.Add(ErrorType.Error, "Unable to restore the map data structures from the backup. " + e.GetType().Name + ": " + e.Message);
                            General.ShowErrorMessage("Unable to restore the map data structures from the backup.", MessageBoxButtons.OK);
                            return(false);
                        }
#endif
                        maprestored = true;
                    }
                }
            }

            // Read the map from file
            if (!maprestored)
            {
                map.BeginAddRemove();

                General.WriteLogLine("Initializing map format interface " + config.FormatInterface + "...");
                io = MapSetIO.Create(config.FormatInterface, this);
                General.WriteLogLine("Reading map data structures from file...");

#if !DEBUG
                try
                {
#endif
                using (FileStream stream = File.OpenRead(filepathname))
                {
                    io.Read(map, stream);
                }
#if !DEBUG
            }
            catch (Exception e)
            {
                General.ErrorLogger.Add(ErrorType.Error, "Unable to read the map data structures with the specified configuration. " + e.GetType().Name + ": " + e.Message);
                General.ShowErrorMessage("Unable to read the map data structures with the specified configuration.", MessageBoxButtons.OK);
                return(false);
            }
#endif

                map.EndAddRemove();
            }


            // Load data manager
            General.WriteLogLine("Loading data resources...");
            data = new DataManager();
            data.Load(CreateResourcesList());

            // Remove unused sectors
            map.RemoveUnusedSectors(true);

            // Update structures
            options.ApplyGridSettings();
            map.UpdateConfiguration();
            //map.SnapAllToAccuracy();
            map.Update();
            thingsfilter.Update();

            // Bind any methods
            General.Actions.BindMethods(this);

            // Set defaults
            this.visualcamera = new VisualCamera();
            General.Editing.ChangeMode(configinfo.StartMode);
            renderer2d.SetViewMode((ViewMode)General.Settings.DefaultViewMode);

            // Center map in screen
            if (General.Editing.Mode is ClassicMode)
            {
                (General.Editing.Mode as ClassicMode).CenterInScreen();
            }

            // Success
            this.changed = maprestored;             //mxd
            General.WriteLogLine("Map loading done");
            General.MainWindow.UpdateTitle();       //mxd
            return(true);
        }