diff --git a/BaseHandlers/AptData.cs b/BaseHandlers/AptData.cs index df6442f..c638e8e 100644 --- a/BaseHandlers/AptData.cs +++ b/BaseHandlers/AptData.cs @@ -304,7 +304,7 @@ public class Character // Data public CharacterData Data; - private static Frame ReadFrame(BinaryReader br, uint dataStart) + private static Frame ReadFrame(BinaryReader2 br, uint dataStart) { Frame frame = new Frame(); @@ -435,7 +435,7 @@ private static Frame ReadFrame(BinaryReader br, uint dataStart) return frame; } - public static Character Read(BinaryReader br, uint dataStart) + public static Character Read(BinaryReader2 br, uint dataStart) { Character result = new Character(); @@ -669,7 +669,7 @@ public static Character Read(BinaryReader br, uint dataStart) return result; } - public void Write(BinaryWriter bw) + public void Write(BinaryWriter2 bw) { bw.Write((uint)Type); bw.Write(Signature); @@ -779,7 +779,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; // TODO: Write diff --git a/BaseHandlers/IDList.cs b/BaseHandlers/IDList.cs index 25aa408..139b247 100644 --- a/BaseHandlers/IDList.cs +++ b/BaseHandlers/IDList.cs @@ -66,7 +66,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; bw.Write(ReferenceEntryIDOffset); bw.Write(Unknown2); diff --git a/BaseHandlers/InstanceList.cs b/BaseHandlers/InstanceList.cs index afb274c..628c642 100644 --- a/BaseHandlers/InstanceList.cs +++ b/BaseHandlers/InstanceList.cs @@ -25,7 +25,7 @@ public class ModelInstance public Vector4 Rotation => Transform.ExtractRotation().ToAxisAngle(); - public static ModelInstance Read(BinaryReader br) + public static ModelInstance Read(BinaryReader2 br) { ModelInstance result = new ModelInstance(); @@ -38,7 +38,7 @@ public static ModelInstance Read(BinaryReader br) return result; } - public void Write(BinaryWriter bw) + public void Write(BinaryWriter2 bw) { bw.Write(ModelEntryPtr); bw.Write(Unknown2); @@ -120,7 +120,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; bw.Write(Unknown1); bw.Write(Instances.Count); diff --git a/BaseHandlers/ProgressionData.cs b/BaseHandlers/ProgressionData.cs index 743e7cb..c88ec2d 100644 --- a/BaseHandlers/ProgressionData.cs +++ b/BaseHandlers/ProgressionData.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using BundleFormat; @@ -603,7 +603,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; // TODO: Write diff --git a/BaseHandlers/Renderable.cs b/BaseHandlers/Renderable.cs index dd07fc7..b55c50f 100644 --- a/BaseHandlers/Renderable.cs +++ b/BaseHandlers/Renderable.cs @@ -128,7 +128,7 @@ public void BuildModel() Model = new Model(meshes); } - public VertexData ReadVertex(BinaryReader br, VertexAttribute attribute) + public VertexData ReadVertex(BinaryReader2 br, VertexAttribute attribute) { switch (attribute.Size) // We don't use type here. This is wrong tbh. { @@ -196,7 +196,7 @@ public VertexData ReadVertex(BinaryReader br, VertexAttribute attribute) return new VertexData(); } - public void ReadBody(BinaryReader br) + public void ReadBody(BinaryReader2 br) { for (int i = 0; i < NumIndices; i++) { diff --git a/BaseHandlers/StreetData.cs b/BaseHandlers/StreetData.cs index 2480306..3fd1c2f 100644 --- a/BaseHandlers/StreetData.cs +++ b/BaseHandlers/StreetData.cs @@ -263,7 +263,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; bw.Write(Unknown1); long fileSizeOffset = bw.BaseStream.Position; diff --git a/BaseHandlers/TriggerData.cs b/BaseHandlers/TriggerData.cs index 4bd34fe..3e054f2 100644 --- a/BaseHandlers/TriggerData.cs +++ b/BaseHandlers/TriggerData.cs @@ -7,6 +7,7 @@ using System.ComponentModel.Design; using System.Drawing.Design; using System.IO; +using System.Numerics; using System.Windows.Forms; namespace BaseHandlers @@ -56,14 +57,14 @@ public byte[] ToByteArray() return BitConverter.GetBytes(m_id); } - public void Read(BinaryReader reader) + public void Read(BinaryReader2 br) { - m_id = reader.ReadUInt64(); + m_id = br.ReadUInt64(); } - public void Write(BinaryWriter writer) + public void Write(BinaryWriter2 bw) { - writer.Write(m_id); + bw.Write(m_id); } public override string ToString() @@ -74,46 +75,48 @@ public override string ToString() public class StartingGrid { - public List StartingPositions { get; set; } = new List(); - public List StartingDirections { get; set; } = new List(); + public List StartingPositions { get; set; } = new List(); + public List StartingDirections { get; set; } = new List(); - public void Read(BinaryReader reader) + public void Read(BinaryReader2 br) { - StartingPositions = new List(8); + StartingPositions = new List(8); for (int i = 0; i < 8; i++) { - StartingPositions[i] = new Vector3I( - reader.ReadSingle(), - reader.ReadSingle(), - reader.ReadSingle(), reader.ReadSingle()); + StartingPositions[i] = new Vector4( + br.ReadSingle(), + br.ReadSingle(), + br.ReadSingle(), + br.ReadSingle()); } - StartingDirections = new List(8); + StartingDirections = new List(8); for (int i = 0; i < 8; i++) { - StartingDirections[i] = new Vector3I( - reader.ReadSingle(), - reader.ReadSingle(), - reader.ReadSingle(), reader.ReadSingle()); + StartingDirections[i] = new Vector4( + br.ReadSingle(), + br.ReadSingle(), + br.ReadSingle(), + br.ReadSingle()); } } - public void Write(BinaryWriter writer) + public void Write(BinaryWriter2 bw) { for (int i = 0; i < 8; i++) { - writer.Write(StartingPositions[i].X); - writer.Write(StartingPositions[i].Y); - writer.Write(StartingPositions[i].Z); - writer.Write(StartingPositions[i].S); + bw.Write(StartingPositions[i].X); + bw.Write(StartingPositions[i].Y); + bw.Write(StartingPositions[i].Z); + bw.Write(StartingPositions[i].W); } for (int i = 0; i < 8; i++) { - writer.Write(StartingDirections[i].X); - writer.Write(StartingDirections[i].Y); - writer.Write(StartingDirections[i].Z); - writer.Write(StartingDirections[i].S); + bw.Write(StartingDirections[i].X); + bw.Write(StartingDirections[i].Y); + bw.Write(StartingDirections[i].Z); + bw.Write(StartingDirections[i].W); } } } @@ -130,30 +133,30 @@ public class BoxRegion public float DimensionY { get; set; } = 0; public float DimensionZ { get; set; } = 0; - public void Read(BinaryReader reader) + public void Read(BinaryReader2 br) { - PositionX = reader.ReadSingle(); - PositionY = reader.ReadSingle(); - PositionZ = reader.ReadSingle(); - RotationX = reader.ReadSingle(); - RotationY = reader.ReadSingle(); - RotationZ = reader.ReadSingle(); - DimensionX = reader.ReadSingle(); - DimensionY = reader.ReadSingle(); - DimensionZ = reader.ReadSingle(); + PositionX = br.ReadSingle(); + PositionY = br.ReadSingle(); + PositionZ = br.ReadSingle(); + RotationX = br.ReadSingle(); + RotationY = br.ReadSingle(); + RotationZ = br.ReadSingle(); + DimensionX = br.ReadSingle(); + DimensionY = br.ReadSingle(); + DimensionZ = br.ReadSingle(); } - public void Write(BinaryWriter writer) + public void Write(BinaryWriter2 bw) { - writer.Write(PositionX); - writer.Write(PositionY); - writer.Write(PositionZ); - writer.Write(RotationX); - writer.Write(RotationY); - writer.Write(RotationZ); - writer.Write(DimensionX); - writer.Write(DimensionY); - writer.Write(DimensionZ); + bw.Write(PositionX); + bw.Write(PositionY); + bw.Write(PositionZ); + bw.Write(RotationX); + bw.Write(RotationY); + bw.Write(RotationZ); + bw.Write(DimensionX); + bw.Write(DimensionY); + bw.Write(DimensionZ); } } @@ -174,24 +177,24 @@ public enum RegionType public RegionType meType { get; set; } = RegionType.E_TYPE_LANDMARK; private byte[] muPad { get; set; } = new byte[1]; - public virtual void Read(BinaryReader reader) + public virtual void Read(BinaryReader2 br) { mBoxRegion = new BoxRegion(); - mBoxRegion.Read(reader); - mId = reader.ReadInt32(); - miRegionIndex = reader.ReadInt16(); - meType = (RegionType)reader.ReadByte(); - muPad = reader.ReadBytes(1); + mBoxRegion.Read(br); + mId = br.ReadInt32(); + miRegionIndex = br.ReadInt16(); + meType = (RegionType)br.ReadByte(); + muPad = br.ReadBytes(1); } - public virtual void Write(BinaryWriter writer) + public virtual void Write(BinaryWriter2 bw) { - mBoxRegion.Write(writer); + mBoxRegion.Write(bw); - writer.Write(mId); - writer.Write(miRegionIndex); - writer.Write((byte)meType); - writer.Write(muPad); + bw.Write(mId); + bw.Write(miRegionIndex); + bw.Write((byte)meType); + bw.Write(muPad); } } @@ -210,47 +213,47 @@ public Landmark() : base() public byte muDistrict { get; set; } = 0; public byte mu8Flags { get; set; } = 0; - public new void Read(BinaryReader reader) + public new void Read(BinaryReader2 br) { - base.Read(reader); - long startingGridOffset = reader.ReadUInt32(); - int miStartingGridCount = reader.ReadByte(); - muDesignIndex = reader.ReadByte(); - muDistrict = reader.ReadByte(); - mu8Flags = reader.ReadByte(); + base.Read(br); + long startingGridOffset = br.ReadUInt32(); + int miStartingGridCount = br.ReadByte(); + muDesignIndex = br.ReadByte(); + muDistrict = br.ReadByte(); + mu8Flags = br.ReadByte(); - long currentPosition = reader.BaseStream.Position; - reader.BaseStream.Position = startingGridOffset; + long currentPosition = br.BaseStream.Position; + br.BaseStream.Position = startingGridOffset; for (int i = 0; i < miStartingGridCount; i++) { StartingGrid startingGrid = new StartingGrid(); - startingGrid.Read(reader); + startingGrid.Read(br); mpaStartingGrids.Add(startingGrid); } - reader.BaseStream.Position = currentPosition; + br.BaseStream.Position = currentPosition; } - public new void Write(BinaryWriter writer) + public new void Write(BinaryWriter2 bw) { - base.Write(writer); - startingGridOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write((byte)mpaStartingGrids.Count); - writer.Write(muDesignIndex); - writer.Write(muDistrict); - writer.Write(mu8Flags); + base.Write(bw); + startingGridOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write((byte)mpaStartingGrids.Count); + bw.Write(muDesignIndex); + bw.Write(muDistrict); + bw.Write(mu8Flags); } - public void WriteStartingGrid(BinaryWriter writer){ + public void WriteStartingGrid(BinaryWriter2 bw){ - long currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = startingGridOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + long currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = startingGridOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; foreach (StartingGrid grid in mpaStartingGrids) { - grid.Write(writer); + grid.Write(bw); } } } @@ -317,28 +320,28 @@ public GenericRegion() : base() public Type GenericRegionType { get; set; } = Type.E_TYPE_JUNK_YARD; public sbyte IsOneWay { get; set; } = 0; - public new void Read(BinaryReader reader) + public new void Read(BinaryReader2 br) { - base.Read(reader); - GroupID = reader.ReadInt32(); - CameraCut1 = reader.ReadInt16(); - CameraCut2 = reader.ReadInt16(); - CameraType1 = (StuntCameraType)reader.ReadSByte(); - CameraType2 = (StuntCameraType)reader.ReadSByte(); - GenericRegionType = (Type)reader.ReadByte(); - IsOneWay = reader.ReadSByte(); + base.Read(br); + GroupID = br.ReadInt32(); + CameraCut1 = br.ReadInt16(); + CameraCut2 = br.ReadInt16(); + CameraType1 = (StuntCameraType)br.ReadSByte(); + CameraType2 = (StuntCameraType)br.ReadSByte(); + GenericRegionType = (Type)br.ReadByte(); + IsOneWay = br.ReadSByte(); } - public new void Write(BinaryWriter writer) + public new void Write(BinaryWriter2 bw) { - base.Write(writer); - writer.Write(GroupID); - writer.Write(CameraCut1); - writer.Write(CameraCut2); - writer.Write((sbyte)CameraType1); - writer.Write((sbyte)CameraType2); - writer.Write((byte)GenericRegionType); - writer.Write(IsOneWay); + base.Write(bw); + bw.Write(GroupID); + bw.Write(CameraCut1); + bw.Write(CameraCut2); + bw.Write((sbyte)CameraType1); + bw.Write((sbyte)CameraType2); + bw.Write((byte)GenericRegionType); + bw.Write(IsOneWay); } } @@ -359,22 +362,22 @@ public Blackspot() : base() public ScoreType muScoreType { get; set; } = ScoreType.E_SCORE_TYPE_DISTANCE; public int miScoreAmount { get; set; } = 0; - public override void Read(BinaryReader reader) + public override void Read(BinaryReader2 br) { - base.Read(reader); + base.Read(br); - muScoreType = (ScoreType)reader.ReadByte(); - reader.ReadBytes(3); // Padding - miScoreAmount = reader.ReadInt32(); + muScoreType = (ScoreType)br.ReadByte(); + br.ReadBytes(3); // Padding + miScoreAmount = br.ReadInt32(); } - public override void Write(BinaryWriter writer) + public override void Write(BinaryWriter2 bw) { - base.Write(writer); + base.Write(bw); - writer.Write((byte)muScoreType); - writer.Write(new byte[3]); // Padding - writer.Write(miScoreAmount); + bw.Write((byte)muScoreType); + bw.Write(new byte[3]); // Padding + bw.Write(miScoreAmount); } } @@ -387,84 +390,84 @@ public class Killzone private long TriggerOffsetPosition = 0; private long CGSIDOffsetPosition = 0; - public void Read(BinaryReader reader) + public void Read(BinaryReader2 br) { // Read the trigger pointer array - long genericRegionOffset = reader.ReadUInt32(); - int TriggerCount = reader.ReadInt32(); + long genericRegionOffset = br.ReadUInt32(); + int TriggerCount = br.ReadInt32(); // Read the region ID array - long cgsOffset = reader.ReadUInt32(); - int RegionIdCount = reader.ReadInt32(); - long currentPosition = reader.BaseStream.Position; + long cgsOffset = br.ReadUInt32(); + int RegionIdCount = br.ReadInt32(); + long currentPosition = br.BaseStream.Position; - reader.BaseStream.Position = genericRegionOffset; + br.BaseStream.Position = genericRegionOffset; uint[] Triggers = new uint[TriggerCount]; for (int i = 0; i < TriggerCount; i++) { - Triggers[i] = reader.ReadUInt32(); + Triggers[i] = br.ReadUInt32(); } TriggerIds = new List(); foreach (uint trigger in Triggers) { - reader.BaseStream.Position = trigger; + br.BaseStream.Position = trigger; GenericRegion region = new GenericRegion(); - region.Read(reader); + region.Read(br); TriggerIds.Add(region.mId); } - reader.BaseStream.Position = cgsOffset; + br.BaseStream.Position = cgsOffset; RegionIds = new List(); for (int i = 0; i < RegionIdCount; i++) { CgsID id = new CgsID(); - id.Read(reader); + id.Read(br); RegionIds.Add(id); } - reader.BaseStream.Position = currentPosition; + br.BaseStream.Position = currentPosition; } - public void Write(BinaryWriter writer) + public void Write(BinaryWriter2 bw) { - TriggerOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(TriggerIds.Count); - CGSIDOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(RegionIds.Count); + TriggerOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(TriggerIds.Count); + CGSIDOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(RegionIds.Count); } - public void WritePointerStuff(BinaryWriter writer, Dictionary genericRegionOffsets) { + public void WritePointerStuff(BinaryWriter2 bw, Dictionary genericRegionOffsets) { - long currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = TriggerOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + long currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = TriggerOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; foreach (int trigger in TriggerIds) { - writer.Write(genericRegionOffsets[trigger]); + bw.Write(genericRegionOffsets[trigger]); } - long paddingCount = 16 - (writer.BaseStream.Position % 16); + long paddingCount = 16 - (bw.BaseStream.Position % 16); if (paddingCount < 16) { for (int i = 0; i < paddingCount; i++) - writer.Write((byte)0); + bw.Write((byte)0); } - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = CGSIDOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = CGSIDOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; foreach (CgsID ids in RegionIds) { - ids.Write(writer); + ids.Write(bw); } - paddingCount = 16 - (writer.BaseStream.Position % 16); + paddingCount = 16 - (bw.BaseStream.Position % 16); if (paddingCount < 16) { for (int i = 0; i < paddingCount; i++) - writer.Write((byte)0); + bw.Write((byte)0); } } } @@ -478,52 +481,52 @@ public class SignatureStunt private long StuntElementOffsetPosition = 0; - public void Read(BinaryReader reader) + public void Read(BinaryReader2 br) { mId = new CgsID(); - mId.Read(reader); - miCamera = reader.ReadInt64(); + mId.Read(br); + miCamera = br.ReadInt64(); - uint mppStuntElementsOffset = reader.ReadUInt32(); - int miStuntElementCount = reader.ReadInt32(); + uint mppStuntElementsOffset = br.ReadUInt32(); + int miStuntElementCount = br.ReadInt32(); - long currentPosition = reader.BaseStream.Position; - reader.BaseStream.Position = mppStuntElementsOffset; + long currentPosition = br.BaseStream.Position; + br.BaseStream.Position = mppStuntElementsOffset; uint[] Triggers = new uint[miStuntElementCount]; for (int i = 0; i < miStuntElementCount; i++) { - Triggers[i] = reader.ReadUInt32(); + Triggers[i] = br.ReadUInt32(); } stuntElementRegions = new List(); foreach (uint trigger in Triggers) { - reader.BaseStream.Position = trigger; + br.BaseStream.Position = trigger; GenericRegion region = new GenericRegion(); - region.Read(reader); + region.Read(br); stuntElementRegions.Add(region.mId); } - reader.BaseStream.Position = currentPosition; + br.BaseStream.Position = currentPosition; } // Write something in triggerIds, because we dont have the actual positions yet - public void Write(BinaryWriter writer) + public void Write(BinaryWriter2 bw) { - mId.Write(writer); - writer.Write(miCamera); - StuntElementOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(stuntElementRegions.Count); + mId.Write(bw); + bw.Write(miCamera); + StuntElementOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(stuntElementRegions.Count); } - public void WriteStuntElements(BinaryWriter writer, Dictionary genericRegionOffsets) { - long currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = StuntElementOffsetPosition; - writer.Write(currentPosition); - writer.BaseStream.Position = currentPosition; + public void WriteStuntElements(BinaryWriter2 bw, Dictionary genericRegionOffsets) { + long currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = StuntElementOffsetPosition; + bw.Write(currentPosition); + bw.BaseStream.Position = currentPosition; foreach (int trigger in stuntElementRegions) { - writer.Write(genericRegionOffsets[trigger]); + bw.Write(genericRegionOffsets[trigger]); } } } @@ -531,31 +534,31 @@ public void WriteStuntElements(BinaryWriter writer, Dictionary generi public class RoamingLocation { [TypeConverter(typeof(ExpandableObjectConverter))] - public Vector3I Position { get; set; } = new Vector3I(0,0,0,0); + public Vector4 Position { get; set; } = new Vector4(0,0,0,0); public byte DistrictIndex { get; set; } = 0; - public void Read(BinaryReader reader) + public void Read(BinaryReader2 br) { - Position = new Vector3I( - reader.ReadSingle(), - reader.ReadSingle(), - reader.ReadSingle(), - reader.ReadSingle()); - DistrictIndex = reader.ReadByte(); + Position = new Vector4( + br.ReadSingle(), + br.ReadSingle(), + br.ReadSingle(), + br.ReadSingle()); + DistrictIndex = br.ReadByte(); // Read and discard padding - reader.ReadBytes(15); + br.ReadBytes(15); } - public void Write(BinaryWriter writer) + public void Write(BinaryWriter2 bw) { - writer.Write(Position.X); - writer.Write(Position.Y); - writer.Write(Position.Z); - writer.Write(Position.S); - writer.Write(DistrictIndex); + bw.Write(Position.X); + bw.Write(Position.Y); + bw.Write(Position.Z); + bw.Write(Position.W); + bw.Write(DistrictIndex); // Write padding - writer.Write(new byte[15]); + bw.Write(new byte[15]); } } @@ -567,14 +570,14 @@ public VFXBoxRegion() : base() meType = RegionType.E_TYPE_VFXBOX_REGION; } - public override void Read(BinaryReader reader) + public override void Read(BinaryReader2 br) { - base.Read(reader); + base.Read(br); } - public override void Write(BinaryWriter writer) + public override void Write(BinaryWriter2 bw) { - base.Write(writer); + base.Write(bw); } } @@ -589,37 +592,37 @@ public enum Type } [TypeConverter(typeof(ExpandableObjectConverter))] - public Vector3I mPosition { get; set; } = new Vector3I(0, 0, 0, 0); + public Vector4 mPosition { get; set; } = new Vector4(0, 0, 0, 0); [TypeConverter(typeof(ExpandableObjectConverter))] - public Vector3I mDirection { get; set; } = new Vector3I(0, 0, 0, 0); + public Vector4 mDirection { get; set; } = new Vector4(0, 0, 0, 0); [TypeConverter(typeof(ExpandableObjectConverter))] public CgsID mJunkyardId { get; set; } = new CgsID(); public Type muType { get; set; } = 0; private byte[] padding = new byte[7]; - public void Read(BinaryReader reader) + public void Read(BinaryReader2 br) { - mPosition = new Vector3I(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); - mDirection = new Vector3I(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); + mPosition = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + mDirection = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); mJunkyardId = new CgsID(); - mJunkyardId.Read(reader); - muType = (Type)reader.ReadByte(); - padding = reader.ReadBytes(7); + mJunkyardId.Read(br); + muType = (Type)br.ReadByte(); + padding = br.ReadBytes(7); } - public void Write(BinaryWriter writer) + public void Write(BinaryWriter2 bw) { - writer.Write(mPosition.X); - writer.Write(mPosition.Y); - writer.Write(mPosition.Z); - writer.Write(mPosition.S); - writer.Write(mDirection.X); - writer.Write(mDirection.Y); - writer.Write(mDirection.Z); - writer.Write(mDirection.S); - mJunkyardId.Write(writer); - writer.Write((byte)muType); - writer.Write(padding); + bw.Write(mPosition.X); + bw.Write(mPosition.Y); + bw.Write(mPosition.Z); + bw.Write(mPosition.W); + bw.Write(mDirection.X); + bw.Write(mDirection.Y); + bw.Write(mDirection.Z); + bw.Write(mDirection.W); + mJunkyardId.Write(bw); + bw.Write((byte)muType); + bw.Write(padding); } } @@ -627,8 +630,8 @@ public class TriggerData : IEntryData { public int miVersionNumber { get; set; } public uint muSize { get; set; } - public Vector3I mPlayerStartPosition { get; set; } - public Vector3I mPlayerStartDirection { get; set; } + public Vector4 mPlayerStartPosition { get; set; } + public Vector4 mPlayerStartDirection { get; set; } public List mpLandmarks { get; set; } public int miOnlineLandmarkCount { get; set; } public List mpSignatureStunts { get; set; } @@ -651,8 +654,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) miVersionNumber = reader.ReadInt32(); muSize = reader.ReadUInt32(); reader.ReadBytes(8);// skip 8 bytes of padding - mPlayerStartPosition = new Vector3I(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); - mPlayerStartDirection = new Vector3I(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); + mPlayerStartPosition = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); + mPlayerStartDirection = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); // read landmarks long LandmarkTriggersOffset = reader.ReadUInt32(); int miLandmarkCount = reader.ReadInt32(); @@ -797,172 +800,173 @@ public EntryType GetEntryType(BundleEntry entry) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter writer = new BinaryWriter(ms); - writer.Write(miVersionNumber); - long SizePosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.WriteUniquePadding(8); //padding - writer.Write(mPlayerStartPosition.X); - writer.Write(mPlayerStartPosition.Y); - writer.Write(mPlayerStartPosition.Z); - writer.Write(mPlayerStartPosition.S); - writer.Write(mPlayerStartDirection.X); - writer.Write(mPlayerStartDirection.Y); - writer.Write(mPlayerStartDirection.Z); - writer.Write(mPlayerStartDirection.S); - - long LandmarkOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpLandmarks.Count); - writer.Write(miOnlineLandmarkCount); - long SignatureStuntskOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpSignatureStunts.Count); - long GenericRegionsOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpGenericRegions.Count); - long KillzoneOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpKillzones.Count); - long BlackspotOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpBlackspots.Count); - long VFXBoxRegionsOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpVFXBoxRegions.Count); - long RoamingLocationsOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpRoamingLocations.Count); - long SpawnLocationsOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpSpawnLocations.Count); - long TriggerOffsetPosition = writer.BaseStream.Position; - writer.WriteUniquePadding(4); - writer.Write(mpLandmarks.Count + mpGenericRegions.Count + mpBlackspots.Count + mpVFXBoxRegions.Count); - writer.WriteUniquePadding(4); // padding - - long currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = LandmarkOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; + bw.Write(miVersionNumber); + long SizePosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.WriteUniquePadding(8); //padding + bw.Write(mPlayerStartPosition.X); + bw.Write(mPlayerStartPosition.Y); + bw.Write(mPlayerStartPosition.Z); + bw.Write(mPlayerStartPosition.W); + bw.Write(mPlayerStartDirection.X); + bw.Write(mPlayerStartDirection.Y); + bw.Write(mPlayerStartDirection.Z); + bw.Write(mPlayerStartDirection.W); + + long LandmarkOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpLandmarks.Count); + bw.Write(miOnlineLandmarkCount); + long SignatureStuntskOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpSignatureStunts.Count); + long GenericRegionsOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpGenericRegions.Count); + long KillzoneOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpKillzones.Count); + long BlackspotOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpBlackspots.Count); + long VFXBoxRegionsOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpVFXBoxRegions.Count); + long RoamingLocationsOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpRoamingLocations.Count); + long SpawnLocationsOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpSpawnLocations.Count); + long TriggerOffsetPosition = bw.BaseStream.Position; + bw.WriteUniquePadding(4); + bw.Write(mpLandmarks.Count + mpGenericRegions.Count + mpBlackspots.Count + mpVFXBoxRegions.Count); + bw.WriteUniquePadding(4); // padding + + long currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = LandmarkOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; List landmarkOffsets = new List(); foreach (Landmark landmark in mpLandmarks) { - landmarkOffsets.Add((uint)writer.BaseStream.Position); - landmark.Write(writer); + landmarkOffsets.Add((uint)bw.BaseStream.Position); + landmark.Write(bw); } - writer.WritePadding(); + bw.WritePadding(); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = SignatureStuntskOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = SignatureStuntskOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; foreach (SignatureStunt stunt in mpSignatureStunts) { - stunt.Write(writer); + stunt.Write(bw); } - writer.WritePadding(); + bw.WritePadding(); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = GenericRegionsOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = GenericRegionsOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; Dictionary genericRegionOffsets = new Dictionary(); foreach (GenericRegion region in mpGenericRegions) { - genericRegionOffsets.Add(region.mId, (uint)writer.BaseStream.Position); - region.Write(writer); + genericRegionOffsets.Add(region.mId, (uint)bw.BaseStream.Position); + region.Write(bw); } - writer.WritePadding(); + bw.WritePadding(); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = KillzoneOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = KillzoneOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; foreach (Killzone killzone in mpKillzones) { - killzone.Write(writer); + killzone.Write(bw); }; - writer.WritePadding(); + bw.WritePadding(); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = BlackspotOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = BlackspotOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; List blackspotOffsets = new List(); foreach (Blackspot blackspot in mpBlackspots) { - blackspotOffsets.Add((uint)writer.BaseStream.Position); - blackspot.Write(writer); + blackspotOffsets.Add((uint)bw.BaseStream.Position); + blackspot.Write(bw); } - writer.WritePadding(); + bw.WritePadding(); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = VFXBoxRegionsOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = VFXBoxRegionsOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; List vfxBoxRegionOffsets = new List(); foreach (VFXBoxRegion region in mpVFXBoxRegions) { - vfxBoxRegionOffsets.Add((uint)writer.BaseStream.Position); - region.Write(writer); + vfxBoxRegionOffsets.Add((uint)bw.BaseStream.Position); + region.Write(bw); } - writer.WritePadding(); + bw.WritePadding(); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = RoamingLocationsOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = RoamingLocationsOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; foreach (RoamingLocation location in mpRoamingLocations) { - location.Write(writer); + location.Write(bw); } - writer.WritePadding(); + bw.WritePadding(); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = SpawnLocationsOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = SpawnLocationsOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; foreach (SpawnLocation location in mpSpawnLocations) { - location.Write(writer); + location.Write(bw); } - writer.WritePadding(); + bw.WritePadding(); foreach (Landmark land in mpLandmarks) { - land.WriteStartingGrid(writer); + land.WriteStartingGrid(bw); } foreach (SignatureStunt stunt in mpSignatureStunts) { - stunt.WriteStuntElements(writer, genericRegionOffsets); + stunt.WriteStuntElements(bw, genericRegionOffsets); } foreach (Killzone killzone in mpKillzones) { - killzone.WritePointerStuff(writer, genericRegionOffsets); + killzone.WritePointerStuff(bw, genericRegionOffsets); }; - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = TriggerOffsetPosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; - foreach (uint region in vfxBoxRegionOffsets) writer.Write(region); - foreach (uint region in blackspotOffsets) writer.Write(region); - foreach (uint region in genericRegionOffsets.Values) writer.Write(region); - foreach (uint region in landmarkOffsets) writer.Write(region); + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = TriggerOffsetPosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; + foreach (uint region in vfxBoxRegionOffsets) bw.Write(region); + foreach (uint region in blackspotOffsets) bw.Write(region); + foreach (uint region in genericRegionOffsets.Values) bw.Write(region); + foreach (uint region in landmarkOffsets) bw.Write(region); - currentPosition = writer.BaseStream.Position; - writer.BaseStream.Position = SizePosition; - writer.Write((uint)currentPosition); - writer.BaseStream.Position = currentPosition; - writer.WritePadding(); + currentPosition = bw.BaseStream.Position; + bw.BaseStream.Position = SizePosition; + bw.Write((uint)currentPosition); + bw.BaseStream.Position = currentPosition; + bw.WritePadding(); - writer.Flush(); + bw.Flush(); byte[] data = ms.ToArray(); - writer.Close(); + bw.Close(); ms.Close(); entry.EntryBlocks[0].Data = data; diff --git a/BundleFormat/BundleArchive.cs b/BundleFormat/BundleArchive.cs index e18847f..674138a 100644 --- a/BundleFormat/BundleArchive.cs +++ b/BundleFormat/BundleArchive.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -429,7 +430,7 @@ private bool ReadBND1(BinaryReader2 br) br2.BigEndian = false; // TODO: Store only the debug info and not the full string - ResourceStringTable = br2.ReadLenString(); + ResourceStringTable = br2.ReadLenString(br2.ReadInt32()); br2.Close(); @@ -454,7 +455,7 @@ private bool ReadBND2(BinaryReader2 br) int platform = br.ReadInt32(); if (platform != 1) - platform = Util.ReverseBytes(platform); + platform = BinaryPrimitives.ReverseEndianness(platform); Platform = (BundlePlatform)platform; br.BigEndian = Console; @@ -568,7 +569,8 @@ private bool ReadBND2(BinaryReader2 br) public void Write(string path) { Stream s = File.Open(path, FileMode.Create); - BinaryWriter bw = new BinaryWriter(s); + BinaryWriter2 bw = new BinaryWriter2(s); + bw.BigEndian = Console; Write(bw); @@ -576,19 +578,19 @@ public void Write(string path) bw.Close(); } - public void Write(BinaryWriter bw) + public void Write(BinaryWriter2 bw) { - bw.Write(BundleArchive.BND2Magic); - bw.Write(this.Version); - bw.Write((int)this.Platform); + bw.Write(BND2Magic); + bw.Write(Version); + bw.Write((int)Platform); long rstOffset = bw.BaseStream.Position; - bw.Write((int)0); + bw.Write(0); - bw.Write(this.Entries.Count); + bw.Write(Entries.Count); long idBlockOffsetPos = bw.BaseStream.Position; - bw.Write((int)0); + bw.Write(0); long[] fileBlockOffsetsPos = new long[3]; @@ -598,7 +600,7 @@ public void Write(BinaryWriter bw) bw.BaseStream.Position += 4; } - bw.Write((int)this.Flags); + bw.Write((int)Flags); bw.Align(16); @@ -607,10 +609,10 @@ public void Write(BinaryWriter bw) bw.Write((uint)currentOffset); bw.BaseStream.Position = currentOffset; - if (this.Flags.HasFlag(Flags.HasResourceStringTable)) + if (Flags.HasFlag(Flags.HasResourceStringTable)) { // TODO: Rebuild RST from DebugInfo - bw.WriteCStr(this.ResourceStringTable); + bw.WriteCStr(ResourceStringTable); bw.Align(16); } @@ -678,11 +680,11 @@ public void Write(BinaryWriter bw) bw.Write((uint)blockStart); bw.BaseStream.Position = blockStart; - for (int j = 0; j < this.Entries.Count; j++) + for (int j = 0; j < Entries.Count; j++) { - BundleEntry entry = this.Entries[j]; + BundleEntry entry = Entries[j]; EntryBlock entryBlock = entry.EntryBlocks[i]; - bool compressed = this.Flags.HasFlag(Flags.Compressed); + bool compressed = Flags.HasFlag(Flags.Compressed); uint size = compressed ? (compressedBlocks[j][i] == null ? 0 : (uint)compressedBlocks[j][i].Length) : (uint)entryBlock.Data.Length; if (size > 0) @@ -697,7 +699,7 @@ public void Write(BinaryWriter bw) else bw.Write(entryBlock.Data); - bw.Align((i != 0 && j != this.Entries.Count - 1) ? (byte)0x80 : (byte)16); + bw.Align((i != 0 && j != Entries.Count - 1) ? (byte)0x80 : (byte)16); } } @@ -706,6 +708,14 @@ public void Write(BinaryWriter bw) } } + public static bool VerifyMagic(BinaryReader2 br, byte[] magic) + { + byte[] readMagic = br.ReadBytes(magic.Length); + if (readMagic.Matches(magic)) + return true; + return false; + } + public static bool IsBundle(string path) { bool result; @@ -715,7 +725,7 @@ public static bool IsBundle(string path) Stream s = File.Open(path, FileMode.Open, FileAccess.Read); BinaryReader2 br = new BinaryReader2(s); - result = br.VerifyMagic(BND2Magic); + result = VerifyMagic(br, BND2Magic); br.Close(); s.Close(); @@ -736,7 +746,7 @@ public static List GetEntryIDs(string path, bool console = false) Stream s = File.Open(path, FileMode.Open, FileAccess.Read); BinaryReader2 br = new BinaryReader2(s); - if (!br.VerifyMagic(BND2Magic)) + if (!VerifyMagic(br, BND2Magic)) { br.Close(); s.Close(); @@ -747,7 +757,7 @@ public static List GetEntryIDs(string path, bool console = false) int platformInt = br.ReadInt32(); if (platformInt != 1) - platformInt = Util.ReverseBytes(platformInt); + platformInt = BinaryPrimitives.ReverseEndianness(platformInt); BundlePlatform platform = (BundlePlatform)platformInt; br.BigEndian = platform == BundlePlatform.X360 || platform == BundlePlatform.PS3; @@ -778,7 +788,7 @@ public static List GetEntryInfos(string path, bool console = false) Stream s = File.Open(path, FileMode.Open, FileAccess.Read); BinaryReader2 br = new BinaryReader2(s); - if (!br.VerifyMagic(BND2Magic)) + if (!VerifyMagic(br, BND2Magic)) { br.Close(); s.Close(); @@ -789,7 +799,7 @@ public static List GetEntryInfos(string path, bool console = false) int platformInt = br.ReadInt32(); if (platformInt != 1) - platformInt = Util.ReverseBytes(platformInt); + platformInt = BinaryPrimitives.ReverseEndianness(platformInt); BundlePlatform platform = (BundlePlatform)platformInt; br.BigEndian = platform == BundlePlatform.X360 || platform == BundlePlatform.PS3; @@ -844,7 +854,7 @@ public static EntryType GetEntryType(string path, uint id, bool console = false) Stream s = File.Open(path, FileMode.Open, FileAccess.Read); BinaryReader2 br = new BinaryReader2(s); - if (!br.VerifyMagic(BND2Magic)) + if (!VerifyMagic(br, BND2Magic)) { timer.StopLog(); br.Close(); @@ -856,7 +866,7 @@ public static EntryType GetEntryType(string path, uint id, bool console = false) int platformInt = br.ReadInt32(); if (platformInt != 1) - platformInt = Util.ReverseBytes(platformInt); + platformInt = BinaryPrimitives.ReverseEndianness(platformInt); BundlePlatform platform = (BundlePlatform)platformInt; br.BigEndian = platform == BundlePlatform.X360 || platform == BundlePlatform.PS3; diff --git a/BundleFormat/Extensions.cs b/BundleFormat/Extensions.cs index 4d4358c..0ce0d8a 100644 --- a/BundleFormat/Extensions.cs +++ b/BundleFormat/Extensions.cs @@ -1,5 +1,8 @@ using System; +using System.Buffers.Binary; +using System.Collections.Generic; using System.IO; +using System.Numerics; using System.Text; using System.Windows.Forms; using Ionic.Zlib; @@ -115,28 +118,24 @@ public static bool Matches(this byte[] self, byte[] other) return true; } - public static byte Peek(this BinaryReader self) + public static byte[] toBytes(this Vector4 vec, bool swap = false) { - byte b = self.ReadByte(); - self.BaseStream.Seek(-1, SeekOrigin.Current); - return b; - } - - public static bool VerifyMagic(this BinaryReader self, byte[] magic) - { - byte[] readMagic = self.ReadBytes(magic.Length); - if (readMagic.Matches(magic)) - return true; - return false; - } - - public static void Align(this BinaryWriter self, byte alignment) - { - if (self.BaseStream.Position % alignment == 0) - return; - self.BaseStream.Position = alignment * ((self.BaseStream.Position + (alignment - 1)) / alignment); - self.BaseStream.Position--; - self.Write((byte)0); + List bytes = new List(); + if (swap) + { + bytes.AddRange(BitConverter.GetBytes(BinaryPrimitives.ReadSingleBigEndian(BitConverter.GetBytes(vec.X)))); + bytes.AddRange(BitConverter.GetBytes(BinaryPrimitives.ReadSingleBigEndian(BitConverter.GetBytes(vec.Y)))); + bytes.AddRange(BitConverter.GetBytes(BinaryPrimitives.ReadSingleBigEndian(BitConverter.GetBytes(vec.Z)))); + bytes.AddRange(BitConverter.GetBytes(BinaryPrimitives.ReadSingleBigEndian(BitConverter.GetBytes(vec.W)))); + } + else + { + bytes.AddRange(BitConverter.GetBytes(vec.X)); + bytes.AddRange(BitConverter.GetBytes(vec.Y)); + bytes.AddRange(BitConverter.GetBytes(vec.Z)); + bytes.AddRange(BitConverter.GetBytes(vec.W)); + } + return bytes.ToArray(); } } } diff --git a/BundleFormat/Util.cs b/BundleFormat/Util.cs deleted file mode 100644 index 962a349..0000000 --- a/BundleFormat/Util.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; - -namespace BundleFormat -{ - public static class Util - { - // reverse byte order (16-bit) - public static UInt16 ReverseBytes(UInt16 value) - { - return (UInt16)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8); - } - - // reverse byte order (32-bit) - public static UInt32 ReverseBytes(UInt32 value) - { - return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 | - (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24; - } - - // reverse byte order (64-bit) - public static UInt64 ReverseBytes(UInt64 value) - { - return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 | - (value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 | - (value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 | - (value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56; - } - - // reverse byte order (16-bit) - public static Int16 ReverseBytes(Int16 value) - { - UInt16 v = (UInt16)value; - return (Int16)((v & 0xFFU) << 8 | (v & 0xFF00U) >> 8); - } - - // reverse byte order (32-bit) - public static Int32 ReverseBytes(Int32 value) - { - UInt32 v = (UInt32)value; - return (Int32)((v & 0x000000FFU) << 24 | (v & 0x0000FF00U) << 8 | - (v & 0x00FF0000U) >> 8 | (v & 0xFF000000U) >> 24); - } - - // reverse byte order (64-bit) - public static Int64 ReverseBytes(Int64 value) - { - UInt64 v = (UInt64)value; - return (Int64)((v & 0x00000000000000FFUL) << 56 | (v & 0x000000000000FF00UL) << 40 | - (v & 0x0000000000FF0000UL) << 24 | (v & 0x00000000FF000000UL) << 8 | - (v & 0x000000FF00000000UL) >> 8 | (v & 0x0000FF0000000000UL) >> 24 | - (v & 0x00FF000000000000UL) >> 40 | (v & 0xFF00000000000000UL) >> 56); - } - } -} diff --git a/BundleFormat/VertexDesc.cs b/BundleFormat/VertexDesc.cs index ff21784..f6082f4 100644 --- a/BundleFormat/VertexDesc.cs +++ b/BundleFormat/VertexDesc.cs @@ -124,7 +124,8 @@ public static VertexDesc Read(BundleEntry entry) public void Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; bw.Write(Unknown1); bw.Write(Unknown2); diff --git a/BundleManager/EntryEditor.cs b/BundleManager/EntryEditor.cs index a3c6e39..8bfe971 100644 --- a/BundleManager/EntryEditor.cs +++ b/BundleManager/EntryEditor.cs @@ -653,7 +653,7 @@ private void calcLookup8_Click(object sender, EventArgs e) string value = InputDialog.ShowInput(this, "Please enter the value to get the lookup 8."); if (value == null) return; - ulong result = Utilities.calcLookup8(value); + ulong result = Utilities.CalcLookup8(value); Task.Run(() => UpdateDisplay()); MessageBox.Show(this, "The lookup 8 hashed value is: " + result.ToString("X16"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } diff --git a/BundleUtilities/BinaryReader2.cs b/BundleUtilities/BinaryReader2.cs index 26fa220..9506696 100644 --- a/BundleUtilities/BinaryReader2.cs +++ b/BundleUtilities/BinaryReader2.cs @@ -1,169 +1,163 @@ -using System; +using System.Buffers.Binary; using System.Collections.Generic; -using System.ComponentModel; using System.IO; using System.Text; namespace BundleUtilities { - [TypeConverter(typeof(ExpandableObjectConverter))] - public class Vector3I - { - public float X { get; set; } - public float Y { get; set; } - public float Z { get; set; } - public float S { get; set; } - - public Vector3I() { - X = 0; - Y = 0; - Z = 0; - S = 0; - } - public Vector3I(float x, float y, float z, float s) - { - X = x; - Y = y; - Z = z; - S = s; - } - - public byte[] toBytes() - { - List bytes = new List(); - bytes.AddRange(BitConverter.GetBytes(X)); - bytes.AddRange(BitConverter.GetBytes(Y)); - bytes.AddRange(BitConverter.GetBytes(Z)); - bytes.AddRange(BitConverter.GetBytes(S)); - return bytes.ToArray(); - } - - } - public class BinaryReader2 : BinaryReader { - public bool BigEndian { get; set; } + public bool BigEndian { get; set; } = false; public BinaryReader2(Stream input) : base(input) { - BigEndian = false; - } - - public BinaryReader2(Stream input, Encoding encoding) : base(input, encoding) - { - BigEndian = false; - } - - public BinaryReader2(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen) - { - BigEndian = false; - } - - private bool ShouldFlip() - { - return (BigEndian && BitConverter.IsLittleEndian) || (!BigEndian && !BitConverter.IsLittleEndian); + } public override short ReadInt16() { - var data = base.ReadBytes(2); - if (data.Length < 2) + if (BaseStream.Length - BaseStream.Position < 2) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToInt16(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadInt16BigEndian(ReadBytes(2)); + return BinaryPrimitives.ReadInt16LittleEndian(ReadBytes(2)); } public override int ReadInt32() { - var data = base.ReadBytes(4); - if (data.Length < 4) + if (BaseStream.Length - BaseStream.Position < 4) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToInt32(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadInt32BigEndian(ReadBytes(4)); + return BinaryPrimitives.ReadInt32LittleEndian(ReadBytes(4)); } public override long ReadInt64() { - var data = base.ReadBytes(8); - if (data.Length < 8) + if (BaseStream.Length - BaseStream.Position < 8) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToInt64(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadInt64BigEndian(ReadBytes(8)); + return BinaryPrimitives.ReadInt64LittleEndian(ReadBytes(8)); } public override ushort ReadUInt16() { - var data = base.ReadBytes(2); - if (data.Length < 2) + if (BaseStream.Length - BaseStream.Position < 2) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToUInt16(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadUInt16BigEndian(ReadBytes(2)); + return BinaryPrimitives.ReadUInt16LittleEndian(ReadBytes(2)); } public override uint ReadUInt32() { - var data = base.ReadBytes(4); - if (data.Length < 4) + if (BaseStream.Length - BaseStream.Position < 4) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToUInt32(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadUInt32BigEndian(ReadBytes(4)); + return BinaryPrimitives.ReadUInt32LittleEndian(ReadBytes(4)); } public override ulong ReadUInt64() { - var data = base.ReadBytes(8); - if (data.Length < 8) + if (BaseStream.Length - BaseStream.Position < 8) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToUInt64(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadUInt64BigEndian(ReadBytes(8)); + return BinaryPrimitives.ReadUInt64LittleEndian(ReadBytes(8)); } public override float ReadSingle() { - var data = base.ReadBytes(4); - if (data.Length < 4) + if (BaseStream.Length - BaseStream.Position < 4) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToSingle(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadSingleBigEndian(ReadBytes(4)); + return BinaryPrimitives.ReadSingleLittleEndian(ReadBytes(4)); } public override double ReadDouble() { - var data = base.ReadBytes(8); - if (data.Length < 8) + if (BaseStream.Length - BaseStream.Position < 8) throw new EndOfStreamException(); - if (ShouldFlip()) - Array.Reverse(data); - return BitConverter.ToDouble(data, 0); + if (BigEndian) + return BinaryPrimitives.ReadDoubleBigEndian(ReadBytes(8)); + return BinaryPrimitives.ReadDoubleLittleEndian(ReadBytes(8)); } - public void SkipUniquePadding(int numberOfBytes) { - base.BaseStream.Position = base.BaseStream.Position + numberOfBytes; + public string ReadCStr() + { + List bytes = new List(); + + try + { + while (true) + { + byte c = ReadByte(); + if (c == '\0') + break; + bytes.Add(c); + } + } + catch (EndOfStreamException) + { + // Ignore + } + return Encoding.UTF8.GetString(bytes.ToArray()); } - public void SkipPadding() + public string ReadCStringPtr() { - long currentLength = base.BaseStream.Position; - if (currentLength % 16 != 0) + uint ptr = ReadUInt32(); + long pos = BaseStream.Position; + BaseStream.Position = ptr; + string result = ReadCStr(); + BaseStream.Position = pos; + + return result; + } + + public string ReadLenString(int size) + { + string result = ""; + bool readNull = false; + for (int i = 0; i < size; i++) { - base.BaseStream.Position = base.BaseStream.Position + (16 - currentLength % 16); - }; + char c = (char)ReadByte(); + if (c == '\0') + readNull = true; + if (!readNull) + result += c; + } + + return result; } - public Vector3I ReadVector3I() + public bool EOF() { - float x = base.ReadSingle(); - float y = base.ReadSingle(); - float z = base.ReadSingle(); - float s = base.ReadSingle(); - return new Vector3I(x, y, z, s); + return BaseStream.Position >= BaseStream.Length; + } + + public byte Peek() + { + byte b = ReadByte(); + BaseStream.Seek(-1, SeekOrigin.Current); + return b; + } + + public void SkipUniquePadding(int numberOfBytes) + { + BaseStream.Position = BaseStream.Position + numberOfBytes; + } + + public void SkipPadding() + { + long currentLength = BaseStream.Position; + if (currentLength % 16 != 0) + { + BaseStream.Position = BaseStream.Position + (16 - currentLength % 16); + }; } } } diff --git a/BundleUtilities/BinaryWriter2.cs b/BundleUtilities/BinaryWriter2.cs new file mode 100644 index 0000000..dd261e6 --- /dev/null +++ b/BundleUtilities/BinaryWriter2.cs @@ -0,0 +1,134 @@ +using System; +using System.Buffers.Binary; +using System.IO; +using System.Text; + +namespace BundleUtilities +{ + public class BinaryWriter2 : BinaryWriter + { + public bool BigEndian { get; set; } = false; + + public BinaryWriter2(Stream input) : base(input) + { + + } + + public override void Write(short value) + { + if (BigEndian) + base.Write(BinaryPrimitives.ReverseEndianness(value)); + else + base.Write(value); + } + + public override void Write(int value) + { + if (BigEndian) + base.Write(BinaryPrimitives.ReverseEndianness(value)); + else + base.Write(value); + } + + public override void Write(long value) + { + if (BigEndian) + base.Write(BinaryPrimitives.ReverseEndianness(value)); + else + base.Write(value); + } + + public override void Write(ushort value) + { + if (BigEndian) + base.Write(BinaryPrimitives.ReverseEndianness(value)); + else + base.Write(value); + } + + public override void Write(uint value) + { + if (BigEndian) + base.Write(BinaryPrimitives.ReverseEndianness(value)); + else + base.Write(value); + } + + public override void Write(ulong value) + { + if (BigEndian) + base.Write(BinaryPrimitives.ReverseEndianness(value)); + else + base.Write(value); + } + + public override void Write(float value) + { + if (BigEndian) + { + var bytes = BitConverter.GetBytes(value); + Array.Reverse(bytes); + base.Write(bytes); + } + else + base.Write(value); + } + + public override void Write(double value) + { + if (BigEndian) + { + var bytes = BitConverter.GetBytes(value); + Array.Reverse(bytes); + base.Write(bytes); + } + else + base.Write(value); + } + + public void Align(byte alignment) + { + if (BaseStream.Position % alignment == 0) + return; + BaseStream.Position = alignment * ((BaseStream.Position + (alignment - 1)) / alignment); + BaseStream.Position--; + Write((byte)0); + } + + public void WriteCStr(string value) + { + byte[] bytes = Encoding.UTF8.GetBytes(value); + for (int i = 0; i < bytes.Length; i++) + Write(bytes[i]); + Write((byte)0); + } + + public void WriteLenString(string s, int len) + { + for (int i = 0; i < len; i++) + { + if (i < s.Length) + Write((byte)s[i]); + else + Write((byte)0); + } + } + + public void WriteUniquePadding(int numberOfPadding) + { + for (int i = 0; i < numberOfPadding; i++) + Write((byte)0); + } + + // Add padding: Has to be divisible by 16, else add padding + public void WritePadding() + { + long currentLength = BaseStream.Length; + if (currentLength % 16 != 0) + { + for (int i = 0; i < (16 - currentLength % 16); i++) + Write((byte)0); + }; + } + } +} diff --git a/BundleUtilities/Utilities.cs b/BundleUtilities/Utilities.cs index 155220d..40b1ba1 100644 --- a/BundleUtilities/Utilities.cs +++ b/BundleUtilities/Utilities.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.Buffers.Binary; using System.ComponentModel; using System.IO; using System.Text; @@ -7,14 +7,15 @@ namespace BundleUtilities { - public class LogWriter { private string m_exePath = string.Empty; + public LogWriter(string logMessage) { LogWrite(logMessage); } + public void LogWrite(string logMessage) { m_exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); @@ -43,20 +44,9 @@ public void Log(string logMessage, TextWriter txtWriter) } } - public static class Util - { - // reverse byte order (64-bit) - public static UInt64 ReverseBytes(UInt64 value) - { - return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 | - (value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 | - (value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 | - (value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56; - } - } public static class Utilities { - public static ulong calcLookup8(string text) + public static ulong CalcLookup8(string text) { byte[] message = Encoding.ASCII.GetBytes(text); var hashValue = Lookup8.Hash(message, (ulong)message.Length, 0xABCDEF0011223344); @@ -64,21 +54,6 @@ public static ulong calcLookup8(string text) return hashValue; } - public static EncryptedString ReadEncryptedString(this BinaryReader self) - { - ulong value = self.ReadUInt64(); - EncryptedString id = new EncryptedString(value); - return id; - } - - public static void WriteEncryptedString(this BinaryWriter self, EncryptedString id, bool xbox = false) - { - ulong value = id.Encrypted; - if (xbox) - value = Util.ReverseBytes(value); - self.Write(value); - } - public static bool IsValidPath(string path) { return !string.IsNullOrEmpty(path); @@ -107,135 +82,6 @@ public static byte[] Pad(this byte[] self, int bytes) return result; } - public static bool EOF(this BinaryReader self) - { - return self.BaseStream.Position >= self.BaseStream.Length; - } - - public static string ReadCStr(this BinaryReader self) - { - List bytes = new List(); - - try - { - while (true) - { - byte c = self.ReadByte(); - if (c == '\0') - break; - bytes.Add(c); - } - } - catch (EndOfStreamException) - { - // Ignore - } - return Encoding.UTF8.GetString(bytes.ToArray()); - } - - public static byte[] Flip(this byte[] self) - { - byte[] result = new byte[self.Length]; - - int j = 0; - for (int i = self.Length - 1; i >= 0; i--) - { - result[j] = self[i]; - j++; - } - - return result; - } - - public static bool NoData(this byte[] self) - { - for (int i = 0; i < self.Length; i++) - { - if (self[i] != 0x00) - return false; - } - - return true; - } - - public static string ReadLenString(this BinaryReader self, int size) - { - string result = ""; - bool readNull = false; - for (int i = 0; i < size; i++) - { - char c = (char)self.ReadByte(); - if (c == '\0') - readNull = true; - if (!readNull) - result += c; - } - - return result; - } - - public static string ReadLenString(this BinaryReader self) - { - int len = self.ReadInt32(); - - return self.ReadLenString(len); - } - - public static string ReadCStringPtr(this BinaryReader self) - { - uint ptr = self.ReadUInt32(); - long pos = self.BaseStream.Position; - self.BaseStream.Position = ptr; - string result = self.ReadCStr(); - self.BaseStream.Position = pos; - - return result; - } - - public static void WriteCStr(this BinaryWriter self, string value) - { - byte[] bytes = Encoding.UTF8.GetBytes(value); - for (int i = 0; i < bytes.Length; i++) - { - self.Write(bytes[i]); - } - self.Write((byte)0); - } - - public static void WriteUniquePadding(this BinaryWriter self, int numberOfPadding) - { - for (int i = 0; i < numberOfPadding; i++) - { - self.Write((byte)0); - } - - } - - public static void WriteStringPadding(this BinaryWriter self) - { - long currentLength = self.BaseStream.Length; - if (currentLength % 8 != 0) - { - for (int i = 0; i < (8 - currentLength % 8); i++) - { - self.Write((byte)0); - } - }; - } - - // Add padding: Has to be divisible by 16, else add padding - public static void WritePadding(this BinaryWriter self) - { - long currentLength = self.BaseStream.Length; - if (currentLength % 16 != 0) - { - for (int i = 0; i < (16 - currentLength % 16); i++) - { - self.Write((byte)0); - } - }; - } - /// /// Converts the specified text to an object. /// diff --git a/BurnoutImage/GameImage.cs b/BurnoutImage/GameImage.cs index b343b72..148b819 100644 --- a/BurnoutImage/GameImage.cs +++ b/BurnoutImage/GameImage.cs @@ -4,6 +4,7 @@ using System.IO; using System.Windows.Forms; using BCnEncoder.Shared; +using BundleUtilities; namespace BurnoutImage { @@ -68,7 +69,7 @@ public static ImageInfo SetImage(string path, int width, int height, Compression } MemoryStream msx = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(msx); + BinaryWriter2 bw = new BinaryWriter2(msx); // Original game header: https://burnout.wiki/wiki/Texture/PC // TODO: Implement as separate option @@ -127,7 +128,7 @@ public static ImageHeader GetImageHeader(byte[] data) try { MemoryStream ms = new MemoryStream(data); - BinaryReader br = new BinaryReader(ms); + BinaryReader2 br = new BinaryReader2(ms); if (data.Length == 0x40 || data.Length == 0x30) { // Remaster @@ -276,7 +277,7 @@ public static Image GetImagePS3(byte[] data, byte[] extraData) try { MemoryStream ms = new MemoryStream(data); - BinaryReader br = new BinaryReader(ms); + BinaryReader2 br = new BinaryReader2(ms); byte compression = br.ReadByte(); byte[] unknown1 = br.ReadBytes(3); diff --git a/LangEditor/Language.cs b/LangEditor/Language.cs index efb87e8..61cea35 100644 --- a/LangEditor/Language.cs +++ b/LangEditor/Language.cs @@ -72,7 +72,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; bw.Write((uint)meLanguageID); bw.Write(mpEntries.Count); diff --git a/LuaList/LuaList.cs b/LuaList/LuaList.cs index fdafe4d..0d7fd39 100644 --- a/LuaList/LuaList.cs +++ b/LuaList/LuaList.cs @@ -83,19 +83,20 @@ public bool Read(BundleEntry entry, ILoader loader = null) { MemoryStream ms = entry.MakeStream(); BinaryReader2 br = new BinaryReader2(ms); + br.BigEndian = entry.Console; - version = br.ReadInt32(); //0x0 0x4 int32_t Version number 1 - br.ReadBytes(4); // 0x4 0x4 padding - CgsId = br.ReadEncryptedString(); //0x8 0x8 CgsID List ID Encoded - var entriesPointer = br.ReadInt32(); //0x10 0x4 Unk0* Script list Unk0 format - var typePointer = br.ReadInt32(); //0x14 0x4 char[32] * * Types - var variablePointer = br.ReadInt32(); //0x18 0x4 char[32] * * Variables - var numScripts = br.ReadInt32(); //0x1C 0x4 uint32_t Num scripts - var dataLength = br.ReadInt32(); //0x20 0x4 uint32_t Data length Not including padding to end - listTitle = br.ReadLenString(128); //0x24 0x80 char[128] List title - var numTypes = br.ReadByte(); //0xA4 0x1 uint8_t Num types - var numVariables = br.ReadByte(); //0xA5 0x1 uint8_t Num variables - br.ReadBytes(26); // padding + version = br.ReadInt32(); + br.ReadBytes(4); + CgsId = new EncryptedString(br.ReadUInt64()); + var entriesPointer = br.ReadInt32(); + var typePointer = br.ReadInt32(); + var variablePointer = br.ReadInt32(); + var numScripts = br.ReadInt32(); + var dataLength = br.ReadInt32(); + listTitle = br.ReadLenString(128); + var numTypes = br.ReadByte(); + var numVariables = br.ReadByte(); + br.ReadBytes(26); entries = new List(); for (int i = 0; i < numScripts; i++) @@ -145,10 +146,11 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; bw.Write(version); bw.WriteUniquePadding(4); - bw.WriteEncryptedString(CgsId); + bw.Write(CgsId.Encrypted); bw.Write(getLengthOfHeader()); bw.Write(getLengthOfHeader() + getLengthOfEntries()); bw.Write(getLengthOfHeader() + getLengthOfEntries() + getLengthOfTypes()); diff --git a/LuaList/LuaListEntry.cs b/LuaList/LuaListEntry.cs index 5eabf36..50be58d 100644 --- a/LuaList/LuaListEntry.cs +++ b/LuaList/LuaListEntry.cs @@ -49,7 +49,7 @@ public int getDataSize() { } public void Read(ILoader loader, BinaryReader2 br) { - CgsId = br.ReadEncryptedString(); + CgsId = new EncryptedString(br.ReadUInt64()); Name = br.ReadLenString(128); Goal = br.ReadLenString(128); Description = br.ReadLenString(256); @@ -60,17 +60,17 @@ public void Read(ILoader loader, BinaryReader2 br) { br.ReadBytes(8); // padding } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.WriteEncryptedString(CgsId); - wr.Write(Encoding.ASCII.GetBytes((Name.PadRight(128, '\0').Substring(0, 128).ToCharArray()))); - wr.Write(Encoding.ASCII.GetBytes((Goal.PadRight(128, '\0').Substring(0, 128).ToCharArray()))); - wr.Write(Encoding.ASCII.GetBytes((Description.PadRight(256, '\0').Substring(0, 256).ToCharArray()))); - wr.Write((int)ScoreMultiplier); - wr.Write((int)ScoringMethod); - wr.Write(Type); - wr.Write(Variables); - wr.WriteUniquePadding(8); + bw.Write(CgsId.Encrypted); + bw.Write(Encoding.ASCII.GetBytes((Name.PadRight(128, '\0').Substring(0, 128).ToCharArray()))); + bw.Write(Encoding.ASCII.GetBytes((Goal.PadRight(128, '\0').Substring(0, 128).ToCharArray()))); + bw.Write(Encoding.ASCII.GetBytes((Description.PadRight(256, '\0').Substring(0, 256).ToCharArray()))); + bw.Write((int)ScoreMultiplier); + bw.Write((int)ScoringMethod); + bw.Write(Type); + bw.Write(Variables); + bw.WriteUniquePadding(8); } } } diff --git a/PVSFormat/PVS.cs b/PVSFormat/PVS.cs index ae3eca3..8b67ae7 100644 --- a/PVSFormat/PVS.cs +++ b/PVSFormat/PVS.cs @@ -135,7 +135,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; // Update neighbours in case of zone deletion for (int i = 0; i < data.TotalZones; ++i) diff --git a/VaultFormat/AttribSys.cs b/VaultFormat/AttribSys.cs index f3217b7..692b0d2 100644 --- a/VaultFormat/AttribSys.cs +++ b/VaultFormat/AttribSys.cs @@ -181,17 +181,17 @@ public AttribSys() Strings = new List(); } - private void ReadChunk(ILoader loader, BinaryReader br) + private void ReadChunk(ILoader loader, BinaryReader2 br) { long initialPos = br.BaseStream.Position; - string fourcc = Encoding.ASCII.GetString(BitConverter.GetBytes(br.ReadInt32()).Flip()); + uint fourcc = br.ReadUInt32(); int size = br.ReadInt32(); switch (fourcc) { - case "Vers": + case 0x56657273: // Vers VersionHash = br.ReadUInt64(); break; - case "DepN": + case 0x4465704E: // DepN long entryCount = br.ReadInt64(); DepHash1 = br.ReadUInt64(); DepHash2 = br.ReadUInt64(); @@ -204,13 +204,13 @@ private void ReadChunk(ILoader loader, BinaryReader br) } break; - case "StrN": + case 0x5374724E: // StrN StrUnknown1 = br.ReadInt64(); break; - case "DatN": + case 0x4461744E: // DatN // Handled in ExpN break; - case "ExpN": + case 0x4578704E: // ExpN long nestedChunkCount = br.ReadInt64(); for (long i = 0; i < nestedChunkCount; i++) { @@ -259,7 +259,7 @@ private void ReadChunk(ILoader loader, BinaryReader br) br.BaseStream.Position = pos; } break; - case "PtrN": + case 0x5074724E: // PtrN // Unknown int dataSize = size - 8; PtrN = new PtrChunk(); @@ -278,17 +278,13 @@ private void ReadChunk(ILoader loader, BinaryReader br) throw new ReadFailedError("Unknown Chunk: " + fourcc); } - br.BaseStream.Position = initialPos + size; - } - private void ReadVlt(ILoader loader, BinaryReader br) + private void ReadVlt(ILoader loader, BinaryReader2 br) { while (!br.EOF()) - { ReadChunk(loader, br); - } } private void ReadBin(ILoader loader, BinaryReader2 br) @@ -296,7 +292,7 @@ private void ReadBin(ILoader loader, BinaryReader2 br) try { long initialPos = br.BaseStream.Position; - string fourcc = Encoding.ASCII.GetString(br.ReadBytes(4).Flip()); + uint fourcc = br.ReadUInt32(); // StrE int size = br.ReadInt32(); // Size of Strings Array in hex while (br.BaseStream.Position < initialPos + size) @@ -321,24 +317,20 @@ private void ReadBin(ILoader loader, BinaryReader2 br) } } - private void WriteBin(BinaryWriter wr) + private void WriteBin(BinaryWriter2 bw) { try { - wr.Write(Utilities.Flip(Encoding.ASCII.GetBytes("StrE"))); - wr.Write(getSizeOfStrE()); + bw.Write(0x53747245); // StrE + bw.Write(getSizeOfStrE()); foreach (string String in Strings) - { - wr.WriteCStr(String); - } - wr.WritePadding(); + bw.WriteCStr(String); + bw.WritePadding(); foreach (IAttribute attribute in Attributes) - { - attribute.Write(wr); - } + attribute.Write(bw); // Write Data of Unimplemented types - wr.Write(Data); - wr.WritePadding(); + bw.Write(Data); + bw.WritePadding(); } catch (IOException ex) { @@ -349,20 +341,16 @@ private void WriteBin(BinaryWriter wr) public int addPadding(List bytes) { if (bytes.SelectMany(i => i).Count() % 16 == 0) - { return bytes.SelectMany(i => i).Count(); - } else - { return ((bytes.SelectMany(i => i).Count() / 16) * 16) + 16; - } } private int getSizeOfStrE() { List bytes = new List { - Utilities.Flip(Encoding.ASCII.GetBytes("StrE")), + Encoding.ASCII.GetBytes("StrE"), BitConverter.GetBytes(16) }; foreach (string String in Strings) @@ -377,7 +365,7 @@ private int getSizeOfVers() { List bytes = new List { - Utilities.Flip(Encoding.ASCII.GetBytes("Vers")), + Encoding.ASCII.GetBytes("Vers"), BitConverter.GetBytes(16), BitConverter.GetBytes(VersionHash) }; @@ -388,7 +376,7 @@ private int getSizeOfDepN() { List bytes = new List { - Utilities.Flip(Encoding.ASCII.GetBytes("DepN")), + Encoding.ASCII.GetBytes("DepN"), BitConverter.GetBytes(96), BitConverter.GetBytes((UInt64)Dependencies.Count()), BitConverter.GetBytes(DepHash1), @@ -408,7 +396,7 @@ private int getSizeOfDatN() { List bytes = new List { - Utilities.Flip(Encoding.ASCII.GetBytes("DatN")) + Encoding.ASCII.GetBytes("DatN") }; foreach (AttributeHeader dataChunk in Attributes.Select(x => x.getHeader())) { @@ -425,8 +413,7 @@ private int getSizeOfDatN() bytes.Add(BitConverter.GetBytes((dataChunk.ParameterTypeHashes[j]))); for (int j = 0; j < (dataChunk.ParametersToRead - dataChunk.ParameterCount); j++) { - UInt64 zero = 0; - bytes.Add(BitConverter.GetBytes(zero)); // zero + bytes.Add(BitConverter.GetBytes((ulong)0)); } foreach (DataItem item in dataChunk.Items) { @@ -443,7 +430,7 @@ private int getSizeOfExpN() { List bytes = new List { - Utilities.Flip(Encoding.ASCII.GetBytes("ExpN")), + Encoding.ASCII.GetBytes("ExpN"), BitConverter.GetBytes(16), BitConverter.GetBytes((UInt64)Attributes.Select(x => x.getInfo()).Count()) }; @@ -461,7 +448,7 @@ private int getSizeOfStrN() { List bytes = new List { - Utilities.Flip(Encoding.ASCII.GetBytes("StrN")), + Encoding.ASCII.GetBytes("StrN"), BitConverter.GetBytes(16), BitConverter.GetBytes(StrUnknown1) }; @@ -472,7 +459,7 @@ private int getSizeOfPtrN() { List bytes = new List { - Utilities.Flip(Encoding.ASCII.GetBytes("PtrN")), + Encoding.ASCII.GetBytes("PtrN"), BitConverter.GetBytes(16) }; foreach (PtrChunkData data in PtrN.allData) @@ -485,87 +472,81 @@ private int getSizeOfPtrN() return addPadding(bytes); } - - private void WriteVlt(BinaryWriter wr) + private void WriteVlt(BinaryWriter2 bw) { try { - wr.Write(Utilities.Flip(Encoding.ASCII.GetBytes("Vers"))); - wr.Write(getSizeOfVers()); - wr.Write(VersionHash); - wr.WritePadding(); - - wr.Write(Utilities.Flip(Encoding.ASCII.GetBytes("DepN"))); - wr.Write(getSizeOfDepN()); - wr.Write((UInt64)Dependencies.Count()); - wr.Write(DepHash1); - wr.Write(DepHash2); - wr.Write(DepNop); + bw.Write(0x56657273); // Vers + bw.Write(getSizeOfVers()); + bw.Write(VersionHash); + bw.WritePadding(); + + bw.Write(0x4465704E); // DepN + bw.Write(getSizeOfDepN()); + bw.Write((UInt64)Dependencies.Count()); + bw.Write(DepHash1); + bw.Write(DepHash2); + bw.Write(DepNop); // Have to add null terminating byte to length - wr.Write(Dependencies[0].Length + 1); + bw.Write(Dependencies[0].Length + 1); foreach (string d in Dependencies) - { - wr.WriteCStr(d); - } - wr.WritePadding(); + bw.WriteCStr(d); + bw.WritePadding(); - wr.Write(Utilities.Flip(Encoding.ASCII.GetBytes("StrN"))); - wr.Write(getSizeOfStrN()); - wr.Write(StrUnknown1); - wr.WritePadding(); + bw.Write(0x5374724E); // StrN + bw.Write(getSizeOfStrN()); + bw.Write(StrUnknown1); + bw.WritePadding(); - wr.Write(Utilities.Flip(Encoding.ASCII.GetBytes("DatN"))); - wr.Write(getSizeOfDatN()); + bw.Write(0x4461744E); // DatN + bw.Write(getSizeOfDatN()); foreach (AttributeHeader dataChunk in Attributes.Select(x => x.getHeader())) { - wr.Write(dataChunk.CollectionHash); - wr.Write(dataChunk.ClassHash); - wr.Write(dataChunk.Unknown1); - wr.Write(dataChunk.ItemCount); - wr.Write(dataChunk.Unknown2); - wr.Write(dataChunk.ItemCountDup); - wr.Write(dataChunk.ParameterCount); - wr.Write(dataChunk.ParametersToRead); - wr.Write(dataChunk.Unknown3); + bw.Write(dataChunk.CollectionHash); + bw.Write(dataChunk.ClassHash); + bw.Write(dataChunk.Unknown1); + bw.Write(dataChunk.ItemCount); + bw.Write(dataChunk.Unknown2); + bw.Write(dataChunk.ItemCountDup); + bw.Write(dataChunk.ParameterCount); + bw.Write(dataChunk.ParametersToRead); + bw.Write(dataChunk.Unknown3); for (int j = 0; j < dataChunk.ParameterCount; j++) - wr.Write(dataChunk.ParameterTypeHashes[j]); + bw.Write(dataChunk.ParameterTypeHashes[j]); for (int j = 0; j < (dataChunk.ParametersToRead - dataChunk.ParameterCount); j++) - { - UInt64 zero = 0; - wr.Write(zero); // zero - } + bw.Write((ulong)0); foreach (DataItem item in dataChunk.Items) { - wr.Write(item.Hash); - wr.Write(item.Unknown1); - wr.Write(item.ParameterIdx); - wr.Write(item.Unknown2); + bw.Write(item.Hash); + bw.Write(item.Unknown1); + bw.Write(item.ParameterIdx); + bw.Write(item.Unknown2); } } - wr.WritePadding(); + bw.WritePadding(); - wr.Write(Utilities.Flip(Encoding.ASCII.GetBytes("ExpN"))); - wr.Write(getSizeOfExpN()); - wr.Write((UInt64)Attributes.Select(x => x.getInfo()).Count()); + bw.Write(0x4578704E); // ExpN + bw.Write(getSizeOfExpN()); + bw.Write((ulong)Attributes.Select(x => x.getInfo()).Count()); foreach (SizeAndPositionInformation chunk in Attributes.Select(x => x.getInfo())) { - wr.Write(chunk.Hash); - wr.Write(chunk.EntryTypeHash); - wr.Write(chunk.DataChunkSize); - wr.Write(chunk.DataChunkPosition); + bw.Write(chunk.Hash); + bw.Write(chunk.EntryTypeHash); + bw.Write(chunk.DataChunkSize); + bw.Write(chunk.DataChunkPosition); } - wr.WritePadding(); + bw.WritePadding(); - wr.Write(Utilities.Flip(Encoding.ASCII.GetBytes("PtrN"))); - wr.Write(getSizeOfPtrN()); + bw.Write(0x5074724E); // PtrN + bw.Write(getSizeOfPtrN()); foreach (PtrChunkData data in PtrN.allData) { - wr.Write(data.Ptr); - wr.Write(data.type); - wr.Write(data.flag); - wr.Write(data.Data); + bw.Write(data.Ptr); + bw.Write(data.type); + bw.Write(data.flag); + bw.Write(data.Data); } - wr.WritePadding(); + bw.WritePadding(); } catch (IOException ex) { @@ -628,7 +609,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; int vltPos = 16; // Because vlt begins after the sizes int vltSize = getSizeOfDatN() + getSizeOfDepN() + getSizeOfExpN() + getSizeOfPtrN() + getSizeOfStrN() + getSizeOfVers(); diff --git a/VaultFormat/AttribSysVaultForm.cs b/VaultFormat/AttribSysVaultForm.cs index 0983a67..95af9bb 100644 --- a/VaultFormat/AttribSysVaultForm.cs +++ b/VaultFormat/AttribSysVaultForm.cs @@ -109,7 +109,7 @@ private void changeCollectionHashToolStripMenuItem_Click(object sender, EventArg string value = InputDialog.ShowInput(this, "Please enter the value to get the lookup 8."); if (value == null) return; - ulong result = Utilities.calcLookup8(value); + ulong result = Utilities.CalcLookup8(value); int index = AttribSys.Attributes.FindIndex(i => i.getHeader().ClassName == lstDataChunks.SelectedItems[0].Text); AttribSys.Attributes[index].getHeader().CollectionHash = result; MessageBox.Show(this, "The lookup 8 hashed value is: " + result.ToString("X16"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); diff --git a/VaultFormat/Burnoutcarasset.cs b/VaultFormat/Burnoutcarasset.cs index 9c9e08e..c54ce4a 100644 --- a/VaultFormat/Burnoutcarasset.cs +++ b/VaultFormat/Burnoutcarasset.cs @@ -96,9 +96,7 @@ public void Read(ILoader loader, BinaryReader2 br) { Offences = new RefSpec[12]; for (int i = 0; i < 12; i++) - { Offences[i] = new RefSpec(loader, br); - } SoundExhaustAsset = new RefSpec(loader, br); SoundEngineAsset = new RefSpec(loader, br); PhysicsVehicleHandlingAsset = new RefSpec(loader, br); @@ -120,31 +118,29 @@ public void Read(ILoader loader, BinaryReader2 br) br.SkipUniquePadding(3); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { foreach (RefSpec r in Offences) - { - r.Write(wr); - } - SoundExhaustAsset.Write(wr); - SoundEngineAsset.Write(wr); - PhysicsVehicleHandlingAsset.Write(wr); - GraphicsAsset.Write(wr); - CarUnlockShot.Write(wr); - CameraExternalBehaviourAsset.Write(wr); - CameraBumperBehaviourAsset.Write(wr); - wr.Write(VehicleID); - wr.Write(PhysicsAsset); - wr.Write(MasterSceneMayaBinaryFile); - wr.Write(InGameName); - wr.Write(GameplayAsset); - wr.Write(ExhaustName); - wr.Write(ExhaustEntityKey); - wr.Write(EngineName); - wr.Write(EngineEntityKey); - wr.Write(DefaultWheel); - wr.Write(BuildThisVehicle); - wr.WriteUniquePadding(3); + r.Write(bw); + SoundExhaustAsset.Write(bw); + SoundEngineAsset.Write(bw); + PhysicsVehicleHandlingAsset.Write(bw); + GraphicsAsset.Write(bw); + CarUnlockShot.Write(bw); + CameraExternalBehaviourAsset.Write(bw); + CameraBumperBehaviourAsset.Write(bw); + bw.Write(VehicleID); + bw.Write(PhysicsAsset); + bw.Write(MasterSceneMayaBinaryFile); + bw.Write(InGameName); + bw.Write(GameplayAsset); + bw.Write(ExhaustName); + bw.Write(ExhaustEntityKey); + bw.Write(EngineName); + bw.Write(EngineEntityKey); + bw.Write(DefaultWheel); + bw.Write(BuildThisVehicle); + bw.WriteUniquePadding(3); } } } diff --git a/VaultFormat/Burnoutcargraphicsasset.cs b/VaultFormat/Burnoutcargraphicsasset.cs index 3fde1e0..efa222c 100644 --- a/VaultFormat/Burnoutcargraphicsasset.cs +++ b/VaultFormat/Burnoutcargraphicsasset.cs @@ -75,19 +75,19 @@ public void Read(ILoader loader, BinaryReader2 br) EncodedTypePad_Offences = br.ReadInt16(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(PlayerPalletteIndex); - wr.Write(PlayerColourIndex); - wr.Write(Alloc); //Allocate space for x* Size Always 1 in VEH_P* - wr.Write(Num_RandomTrafficColours); // Count - wr.Write(Size); // Size of each entry - wr.Write(EncodedTypePad); // padding - wr.Write(RandomTrafficColours); - wr.Write(Alloc_Offences); // Allocate space for x* Size - wr.Write(Num_Offences); // Count - wr.Write(Size_Offences); // Size of ea // ch entry - wr.Write(EncodedTypePad_Offences); //padding + bw.Write(PlayerPalletteIndex); + bw.Write(PlayerColourIndex); + bw.Write(Alloc); //Allocate space for x* Size Always 1 in VEH_P* + bw.Write(Num_RandomTrafficColours); // Count + bw.Write(Size); // Size of each entry + bw.Write(EncodedTypePad); // padding + bw.Write(RandomTrafficColours); + bw.Write(Alloc_Offences); // Allocate space for x* Size + bw.Write(Num_Offences); // Count + bw.Write(Size_Offences); // Size of ea // ch entry + bw.Write(EncodedTypePad_Offences); //padding } } } diff --git a/VaultFormat/Camerabumperbehaviour.cs b/VaultFormat/Camerabumperbehaviour.cs index 58239fc..94ca85c 100644 --- a/VaultFormat/Camerabumperbehaviour.cs +++ b/VaultFormat/Camerabumperbehaviour.cs @@ -73,19 +73,19 @@ public void Read(ILoader loader, BinaryReader2 br) AccelerationDampening = br.ReadSingle(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(ZOffset); - wr.Write(YOffset); - wr.Write(YawSpring); - wr.Write(RollSpring); - wr.Write(PitchSpring); - wr.Write(FieldOfView); - wr.Write(BoostFieldOfView); - wr.Write(BodyRollScale); - wr.Write(BodyPitchScale); - wr.Write(AccelerationResponse); - wr.Write(AccelerationDampening); + bw.Write(ZOffset); + bw.Write(YOffset); + bw.Write(YawSpring); + bw.Write(RollSpring); + bw.Write(PitchSpring); + bw.Write(FieldOfView); + bw.Write(BoostFieldOfView); + bw.Write(BodyRollScale); + bw.Write(BodyPitchScale); + bw.Write(AccelerationResponse); + bw.Write(AccelerationDampening); } } } diff --git a/VaultFormat/Cameraexternalbehaviour.cs b/VaultFormat/Cameraexternalbehaviour.cs index 6b6995a..9ae68e9 100644 --- a/VaultFormat/Cameraexternalbehaviour.cs +++ b/VaultFormat/Cameraexternalbehaviour.cs @@ -89,25 +89,25 @@ public void Read(ILoader loader, BinaryReader2 br) BoostFieldOfView = br.ReadSingle(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(ZDistanceScale); - wr.Write(ZAndTiltCutoffSpeedMPH); - wr.Write(YawSpring); - wr.Write(TiltCameraScale); - wr.Write(TiltAroundCar); - wr.Write(SlideZOffsetMax); - wr.Write(SlideYScale); - wr.Write(SlideXScale); - wr.Write(PivotZOffset); - wr.Write(PivotLength); - wr.Write(PivotHeight); - wr.Write(PitchSpring); - wr.Write(FieldOfView); - wr.Write(DriftYawSpring); - wr.Write(DownAngle); - wr.Write(BoostFieldOfViewZoom); - wr.Write(BoostFieldOfView); + bw.Write(ZDistanceScale); + bw.Write(ZAndTiltCutoffSpeedMPH); + bw.Write(YawSpring); + bw.Write(TiltCameraScale); + bw.Write(TiltAroundCar); + bw.Write(SlideZOffsetMax); + bw.Write(SlideYScale); + bw.Write(SlideXScale); + bw.Write(PivotZOffset); + bw.Write(PivotLength); + bw.Write(PivotHeight); + bw.Write(PitchSpring); + bw.Write(FieldOfView); + bw.Write(DriftYawSpring); + bw.Write(DownAngle); + bw.Write(BoostFieldOfViewZoom); + bw.Write(BoostFieldOfView); } } } diff --git a/VaultFormat/IAttribute.cs b/VaultFormat/IAttribute.cs index 5329496..be30ee9 100644 --- a/VaultFormat/IAttribute.cs +++ b/VaultFormat/IAttribute.cs @@ -13,6 +13,6 @@ public interface IAttribute void Read(ILoader loader, BinaryReader2 br); - void Write(BinaryWriter wr); + void Write(BinaryWriter2 bw); } } diff --git a/VaultFormat/Physicsvehiclebaseattribs.cs b/VaultFormat/Physicsvehiclebaseattribs.cs index 23a3965..a0a0cb4 100644 --- a/VaultFormat/Physicsvehiclebaseattribs.cs +++ b/VaultFormat/Physicsvehiclebaseattribs.cs @@ -2,16 +2,18 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; +using BundleFormat; using BundleUtilities; namespace VaultFormat { public class Physicsvehiclebaseattribs : IAttribute { - public Vector3I RearRightWheelPosition { get; set; } - public Vector3I FrontRightWheelPosition { get; set; } - public Vector3I CoMOffset { get; set; } - public Vector3I BrakeScaleToFactor { get; set; } + public Vector4 RearRightWheelPosition { get; set; } + public Vector4 FrontRightWheelPosition { get; set; } + public Vector4 CoMOffset { get; set; } + public Vector4 BrakeScaleToFactor { get; set; } public float YawDampingOnTakeOff { get; set; } public float TractionLineLength { get; set; } public float TimeForFullBrake { get; set; } @@ -168,10 +170,10 @@ public SizeAndPositionInformation getInfo() public void Read(ILoader loader, BinaryReader2 br) { - RearRightWheelPosition = br.ReadVector3I(); - FrontRightWheelPosition = br.ReadVector3I(); - CoMOffset = br.ReadVector3I(); - BrakeScaleToFactor = br.ReadVector3I(); + RearRightWheelPosition = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + FrontRightWheelPosition = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + CoMOffset = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + BrakeScaleToFactor = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); YawDampingOnTakeOff = br.ReadSingle(); TractionLineLength = br.ReadSingle(); TimeForFullBrake = br.ReadSingle(); @@ -235,73 +237,73 @@ public void Read(ILoader loader, BinaryReader2 br) AngularDrag = br.ReadSingle(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(RearRightWheelPosition.toBytes()); - wr.Write(FrontRightWheelPosition.toBytes()); - wr.Write(CoMOffset.toBytes()); - wr.Write(BrakeScaleToFactor.toBytes()); - wr.Write(YawDampingOnTakeOff); - wr.Write(TractionLineLength); - wr.Write(TimeForFullBrake); - wr.Write(SurfaceRoughnessFactor); - wr.Write(SurfaceRearGripFactor); - wr.Write(SurfaceFrontGripFactor); - wr.Write(SurfaceDragFactor); - wr.Write(RollLimitOnTakeOff); - wr.Write(RollDampingOnTakeOff); - wr.Write(RearWheelMass); - wr.Write(RearTireStaticFrictionCoefficient); - wr.Write(RearTireLongForceBias); - wr.Write(RearTireDynamicFrictionCoefficient); - wr.Write(RearTireAdhesiveLimit); - wr.Write(RearLongGripCurvePeakSlipRatio); - wr.Write(RearLongGripCurvePeakCoefficient); - wr.Write(RearLongGripCurveFloorSlipRatio); - wr.Write(RearLongGripCurveFallCoefficient); - wr.Write(RearLatGripCurvePeakSlipRatio); - wr.Write(RearLatGripCurvePeakCoefficient); - wr.Write(RearLatGripCurveFloorSlipRatio); - wr.Write(RearLatGripCurveFallCoefficient); - wr.Write(RearLatGripCurveDriftPeakSlipRatio); - wr.Write(PowerToRear); - wr.Write(PowerToFront); - wr.Write(PitchDampingOnTakeOff); - wr.Write(MaxSpeed); - wr.Write(MagicBrakeFactorTurning); - wr.Write(MagicBrakeFactorStraightLine); - wr.Write(LowSpeedTyreFrictionTractionControl); - wr.Write(LowSpeedThrottleTractionControl); - wr.Write(LowSpeedDrivingSpeed); - wr.Write(LockBrakeScale); - wr.Write(LinearDrag); - wr.Write(HighSpeedAngularDamping); - wr.Write(FrontWheelMass); - wr.Write(FrontTireStaticFrictionCoefficient); - wr.Write(FrontTireLongForceBias); - wr.Write(FrontTireDynamicFrictionCoefficient); - wr.Write(FrontTireAdhesiveLimit); - wr.Write(FrontLongGripCurvePeakSlipRatio); - wr.Write(FrontLongGripCurvePeakCoefficient); - wr.Write(FrontLongGripCurveFloorSlipRatio); - wr.Write(FrontLongGripCurveFallCoefficient); - wr.Write(FrontLatGripCurvePeakSlipRatio); - wr.Write(FrontLatGripCurvePeakCoefficient); - wr.Write(FrontLatGripCurveFloorSlipRatio); - wr.Write(FrontLatGripCurveFallCoefficient); - wr.Write(FrontLatGripCurveDriftPeakSlipRatio); - wr.Write(DrivingMass); - wr.Write(DriveTimeDeformLimitX); - wr.Write(DriveTimeDeformLimitPosZ); - wr.Write(DriveTimeDeformLimitNegZ); - wr.Write(DriveTimeDeformLimitNegY); - wr.Write(DownForceZOffset); - wr.Write(DownForce); - wr.Write(CrashExtraYawVelocityFactor); - wr.Write(CrashExtraRollVelocityFactor); - wr.Write(CrashExtraPitchVelocityFactor); - wr.Write(CrashExtraLinearVelocityFactor); - wr.Write(AngularDrag); + bw.Write(RearRightWheelPosition.toBytes(bw.BigEndian)); + bw.Write(FrontRightWheelPosition.toBytes(bw.BigEndian)); + bw.Write(CoMOffset.toBytes(bw.BigEndian)); + bw.Write(BrakeScaleToFactor.toBytes(bw.BigEndian)); + bw.Write(YawDampingOnTakeOff); + bw.Write(TractionLineLength); + bw.Write(TimeForFullBrake); + bw.Write(SurfaceRoughnessFactor); + bw.Write(SurfaceRearGripFactor); + bw.Write(SurfaceFrontGripFactor); + bw.Write(SurfaceDragFactor); + bw.Write(RollLimitOnTakeOff); + bw.Write(RollDampingOnTakeOff); + bw.Write(RearWheelMass); + bw.Write(RearTireStaticFrictionCoefficient); + bw.Write(RearTireLongForceBias); + bw.Write(RearTireDynamicFrictionCoefficient); + bw.Write(RearTireAdhesiveLimit); + bw.Write(RearLongGripCurvePeakSlipRatio); + bw.Write(RearLongGripCurvePeakCoefficient); + bw.Write(RearLongGripCurveFloorSlipRatio); + bw.Write(RearLongGripCurveFallCoefficient); + bw.Write(RearLatGripCurvePeakSlipRatio); + bw.Write(RearLatGripCurvePeakCoefficient); + bw.Write(RearLatGripCurveFloorSlipRatio); + bw.Write(RearLatGripCurveFallCoefficient); + bw.Write(RearLatGripCurveDriftPeakSlipRatio); + bw.Write(PowerToRear); + bw.Write(PowerToFront); + bw.Write(PitchDampingOnTakeOff); + bw.Write(MaxSpeed); + bw.Write(MagicBrakeFactorTurning); + bw.Write(MagicBrakeFactorStraightLine); + bw.Write(LowSpeedTyreFrictionTractionControl); + bw.Write(LowSpeedThrottleTractionControl); + bw.Write(LowSpeedDrivingSpeed); + bw.Write(LockBrakeScale); + bw.Write(LinearDrag); + bw.Write(HighSpeedAngularDamping); + bw.Write(FrontWheelMass); + bw.Write(FrontTireStaticFrictionCoefficient); + bw.Write(FrontTireLongForceBias); + bw.Write(FrontTireDynamicFrictionCoefficient); + bw.Write(FrontTireAdhesiveLimit); + bw.Write(FrontLongGripCurvePeakSlipRatio); + bw.Write(FrontLongGripCurvePeakCoefficient); + bw.Write(FrontLongGripCurveFloorSlipRatio); + bw.Write(FrontLongGripCurveFallCoefficient); + bw.Write(FrontLatGripCurvePeakSlipRatio); + bw.Write(FrontLatGripCurvePeakCoefficient); + bw.Write(FrontLatGripCurveFloorSlipRatio); + bw.Write(FrontLatGripCurveFallCoefficient); + bw.Write(FrontLatGripCurveDriftPeakSlipRatio); + bw.Write(DrivingMass); + bw.Write(DriveTimeDeformLimitX); + bw.Write(DriveTimeDeformLimitPosZ); + bw.Write(DriveTimeDeformLimitNegZ); + bw.Write(DriveTimeDeformLimitNegY); + bw.Write(DownForceZOffset); + bw.Write(DownForce); + bw.Write(CrashExtraYawVelocityFactor); + bw.Write(CrashExtraRollVelocityFactor); + bw.Write(CrashExtraPitchVelocityFactor); + bw.Write(CrashExtraLinearVelocityFactor); + bw.Write(AngularDrag); } } } diff --git a/VaultFormat/Physicsvehiclebodyrollattribs.cs b/VaultFormat/Physicsvehiclebodyrollattribs.cs index c0ff815..c08d377 100644 --- a/VaultFormat/Physicsvehiclebodyrollattribs.cs +++ b/VaultFormat/Physicsvehiclebodyrollattribs.cs @@ -72,19 +72,19 @@ public void Read(ILoader loader, BinaryReader2 br) br.SkipUniquePadding(4); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(WheelLongForceHeightOffset); - wr.Write(WheelLatForceHeightOffset); - wr.Write(WeightTransferDecayZ); - wr.Write(WeightTransferDecayX); - wr.Write(RollSpringStiffness); - wr.Write(RollSpringDampening); - wr.Write(PitchSpringStiffness); - wr.Write(PitchSpringDampening); - wr.Write(FactorOfWeightZ); - wr.Write(FactorOfWeightX); - wr.WriteUniquePadding(4); + bw.Write(WheelLongForceHeightOffset); + bw.Write(WheelLatForceHeightOffset); + bw.Write(WeightTransferDecayZ); + bw.Write(WeightTransferDecayX); + bw.Write(RollSpringStiffness); + bw.Write(RollSpringDampening); + bw.Write(PitchSpringStiffness); + bw.Write(PitchSpringDampening); + bw.Write(FactorOfWeightZ); + bw.Write(FactorOfWeightX); + bw.WriteUniquePadding(4); } } } diff --git a/VaultFormat/Physicsvehicleboostattribs.cs b/VaultFormat/Physicsvehicleboostattribs.cs index 854ef16..314ba11 100644 --- a/VaultFormat/Physicsvehicleboostattribs.cs +++ b/VaultFormat/Physicsvehicleboostattribs.cs @@ -88,24 +88,24 @@ public void Read(ILoader loader, BinaryReader2 br) BlueBoostBase = br.ReadSingle(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(MaxBoostSpeed); - wr.Write(BoostRule); - wr.Write(BoostKickTime); - wr.Write(BoostKickMinTime); - wr.Write(BoostKickMaxTime); - wr.Write(BoostKickMaxStartSpeed); - wr.Write(BoostKickHeightOffset); - wr.Write(BoostKickAcceleration); - wr.Write(BoostKick); - wr.Write(BoostHeightOffset); - wr.Write(BoostBase); - wr.Write(BoostAcceleration); - wr.Write(BlueMaxBoostSpeed); - wr.Write(BlueBoostKickTime); - wr.Write(BlueBoostKick); - wr.Write(BlueBoostBase); + bw.Write(MaxBoostSpeed); + bw.Write(BoostRule); + bw.Write(BoostKickTime); + bw.Write(BoostKickMinTime); + bw.Write(BoostKickMaxTime); + bw.Write(BoostKickMaxStartSpeed); + bw.Write(BoostKickHeightOffset); + bw.Write(BoostKickAcceleration); + bw.Write(BoostKick); + bw.Write(BoostHeightOffset); + bw.Write(BoostBase); + bw.Write(BoostAcceleration); + bw.Write(BlueMaxBoostSpeed); + bw.Write(BlueBoostKickTime); + bw.Write(BlueBoostKick); + bw.Write(BlueBoostBase); } } } diff --git a/VaultFormat/Physicsvehiclecollisionattribs.cs b/VaultFormat/Physicsvehiclecollisionattribs.cs index af89053..74b56cf 100644 --- a/VaultFormat/Physicsvehiclecollisionattribs.cs +++ b/VaultFormat/Physicsvehiclecollisionattribs.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; +using BundleFormat; using BundleUtilities; namespace VaultFormat @@ -11,7 +13,7 @@ public class Physicsvehiclecollisionattribs : IAttribute public AttributeHeader header { get; set; } public SizeAndPositionInformation info { get; set; } - public Vector3I BodyBox { get; set; } + public Vector4 BodyBox { get; set; } public Physicsvehiclecollisionattribs(SizeAndPositionInformation chunk, AttributeHeader dataChunk) { header = dataChunk; @@ -40,12 +42,12 @@ public SizeAndPositionInformation getInfo() public void Read(ILoader loader, BinaryReader2 br) { - BodyBox = br.ReadVector3I(); + BodyBox = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(BodyBox.toBytes()); + bw.Write(BodyBox.toBytes(bw.BigEndian)); } } } diff --git a/VaultFormat/Physicsvehicledriftattribs.cs b/VaultFormat/Physicsvehicledriftattribs.cs index 1091d29..6286c41 100644 --- a/VaultFormat/Physicsvehicledriftattribs.cs +++ b/VaultFormat/Physicsvehicledriftattribs.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; +using BundleFormat; using BundleUtilities; namespace VaultFormat @@ -11,7 +13,7 @@ public class Physicsvehicledriftattribs : IAttribute public AttributeHeader header { get; set; } public SizeAndPositionInformation info { get; set; } - public Vector3I DriftScaleToYawTorque { get; set; } + public Vector4 DriftScaleToYawTorque { get; set; } public float WheelSlip { get; set; } public float TimeToCapScale { get; set; } public float TimeForNaturalDrift { get; set; } @@ -109,7 +111,7 @@ public SizeAndPositionInformation getInfo() public void Read(ILoader loader, BinaryReader2 br) { - DriftScaleToYawTorque = br.ReadVector3I(); + DriftScaleToYawTorque = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); WheelSlip = br.ReadSingle(); TimeToCapScale = br.ReadSingle(); TimeForNaturalDrift = br.ReadSingle(); @@ -147,45 +149,44 @@ public void Read(ILoader loader, BinaryReader2 br) br.SkipPadding(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(DriftScaleToYawTorque.toBytes()); - wr.Write(WheelSlip); - wr.Write(TimeToCapScale); - wr.Write(TimeForNaturalDrift); - wr.Write(SteeringDriftScaleFactor); - wr.Write(SideForcePeakDriftAngle); - wr.Write(SideForceMagnitude); - wr.Write(SideForceDriftSpeedCutOff); - wr.Write(SideForceDriftAngleCutOff); - wr.Write(SideForceDirftScaleCutOff); - wr.Write(NeutralTimeToReduceDrift); - wr.Write(NaturalYawTorqueCutOffAngle); - wr.Write(NaturalYawTorque); - wr.Write(NaturalDriftTimeToReachBaseSlip); - wr.Write(NaturalDriftStartSlip); - wr.Write(NaturalDriftScaleDecay); - wr.Write(MinSpeedForDrift); - wr.Write(InitialDriftPushTime); - wr.Write(InitialDriftPushScaleLimit); - wr.Write(InitialDriftPushDynamicInc); - wr.Write(InitialDriftPushBaseInc); - wr.Write(GripFromSteering); - wr.Write(GripFromGasLetOff); - wr.Write(GripFromBrake); - wr.Write(GasDriftScaleFactor); - wr.Write(ForcedDriftTimeToReachBaseSlip); - wr.Write(ForcedDriftStartSlip); - wr.Write(DriftTorqueFallOff); - wr.Write(DriftSidewaysDamping); - wr.Write(DriftMaxAngle); - wr.Write(DriftAngularDamping); - wr.Write(CounterSteeringDriftScaleFactor); - wr.Write(CappedScale); - wr.Write(BrakingDriftScaleFactor); - wr.Write(BaseCounterSteeringDriftScaleFactor); - wr.WritePadding(); + bw.Write(DriftScaleToYawTorque.toBytes(bw.BigEndian)); + bw.Write(WheelSlip); + bw.Write(TimeToCapScale); + bw.Write(TimeForNaturalDrift); + bw.Write(SteeringDriftScaleFactor); + bw.Write(SideForcePeakDriftAngle); + bw.Write(SideForceMagnitude); + bw.Write(SideForceDriftSpeedCutOff); + bw.Write(SideForceDriftAngleCutOff); + bw.Write(SideForceDirftScaleCutOff); + bw.Write(NeutralTimeToReduceDrift); + bw.Write(NaturalYawTorqueCutOffAngle); + bw.Write(NaturalYawTorque); + bw.Write(NaturalDriftTimeToReachBaseSlip); + bw.Write(NaturalDriftStartSlip); + bw.Write(NaturalDriftScaleDecay); + bw.Write(MinSpeedForDrift); + bw.Write(InitialDriftPushTime); + bw.Write(InitialDriftPushScaleLimit); + bw.Write(InitialDriftPushDynamicInc); + bw.Write(InitialDriftPushBaseInc); + bw.Write(GripFromSteering); + bw.Write(GripFromGasLetOff); + bw.Write(GripFromBrake); + bw.Write(GasDriftScaleFactor); + bw.Write(ForcedDriftTimeToReachBaseSlip); + bw.Write(ForcedDriftStartSlip); + bw.Write(DriftTorqueFallOff); + bw.Write(DriftSidewaysDamping); + bw.Write(DriftMaxAngle); + bw.Write(DriftAngularDamping); + bw.Write(CounterSteeringDriftScaleFactor); + bw.Write(CappedScale); + bw.Write(BrakingDriftScaleFactor); + bw.Write(BaseCounterSteeringDriftScaleFactor); + bw.WritePadding(); } - } } diff --git a/VaultFormat/Physicsvehicleengineattribs.cs b/VaultFormat/Physicsvehicleengineattribs.cs index 6ec538a..6190574 100644 --- a/VaultFormat/Physicsvehicleengineattribs.cs +++ b/VaultFormat/Physicsvehicleengineattribs.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; -using System.IO; +using System.Numerics; +using BundleFormat; using BundleUtilities; namespace VaultFormat @@ -10,12 +11,12 @@ public class Physicsvehicleengineattribs : IAttribute public AttributeHeader header { get; set; } public SizeAndPositionInformation info { get; set; } - public Vector3I TorqueScales2 { get; set; } - public Vector3I TorqueScales1 { get; set; } - public Vector3I GearUpRPMs2 { get; set; } - public Vector3I GearUpRPMs1 { get; set; } - public Vector3I GearRatios2 { get; set; } - public Vector3I GearRatios1 { get; set; } + public Vector4 TorqueScales2 { get; set; } + public Vector4 TorqueScales1 { get; set; } + public Vector4 GearUpRPMs2 { get; set; } + public Vector4 GearUpRPMs1 { get; set; } + public Vector4 GearRatios2 { get; set; } + public Vector4 GearRatios1 { get; set; } public float TransmissionEfficiency { get; set; } public float TorqueFallOffRPM { get; set; } public float MaxTorque { get; set; } @@ -59,36 +60,36 @@ public int getDataSize() return CountingUtilities.AddPadding(bytes); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(TorqueScales2.toBytes()); - wr.Write(TorqueScales1.toBytes()); - wr.Write(GearUpRPMs2.toBytes()); - wr.Write(GearUpRPMs1.toBytes()); - wr.Write(GearRatios2.toBytes()); - wr.Write(GearRatios1.toBytes()); - wr.Write(TransmissionEfficiency); - wr.Write(TorqueFallOffRPM); - wr.Write(MaxTorque); - wr.Write(MaxRPM); - wr.Write(LSDMGearUpSpeed); - wr.Write(GearChangeTime); - wr.Write(FlyWheelInertia); - wr.Write(FlyWheelFriction); - wr.Write(EngineResistance); - wr.Write(EngineLowEndTorqueFactor); - wr.Write(EngineBraking); - wr.Write(Differential); + bw.Write(TorqueScales2.toBytes(bw.BigEndian)); + bw.Write(TorqueScales1.toBytes(bw.BigEndian)); + bw.Write(GearUpRPMs2.toBytes(bw.BigEndian)); + bw.Write(GearUpRPMs1.toBytes(bw.BigEndian)); + bw.Write(GearRatios2.toBytes(bw.BigEndian)); + bw.Write(GearRatios1.toBytes(bw.BigEndian)); + bw.Write(TransmissionEfficiency); + bw.Write(TorqueFallOffRPM); + bw.Write(MaxTorque); + bw.Write(MaxRPM); + bw.Write(LSDMGearUpSpeed); + bw.Write(GearChangeTime); + bw.Write(FlyWheelInertia); + bw.Write(FlyWheelFriction); + bw.Write(EngineResistance); + bw.Write(EngineLowEndTorqueFactor); + bw.Write(EngineBraking); + bw.Write(Differential); } public void Read(ILoader loader, BinaryReader2 br) { - TorqueScales2 = br.ReadVector3I(); - TorqueScales1 = br.ReadVector3I(); - GearUpRPMs2 = br.ReadVector3I(); - GearUpRPMs1 = br.ReadVector3I(); - GearRatios2 = br.ReadVector3I(); - GearRatios1 = br.ReadVector3I(); + TorqueScales2 = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + TorqueScales1 = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + GearUpRPMs2 = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + GearUpRPMs1 = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + GearRatios2 = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); + GearRatios1 = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); TransmissionEfficiency = br.ReadSingle(); TorqueFallOffRPM = br.ReadSingle(); MaxTorque = br.ReadSingle(); diff --git a/VaultFormat/Physicsvehiclehandling.cs b/VaultFormat/Physicsvehiclehandling.cs index 46e2302..5955fe9 100644 --- a/VaultFormat/Physicsvehiclehandling.cs +++ b/VaultFormat/Physicsvehiclehandling.cs @@ -63,16 +63,16 @@ public void Read(ILoader loader, BinaryReader2 br) PhysicsVehicleBaseAttribs = new RefSpec(loader, br); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - PhysicsVehicleSuspensionAttribs.Write(wr); - PhysicsVehicleSteeringAttribs.Write(wr); - PhysicsVehicleEngineAttribs.Write(wr); - PhysicsVehicleDriftAttribs.Write(wr); - PhysicsVehicleCollisionAttribs.Write(wr); - PhysicsVehicleBoostAttribs.Write(wr); - PhysicsVehicleBodyRollAttribs.Write(wr); - PhysicsVehicleBaseAttribs.Write(wr); + PhysicsVehicleSuspensionAttribs.Write(bw); + PhysicsVehicleSteeringAttribs.Write(bw); + PhysicsVehicleEngineAttribs.Write(bw); + PhysicsVehicleDriftAttribs.Write(bw); + PhysicsVehicleCollisionAttribs.Write(bw); + PhysicsVehicleBoostAttribs.Write(bw); + PhysicsVehicleBodyRollAttribs.Write(bw); + PhysicsVehicleBaseAttribs.Write(bw); } } } diff --git a/VaultFormat/Physicsvehiclesteeringattribs.cs b/VaultFormat/Physicsvehiclesteeringattribs.cs index ba8d98b..e356604 100644 --- a/VaultFormat/Physicsvehiclesteeringattribs.cs +++ b/VaultFormat/Physicsvehiclesteeringattribs.cs @@ -85,23 +85,23 @@ public void Read(ILoader loader, BinaryReader2 br) br.SkipPadding(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(TimeForLock); - wr.Write(StraightReactionBias); - wr.Write(SpeedForMinAngle); - wr.Write(SpeedForMaxAngle); - wr.Write(MinAngle); - wr.Write(MaxAngle); - wr.Write(AiPidCoefficientP); - wr.Write(AiPidCoefficientI); - wr.Write(AiPidCoefficientDriftP); - wr.Write(AiPidCoefficientDriftI); - wr.Write(AiPidCoefficientDriftD); - wr.Write(AiPidCoefficientD); - wr.Write(AiMinLookAheadDistanceForDrift); - wr.Write(AiLookAheadTimeForDrift); - wr.WritePadding(); + bw.Write(TimeForLock); + bw.Write(StraightReactionBias); + bw.Write(SpeedForMinAngle); + bw.Write(SpeedForMaxAngle); + bw.Write(MinAngle); + bw.Write(MaxAngle); + bw.Write(AiPidCoefficientP); + bw.Write(AiPidCoefficientI); + bw.Write(AiPidCoefficientDriftP); + bw.Write(AiPidCoefficientDriftI); + bw.Write(AiPidCoefficientDriftD); + bw.Write(AiPidCoefficientD); + bw.Write(AiMinLookAheadDistanceForDrift); + bw.Write(AiLookAheadTimeForDrift); + bw.WritePadding(); } } } diff --git a/VaultFormat/Physicsvehiclesuspensionattribs.cs b/VaultFormat/Physicsvehiclesuspensionattribs.cs index 8b5627f..6d3744a 100644 --- a/VaultFormat/Physicsvehiclesuspensionattribs.cs +++ b/VaultFormat/Physicsvehiclesuspensionattribs.cs @@ -103,21 +103,21 @@ public void Read(ILoader loader, BinaryReader2 br) Dampening = br.ReadSingle(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(UpwardMovement); - wr.Write(TimeToDampAfterLanding); - wr.Write(Strength); - wr.Write(SpringLength); - wr.Write(RearHeight); - wr.Write(MaxYawDampingOnLanding); - wr.Write(MaxVertVelocityDampingOnLanding); - wr.Write(MaxRollDampingOnLanding); - wr.Write(MaxPitchDampingOnLanding); - wr.Write(InAirDamping); - wr.Write(FrontHeight); - wr.Write(DownwardMovement); - wr.Write(Dampening); + bw.Write(UpwardMovement); + bw.Write(TimeToDampAfterLanding); + bw.Write(Strength); + bw.Write(SpringLength); + bw.Write(RearHeight); + bw.Write(MaxYawDampingOnLanding); + bw.Write(MaxVertVelocityDampingOnLanding); + bw.Write(MaxRollDampingOnLanding); + bw.Write(MaxPitchDampingOnLanding); + bw.Write(InAirDamping); + bw.Write(FrontHeight); + bw.Write(DownwardMovement); + bw.Write(Dampening); } } } diff --git a/VaultFormat/RefSpec.cs b/VaultFormat/RefSpec.cs index c8a8667..1bdd1bc 100644 --- a/VaultFormat/RefSpec.cs +++ b/VaultFormat/RefSpec.cs @@ -44,12 +44,12 @@ private byte[] AddPadding(List bytes) bytes.Add(padding.ToArray()); return bytes.SelectMany(i => i).ToArray(); } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { - wr.Write(ClassKey); - wr.Write(CollectionKey); - wr.Write(CollectionPtr); - wr.WriteUniquePadding(4); + bw.Write(ClassKey); + bw.Write(CollectionKey); + bw.Write(CollectionPtr); + bw.WriteUniquePadding(4); } diff --git a/VaultFormat/UnimplementedAttribs.cs b/VaultFormat/UnimplementedAttribs.cs index 58f2bf0..aa4c486 100644 --- a/VaultFormat/UnimplementedAttribs.cs +++ b/VaultFormat/UnimplementedAttribs.cs @@ -1,4 +1,3 @@ -using System.IO; using BundleUtilities; namespace VaultFormat @@ -11,8 +10,8 @@ public class UnimplementedAttribs : IAttribute public UnimplementedAttribs(SizeAndPositionInformation chunk, AttributeHeader dataChunk) { - this.info = chunk; - this.header = dataChunk; + info = chunk; + header = dataChunk; } public int getDataSize() @@ -32,10 +31,12 @@ public SizeAndPositionInformation getInfo() public void Read(ILoader loader, BinaryReader2 br) { + } - public void Write(BinaryWriter wr) + public void Write(BinaryWriter2 bw) { + } } } diff --git a/VehicleList/Extensions.cs b/VehicleList/Extensions.cs deleted file mode 100644 index 666352d..0000000 --- a/VehicleList/Extensions.cs +++ /dev/null @@ -1,73 +0,0 @@ -using BundleUtilities; -using System.IO; -using System.Text; - -namespace VehicleList -{ - public static class Extensions - { - public static string MakeString(this byte[] self, int len, bool trim = false) - { - StringBuilder sb = new StringBuilder(); - - if (len < self.Length) - len = self.Length; - - for (int i = 0; i < len; i++) - { - if (i >= self.Length) - { - sb.Append("00"); - } - else - { - if ((self[i] == 0x20 || self[i] == 0x00) && trim) - sb.Append(" "); - else - sb.Append(self[i].ToString("X2")); - } - } - - return sb.ToString(); - } - - public static EncryptedString ReadEncryptedString(this BinaryReader self) - { - ulong value = self.ReadUInt64(); - EncryptedString id = new EncryptedString(value); - return id; - } - - public static void WriteEncryptedString(this BinaryWriter self, EncryptedString id, bool xbox = false) - { - ulong value = id.Encrypted; - if (xbox) - value = Util.ReverseBytes(value); - self.Write(value); - } - - public static void WriteLenString(this BinaryWriter self, string s, int len, bool console = false) - { - if (console) - { - for (int i = len; i >= 0; i--) - { - if (i < s.Length) - self.Write((byte)s[i]); - else - self.Write((byte)0); - } - } - else - { - for (int i = 0; i < len; i++) - { - if (i < s.Length) - self.Write((byte)s[i]); - else - self.Write((byte)0); - } - } - } - } -} diff --git a/VehicleList/Util.cs b/VehicleList/Util.cs deleted file mode 100644 index 22a1fa3..0000000 --- a/VehicleList/Util.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; -using System.IO; - -namespace VehicleList -{ - public static class Util - { - // reverse byte order (16-bit) - public static UInt16 ReverseBytes(UInt16 value) - { - return (UInt16)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8); - } - - // reverse byte order (32-bit) - public static UInt32 ReverseBytes(UInt32 value) - { - return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 | - (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24; - } - - // reverse byte order (64-bit) - public static UInt64 ReverseBytes(UInt64 value) - { - return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 | - (value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 | - (value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 | - (value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56; - } - - // reverse byte order (16-bit) - public static Int16 ReverseBytes(Int16 value) - { - UInt16 v = (UInt16)value; - return (Int16)((v & 0xFFU) << 8 | (v & 0xFF00U) >> 8); - } - - // reverse byte order (32-bit) - public static Int32 ReverseBytes(Int32 value) - { - UInt32 v = (UInt32)value; - return (Int32)((v & 0x000000FFU) << 24 | (v & 0x0000FF00U) << 8 | - (v & 0x00FF0000U) >> 8 | (v & 0xFF000000U) >> 24); - } - - // reverse byte order (64-bit) - public static Int64 ReverseBytes(Int64 value) - { - UInt64 v = (UInt64)value; - return (Int64)((v & 0x00000000000000FFUL) << 56 | (v & 0x000000000000FF00UL) << 40 | - (v & 0x0000000000FF0000UL) << 24 | (v & 0x00000000FF000000UL) << 8 | - (v & 0x000000FF00000000UL) >> 8 | (v & 0x0000FF0000000000UL) >> 24 | - (v & 0x00FF000000000000UL) >> 40 | (v & 0xFF00000000000000UL) >> 56); - } - - // reverse byte order (32-bit) - public static float ReverseBytes(float value) - { - return intToFloat(ReverseBytes(floatToInt(value))); - } - - public static byte[] ReverseBytes(byte[] value) - { - int len = value.Length; - byte[] result = new byte[len]; - - for (int i = 0; i < len; i++) - { - result[(len-1) - i] = value[i]; - } - - return result; - } - - public static uint floatToInt(float value) - { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); - bw.Write(value); - byte[] b = ms.ToArray(); - bw.Close(); - ms = new MemoryStream(b); - BinaryReader br = new BinaryReader(ms); - UInt32 v = br.ReadUInt32(); - br.Close(); - return v; - } - - public static float intToFloat(uint value) - { - MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); - bw.Write(value); - byte[] b = ms.ToArray(); - bw.Close(); - ms = new MemoryStream(b); - BinaryReader br = new BinaryReader(ms); - float v = br.ReadSingle(); - br.Close(); - return v; - } - - public static uint LOWORD(ulong val) - { - return (uint)((val) & 0xFFFFFFFF); - } - - public static uint HIWORD(ulong val) - { - return (uint)((val >> 32) & 0xFFFFFFFF); - } - - public static ulong JOIN(uint low, uint high) - { - ulong l = low; - ulong h = high; - return (h << 32) + l; - } - - public static string GetEngineFilenameByID(string engineID) - { - string encryptedNameHex = EncryptionData.EncryptString(engineID).ToString("X8"); - return encryptedNameHex + ".bundle"; - } - } -} diff --git a/VehicleList/VehicleListData.cs b/VehicleList/VehicleListData.cs index 769adf6..ad9fd47 100644 --- a/VehicleList/VehicleListData.cs +++ b/VehicleList/VehicleListData.cs @@ -178,8 +178,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) vehicle.Index = i; - vehicle.ID = br.ReadEncryptedString(); - vehicle.ParentID = br.ReadEncryptedString(); + vehicle.ID = new EncryptedString(br.ReadUInt64()); + vehicle.ParentID = new EncryptedString(br.ReadUInt64()); vehicle.WheelType = Encoding.ASCII.GetString(br.ReadBytes(32)); vehicle.CarName = Encoding.ASCII.GetString(br.ReadBytes(64)); vehicle.CarBrand = Encoding.ASCII.GetString(br.ReadBytes(32)); @@ -191,10 +191,10 @@ public bool Read(BundleEntry entry, ILoader loader = null) vehicle.DisplayStrength = br.ReadByte(); vehicle.padding0 = br.ReadInt32(); vehicle.AttribSysCollectionKey = br.ReadInt64(); - vehicle.ExhaustName = br.ReadEncryptedString(); + vehicle.ExhaustName = new EncryptedString(br.ReadUInt64()); vehicle.ExhaustID = br.ReadInt64(); vehicle.EngineID = br.ReadInt64(); - vehicle.EngineName = br.ReadEncryptedString(); + vehicle.EngineName = new EncryptedString(br.ReadUInt64()); vehicle.ClassUnlockStreamHash = (ClassUnlock)br.ReadInt32(); vehicle.padding1 = br.ReadInt32(); vehicle.CarShutdownStreamID = br.ReadInt64(); @@ -230,16 +230,13 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; - bool console = entry.Console; - // TODO: Implement Console Saving - - bw.Write(console ? Util.ReverseBytes((int)Entries.Count) : (int)Entries.Count); - bw.Write(console ? Util.ReverseBytes((int)0x10) : (int)0x10); - - bw.Write(console ? Util.ReverseBytes(Unknown1) : Unknown1); - bw.Write(console ? Util.ReverseBytes(Unknown2) : Unknown2); + bw.Write(Entries.Count); + bw.Write(0x10); + bw.Write(Unknown1); + bw.Write(Unknown2); for (int i = 0; i < Entries.Count; i++) { @@ -248,35 +245,35 @@ public bool Write(BundleEntry entry) // Need to create the correct byte before writing vehicle.VehicleAndBoostType = (byte)(((byte)vehicle.VehicleType << 4) + (byte)vehicle.BoostType); - bw.WriteEncryptedString(vehicle.ID, console); - bw.WriteEncryptedString(vehicle.ParentID, console); - bw.WriteLenString(vehicle.WheelType, 32, console); - bw.WriteLenString(vehicle.CarName, 64, console); - bw.WriteLenString(vehicle.CarBrand, 32, console); - bw.Write(console ? Util.ReverseBytes(vehicle.DamageLimit) : vehicle.DamageLimit); - bw.Write(console ? Util.ReverseBytes((uint)vehicle.Flags) : (uint)vehicle.Flags); + bw.Write(vehicle.ID.Encrypted); + bw.Write(vehicle.ParentID.Encrypted); + bw.WriteLenString(vehicle.WheelType, 32); + bw.WriteLenString(vehicle.CarName, 64); + bw.WriteLenString(vehicle.CarBrand, 32); + bw.Write(vehicle.DamageLimit); + bw.Write((uint)vehicle.Flags); bw.Write(vehicle.BoostLength); bw.Write((byte)vehicle.VehicleRank); bw.Write(vehicle.BoostCapacity); bw.Write(vehicle.DisplayStrength); bw.Write(vehicle.padding0); - bw.Write(console ? Util.ReverseBytes(vehicle.AttribSysCollectionKey) : vehicle.AttribSysCollectionKey); - bw.WriteEncryptedString(vehicle.ExhaustName, console); - bw.Write(console ? Util.ReverseBytes(vehicle.ExhaustID) : vehicle.ExhaustID); - bw.Write(console ? Util.ReverseBytes(vehicle.EngineID) : vehicle.EngineID); - bw.WriteEncryptedString(vehicle.EngineName, console); - bw.Write(console ? Util.ReverseBytes((uint)vehicle.ClassUnlockStreamHash) : (uint)vehicle.ClassUnlockStreamHash); + bw.Write(vehicle.AttribSysCollectionKey); + bw.Write(vehicle.ExhaustName.Encrypted); + bw.Write(vehicle.ExhaustID); + bw.Write(vehicle.EngineID); + bw.Write(vehicle.EngineName.Encrypted); + bw.Write((uint)vehicle.ClassUnlockStreamHash); bw.Write(vehicle.padding1); - bw.Write(console ? Util.ReverseBytes(vehicle.CarShutdownStreamID) : vehicle.CarShutdownStreamID); - bw.Write(console ? Util.ReverseBytes(vehicle.CarReleasedStreamID) : vehicle.CarReleasedStreamID); - bw.Write(console ? Util.ReverseBytes((uint)vehicle.AIMusicHash) : (uint)vehicle.AIMusicHash); + bw.Write(vehicle.CarShutdownStreamID); + bw.Write(vehicle.CarReleasedStreamID); + bw.Write((uint)vehicle.AIMusicHash); bw.Write((byte)vehicle.AIExhaustIndex); bw.Write((byte)vehicle.AIExhaustIndex2); bw.Write((byte)vehicle.AIExhaustIndex3); bw.Write(vehicle.padding2); bw.Write(vehicle.Unknown[0]); bw.Write(vehicle.Unknown[1]); - bw.Write(console ? Util.ReverseBytes((uint)vehicle.Category) : (uint)vehicle.Category); + bw.Write((uint)vehicle.Category); bw.Write(vehicle.VehicleAndBoostType); bw.Write((byte)vehicle.FinishType); bw.Write(vehicle.MaxSpeedNoBoost); diff --git a/WorldCollisionHandler/PolygonSoupList.cs b/WorldCollisionHandler/PolygonSoupList.cs index 1f633fe..4d54045 100644 --- a/WorldCollisionHandler/PolygonSoupList.cs +++ b/WorldCollisionHandler/PolygonSoupList.cs @@ -42,7 +42,7 @@ public PolygonSoupProperty Copy() return result; } - public static PolygonSoupProperty Read(BinaryReader br) + public static PolygonSoupProperty Read(BinaryReader2 br) { PolygonSoupProperty result = new PolygonSoupProperty(); @@ -61,7 +61,7 @@ public static PolygonSoupProperty Read(BinaryReader br) return result; } - public void Write(BinaryWriter bw) + public void Write(BinaryWriter2 bw) { // Cap to 0x9D64 for PC ushort unknownProperty1 = (ushort)(UnknownProperty & 0xFFFF); @@ -137,7 +137,7 @@ public PolygonSoupChunk() PropertyList = new List(); } - public static PolygonSoupChunk Read(BinaryReader br) + public static PolygonSoupChunk Read(BinaryReader2 br) { PolygonSoupChunk result = new PolygonSoupChunk(); @@ -168,7 +168,7 @@ public static PolygonSoupChunk Read(BinaryReader br) return result; } - public void Write(BinaryWriter bw) + public void Write(BinaryWriter2 bw) { long chunkStartPtr = bw.BaseStream.Position; bw.Write(Position); @@ -574,7 +574,8 @@ public bool Read(BundleEntry entry, ILoader loader = null) public bool Write(BundleEntry entry) { MemoryStream ms = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(ms); + BinaryWriter2 bw = new BinaryWriter2(ms); + bw.BigEndian = entry.Console; bw.Write(Min); bw.Write(Unknown4); diff --git a/WorldCollisionHandler/WorldCollisionPlugin.cs b/WorldCollisionHandler/WorldCollisionPlugin.cs index 62051e0..ed44f44 100644 --- a/WorldCollisionHandler/WorldCollisionPlugin.cs +++ b/WorldCollisionHandler/WorldCollisionPlugin.cs @@ -65,7 +65,7 @@ private void DumpAllCollisions(IWin32Window window, BundleArchive archive) if (entry == null) break; Stream outFile = File.Open(path + "/" + idListName + ".bin", FileMode.Create, FileAccess.Write); - BinaryWriter bw = new BinaryWriter(outFile); + BinaryWriter2 bw = new BinaryWriter2(outFile); bw.Write(entry.EntryBlocks[0].Data); bw.Flush(); bw.Close(); @@ -75,7 +75,7 @@ private void DumpAllCollisions(IWin32Window window, BundleArchive archive) if (polyEntry == null) break; Stream outFilePoly = File.Open(path + "/" + polyName + ".bin", FileMode.Create, FileAccess.Write); - BinaryWriter bwPoly = new BinaryWriter(outFilePoly); + BinaryWriter2 bwPoly = new BinaryWriter2(outFilePoly); bwPoly.Write(polyEntry.EntryBlocks[0].Data); bwPoly.Flush(); bwPoly.Close();