public void ClearElements()
 {
     Karts.Clear();
     Array.Clear(SelectedKarts, 0, SelectedKarts.Length);
     TurnKartPaletteBlocks.Clear();
     SpinKartPaletteBlocks.Clear();
     //Tracks.Clear();
     //Array.Clear(SelectedTracks, 0, SelectedTracks.Length);
     KartGraphicsBlock = null;
     TextureHub.ClearTextureData();
 }
        public void LoadFromXML()
        {
            //TextBank/TextReferences - Only use the offset currently being used
            //KartReference - Only use the names of the karts selected
            //MIOBlocks/TKMK00Bocks - Offsets for each one
            //Karts - Full listing of information

            //Elements should already have been cleared
            //ClearElements();

            NewElementOffset = int.Parse(_loadedXml.Attribute(NEW_ELEMENT_OFFSET).Value);

            //Before we start, load up all saved karts and tracks
            ProgressService.SetMessage("Loading Kart Resources");
            foreach (RomItem item in RomProject.Instance.Items)
            {
                //If the same name kart hasn't been loaded yet
                if (item is KartInfo && Karts.FirstOrDefault(k => k.KartName == ((KartInfo)item).KartName) == null)
                {
                    this.Karts.Add((KartInfo)item);
                }
                //else if is trackinfo
            }

            //Also the text bank is all elements, so we don't need an xml in here for it
            ProgressService.SetMessage("Loading Text Blocks");
            N64DataElement textRefEl, textBlockEl;

            if (RomProject.Instance.Files[0].HasElementExactlyAt(TextReferenceBlock.TEXT_REFERENCE_SECTION_1, out textRefEl) &&
                RomProject.Instance.Files[0].HasElementExactlyAt(TextBankBlock.TEXT_BLOCK_START, out textBlockEl))
            {
                TextReferenceBlock refBlock  = (TextReferenceBlock)textRefEl;
                TextBankBlock      bankBlock = (TextBankBlock)textBlockEl;

                TextBank = new TextBank(bankBlock, refBlock, true);
            }

            int            offset;
            int            count     = 0;
            int            fullCount = _loadedXml.Elements().Count();
            N64DataElement n64Element;

            foreach (XElement element in _loadedXml.Elements())
            {
                ProgressService.SetMessage(string.Format("Storing Special Elements {0:0.0}%", (double)count / fullCount));
                switch (element.Name.ToString())
                {
                case TURN_PALETTE_BLOCK:
                    foreach (XElement el in element.Elements())
                    {
                        offset = int.Parse(el.Value);
                        if (RomProject.Instance.Files[0].HasElementExactlyAt(offset, out n64Element))
                        {
                            int            paletteOffset = offset;
                            List <Palette> palettes      = new List <Palette>();
                            palettes.Add((Palette)n64Element);
                            paletteOffset += 0x40 * 2;

                            for (int i = 1; i < 84; i++)     //Make not hardcoded later
                            {
                                if (!RomProject.Instance.Files[0].HasElementAt(paletteOffset, out n64Element))
                                {
                                    throw new Exception();
                                }

                                palettes.Add((Palette)n64Element);
                                paletteOffset += 0x40 * 2;
                            }

                            KartPaletteBlock block = new KartPaletteBlock(offset, palettes);
                            TurnKartPaletteBlocks.Add(block);
                        }
                    }
                    break;

                case SPIN_PALETTE_BLOCK:
                    foreach (XElement el in element.Elements())
                    {
                        offset = int.Parse(el.Value);
                        if (RomProject.Instance.Files[0].HasElementExactlyAt(offset, out n64Element))
                        {
                            int            paletteOffset = offset;
                            List <Palette> palettes      = new List <Palette>();
                            palettes.Add((Palette)n64Element);
                            paletteOffset += 0x40 * 2;

                            for (int i = 1; i < 80; i++)     //Make not hardcoded later
                            {
                                if (!RomProject.Instance.Files[0].HasElementAt(paletteOffset, out n64Element))
                                {
                                    throw new Exception();
                                }

                                palettes.Add((Palette)n64Element);
                                paletteOffset += 0x40 * 2;
                            }

                            KartPaletteBlock block = new KartPaletteBlock(offset, palettes);
                            SpinKartPaletteBlocks.Add(block);
                        }
                    }
                    break;

                case KARTS_GRAPHICS_REFERENCE_BLOCK:
                    offset = int.Parse(element.Value);
                    if (RomProject.Instance.Files[0].HasElementExactlyAt(offset, out n64Element))
                    {
                        if (n64Element is KartGraphicsReferenceBlock)
                        {
                            KartGraphicsBlock = (KartGraphicsReferenceBlock)n64Element;
                            //KartReader.LoadKartGraphicDmaReferences(KartGraphicsBlock);
                        }
                    }
                    break;

                case KARTS_PORTRAITS_REFERENCE_TABLE:
                    offset = int.Parse(element.Value);
                    if (RomProject.Instance.Files[0].HasElementExactlyAt(offset, out n64Element))
                    {
                        if (n64Element is KartPortraitTable)
                        {
                            KartPortraitsTable = (KartPortraitTable)n64Element;
                            //KartReader.LoadKartPortraitDmaReferences(KartPortraitsTable);
                        }
                    }
                    break;

                case SELECTED_KARTS:
                    int kartIndex = 0;
                    foreach (XElement selKart in element.Elements())
                    {
                        KartInfo selectedKart = Karts.SingleOrDefault(k => k.KartName == selKart.Name);
                        if (selectedKart != null)
                        {
                            SelectedKarts[kartIndex] = selectedKart;
                        }
                        kartIndex++;
                    }
                    break;

                case TextureHub.TEXTURE_HUB:
                    TextureHub.LoadReferencesFromXML(element);
                    break;
                }
                count++;
            }
        }