public RomGBA(string filename) : base() { byte[] header = new byte[0xC0]; FileStream fs = File.OpenRead(filename); fs.Read(header, 0, 0xC0); fs.Close(); if (Validate(header)) { byte uniqueCode; byte[] shortTitle = new byte[2]; byte region; uniqueCode = header[0xAC]; shortTitle[0] = header[0xAD]; shortTitle[1] = header[0xAE]; region = header[0xAF]; if (Useful.IsUpperLetterOrDigit(uniqueCode)) { FormatCode = (char)uniqueCode; } if (Useful.IsUpperLetterOrDigit(shortTitle[0]) && Useful.IsUpperLetterOrDigit(shortTitle[1])) { ShortId = Encoding.ASCII.GetString(shortTitle); } if (Useful.IsUpperLetterOrDigit(region)) { RegionCode = (char)region; } if (Useful.IsUpperLetterOrDigit(header[0xBC])) { Version = header[0xBC]; } byte[] titleBytes = new byte[0x0C]; Array.Copy(header, 0xA0, titleBytes, 0, 0x0C); int count = 0x0C; while (--count >= 0 && titleBytes[count] == 0) { ; } Title = Encoding.ASCII.GetString(titleBytes, 0, count + 1); fs = File.Open(filename, FileMode.Open); Size = (int)fs.Length; HashCRC16 = Cll.Security.ComputeCRC16(fs); fs.Close(); if (Size > 33554432) { throw new FormatException("The GBA ROM has more than 32 MiB."); } IsValid = true; Console = Format.GBA; } else { throw new FormatException("Check bytes in the GBA ROM header are invalid."); } }
public RomN64(string filename) : base() { Endianness = Subformat.Indeterminate; byte[] header = new byte[0x40]; FileStream fs = File.Open(filename, FileMode.Open); Size = (int)fs.Length; fs.Read(header, 0, 0x40); fs.Close(); Endianness = GetFormat(header); if (Endianness == Subformat.BigEndian || (Endianness != Subformat.Indeterminate && Size % 4 == 0)) { byte uniqueCode; byte[] shortTitle = new byte[2]; byte region; header = ToBigEndian(header, Endianness); uniqueCode = header[0x3B]; shortTitle[0] = header[0x3C]; shortTitle[1] = header[0x3D]; region = header[0x3E]; if (Useful.IsUpperLetterOrDigit(uniqueCode)) { FormatCode = (char)uniqueCode; } if (Useful.IsUpperLetterOrDigit(shortTitle[0]) && Useful.IsUpperLetterOrDigit(shortTitle[1])) { ShortId = Encoding.ASCII.GetString(shortTitle); } if (Useful.IsUpperLetterOrDigit(region)) { RegionCode = (char)region; } if (Useful.IsUpperLetterOrDigit(header[0x3F])) { Version = header[0x3F]; } byte[] titleBytes = new byte[20]; Array.Copy(header, 0x20, titleBytes, 0, 20); int count = 20; while (--count >= 0 && titleBytes[count] == 0x20) { ; } Title = Encoding.ASCII.GetString(titleBytes, 0, count + 1); fs = File.Open(filename, FileMode.Open); HashCRC16 = Cll.Security.ComputeCRC16(fs); fs.Close(); IsValid = true; Console = Format.N64; } else { Size = 0; throw new FormatException("It was not possible to determine the N64 ROM format."); } }
public RomSNES(string filename) : base() { IsSMC = false; Mode = Subformat.Indeterminate; FileStream fs = File.OpenRead(filename); int smcHeaderSize = SMCHeaderSize((int)fs.Length); byte[] data = GetData(fs, smcHeaderSize); fs.Close(); if (smcHeaderSize == 0x200) { IsSMC = true; } int headerOffset = -1; Mode = GetFormat(data, ref headerOffset); if (Mode != Subformat.Indeterminate) { if (data[headerOffset + 0x2A] == 0x33) { byte uniqueCode; byte[] shortTitle = new byte[2]; byte region; uniqueCode = data[headerOffset + 0x02]; shortTitle[0] = data[headerOffset + 0x03]; shortTitle[1] = data[headerOffset + 0x04]; region = data[headerOffset + 0x05]; if (Useful.IsUpperLetterOrDigit(uniqueCode)) { FormatCode = (char)uniqueCode; } if (Useful.IsUpperLetterOrDigit(shortTitle[0]) && Useful.IsUpperLetterOrDigit(shortTitle[1])) { ShortId = Encoding.ASCII.GetString(shortTitle); } if (Useful.IsUpperLetterOrDigit(region)) { RegionCode = (char)region; } } Version = data[headerOffset + 0x2B]; byte[] titleBytes = new byte[21]; Array.Copy(data, headerOffset + 0x10, titleBytes, 0, 21); int count = 21; while (--count >= 0 && titleBytes[count] == 0x20) { ; } Title = Encoding.ASCII.GetString(titleBytes, 0, count + 1); Size = data.Length; HashCRC16 = Cll.Security.ComputeCRC16_ARC(data, 0, data.Length); IsValid = true; Console = Format.SNES_USA; } else { Size = 0; throw new FormatException("It was not possible to determine the SNES ROM format."); } }
public RomNDS(string filename) : base() { TitleLine1 = ""; TitleLine2 = ""; Icon = null; byte[] header = new byte[0x200]; FileStream fs = File.OpenRead(filename); fs.Read(header, 0, 0x200); fs.Close(); if (Validate(header)) { byte uniqueCode; byte[] shortTitle = new byte[2]; byte region; uniqueCode = header[0x0C]; shortTitle[0] = header[0x0D]; shortTitle[1] = header[0x0E]; region = header[0x0F]; if (Useful.IsUpperLetterOrDigit(uniqueCode)) { FormatCode = (char)uniqueCode; } if (Useful.IsUpperLetterOrDigit(shortTitle[0]) && Useful.IsUpperLetterOrDigit(shortTitle[1])) { ShortId = Encoding.ASCII.GetString(shortTitle); } if (Useful.IsUpperLetterOrDigit(region)) { RegionCode = (char)region; } if (Useful.IsUpperLetterOrDigit(header[0x1E])) { Version = header[0x1E]; } byte[] offsetBytes = new byte[4]; byte[] bitmapBytes = new byte[0x200]; byte[] paletteBytes = new byte[0x20]; byte[] titleBytes = new byte[0x100]; fs = File.OpenRead(filename); Size = (int)fs.Length; fs.Seek(0x68, SeekOrigin.Begin); fs.Read(offsetBytes, 0, 4); int offset = (offsetBytes[3] << 24) + (offsetBytes[2] << 16) + (offsetBytes[1] << 8) + offsetBytes[0]; fs.Seek(offset + 0x20, SeekOrigin.Begin); fs.Read(bitmapBytes, 0, 0x200); fs.Read(paletteBytes, 0, 0x20); fs.Read(titleBytes, 0, 0x100); fs.Position = 0; HashCRC16 = Cll.Security.ComputeCRC16(fs); fs.Close(); string title = Encoding.Unicode.GetString(titleBytes); string[] lines = title.Split(new char[] { '\n' }); if (lines.Length == 2) { Title = lines[0]; TitleLine1 = lines[0]; } else if (lines.Length >= 3) { Title = lines[0] + " " + lines[1]; TitleLine1 = lines[0]; TitleLine2 = lines[1]; } Color[] palette = new Color[16]; int j = -1; for (int i = 0; i < 16; i++) { palette[i] = Color.FromArgb( (paletteBytes[++j] & 0x1F) << 3, //0000 0000 0001 1111 ((paletteBytes[j] & 0xE0) >> 2) + //0000 0000 1110 0000 ((paletteBytes[++j] & 0x03) << 6), //0000 0011 0000 0000 (paletteBytes[j] & 0x7C) << 1); //0111 1100 0000 0000 } palette[0] = Color.FromArgb(0, palette[0].R, palette[0].G, palette[0].B); byte[] pix = new byte[1024]; int pixIndex; int bytesIndex; for (int tileY = 0; tileY < 4; tileY++) { for (int tileX = 0; tileX < 4; tileX++) { for (int i = 0; i < 8; i++) { pixIndex = i * 32 + tileX * 8 + tileY * 256; bytesIndex = i * 4 + tileX * 32 + tileY * 128; pix[pixIndex] = (byte)(bitmapBytes[bytesIndex] & 0x0F); pix[pixIndex + 1] = (byte)((bitmapBytes[bytesIndex] & 0xF0) >> 4); pix[pixIndex + 2] = (byte)(bitmapBytes[bytesIndex + 1] & 0x0F); pix[pixIndex + 3] = (byte)((bitmapBytes[bytesIndex + 1] & 0xF0) >> 4); pix[pixIndex + 4] = (byte)(bitmapBytes[bytesIndex + 2] & 0x0F); pix[pixIndex + 5] = (byte)((bitmapBytes[bytesIndex + 2] & 0xF0) >> 4); pix[pixIndex + 6] = (byte)(bitmapBytes[bytesIndex + 3] & 0x0F); pix[pixIndex + 7] = (byte)((bitmapBytes[bytesIndex + 3] & 0xF0) >> 4); } } } Bitmap icon = new Bitmap(32, 32); Rectangle rect = new Rectangle(0, 0, icon.Width, icon.Height); System.Drawing.Imaging.BitmapData data = icon.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, icon.PixelFormat); int length = data.Width * data.Height * 4; byte[] iconBytes = new byte[length]; IntPtr ptr = data.Scan0; System.Runtime.InteropServices.Marshal.Copy(ptr, iconBytes, 0, length); Color color; for (int i = 0; i < 1024; i++) { color = palette[pix[i]]; iconBytes[i * 4] = color.B; iconBytes[i * 4 + 1] = color.G; iconBytes[i * 4 + 2] = color.R; iconBytes[i * 4 + 3] = color.A; } System.Runtime.InteropServices.Marshal.Copy(iconBytes, 0, ptr, length); icon.UnlockBits(data); Icon = icon; IsValid = true; Console = Format.NDS; } else { throw new FormatException("Checksums in the NDS ROM header are invalid."); } }