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++; } }
private static void ReadRomPayload(object sender, DoWorkEventArgs args) { BackgroundWorker worker = sender as BackgroundWorker; MarioKart64ReaderResults results = new MarioKart64ReaderResults(); //The rom should be loaded as the first file in the rom project byte[] rawData = RomProject.Instance.Files[0].GetAsBytes(); //NOTE: THIS IS HOW YOU DO A CANCELLATION //if ((worker.CancellationPending == true)) //{ // args.Cancel = true; // return; //} /*ProgressService.SetMessage("Reading TKMK00 Textures"); * * N64DataElement preExistingElement; * * //Now read the different data bits here, if they haven't been read in yet * for (int i = 0; i < MarioKartRomInfo.TKMK00TextureLocations.Length; i++) * { * preExistingElement = RomProject.Instance.Files[0].GetElementAt(MarioKartRomInfo.TKMK00TextureLocations[i].RomOffset); * if (preExistingElement != null && preExistingElement.GetType() == typeof(UnknownData)) * { * ushort alpha = MarioKartRomInfo.TKMK00TextureLocations[i].AlphaColor; * int offset = MarioKartRomInfo.TKMK00TextureLocations[i].RomOffset; * int length = MarioKartRomInfo.TKMK00TextureLocations[i].Length; * * TKMK00Block tkmk; * * byte[] bytes = new byte[length]; * Array.Copy(rawData, offset, bytes, 0, length); * * tkmk = new TKMK00Block(offset, bytes, alpha); * * if (MarioKart64ElementHub.Instance.OriginalTKMK00Blocks.SingleOrDefault(t => t.FileOffset == tkmk.FileOffset) == null) * { * results.NewElements.Add(tkmk); * results.OriginalTKMK00Blocks.Add(tkmk); * } * } * else if (preExistingElement is TKMK00Block && * MarioKart64ElementHub.Instance.OriginalTKMK00Blocks.SingleOrDefault(t => t.FileOffset == preExistingElement.FileOffset) == null) * { * results.OriginalTKMK00Blocks.Add((TKMK00Block)preExistingElement); * } * } */ //Text bank! TextBankBlock textBankBlock = null; TextReferenceBlock textReferenceBlock = null; bool previouslyLoadedText = false; //To do: add a function to automate this pre-existing check, like // bool hasExistingElement<T:N64DataElement> (offset, out T) N64DataElement preExistingElement; if (RomProject.Instance.Files[0].HasElementAt(TextBankBlock.TEXT_BLOCK_START, out preExistingElement) && preExistingElement.GetType() == typeof(UnknownData)) { byte[] bytes = new byte[TextBankBlock.TEXT_BLOCK_LENGTH]; Array.Copy(rawData, TextBankBlock.TEXT_BLOCK_START, bytes, 0, bytes.Length); textBankBlock = new TextBankBlock(TextBankBlock.TEXT_BLOCK_START, bytes); results.NewElements.Add(textBankBlock); } else if (preExistingElement.GetType() == typeof(TextBankBlock)) { previouslyLoadedText = true; textBankBlock = (TextBankBlock)preExistingElement; } if (RomProject.Instance.Files[0].HasElementAt(TextReferenceBlock.TEXT_REFERENCE_SECTION_1, out preExistingElement) && preExistingElement.GetType() == typeof(UnknownData)) { byte[] bytes = new byte[TextReferenceBlock.TEXT_REFERENCE_END - TextReferenceBlock.TEXT_REFERENCE_SECTION_1]; Array.Copy(rawData, TextReferenceBlock.TEXT_REFERENCE_SECTION_1, bytes, 0, bytes.Length); textReferenceBlock = new TextReferenceBlock(TextReferenceBlock.TEXT_REFERENCE_SECTION_1, bytes); results.NewElements.Add(textReferenceBlock); } else if (preExistingElement.GetType() == typeof(TextReferenceBlock)) { previouslyLoadedText = true; textReferenceBlock = (TextReferenceBlock)preExistingElement; } if (textBankBlock != null && textReferenceBlock != null) { TextBank textBank = new TextBank(textBankBlock, textReferenceBlock, !previouslyLoadedText); results.TextBank = textBank; } TextureReader.ReadRom(worker, rawData, results); KartReader.ReadRom(worker, rawData, results); args.Result = results; }