public override void Read(BinaryReaderExt r) { base.Read(r); r.Skip(1); Type = (LVDDamageShapeType)r.ReadInt32(); if (!Enum.IsDefined(typeof(LVDDamageShapeType), Type)) { throw new NotImplementedException($"Unknown damage shape type {Type} at offset {r.BaseStream.Position - 4}"); } X = r.ReadSingle(); Y = r.ReadSingle(); Z = r.ReadSingle(); if (Type == LVDDamageShapeType.Sphere) { Radius = r.ReadSingle(); Dx = r.ReadSingle(); Dy = r.ReadSingle(); Dz = r.ReadSingle(); } if (Type == LVDDamageShapeType.Capsule) { Dx = r.ReadSingle(); Dy = r.ReadSingle(); Dz = r.ReadSingle(); Radius = r.ReadSingle(); } Unknown1 = r.ReadByte(); Unknown2 = r.ReadInt32(); }
public override void Read(BinaryReaderExt r) { base.Read(r); r.ReadByte(); X = r.ReadSingle(); Y = r.ReadSingle(); }
public static Region[] GetRegions(this Stream s) { using (BinaryReader br = new BinaryReaderExt(s, Encoding.ASCII, true)) { s.Seek(0xF70, SeekOrigin.Begin); bool isDecrypting = br.ReadByte() != 0x44; // Begins with E or D s.Seek(0xF70 + 0xD, SeekOrigin.Begin); bool isBuildedISO = br.ReadByte() == 0x42; // End in BLD s.Seek(0xF70 + 0xF, SeekOrigin.Begin); bool isValidHash = isBuildedISO && br.ReadByte() != 0x44; // Ends in BLF /* Read the game Key */ s.Seek(0xF80, SeekOrigin.Begin); byte[] d1 = br.ReadBytes(0x10); byte[] d2 = br.ReadBytes(0x10); s.Seek(0, SeekOrigin.Begin); uint plainRegions = br.ReadUInt32().Swap(); Region[] regions = new Region[(plainRegions * 2) - 1]; s.Seek(4, SeekOrigin.Current); uint current = br.ReadUInt32().Swap(); bool isPlain = true; for (uint i = 0; i < regions.Length; i++) { uint next = br.ReadUInt32().Swap(); if (isPlain) { regions[i] = new PlainRegion(i, isDecrypting, isBuildedISO, isValidHash, current, next); } else { regions[i] = isDecrypting ? (Region) new CryptedRegion(i, d1, d2, current, next, false) : new DecryptedRegion(i, d1, d2, current, next, false); } isPlain = !isPlain; current = next; } s.Seek(0, SeekOrigin.Begin); return(regions); } }
public void Read(BinaryReaderExt r) { r.ReadByte(); Type = (LVDShapeType)r.ReadInt32(); X = r.ReadSingle(); Y = r.ReadSingle(); Z = r.ReadSingle(); W = r.ReadSingle(); r.ReadByte(); r.ReadByte(); int pointCount = r.ReadInt32(); for (int i = 0; i < pointCount; i++) { r.Skip(1); Points.Add(new LVDVector2(r.ReadSingle(), r.ReadSingle())); } }
public override void Read(BinaryReaderExt r) { base.Read(r); r.ReadByte(); Vector1 = new LVDVector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle()); r.ReadByte(); Vector2 = new LVDVector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle()); r.ReadByte(); int vec3Count = r.ReadInt32(); for (int i = 0; i < vec3Count; i++) { r.ReadByte(); Vectors.Add(new LVDVector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle())); } r.ReadByte(); Mat4x4_1 = new float[16]; for (int i = 0; i < 16; i++) { Mat4x4_1[i] = r.ReadSingle(); } r.ReadByte(); Mat4x4_2 = new float[16]; for (int i = 0; i < 16; i++) { Mat4x4_2[i] = r.ReadSingle(); } }
/// <summary> /// /// </summary> /// <param name="ramOffset"></param> public HSD_String ReadStringAt(uint ramOffset, bool shiftJis = false) { if (ramOffset == 0 || ramOffset == 0xFFFFFFFF) { return(null); } var off = RAMToDOL(ramOffset); r.BaseStream.Position = off; var str = ""; if (shiftJis) { byte[] c = r.ReadBytes(2); while (c.Length >= 2 && !(c[0] == 0 && c[1] == 0)) { str += System.Text.Encoding.GetEncoding(932).GetString(c); c = r.ReadBytes(2); } } else { byte c = r.ReadByte(); while (c != 0) { str += (char)c; c = r.ReadByte(); } } return(new HSD_String() { Value = str }); }
public static int ReadLEB123(BinaryReaderExt e) { int result = 0; int shift = 0; while (true) { byte b = e.ReadByte(); result |= (b & 0x7F) << shift; if ((b & 0x80) == 0) { break; } shift += 7; } return(result); }
public RemoteConPacket(byte[] packetBytes) { using (var ms = new MemoryStream(packetBytes)) { using (var reader = new BinaryReaderExt(ms)) { Size = reader.ReadInt32LittleEndian(); // The size field (4-Bytes Little Endian Int) is, according to specification, not included. if (Size + 4 != packetBytes.Length) { throw new LengthMismatchException("packet length mismatch"); } Id = reader.ReadInt32LittleEndian(); var packetType = reader.ReadInt32LittleEndian(); if (!Enum.IsDefined(typeof(PacketType), packetType)) { throw new InvalidPacketTypeException("Invalid packet type"); } Type = (PacketType)Enum.ToObject(typeof(PacketType), packetType); Payload = reader.ReadAscii(); // Get payload length by subtracting 9 bytes (ID 4-Bytes, Type 4-Bytes, Null-terminator 1-Byte) if (Encoding.ASCII.GetByteCount(Payload) > Size - 9) { throw new LengthMismatchException("Payload length mismatch"); } var nullTerminator = reader.ReadByte(); if (nullTerminator != 0x00) { throw new NullTerminatorMissingException("Missing last null-terminator"); } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new Exception("More data to read"); } } } }
/// <summary> /// /// </summary> public void LoadFromStream(Stream s) { using (BinaryReaderExt r = new BinaryReaderExt(s)) { if (s.Length < 0x14) { return; } if (new string(r.ReadChars(4)) != "SPKG") { return; } GroupFlags = r.ReadUInt32(); Flags = r.ReadUInt32(); var ssmSize = r.ReadInt32(); ScriptBank = new SEMBank(); ScriptBank.Scripts = new SEMBankScript[r.ReadInt32()]; for (int i = 0; i < ScriptBank.Scripts.Length; i++) { ScriptBank.Scripts[i] = new SEMBankScript(); ScriptBank.Scripts[i].Decompile(r.GetSection(r.ReadUInt32(), r.ReadInt32())); } var name = r.ReadString(r.ReadByte()); if (ssmSize == 0) { SoundBank = null; } else { SoundBank = new SSM(); using (MemoryStream ssmStream = new MemoryStream(r.ReadBytes(ssmSize))) SoundBank.Open(name, ssmStream); } } }
private static double ParseFloat(BinaryReaderExt d, GXAnimDataFormat Format, float Scale) { d.BigEndian = false; switch (Format) { case GXAnimDataFormat.HSD_A_FRAC_FLOAT: return(d.ReadSingle()); case GXAnimDataFormat.HSD_A_FRAC_S16: return(d.ReadInt16() / (double)Scale); case GXAnimDataFormat.HSD_A_FRAC_U16: return(d.ReadUInt16() / (double)Scale); case GXAnimDataFormat.HSD_A_FRAC_S8: return(d.ReadSByte() / (double)Scale); case GXAnimDataFormat.HSD_A_FRAC_U8: return(d.ReadByte() / (double)Scale); default: return(0); } }
/// <summary> /// /// </summary> /// <param name="elfFile"></param> public RelocELF(byte[] elfFile) { using (MemoryStream mstream = new MemoryStream(elfFile)) using (BinaryReaderExt r = new BinaryReaderExt(mstream)) { // Parse Header if (!(r.ReadByte() == 0x7F && r.ReadByte() == 0x45 && r.ReadByte() == 0x4C && r.ReadByte() == 0x46)) { throw new InvalidDataException("Not a valid ELF file"); } byte bitType = r.ReadByte(); // 1 - 32, 2 - 64 if (bitType != 1) { throw new NotSupportedException("Only 32 bit ELF files are currently supported"); } r.BigEndian = r.ReadByte() == 2; // I only care about the sections r.Seek(0x20); var sectionOffset = r.ReadUInt32(); r.Seek(0x2E); var sectionHeaderSize = r.ReadUInt16(); var numOfSections = r.ReadInt16(); var StringSectionIndex = r.ReadUInt16(); List <SectionData> DataSections = new List <SectionData>(); // Parse Sections var Sections = new ELFSection[numOfSections]; for (uint i = 0; i < numOfSections; i++) { r.Seek(sectionOffset + sectionHeaderSize * i); Sections[i] = new ELFSection() { sh_name = r.ReadUInt32(), sh_type = (SectionType)r.ReadInt32(), sh_flags = r.ReadUInt32(), sh_addr = r.ReadUInt32(), sh_offset = r.ReadUInt32(), sh_size = r.ReadUInt32(), sh_link = r.ReadUInt32(), sh_info = r.ReadUInt32(), sh_addralign = r.ReadUInt32(), sh_entsize = r.ReadUInt32() }; DataSections.Add(new SectionData()); } // Parse Symbols var symbolSection = Array.Find(Sections, e => r.ReadString((int)(Sections[StringSectionIndex].sh_offset + e.sh_name), -1) == ".symtab"); var Symbols = new ELFSymbol[symbolSection.sh_size / 0x10]; for (uint i = 0; i < Symbols.Length; i++) { r.Seek(symbolSection.sh_offset + 0x10 * i); Symbols[i] = new ELFSymbol() { st_name = r.ReadUInt32(), st_value = r.ReadUInt32(), st_size = r.ReadUInt32(), st_info = r.ReadByte(), st_other = r.ReadByte(), st_shndx = r.ReadInt16() }; SymbolSections.Add(new SymbolData()); } // Grab Relocation Data for (int i = 0; i < Sections.Length; i++) { var section = Sections[i]; var data = DataSections[i]; data.Name = r.ReadString((int)(Sections[StringSectionIndex].sh_offset + Sections[i].sh_name), -1); data.Data = r.GetSection(section.sh_offset, (int)section.sh_size); if (section.sh_type == SectionType.SHT_RELA || section.sh_type == SectionType.SHT_REL) { var relocs = ParseRelocationSection(r, section); foreach (var v in relocs) { DataSections[(int)section.sh_info].Relocations.Add(new RelocData() { Offset = v.r_offset, AddEnd = v.r_addend, Symbol = SymbolSections[(int)v.R_SYM], Type = (RelocType)v.R_TYP, SymbolIndex = v.R_SYM }); } } } var symbolStringSection = Sections[symbolSection.sh_link]; // rip out symbol data for (int i = 0; i < Symbols.Length; i++) { var sym = Symbols[i]; var section = sym.st_shndx >= 0 ? DataSections[sym.st_shndx] : null; byte[] symbolData = new byte[sym.st_size]; List <RelocData> relocations = new List <RelocData>(); if (section != null) { SymbolSections[i].SectionName = section.Name; if (Sections[sym.st_shndx].sh_type == SectionType.SHT_NOBITS) { symbolData = new byte[section.Data.Length]; #if DEBUG // Console.WriteLine($"{section.Name} {(Sections[sym.st_shndx].sh_offset + sym.st_value).ToString("X")} {sym.st_size} {sym.st_value} {symbolData.Length} {Sections[sym.st_shndx].sh_type}"); #endif } else { // If size of section is 0, get all data? if (sym.st_size == 0) { symbolData = section.Data; } //else //if ((sym.st_value & 0x80000000) != 0) //{ // Array.Copy(section.Data, sym.st_value - 0x80000000 - Sections[sym.st_shndx].sh_offset, symbolData, 0, sym.st_size); // Debug.WriteLine($"LONG CALL {section.Relocations.Count} Off: {(sym.st_value - 0x80000000).ToString("X")} SectionOff: {Sections[sym.st_shndx].sh_offset.ToString("X")} {sym.st_value.ToString("X")} Size: {sym.st_size.ToString("X")} Total Size: {section.Data.Length.ToString("X")}"); //} else { Array.Copy(section.Data, sym.st_value, symbolData, 0, sym.st_size); } // TODO: when to get relocations? relocations = section.Relocations.Where( e => e.Offset >= sym.st_value && (e.Offset < sym.st_value + symbolData.Length) ).ToList(); // make relative foreach (var rel in relocations) { rel.Offset -= sym.st_value; } } // if the offset is 0 the function is usually in another file SymbolSections[i].External = Sections[sym.st_shndx].sh_offset == 0; #if DEBUG //Console.WriteLine(section.Name + " " + r.ReadString((int)(symbolStringSection.sh_offset + sym.st_name), -1) // + " " + Sections[sym.st_shndx].sh_info + " " + Sections[sym.st_shndx].sh_addr + " " + relocations.Count); //Debug.WriteLine($"{section.Name} {r.ReadString((int)(symbolStringSection.sh_offset + sym.st_name), -1)} {(Sections[sym.st_shndx].sh_offset + + sym.st_value).ToString("X")} {sym.st_size.ToString("X")}"); if (section.Name == ".debug_line") { //r.Seek(Sections[sym.st_shndx].sh_offset + +sym.st_value); //ParseDebugLine(r); } #endif } SymbolSections[i].Symbol = r.ReadString((int)(symbolStringSection.sh_offset + sym.st_name), -1); SymbolSections[i].Data = symbolData; SymbolSections[i].Relocations = relocations; } } }
/// <summary> /// /// </summary> /// <returns></returns> public object[] Decode(byte code, BinaryReaderExt r) { var output = new List <object>(Parameters.Length); for (int i = 0; i < Parameters.Length; i++) { var k = Parameters[i]; switch (k) { case 'e': // extended byte int e = r.ReadByte(); if ((e & 0x80) > 0) { e = ((e & 0x7F) << 8 | r.ReadByte()); } output.Add((short)e); break; case 'b': output.Add(r.ReadByte()); break; case 'f': output.Add(r.ReadSingle()); break; case 's': output.Add(r.ReadInt16()); break; case 'c': { // custom parameters //var behavior = code >> 4; var param_count = code & 0xF; for (int j = 0; j < 4; j++) { if (((param_count >> i) & 1) == 1) { output.Add(r.ReadByte()); } } } break; case 'p': case 'v': { if ((code & 0x01) != 0) { output.Add(r.ReadSingle()); } else { output.Add(0); } if ((code & 0x02) != 0) { output.Add(r.ReadSingle()); } else { output.Add(0); } if ((code & 0x04) != 0) { output.Add(r.ReadSingle()); } else { output.Add(0); } } break; case 'r': { var behavior = r.ReadByte(); output.Add((behavior & 0x10) != 0); output.Add((behavior & 0x20) != 0); output.Add(r.ReadByte()); if ((behavior & 0x01) != 0) { output.Add(r.ReadByte()); } else { output.Add(0); } if ((behavior & 0x02) != 0) { output.Add(r.ReadByte()); } else { output.Add(0); } if ((behavior & 0x04) != 0) { output.Add(r.ReadByte()); } else { output.Add(0); } if ((behavior & 0x08) != 0) { output.Add(r.ReadByte()); } else { output.Add(0); } } break; case 'm': { var behavior = r.ReadByte(); if ((behavior & 0x01) != 0) { output.Add(r.ReadByte()); } else { output.Add(0); } if ((behavior & 0x08) != 0) { output.Add(r.ReadByte()); } else { output.Add(0); } } break; } } return(output.ToArray()); }
public void Open(string FileName) { using (BinaryReaderExt stream = new BinaryReaderExt(new FileStream(FileName, FileMode.Open))) { stream.BigEndian = true; Heading = stream.ReadInt32(); VersionMinor = stream.ReadByte(); VersionMajor = stream.ReadByte(); Magic = new string(stream.ReadChars(4)); stream.ReadByte(); var collisionCount = stream.ReadInt32(); for (int i = 0; i < collisionCount; i++) { LVDCollision col = new LVDCollision(); col.Read(stream, VersionMinor); Collisions.Add(col); } stream.ReadByte(); var spawnCount = stream.ReadInt32(); for (int i = 0; i < spawnCount; i++) { LVDSpawn spawn = new LVDSpawn(); spawn.Read(stream); Spawns.Add(spawn); } stream.ReadByte(); var respawnCount = stream.ReadInt32(); for (int i = 0; i < respawnCount; i++) { LVDSpawn respawn = new LVDSpawn(); respawn.Read(stream); Respawns.Add(respawn); } stream.ReadByte(); var boundsCount = stream.ReadInt32(); for (int i = 0; i < boundsCount; i++) { LVDBounds bound = new LVDBounds(); bound.Read(stream); CameraBounds.Add(bound); } stream.ReadByte(); var blastCount = stream.ReadInt32(); for (int i = 0; i < blastCount; i++) { LVDBounds blast = new LVDBounds(); blast.Read(stream); BlastZoneBounds.Add(blast); } stream.ReadByte(); int enemyGenCount = stream.ReadInt32(); for (int i = 0; i < enemyGenCount; i++) { LVDEnemyGenerator enGen = new LVDEnemyGenerator(); enGen.Read(stream); EnemyGenerators.Add(enGen); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); int damageCount = stream.ReadInt32(); for (int i = 0; i < damageCount; i++) { LVDDamageShape shape = new LVDDamageShape(); shape.Read(stream); DamageShapes.Add(shape); } stream.ReadByte(); int itemSpawnCount = stream.ReadInt32(); for (int i = 0; i < itemSpawnCount; i++) { LVDItemSpawner spawn = new LVDItemSpawner(); spawn.Read(stream); ItemSpawners.Add(spawn); } if (VersionMinor > 0xA) { stream.ReadByte(); int genCurveCount = stream.ReadInt32(); for (int i = 0; i < genCurveCount; i++) { LVDRangeCurve point = new LVDRangeCurve(); point.Read(stream); RangeCurves.Add(point); } stream.ReadByte(); int genCurveCount2 = stream.ReadInt32(); for (int i = 0; i < genCurveCount2; i++) { LVDGeneralVector point = new LVDGeneralVector(); point.Read(stream); GeneralVectors.Add(point); } } stream.ReadByte(); int genShapeCount = stream.ReadInt32(); for (int i = 0; i < genShapeCount; i++) { LVDGeneralShape shape = new LVDGeneralShape(); shape.Read(stream); GeneralShapes.Add(shape); } stream.ReadByte(); int genPointCount = stream.ReadInt32(); for (int i = 0; i < genPointCount; i++) { LVDGeneralPoint point = new LVDGeneralPoint(); point.Read(stream); GeneralPoints.Add(point); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } stream.ReadByte(); if (stream.ReadInt32() > 0) { throw new NotImplementedException("Unknown LVD Section at 0x" + stream.BaseStream.Position.ToString("X")); } if (VersionMinor > 0xA) { stream.ReadByte(); var shrunkboundsCount = stream.ReadInt32(); for (int i = 0; i < shrunkboundsCount; i++) { LVDBounds bound = new LVDBounds(); bound.Read(stream); ShrunkCameraBounds.Add(bound); } stream.ReadByte(); var shrunkblastCount = stream.ReadInt32(); for (int i = 0; i < shrunkblastCount; i++) { LVDBounds blast = new LVDBounds(); blast.Read(stream); ShrunkBlastZoneBounds.Add(blast); } } if (stream.BaseStream.Length != stream.BaseStream.Position) { stream.PrintPosition(); throw new Exception("Error fully parsing LVD " + stream.BaseStream.Position.ToString("X")); } } }
public void Parse(BinaryReaderExt r) { r.BigEndian = true; var start = r.Position; var sectionCount = r.ReadInt32(); var sectionHeaderLength = r.ReadInt32(); PlaySpeed = r.ReadSingle(); EndTime = r.ReadSingle(); for (int j = 0; j < sectionCount; j++) { Joint joint = new Joint(); Joints.Add(joint); joint.Flag1 = r.ReadByte(); joint.Flag2 = r.ReadByte(); joint.Flag3 = r.ReadUInt16(); joint.BoneID = r.ReadInt16(); var floatCount = r.ReadInt16(); joint.MaxTime = r.ReadSingle(); joint.Unknown = r.ReadInt32(); var offset1 = r.ReadUInt32() + start; var offset2 = r.ReadUInt32() + start; var offset3 = r.ReadUInt32() + start; var offset4 = r.ReadUInt32() + start; if (offset3 != start) { throw new NotSupportedException("Section 3 detected"); } if (offset4 != start) { throw new NotSupportedException("Section 4 detected"); } var temp = r.Position; for (uint k = 0; k < floatCount; k++) { Key key = new Key(); r.Seek(offset1 + 4 * k); key.Time = r.ReadSingle(); if (offset2 != start) { r.Seek(offset2 + 8 * k); key.X = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); key.Y = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); key.Z = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); key.W = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); } joint.Keys.Add(key); } r.Seek(temp); } }
private static byte[] ReadDirectGXColor(BinaryReaderExt Reader, int CompType) { byte[] clr = new byte[] { 255, 255, 255, 255 }; int b; switch (CompType) { case 0: // GX_RGB565 b = Reader.ReadUInt16(); clr[0] = (byte)((((b >> 11) & 0x1F) << 3) | (((b >> 11) & 0x1F) >> 2)); clr[1] = (byte)((((b >> 5) & 0x3F) << 2) | (((b >> 5) & 0x3F) >> 4)); clr[2] = (byte)((((b) & 0x1F) << 3) | (((b) & 0x1F) >> 2)); clr[3] = 255; break; case 1: // GX_RGB888 clr[0] = Reader.ReadByte(); clr[1] = Reader.ReadByte(); clr[2] = Reader.ReadByte(); clr[3] = 255; break; case 2: // GX_RGBX888 clr[0] = Reader.ReadByte(); clr[1] = Reader.ReadByte(); clr[2] = Reader.ReadByte(); clr[3] = Reader.ReadByte(); break; case 3: // GX_RGBA4 b = Reader.ReadUInt16(); clr[0] = (byte)((((b >> 12) & 0xF) << 4) | ((b >> 12) & 0xF)); clr[1] = (byte)((((b >> 8) & 0xF) << 4) | ((b >> 8) & 0xF)); clr[2] = (byte)((((b >> 4) & 0xF) << 4) | ((b >> 4) & 0xF)); clr[3] = (byte)((((b) & 0xF) << 4) | ((b) & 0xF)); break; case 4: // GX_RGBA6 b = (Reader.ReadByte() << 16) | (Reader.ReadByte() << 8) | (Reader.ReadByte()); clr[0] = (byte)((((b >> 18) & 0x3F) << 2) | (((b >> 18) & 0x3F) >> 4)); clr[1] = (byte)((((b >> 12) & 0x3F) << 2) | (((b >> 12) & 0x3F) >> 4)); clr[2] = (byte)((((b >> 6) & 0x3F) << 2) | (((b >> 6) & 0x3F) >> 4)); clr[3] = (byte)((((b) & 0x3F) << 2) | (((b) & 0x3F) >> 4)); break; case 5: // GX_RGBX888 clr[0] = Reader.ReadByte(); clr[1] = Reader.ReadByte(); clr[2] = Reader.ReadByte(); clr[3] = Reader.ReadByte(); break; default: throw new Exception("Unknown Color Type"); } return(clr); }
public void Read(BinaryReaderExt r, int VersionMinor) { base.Read(r); Flag1 = r.ReadBoolean(); Rigged = r.ReadBoolean(); Flag3 = r.ReadBoolean(); PassThrough = r.ReadBoolean(); r.ReadByte(); int vertCount = r.ReadInt32(); for (int i = 0; i < vertCount; i++) { r.ReadByte(); Vertices.Add(new LVDVector2(r.ReadSingle(), r.ReadSingle())); } r.ReadByte(); int normalCount = r.ReadInt32(); for (int i = 0; i < normalCount; i++) { r.ReadByte(); Normals.Add(new LVDVector2(r.ReadSingle(), r.ReadSingle())); } r.ReadByte(); int cliffCount = r.ReadInt32(); for (int i = 0; i < cliffCount; i++) { var cliff = new LVDCollisionCliff(); cliff.Read(r); Cliffs.Add(cliff); } r.ReadByte(); int materialCount = r.ReadInt32(); for (int i = 0; i < materialCount; i++) { var material = new LVDCollisionMaterial(); material.Read(r); Materials.Add(material); } // Ultimate Only? if (VersionMinor > 10) { r.ReadByte(); var vecCount = r.ReadInt32(); for (int i = 0; i < vecCount; i++) { var vec = new LVDCollisionCurve(); vec.Read(r); Curves.Add(vec); } } }
private static void ParseDebugLine(BinaryReaderExt r) { // read header var debug_line_header = new ELF_Debug_Line() { length = r.ReadUInt32(), version = r.ReadUInt16(), header_length = r.ReadUInt32(), min_instruction_length = r.ReadByte(), default_is_stmt = r.ReadByte(), line_base = r.ReadSByte(), line_range = r.ReadByte(), opcode_base = r.ReadByte(), std_opcode_lengths = new byte[12] }; for (int k = 0; k < 12; k++) { debug_line_header.std_opcode_lengths[k] = r.ReadByte(); } // read directories while (r.PeekChar() != 0) { Debug.WriteLine(r.ReadString()); } r.Skip(1); // read files while (r.PeekChar() != 0) { Debug.WriteLine(r.ReadString() + " " + r.ReadByte() + " " + r.ReadByte() + " " + r.ReadByte()); } r.PrintPosition(); int address = 0; int op_index = 0; int file = 1; int line = 1; int column = 0; bool is_stmt = debug_line_header.default_is_stmt != 0; bool basic_block = false; bool end_sequence = false; bool prologue_end = false; bool epilogue_begin = false; int isa = 0; int dicriminator = 0; var op_code_start = r.Position; while (true) { var op = r.ReadByte(); switch (op) { case 0: // extended byte ReadLEB123(r); break; } } }
public void Read(BinaryReaderExt r) { r.ReadByte(); MaterialData = r.ReadBytes(0xC); }
/// <summary> /// /// </summary> /// <param name="output"></param> /// <param name="name"></param> /// <param name="datas"></param> /// <param name="structToFunctionName"></param> private void DecompileGroup(StringBuilder output, string name, HSDStruct datas) { if (structToFunctionName.ContainsKey(datas)) { return; } structToFunctionName.Add(datas, name); output.AppendLine(name); output.AppendLine("{"); using (BinaryReaderExt r = new BinaryReaderExt(new MemoryStream(datas.GetData()))) { byte flag = (byte)(r.ReadByte() >> 2); var cmd = ActionCommon.GetMeleeCMDAction(flag); while (flag != 0) { r.BaseStream.Position -= 1; var size = cmd.ByteSize; var command = r.GetSection(r.Position, size); r.Skip((uint)size); if (flag == 5 || flag == 7) //goto { var re = datas.GetReference <HSDAccessor>((int)r.BaseStream.Position - 4); if (re != null) { if (!tempStructToName.ContainsKey(re._s)) { if (structToFunctionName.ContainsKey(re._s)) { tempStructToName.Add(re._s, structToFunctionName[re._s]); } else { tempStructToName.Add(re._s, name + "_" + ((int)r.BaseStream.Position - 4).ToString("X4")); } } var funcname = tempStructToName[re._s]; output.AppendLine("\t" + (flag == 5 ? "Goto" : "Subroutine") + "(" + funcname + ");"); } } else { output.AppendLine("\t" + DecompileCommand(command)); } if (r.BaseStream.Position >= r.BaseStream.Length) { break; } flag = (byte)(r.ReadByte() >> 2); cmd = ActionCommon.GetMeleeCMDAction(flag); } } output.AppendLine("}"); foreach (var re in datas.References) { DecompileGroup(output, name + "_" + re.Key.ToString("X4"), re.Value); } }
private static void FromBRSTM(this DSP dsp, string filePath) { using (FileStream s = new FileStream(filePath, FileMode.Open)) using (BinaryReaderExt r = new BinaryReaderExt(s)) { if (new string(r.ReadChars(4)) != "RSTM") { throw new NotSupportedException("File is not a valid BRSTM file"); } r.BigEndian = true; r.BigEndian = r.ReadUInt16() == 0xFEFF; r.Skip(2); // 01 00 version r.Skip(4); // filesize r.Skip(2); // 00 40 - header length r.Skip(2); // 00 02 - header version var headOffset = r.ReadUInt32(); var headSize = r.ReadUInt32(); var adpcOffset = r.ReadUInt32(); var adpcSize = r.ReadUInt32(); var dataOffset = r.ReadUInt32(); var dataSize = r.ReadUInt32(); // can skip adpc section when reading because it just contains sample history // parse head section // -------------------------------------------------------------- r.Position = headOffset; if (new string(r.ReadChars(4)) != "HEAD") { throw new NotSupportedException("BRSTM does not have a valid HEAD"); } r.Skip(4); // section size r.Skip(4); // 01 00 00 00 marker var chunk1Offset = r.ReadUInt32() + 8 + headOffset; r.Skip(4); // 01 00 00 00 marker var chunk2Offset = r.ReadUInt32() + 8 + headOffset; r.Skip(4); // 01 00 00 00 marker var chunk3Offset = r.ReadUInt32() + 8 + headOffset; // -------------------------------------------------------------- r.Seek(chunk1Offset); var codec = (BRSTM_CODEC)r.ReadByte(); var loopFlag = r.ReadByte(); var channelCount = r.ReadByte(); r.Skip(1); // padding if (codec != BRSTM_CODEC.ADPCM_4bit) { throw new NotSupportedException("only 4bit ADPCM files currently supported"); } var sampleRate = r.ReadUInt16(); r.Skip(2); // padding dsp.Frequency = sampleRate; var loopStart = r.ReadUInt32(); var totalSamples = r.ReadUInt32(); var dataPointer = r.ReadUInt32(); // DATA offset int blockCount = r.ReadInt32(); var blockSize = r.ReadUInt32(); var samplesPerBlock = r.ReadInt32(); var sizeOfFinalBlock = r.ReadUInt32(); var samplesInFinalBlock = r.ReadInt32(); var sizeOfFinalBlockWithPadding = r.ReadUInt32(); var samplesPerEntry = r.ReadInt32(); var bytesPerEntry = r.ReadInt32(); // -------------------------------------------------------------- r.Seek(chunk2Offset); var numOfTracks = r.ReadByte(); var trackDescType = r.ReadByte(); r.Skip(2); // padding for (uint i = 0; i < numOfTracks; i++) { r.Seek(chunk1Offset + 4 + 8 * i); r.Skip(1); // 01 padding var descType = r.ReadByte(); r.Skip(2); // padding var descOffset = r.ReadUInt32() + 8 + headOffset; r.Seek(descOffset); switch (descType) { case 0: { int channelsInTrack = r.ReadByte(); int leftChannelID = r.ReadByte(); int rightChannelID = r.ReadByte(); r.Skip(1); // padding } break; case 1: { var volume = r.ReadByte(); var panning = r.ReadByte(); r.Skip(2); // padding r.Skip(4); // padding int channelsInTrack = r.ReadByte(); int leftChannelID = r.ReadByte(); int rightChannelID = r.ReadByte(); r.Skip(1); // 01 padding } break; } } // -------------------------------------------------------------- r.Seek(chunk3Offset); var channelCountAgain = r.ReadByte(); r.Skip(3); for (uint i = 0; i < channelCountAgain; i++) { r.Seek(chunk3Offset + 4 + 8 * i); r.Skip(4); // 01000000 marker var offset = r.ReadUInt32() + headOffset + 8; r.Seek(offset); // channel information var channel = new DSPChannel(); dsp.Channels.Add(channel); channel.LoopFlag = loopFlag; channel.LoopStart = (int)loopStart; r.Skip(4); // 01000000 marker r.Skip(4); // offset to coefficients (they follow directly after) for (int k = 0; k < 0x10; k++) { channel.COEF[k] = r.ReadInt16(); } channel.Gain = r.ReadInt16(); channel.InitialPredictorScale = r.ReadInt16(); channel.InitialSampleHistory1 = r.ReadInt16(); channel.InitialSampleHistory2 = r.ReadInt16(); channel.LoopPredictorScale = r.ReadInt16(); channel.LoopSampleHistory1 = r.ReadInt16(); channel.LoopSampleHistory2 = r.ReadInt16(); r.Skip(2); // padding // get channel data using (MemoryStream channelStream = new MemoryStream()) { for (uint j = 0; j < blockCount; j++) { var bs = blockSize; var actualBlockSize = blockSize; if (j == blockCount - 1) { bs = sizeOfFinalBlockWithPadding; actualBlockSize = sizeOfFinalBlock; } channelStream.Write(r.GetSection(dataPointer + j * (blockSize * channelCountAgain) + bs * i, (int)actualBlockSize), 0, (int)actualBlockSize); } channel.Data = channelStream.ToArray(); channel.NibbleCount = channel.Data.Length * 2; } } dsp.LoopPoint = TimeSpan.FromMilliseconds(loopStart / (double)sampleRate * 1000).ToString(); } }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> public static DSP ToDSP(byte[] data) { DSP dsp = new DSP(); dsp.Channels.Clear(); using (BinaryReaderExt r = new BinaryReaderExt(new MemoryStream(data))) { r.BigEndian = true; if (new string(r.ReadChars(7)) != " HALPST") { throw new NotSupportedException("Invalid HPS file"); } r.ReadByte(); dsp.Frequency = r.ReadInt32(); var channelCount = r.ReadInt32(); if (channelCount != 2) { throw new NotSupportedException("Only HPS with 2 channels are currently supported"); } for (int i = 0; i < channelCount; i++) { var channel = new DSPChannel(); channel.LoopFlag = r.ReadInt16(); channel.Format = r.ReadInt16(); var SA = r.ReadInt32(); var EA = r.ReadInt32(); var CA = r.ReadInt32(); for (int k = 0; k < 0x10; k++) { channel.COEF[k] = r.ReadInt16(); } channel.Gain = r.ReadInt16(); channel.InitialPredictorScale = r.ReadInt16(); channel.InitialSampleHistory1 = r.ReadInt16(); channel.InitialSampleHistory1 = r.ReadInt16(); channel.NibbleCount = EA - CA; channel.LoopStart = SA - CA; dsp.Channels.Add(channel); } // read blocks r.Position = 0x80; Dictionary <int, int> OffsetToLoopPosition = new Dictionary <int, int>(); List <byte> channelData1 = new List <byte>(); List <byte> channelData2 = new List <byte>(); while (true) { var pos = r.Position; var length = r.ReadInt32(); var lengthMinusOne = r.ReadInt32(); var next = r.ReadInt32(); { var initPS = r.ReadInt16(); var initsh1 = r.ReadInt16(); var initsh2 = r.ReadInt16(); var gain = r.ReadInt16(); } { var initPS = r.ReadInt16(); var initsh1 = r.ReadInt16(); var initsh2 = r.ReadInt16(); var gain = r.ReadInt16(); } var extra = r.ReadInt32(); OffsetToLoopPosition.Add((int)pos, channelData1.Count * 2); channelData1.AddRange(r.ReadBytes(length / 2)); channelData2.AddRange(r.ReadBytes(length / 2)); if (next < r.Position || next == -1) { if (next != -1) { foreach (var c in dsp.Channels) { c.LoopStart = OffsetToLoopPosition[next]; } } break; } else { r.Position = (uint)next; } } dsp.Channels[0].Data = channelData1.ToArray(); dsp.Channels[1].Data = channelData2.ToArray(); } return(dsp); }
private static void ReadTrack(BinaryReaderExt r, int frameCount, int type, FOBJ_Player track, uint dataOffset, AnimNode node) { var offset = r.ReadUInt32() + dataOffset; var temp = r.Position; r.Seek(offset); int fCount = -1; float scale = 0; float[] frame = null, step = null, tan = null; if (type == 0x1) { fCount = r.ReadUInt16(); r.Skip(2); scale = r.ReadSingle(); float stepb = r.ReadSingle(); float base2 = r.ReadSingle(); frame = new float[fCount]; step = new float[fCount]; tan = new float[fCount]; for (int i = 0; i < fCount; i++) { var v = r.ReadInt32(); frame[i] = (v >> 24) & 0xFF; int th = v & 0xFFFFFF; step[i] = base2 + ((th >> 12) & 0xfff) * stepb; tan[i] = (Sign12Bit(th & 0xfff) / 32f); track.Keys.Add(new FOBJKey() { Frame = frame[i], Value = step[i], Tan = tan[i], InterpolationType = GXInterpolationType.HSD_A_OP_SPL }); } } if (type == 0x2) { fCount = r.ReadUInt16(); r.Skip(2); scale = r.ReadSingle(); float stepb = r.ReadSingle(); float base2 = r.ReadSingle(); frame = new float[fCount]; step = new float[fCount]; tan = new float[fCount]; for (int i = 0; i < fCount; i++) { frame[i] = r.ReadUInt16() / 32f; step[i] = base2 + r.ReadUInt16() * stepb; tan[i] = (r.ReadInt16() / 256f); track.Keys.Add(new FOBJKey() { Frame = frame[i], Value = step[i], Tan = tan[i], InterpolationType = GXInterpolationType.HSD_A_OP_SPL }); } } if (type == 0x3) { fCount = r.ReadUInt16(); r.Skip(2); scale = r.ReadSingle(); frame = new float[fCount]; step = new float[fCount]; tan = new float[fCount]; for (int i = 0; i < fCount; i++) { frame[i] = r.ReadSingle(); step[i] = r.ReadSingle(); tan[i] = r.ReadSingle(); track.Keys.Add(new FOBJKey() { Frame = frame[i], Value = step[i], Tan = tan[i], InterpolationType = GXInterpolationType.HSD_A_OP_SPL }); } } if (type == 0x4) { float stepb = r.ReadSingle(); float base2 = r.ReadSingle(); for (int i = 0; i < frameCount; i++) { float v = base2 + stepb * (r.ReadByte()); track.Keys.Add(new FOBJKey() { Frame = i, Value = v, InterpolationType = GXInterpolationType.HSD_A_OP_LIN }); } } if (type == 0x6) { for (int i = 0; i < frameCount; i++) { float v = r.ReadSingle(); track.Keys.Add(new FOBJKey() { Frame = i, Value = v, InterpolationType = GXInterpolationType.HSD_A_OP_LIN }); } } r.Seek(temp); }