コード例 #1
0
ファイル: Project.cs プロジェクト: matthiaswatkins/trizbort
        public bool Load()
        {
            try
            {
                if (new FileInfo(FileName).Length == 0)
                {
                    // this is an empty file, probably thanks to our Explorer New->Trizbort Map menu option.
                    Settings.Reset();
                    return(true);
                }

                var doc = new XmlDocument();
                doc.Load(FileName);
                var root = new XmlElementReader(doc.DocumentElement);

                if (!root.HasName("trizbort"))
                {
                    throw new InvalidDataException(string.Format("Not a {0} map file.", Application.ProductName));
                }

                // file version
                var versionNumber = root.Attribute("version").Text;
                setVersion(versionNumber);

                // load info
                Title       = root["info"]["title"].Text;
                Author      = root["info"]["author"].Text;
                Description = root["info"]["description"].Text;
                History     = root["info"]["history"].Text;

                // load all elements
                var map = root["map"];
                var mapConnectionToLoadState = new Dictionary <Connection, object>();
                foreach (var element in map.Children)
                {
                    if (element.HasName("room"))
                    {
                        // Changed the constructor used for elements when loading a file for a significant speed increase
                        var room = new Room(this, Elements.Count + 1);
                        room.ID = element.Attribute("id").ToInt(room.ID);
                        room.Load(element);
                        Elements.Add(room);
                    }
                    else if (element.HasName("line"))
                    {
                        // Changed the constructor used for elements when loading a file for a significant speed increase
                        var connection = new Connection(this, Elements.Count + 1);
                        connection.ID = element.Attribute("id").ToInt(connection.ID);
                        var loadState = connection.BeginLoad(element);
                        if (loadState != null)
                        {
                            mapConnectionToLoadState.Add(connection, loadState);
                        }
                        Elements.Add(connection);
                    }
                }

                // connect them together
                foreach (var pair in mapConnectionToLoadState)
                {
                    var connection = pair.Key;
                    var state      = pair.Value;
                    connection.EndLoad(state);
                }

                // load settings last, since their load can't be undone
                Settings.Reset();
                Settings.Load(root["settings"]);
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(Program.MainForm, string.Format("There was a problem loading the map:\n\n{0}", ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
        }
コード例 #2
0
ファイル: Project.cs プロジェクト: ChrisPC/trizbort
        public bool Load()
        {
            try
            {
                if (new FileInfo(FileName).Length == 0)
                {
                    // this is an empty file, probably thanks to our Explorer New->Trizbort Map menu option.
                    Settings.Reset();
                    return true;
                }

                var doc = new XmlDocument();
                doc.Load(FileName);
                var root = new XmlElementReader(doc.DocumentElement);

                if (!root.HasName("trizbort"))
                    throw new InvalidDataException(string.Format("Not a {0} map file.", Application.ProductName));

                // load info
                Title = root["info"]["title"].Text;
                Author = root["info"]["author"].Text;
                Description = root["info"]["description"].Text;
                History = root["info"]["history"].Text;

                // load all elements
                var map = root["map"];
                var mapConnectionToLoadState = new Dictionary<Connection, object>();
                foreach (var element in map.Children)
                {
                    if (element.HasName("room"))
                    {
                        var room = new Room(this);
                        room.ID = element.Attribute("id").ToInt(room.ID);
                        room.Load(element);
                        Elements.Add(room);
                    }
                    else if (element.HasName("line"))
                    {
                        var connection = new Connection(this);
                        connection.ID = element.Attribute("id").ToInt(connection.ID);
                        var loadState = connection.BeginLoad(element);
                        if (loadState != null)
                        {
                            mapConnectionToLoadState.Add(connection, loadState);
                        }
                        Elements.Add(connection);
                    }
                }

                // connect them together
                foreach (var pair in mapConnectionToLoadState)
                {
                    var connection = pair.Key;
                    var state = pair.Value;
                    connection.EndLoad(state);
                }

                // load settings last, since their load can't be undone
                Settings.Reset();
                Settings.Load(root["settings"]);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Program.MainForm, string.Format("There was a problem loading the map:\n\n{0}", ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }