From 1d2259abc62f4dabe26b41ed32cdcbff96dfbbcc Mon Sep 17 00:00:00 2001 From: burninrubber0 Date: Thu, 24 Aug 2023 04:08:09 -0400 Subject: [PATCH] Replace BinaryWriter instances with BinaryWriter2 Minor additional updates to AttribSys writing. Also does further cleanup and normalization. --- BaseHandlers/AptData.cs | 5 +- BaseHandlers/IDList.cs | 3 +- BaseHandlers/InstanceList.cs | 5 +- BaseHandlers/ProgressionData.cs | 5 +- BaseHandlers/StreetData.cs | 3 +- BaseHandlers/TriggerData.cs | 449 +++++++++--------- BundleFormat/BundleArchive.cs | 31 +- BundleFormat/Extensions.cs | 30 +- BundleFormat/VertexDesc.cs | 3 +- BundleUtilities/BinaryReader2.cs | 2 +- BundleUtilities/BinaryWriter2.cs | 134 ++++++ BundleUtilities/Utilities.cs | 40 -- BurnoutImage/GameImage.cs | 2 +- LangEditor/Language.cs | 3 +- LuaList/LuaList.cs | 6 +- LuaList/LuaListEntry.cs | 20 +- PVSFormat/PVS.cs | 3 +- VaultFormat/AttribSys.cs | 141 +++--- VaultFormat/Burnoutcarasset.cs | 46 +- VaultFormat/Burnoutcargraphicsasset.cs | 24 +- VaultFormat/Camerabumperbehaviour.cs | 24 +- VaultFormat/Cameraexternalbehaviour.cs | 36 +- VaultFormat/IAttribute.cs | 2 +- VaultFormat/Physicsvehiclebaseattribs.cs | 132 ++--- VaultFormat/Physicsvehiclebodyrollattribs.cs | 24 +- VaultFormat/Physicsvehicleboostattribs.cs | 34 +- VaultFormat/Physicsvehiclecollisionattribs.cs | 4 +- VaultFormat/Physicsvehicledriftattribs.cs | 75 ++- VaultFormat/Physicsvehicleengineattribs.cs | 39 +- VaultFormat/Physicsvehiclehandling.cs | 18 +- VaultFormat/Physicsvehiclesteeringattribs.cs | 32 +- .../Physicsvehiclesuspensionattribs.cs | 28 +- VaultFormat/RefSpec.cs | 10 +- VaultFormat/UnimplementedAttribs.cs | 9 +- VehicleList/Extensions.cs | 66 --- VehicleList/Util.cs | 126 ----- VehicleList/VehicleListData.cs | 49 +- WorldCollisionHandler/PolygonSoupList.cs | 7 +- WorldCollisionHandler/WorldCollisionPlugin.cs | 4 +- 39 files changed, 784 insertions(+), 890 deletions(-) create mode 100644 BundleUtilities/BinaryWriter2.cs delete mode 100644 VehicleList/Extensions.cs delete mode 100644 VehicleList/Util.cs diff --git a/BaseHandlers/AptData.cs b/BaseHandlers/AptData.cs index 51e6287..c638e8e 100644 --- a/BaseHandlers/AptData.cs +++ b/BaseHandlers/AptData.cs @@ -669,7 +669,7 @@ public static Character Read(BinaryReader2 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 f1ad657..628c642 100644 --- a/BaseHandlers/InstanceList.cs +++ b/BaseHandlers/InstanceList.cs @@ -38,7 +38,7 @@ public static ModelInstance Read(BinaryReader2 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/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 ad6f98b..3e054f2 100644 --- a/BaseHandlers/TriggerData.cs +++ b/BaseHandlers/TriggerData.cs @@ -62,9 +62,9 @@ public void Read(BinaryReader2 br) 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() @@ -101,22 +101,22 @@ public void Read(BinaryReader2 br) } } - 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].W); + 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].W); + bw.Write(StartingDirections[i].X); + bw.Write(StartingDirections[i].Y); + bw.Write(StartingDirections[i].Z); + bw.Write(StartingDirections[i].W); } } } @@ -146,17 +146,17 @@ public void Read(BinaryReader2 br) 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); } } @@ -187,14 +187,14 @@ public virtual void Read(BinaryReader2 br) 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); } } @@ -235,25 +235,25 @@ public Landmark() : base() } - 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); } } } @@ -332,16 +332,16 @@ public GenericRegion() : base() 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); } } @@ -371,13 +371,13 @@ public override void Read(BinaryReader2 br) 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); } } @@ -429,45 +429,45 @@ public void Read(BinaryReader2 br) 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); } } } @@ -510,23 +510,23 @@ public void Read(BinaryReader2 br) } // 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]); } } } @@ -549,16 +549,16 @@ public void Read(BinaryReader2 br) 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.W); - 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]); } } @@ -575,9 +575,9 @@ public override void Read(BinaryReader2 br) base.Read(br); } - public override void Write(BinaryWriter writer) + public override void Write(BinaryWriter2 bw) { - base.Write(writer); + base.Write(bw); } } @@ -610,19 +610,19 @@ public void Read(BinaryReader2 br) 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.W); - writer.Write(mDirection.X); - writer.Write(mDirection.Y); - writer.Write(mDirection.Z); - writer.Write(mDirection.W); - 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); } } @@ -800,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.W); - writer.Write(mPlayerStartDirection.X); - writer.Write(mPlayerStartDirection.Y); - writer.Write(mPlayerStartDirection.Z); - writer.Write(mPlayerStartDirection.W); - - 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 9b0ab9e..674138a 100644 --- a/BundleFormat/BundleArchive.cs +++ b/BundleFormat/BundleArchive.cs @@ -569,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); @@ -577,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]; @@ -599,7 +600,7 @@ public void Write(BinaryWriter bw) bw.BaseStream.Position += 4; } - bw.Write((int)this.Flags); + bw.Write((int)Flags); bw.Align(16); @@ -608,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); } @@ -679,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) @@ -698,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); } } diff --git a/BundleFormat/Extensions.cs b/BundleFormat/Extensions.cs index 8ba96b2..0ce0d8a 100644 --- a/BundleFormat/Extensions.cs +++ b/BundleFormat/Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.IO; using System.Numerics; @@ -117,22 +118,23 @@ public static bool Matches(this byte[] self, byte[] other) return true; } - 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); - } - - public static byte[] toBytes(this Vector4 vec) + public static byte[] toBytes(this Vector4 vec, bool swap = false) { List bytes = new List(); - bytes.AddRange(BitConverter.GetBytes(vec.X)); - bytes.AddRange(BitConverter.GetBytes(vec.Y)); - bytes.AddRange(BitConverter.GetBytes(vec.Z)); - bytes.AddRange(BitConverter.GetBytes(vec.W)); + 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/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/BundleUtilities/BinaryReader2.cs b/BundleUtilities/BinaryReader2.cs index 6b7ef2e..9506696 100644 --- a/BundleUtilities/BinaryReader2.cs +++ b/BundleUtilities/BinaryReader2.cs @@ -73,7 +73,7 @@ public override float ReadSingle() if (BaseStream.Length - BaseStream.Position < 4) throw new EndOfStreamException(); if (BigEndian) - return BinaryPrimitives.ReadSingleBigEndian(ReadBytes(8)); + return BinaryPrimitives.ReadSingleBigEndian(ReadBytes(4)); return BinaryPrimitives.ReadSingleLittleEndian(ReadBytes(4)); } 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 80ffc15..40b1ba1 100644 --- a/BundleUtilities/Utilities.cs +++ b/BundleUtilities/Utilities.cs @@ -54,14 +54,6 @@ public static ulong CalcLookup8(string text) return hashValue; } - public static void WriteEncryptedString(this BinaryWriter self, EncryptedString id, bool xbox = false) - { - ulong value = id.Encrypted; - if (xbox) - value = BinaryPrimitives.ReverseEndianness(value); - self.Write(value); - } - public static bool IsValidPath(string path) { return !string.IsNullOrEmpty(path); @@ -90,38 +82,6 @@ public static byte[] Pad(this byte[] self, int bytes) 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); - } - - } - - // 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 3527c5a..148b819 100644 --- a/BurnoutImage/GameImage.cs +++ b/BurnoutImage/GameImage.cs @@ -69,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 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 15ce3f7..0d7fd39 100644 --- a/LuaList/LuaList.cs +++ b/LuaList/LuaList.cs @@ -83,6 +83,7 @@ public bool Read(BundleEntry entry, ILoader loader = null) { MemoryStream ms = entry.MakeStream(); BinaryReader2 br = new BinaryReader2(ms); + br.BigEndian = entry.Console; version = br.ReadInt32(); br.ReadBytes(4); @@ -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 9e220a0..50be58d 100644 --- a/LuaList/LuaListEntry.cs +++ b/LuaList/LuaListEntry.cs @@ -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 f8d91d6..692b0d2 100644 --- a/VaultFormat/AttribSys.cs +++ b/VaultFormat/AttribSys.cs @@ -284,9 +284,7 @@ private void ReadChunk(ILoader loader, BinaryReader2 br) private void ReadVlt(ILoader loader, BinaryReader2 br) { while (!br.EOF()) - { ReadChunk(loader, br); - } } private void ReadBin(ILoader loader, BinaryReader2 br) @@ -319,24 +317,20 @@ private void ReadBin(ILoader loader, BinaryReader2 br) } } - private void WriteBin(BinaryWriter wr) + private void WriteBin(BinaryWriter2 bw) { try { - wr.Write(BitConverter.ToUInt32(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) { @@ -347,13 +341,9 @@ 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() @@ -423,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) { @@ -483,86 +472,81 @@ private int getSizeOfPtrN() return addPadding(bytes); } - private void WriteVlt(BinaryWriter wr) + private void WriteVlt(BinaryWriter2 bw) { try { - wr.Write(BitConverter.ToUInt32(Encoding.ASCII.GetBytes("Vers"))); - wr.Write(getSizeOfVers()); - wr.Write(VersionHash); - wr.WritePadding(); - - wr.Write(BitConverter.ToUInt32(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(BitConverter.ToUInt32(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(BitConverter.ToUInt32(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(BitConverter.ToUInt32(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(BitConverter.ToUInt32(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) { @@ -625,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/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 2d9d84e..a0a0cb4 100644 --- a/VaultFormat/Physicsvehiclebaseattribs.cs +++ b/VaultFormat/Physicsvehiclebaseattribs.cs @@ -237,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 e922867..74b56cf 100644 --- a/VaultFormat/Physicsvehiclecollisionattribs.cs +++ b/VaultFormat/Physicsvehiclecollisionattribs.cs @@ -45,9 +45,9 @@ public void Read(ILoader loader, BinaryReader2 br) 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 7c0fa8b..6286c41 100644 --- a/VaultFormat/Physicsvehicledriftattribs.cs +++ b/VaultFormat/Physicsvehicledriftattribs.cs @@ -149,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 e358543..6190574 100644 --- a/VaultFormat/Physicsvehicleengineattribs.cs +++ b/VaultFormat/Physicsvehicleengineattribs.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Numerics; using BundleFormat; using BundleUtilities; @@ -61,26 +60,26 @@ 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) 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 0fce4cd..0000000 --- a/VehicleList/Extensions.cs +++ /dev/null @@ -1,66 +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 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 52fd678..0000000 --- a/VehicleList/Util.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.IO; -using BundleUtilities; - -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); - BinaryReader2 br = new BinaryReader2(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); - BinaryReader2 br = new BinaryReader2(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 b164e4f..ad9fd47 100644 --- a/VehicleList/VehicleListData.cs +++ b/VehicleList/VehicleListData.cs @@ -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 ef27c9b..4d54045 100644 --- a/WorldCollisionHandler/PolygonSoupList.cs +++ b/WorldCollisionHandler/PolygonSoupList.cs @@ -61,7 +61,7 @@ public static PolygonSoupProperty Read(BinaryReader2 br) return result; } - public void Write(BinaryWriter bw) + public void Write(BinaryWriter2 bw) { // Cap to 0x9D64 for PC ushort unknownProperty1 = (ushort)(UnknownProperty & 0xFFFF); @@ -168,7 +168,7 @@ public static PolygonSoupChunk Read(BinaryReader2 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();