예제 #1
0
파일: LoadSave.cs 프로젝트: DJuttmann/SM3E
//========================================================================================
// Reading ROM data.


        // Read the project file in Xml format.
        private bool ReadProjectFileXml(
            out List <Tuple <int, int, string> > rooms,
            out List <Tuple <int, int, string> > doorAsms,
            out List <Tuple <int, int, string> > setupAsms,
            out List <Tuple <int, int, string> > mainAsms,
            out List <Tuple <int, string> > backgrounds)
        {
            RomFileName = null;
            rooms       = new List <Tuple <int, int, string> > ();
            doorAsms    = new List <Tuple <int, int, string> > ();
            setupAsms   = new List <Tuple <int, int, string> > ();
            mainAsms    = new List <Tuple <int, int, string> > ();
            backgrounds = new List <Tuple <int, string> > ();

            Stream stream;

            try { stream = new FileStream(ProjectPath + ProjectFileName, FileMode.Open,
                                          FileAccess.Read, FileShare.Read); }
            catch
            {
                throw new ProjectLoadException(ProjectLoadException.Type.ProjectFileNotAccessible,
                                               ProjectPath + ProjectFileName);
            }
            var      reader = XmlReader.Create(stream);
            XElement root   = XElement.Load(reader);

            stream.Close();

            RomFileName = root.Attribute("rom")?.Value;
            if (root.Name != "Project" || RomFileName == null)
            {
                throw new ProjectLoadException(ProjectLoadException.Type.RomFileNotSpecified,
                                               ProjectPath + ProjectFileName);
            }
            try
            {
                CurrentRom = new Rom(ProjectPath + RomFileName);
            }
            catch (FileNotFoundException)
            {
                throw new ProjectLoadException(ProjectLoadException.Type.RomFileNotFound,
                                               ProjectPath + RomFileName);
            }
            catch
            {
                throw new ProjectLoadException(ProjectLoadException.Type.RomFileNotAccessible,
                                               ProjectPath + RomFileName);
            }

            foreach (XElement x in root.Elements())
            {
                switch (x.Name.ToString())
                {
                case "Areas":
                    foreach (XElement area in x.Elements("Area"))
                    {
                        string areaIndex = area.Attribute("index")?.Value;
                        string areaName  = area.Attribute("name")?.Value;
                        if (areaIndex != null && areaName != null)
                        {
                            int i = Convert.ToInt32(areaIndex);
                            Areas [i] = areaName;
                        }
                    }
                    break;

                case "Sections":
                    foreach (XElement section in x.Elements("Section"))
                    {
                        string sectionName = section.Attribute("name")?.Value;
                        string sectionType = section.Attribute("type")?.Value;
                        if (sectionName == null || sectionType == null)
                        {
                            continue;
                        }
                        CurrentRom.AddSection(sectionName, RomSection.StringToType(sectionType));
                        foreach (XElement data in section.Elements("Data"))
                        {
                            string dataName = data.Attribute("name")?.Value;
                            if (dataName != null && DataLists.ContainsKey(dataName))
                            {
                                CurrentRom.AddDataList(sectionName, dataName, DataLists [dataName]);
                            }
                        }
                        foreach (XElement block in section.Elements("Block"))
                        {
                            string blockAddress = block.Attribute("address")?.Value;
                            string blockEnd     = block.Attribute("end")?.Value;
                            if (blockAddress != null && blockEnd != null)
                            {
                                CurrentRom.AddBlock(sectionName, Tools.HexToInt(blockAddress),
                                                    Tools.HexToInt(blockEnd) - Tools.HexToInt(blockAddress));
                            }
                        }
                    }
                    break;

                case "Rooms":
                    foreach (XElement room in x.Elements("Room"))
                    {
                        string roomAddress = room.Attribute("address")?.Value;
                        string doorCount   = room.Attribute("doorCount")?.Value;
                        string roomName    = room.Attribute("name")?.Value;
                        if (roomAddress != null)
                        {
                            int address = Tools.HexToInt(roomAddress);
                            int count   = doorCount != null?Tools.DecToInt(doorCount) : 0;

                            rooms.Add(new Tuple <int, int, string> (address, count,
                                                                    roomName ?? String.Empty));
                        }
                    }
                    break;

                case "Asms":
                    foreach (XElement asm in x.Elements())
                    {
                        List <Tuple <int, int, string> > asmList;
                        switch (asm.Name.ToString())
                        {
                        case "DoorAsm":
                            asmList = doorAsms;
                            break;

                        case "SetupAsm":
                            asmList = setupAsms;
                            break;

                        case "MainAsm":
                            asmList = mainAsms;
                            break;

                        default:
                            continue;
                        }
                        string asmName    = asm.Attribute("name")?.Value;
                        string asmAddress = asm.Attribute("address")?.Value;
                        string asmEnd     = asm.Attribute("end")?.Value;
                        if (asmAddress != null && asmEnd != null)
                        {
                            asmList.Add(new Tuple <int, int, string> (Tools.HexToInt(asmAddress),
                                                                      Tools.HexToInt(asmEnd),
                                                                      asmName ?? asmAddress));
                        }
                    }
                    break;

                case "Backgrounds":
                    foreach (XElement background in x.Elements("Background"))
                    {
                        string address = background.Attribute("address")?.Value;
                        string name    = background.Attribute("name")?.Value;
                        if (address != null)
                        {
                            backgrounds.Add(new Tuple <int, string> (Tools.HexToInt(address),
                                                                     name ?? address));
                        }
                    }
                    break;

                default:
                    break;
                }
            }

            return(true);
        }