Inheritance: Nintenlord.ROMHacking.AbstractROM
 public NPC(int offset, GBAROM ROM)
     : base(offset, ROM)
 {
     rawDataOrig = originROM.GetData(offset, 0x18);
     rawData = (byte[])rawDataOrig.Clone();
     LoadDataFromRaw();
 }
 public static bool HasFooter(GBAROM rom)
 {
     Stream stream = rom.GetStream();
     stream.Seek(-12, SeekOrigin.End);
     bool result = true;
     using (BinaryReader reader = new BinaryReader(stream))
     {
         int length = reader.ReadInt32();
         int signature = reader.ReadInt32();
         uint crc32 = reader.ReadUInt32();
         result = signature == 0x1270D1FE;
     }
     return result;
 }
        public static void RemoveAndReadFooter(GBAROM rom, GBAMemoryManager memMan)
        {
            Stream stream = rom.GetStream();
            stream.Seek(-12, SeekOrigin.End);
            int length;
            uint crc32;
            using (BinaryReader reader = new BinaryReader(stream))
            {
                length = reader.ReadInt32();
                int signature = reader.ReadInt32();
                crc32 = reader.ReadUInt32();
                if (signature != 0x1270D1FE)
                {
                    throw new Exception("No footer present");
                }

                stream.Seek(12 + length + 12, SeekOrigin.End);

                byte[] version = reader.ReadBytes(12); //Like we care

                while (stream.Position < stream.Length - 12)
                {
                    byte[] rawid = reader.ReadBytes(4);
                    string id = new string(Array.ConvertAll(rawid, Convert.ToChar));
                    int chunkLength = reader.ReadInt32();
                    if (id == "Free")
                    {
                        long endOffset = stream.Position + length;
                        int offset = 0;
                        while (stream.Position < endOffset)
                        {
                            int relativeOffset = reader.ReadEncodedInteger();
                            offset += relativeOffset;
                            int freeLength = reader.ReadEncodedInteger();
                            memMan.AddManagedSpace(offset, freeLength);
                            offset += freeLength;
                        }
                    }
                    else
                    {
                        stream.Seek(chunkLength, SeekOrigin.Current);
                    }
                }
            }

            rom.Length -= 12 + length + 12; //Remove footer
        }
        public static Bitmap imageLoader(GBAROM ROM, int imageOffset, int paletteOffset, int width, int height, bool isImageCompressed, bool isPaletteCompressed, bool transparent, GraphicsMode mode)
        {
            DataBuffer rawGraphics = new DataBuffer(0x8000);
            DataBuffer rawPalette = new DataBuffer(0x100);
            if (isImageCompressed)
            {
                rawGraphics.ReadCompressedData(ROM, imageOffset);
            }
            else
            {
                int gfxlength = GBAGraphics.RawGraphicsLength(new Size(width * 8, height * 8), mode);
                rawGraphics.ReadData(ROM, imageOffset, gfxlength);
            }

            if (isPaletteCompressed)
            {
                rawPalette.ReadCompressedData(ROM, paletteOffset);
            }
            else
            {
                rawPalette.ReadData(ROM, paletteOffset, 0x200);
            }

            byte[] graphics = rawGraphics.ToArray();
            int bitsPerPixel = GBAGraphics.BitsPerPixel(mode);

            int length = Math.Min(bitsPerPixel * width * height / 8, graphics.Length);
            Color[] palette;

            if (rawPalette.Length > 0 && paletteOffset != 0)
                palette = GBAPalette.ToPalette(rawPalette.ToArray(), 0, rawPalette.Length / 2);
            else
            {
                palette = new Color[16];
                for (int i = 0; i < palette.Length; i++)
                    palette[i] = Color.FromArgb(i * (256 / palette.Length), i * (256 / palette.Length), i * (256 / palette.Length));
            }

            int empty;
            if(transparent)
                palette[0] = Color.FromArgb(0, palette[0]);
            return GBAGraphics.ToBitmap(graphics, length, 0, palette, width, mode, out empty);
        }
        public Map(int Offset, GBAROM ROM, int CurrentBank, int CurrentMap)
        {
            offset = Offset;
            originROM = ROM;
            currentBank = (byte)CurrentBank;
            currentMap = (byte)CurrentMap;
            rawHeaderOrig = originROM.GetData(offset, 0x1C);
            rawHeader = (byte[])rawHeaderOrig.Clone();

            LoadMapHeaderFromRaw();

            if (Program.mapLayouts.ContainsKey(mapLayoutIndex))
                layout = Program.mapLayouts[mapLayoutIndex];
            else
            {
                Program.mapLayoutNotFoundCount++;
                if (mapLayoutIndex > Program.maxLayout)
                {
                    Program.maxLayout = mapLayoutIndex;
                }
            }
        }
        public MapLayout(int index, int offset, GBAROM ROM)
        {
            layoutIndex = (byte)index;
            originROM = ROM;
            if (Program.currentGame.RomType == "FRLG")
                rawHeaderOrig = originROM.GetData(offset, 0x1C);
            else
                rawHeaderOrig = originROM.GetData(offset, 0x18);
            rawHeader = (byte[])rawHeaderOrig.Clone();
            LoadLayoutHeaderFromRaw();

            if (borderBlocksPointer > 0 && borderBlocksPointer < Program.ROM.Length)
            {
                rawBorderOrig = originROM.GetData(borderBlocksPointer, borderWidth * borderHeight * 4);
                rawBorder = (byte[])rawBorderOrig.Clone();
                LoadBorderFromRaw();
            }

            if (mapDataPointer > 0 && mapDataPointer < Program.ROM.Length)
            {
                rawLayoutOrig = originROM.GetData(mapDataPointer, layoutWidth * layoutHeight * 4);
                rawLayout = (byte[])rawLayoutOrig.Clone();
                LoadLayoutFromRaw();
            }
        }
 public Entity(int Offset, GBAROM ROM)
 {
     offset = Offset;
     originROM = ROM;
 }
 public void ReadCompressedData(GBAROM rom, int offset)
 {
     hasBeenEdited = false;
     data.Clear();
     try
     {
         data.AddRange(rom.DecompressLZ77CompressedData(offset));
     }
     catch (ArgumentNullException e)
     {
         throw new ArgumentException("The data at offset " + offset.ToString("X8") + " can't be decompressed.", e);
     }
 }
 public void WriteData(GBAROM rom, int offset, bool compressed)
 {
     hasBeenEdited = false;
     if (compressed)
     {
         rom.InsertLZ77CompressedData(offset, data.ToArray());
     }
     else
     {
         rom.InsertData(offset, data.ToArray());
     }
 }
 public void ReadData(GBAROM rom, int offset, int length)
 {
     hasBeenEdited = false;
     data.Clear();
     try
     {
         data.AddRange(rom.GetData(offset, length));
     }
     catch (ArgumentException)
     {
         throw;
     }
 }
 public static Bitmap imageLoader(GBAROM ROM, int imageOffset, int paletteOffset, int width, int height, bool isImageCompressed, bool isPaletteCompressed, bool transparent)
 {
     return imageLoader(ROM, imageOffset, paletteOffset, width, height, isImageCompressed, isPaletteCompressed, transparent, GraphicsMode.Tile4bit);
 }
 public static void Initialize(UIInteractionLayer guiMain)
 {
     mainGUI = guiMain;
     ROM = new GBAROM();
     FRLGBehaviorBytes = new Dictionary<int, string>();
 }
        /// <summary>
        /// 
        /// </summary>
        private static void BeginningStuff()
        {
            datas = new List<GraphicsData>(2048);

            rawGraphics = new DataBuffer(0x8000);
            rawPalette = new DataBuffer(0x100);
            rawTSA = new DataBuffer(0x200);

            imageForm = new ImageForm();
            paletteForm = new PaletteForm();
            colorForm = new ColourForm(rawPalette);
            tileForm = new TileForm(rawTSA);

            imageForm.ImageIndexUD.ValueChanged += new EventHandler(ImageIndexUD_ValueChanged);
            imageForm.OffsetUD.ValueChanged += new EventHandler(OffsetUD_ValueChanged);
            paletteForm.ROMPALoffsetUD.ValueChanged += new EventHandler(ROMPALoffsetUD_ValueChanged);
            paletteForm.compROMPalette.CheckedChanged += new EventHandler(compROMPalette_CheckedChanged);

            GUI = new MainForm(new ToolForm[]{imageForm, paletteForm, colorForm, tileForm});
            ROM = new GBAROM();

            grayScalePalette = new Color[0x100];
            for (int x = 0; x < 0x10; x++)
            {
                for (int y = 0; y < 0x10; y++)
                {
                    int value = x * 0x10 + y;
                    grayScalePalette[x + y * 0x10] = Color.FromArgb(value, value, value);
                }
            }
        }
        public MapTileset(int Offset, GBAROM ROM)
        {
            offset = Offset;
            originROM = ROM;
            byte[] temp = originROM.GetData(offset, 0x4);
            isCompressed = temp[0];
            isSecondary = temp[1];
            buffer1 = temp[2];
            buffer2 = temp[3];
            imagePointer = originROM.ReadPointer(offset + 0x4);
            imagePalsPointer = originROM.ReadPointer(offset + 0x8);
            blocksPointer = originROM.ReadPointer(offset + 0xC);

            if (Program.currentGame.RomType == "FRLG")
            {
                animationPointer = originROM.ReadPointer(offset + 0x10);
                behaviorPointer = originROM.ReadPointer(offset + 0x14);
            }
            else if (Program.currentGame.RomType == "E")
            {
                behaviorPointer = originROM.ReadPointer(offset + 0x10);
                animationPointer = originROM.ReadPointer(offset + 0x14);
            }

            blockSet = new Blockset(blocksPointer, behaviorPointer, (isSecondary & 1) == 1, originROM);
        }
        public Blockset(int blocksPointer, int behaviorsPointer,  bool isSecondary, GBAROM ROM)
        {
            originROM = ROM;

            if(!isSecondary)
                blocks = new Block[Program.currentGame.MainTSBlocks];
            else
                blocks = new Block[0x400 - Program.currentGame.MainTSBlocks];  //this needs fixing

            for (int i = 0; i < blocks.Length; i++)
            {
                byte[] rawBehaviorData = null;
                int isTriple = 0;
                if (Program.currentGame.RomType == "FRLG")
                    rawBehaviorData = originROM.GetData(behaviorsPointer + (i * 4), 4);
                else if (Program.currentGame.RomType == "E")
                    rawBehaviorData = originROM.GetData(behaviorsPointer + (i * 2), 2);

                Behavior behavior = new Behavior(rawBehaviorData);
                if (behavior.GetBackground() == 3)
                    isTriple = 1;
                else if (behavior.GetBackground() == 4)
                    isTriple = 2;

                byte[] rawTileData = originROM.GetData(blocksPointer + (i * 16) + ((isTriple == 2) ? 8 : 0), ((isTriple == 0) ? 16 : 24));

                short[] tileData = new short[(int)Math.Ceiling((double)(rawTileData.Length / 2))];
                Buffer.BlockCopy(rawTileData, 0, tileData, 0, rawTileData.Length);

                Tile[] tiles = new Tile[(isTriple == 0) ? 8 : 12];
                for (int j = 0; j < tiles.Length; j++)
                    tiles[j] = new Tile(tileData[j] & 0x3FF, (tileData[j] & 0xF000) >> 12, (tileData[j] & 0x400) == 0x400, (tileData[j] & 0x800) == 0x800);

                blocks[i] = new Block(tiles,behavior);
            }
        }
 public App()
 {
     rom = new GBAROM();
     encoding = new GBATextEncoding();
     metaDataHandler = new NoMetadata();
 }