示例#1
0
 public void Reset()
 {
     _instance = new RomProject();
 }
示例#2
0
        public static void Load(string filePath)
        {
            //XmlDocument doc = new XmlDocument();
            XElement romProjectNode = null;
            XElement romDirectoryNode = null;
            Dictionary<int, byte[]> romData = new Dictionary<int, byte[]>();
            List<Tuple<string, XElement>> romItems = new List<Tuple<string, XElement>>();
            using (ZipFile zip = ZipFile.Read(filePath))
            {
                foreach (ZipEntry e in zip)
                {
                    if (e.FileName == "ProjectInfo")
                    {
                        MemoryStream projectStream = new MemoryStream();
                        e.Extract(projectStream);
                        romProjectNode = XElement.Parse(Encoding.ASCII.GetString(projectStream.ToArray()));
                    }
                    else if (e.FileName == "Directory")
                    {
                        MemoryStream projectStream = new MemoryStream();
                        e.Extract(projectStream);
                        romDirectoryNode = XElement.Parse(Encoding.ASCII.GetString(projectStream.ToArray()));
                    }
                    //else if (Regex.IsMatch(e.FileName, @"^\d+$"))//Numbers only, rom file
                    //{
                    //    MemoryStream projectStream = new MemoryStream();
                    //    e.Extract(projectStream);
                    //    byte[] data = projectStream.ToArray();
                    //    //projectStream.Read(data, 0, (int)projectStream.Length);
                    //    romData.Add(int.Parse(e.FileName), data);
                    //}
                    //else //Rom item
                    //{
                    //    MemoryStream projectStream = new MemoryStream();
                    //    e.Extract(projectStream);
                    //    XElement itemElement = XElement.Parse(Encoding.ASCII.GetString(projectStream.ToArray()));
                    //    romItems.Add(e.FileName, itemElement);
                    //}
                }

                //Load up the directory here
                foreach(XElement element in romDirectoryNode.Elements())
                {
                    if(element.Name == "File")
                    {
                        string elementPath = element.Attribute("Path").Value;
                        int elementNum = int.Parse(element.Attribute("ID").Value);

                        if(zip.ContainsEntry(elementPath))
                        {
                            ZipEntry e = zip.Entries.First(en => en.FileName == elementPath);

                            MemoryStream projectStream = new MemoryStream();
                            e.Extract(projectStream);
                            byte[] data = projectStream.ToArray();

                            romData.Add(elementNum, data);
                        }
                    }
                    else if (element.Name == "Item")
                    {
                        string elementPath = element.Attribute("Path").Value;
                        string elementName = element.Attribute("Name").Value;

                        if (zip.ContainsEntry(elementPath))
                        {
                            ZipEntry e = zip.Entries.First(en => en.FileName == elementPath);

                            MemoryStream projectStream = new MemoryStream();
                            e.Extract(projectStream);
                            XElement itemElement = XElement.Parse(Encoding.ASCII.GetString(projectStream.ToArray()));

                            romItems.Add(new Tuple<string, XElement>(elementName, itemElement));
                        }
                    }
                }

            }

            if (romProjectNode == null)
                return;

            //Start up the monster here boyo
            _instance = new RomProject();
            _instance.ProjectName = romProjectNode.Attribute(PROJECTNAME).Value;
            _instance._dmaProfileIndex = int.Parse(romProjectNode.Attribute(SELECTEDDMA).Value);
            _instance.ProjectPath = Path.GetDirectoryName(filePath);

            foreach (XElement element in romProjectNode.Elements())
            {
                if (element.Name == UserDefinedRomInfo.USERDEFINEDINFO)
                {
                    //Handle loading the user info
                    _instance.RomInfo = new UserDefinedRomInfo(element);
                }
                else if (element.Name == ROMFILES)
                {
                    foreach (XElement fileElement in element.Elements())
                    {
                        //Load a file
                        //Get the raw data
                        XAttribute attribute = fileElement.Attribute(RomFile.FILEID);
                        if (attribute == null)
                            continue;

                        int fileNum;
                        if (!int.TryParse(attribute.Value, out fileNum))
                            continue;

                        //Pass into the file using XElement
                        _instance.AddRomFile(new RomFile(fileElement, romData[fileNum]));
                    }
                }
                else if (element.Name == DMAPROFILES)
                {
                    foreach (XElement profileElement in element.Elements())
                    {
                        //Load the profiles
                        _instance.AddDmaProfile(new DmaProfile(profileElement));
                    }
                }
            }

            //Finish up the rom items here
            foreach(Tuple<string, XElement> itemPair in romItems)
            {
                _instance._items.Add(RomItemFactory.CreateRomItemFromType(itemPair.Item1, itemPair.Item2));
            }

            //Here, do stuff
            foreach (RomFile file in _instance.Files)
            {
                foreach (DataElements.N64DataElement element in file.Elements)
                {
                    element.PostXMLLoad();
                }
            }
        }