Skip to content

Commit

Permalink
fix: parse FRepMovement with RepMoveOptionalAcceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiqan committed Nov 13, 2024
1 parent e5ab740 commit 1de9e63
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [2.4.0] - 2024-11-13
### Changed
- support .net9
- fix parsing of `FRepMovement` with `RepMoveOptionalAcceleration` (thanks @Zaid)

### Removed
- support .net6
Expand Down
7 changes: 2 additions & 5 deletions src/ConsoleReader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Diagnostics;
using System.IO;
using Unreal.Core.Models.Enums;
using static System.Environment;

// Set up dependency injection and logging services
var serviceCollection = new ServiceCollection()
Expand All @@ -16,21 +15,19 @@
var logger = provider.GetService<ILogger<Program>>();

// Define the folder containing replay files
var localAppDataFolder = GetFolderPath(SpecialFolder.LocalApplicationData);
// var replayFilesFolder = Path.Combine(localAppDataFolder, @"FortniteGame\Saved\Demos");
//var replayFilesFolder = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), @"FortniteGame\Saved\Demos");
var replayFilesFolder = @"C:\Users\ferro\Downloads\";
var replayFiles = Directory.EnumerateFiles(replayFilesFolder, "*.replay");

var sw = new Stopwatch();
long total = 0;

#if DEBUG
var reader = new ReplayReader(logger, ParseMode.Minimal);
var reader = new ReplayReader(logger, ParseMode.Normal);
#else
var reader = new ReplayReader(null, ParseMode.Minimal);
#endif

// Process each replay file
foreach (var replayFile in replayFiles)
{
sw.Restart();
Expand Down
17 changes: 13 additions & 4 deletions src/Unreal.Core.Test/NetBitReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,34 @@ public class NetBitReaderTest
// sniper rifle bullet season 11
[InlineData(new byte[] {
0x34, 0x88, 0xDF, 0x03, 0xE0, 0xE9, 0xCB, 0x3F, 0x92, 0x3B, 0x53, 0x3C,
0x47, 0x61, 0xD6, 0x01}, 122,
0x47, 0x61, 0xD6, 0x01 }, 122,
VectorQuantization.RoundWholeNumber, RotatorQuantization.ByteComponents, VectorQuantization.RoundWholeNumber)]
// supply drop season 11
[InlineData(new byte[] {
0x74, 0x20, 0x88, 0x53, 0x86, 0xDA, 0x16, 0xD8, 0x02, 0x40, 0x00, 0x38,
0x2B, 0x00}, 105,
0x2B, 0x00 }, 105,
VectorQuantization.RoundWholeNumber, RotatorQuantization.ByteComponents, VectorQuantization.RoundWholeNumber)]
// meat vehicle 11.40
[InlineData(new byte[] {
0xDA, 0x34, 0x06, 0xCA, 0x0A, 0xFE, 0x68, 0x40, 0x29, 0xBE, 0xB9, 0xFF,
0x83, 0x55, 0x1A, 0xF9, 0x47, 0xF2, 0xBD, 0xBE, 0x54, 0x3B, 0xFB, 0x88,
0xAB, 0xBF, 0x70, 0xB3, 0xCB, 0x02}, 236,
VectorQuantization.RoundWholeNumber, RotatorQuantization.ShortComponents, VectorQuantization.RoundTwoDecimals)]
[InlineData(new byte[] {
0xA0, 0x65, 0x06, 0xE5, 0x68, 0x79, 0x0F, 0x60, 0xD8, 0x85, 0xFD, 0x05,
0x15, 0x04 }, 111, VectorQuantization.RoundTwoDecimals, RotatorQuantization.ByteComponents, VectorQuantization.RoundWholeNumber,
EngineNetworkVersionHistory.CustomExports)]
public void RepMovementTest(byte[] rawData, int bitCount,
VectorQuantization locationQuantizationLevel = VectorQuantization.RoundTwoDecimals,
RotatorQuantization rotationQuantizationLevel = RotatorQuantization.ByteComponents,
VectorQuantization velocityQuantizationLevel = VectorQuantization.RoundWholeNumber)
VectorQuantization velocityQuantizationLevel = VectorQuantization.RoundWholeNumber,
EngineNetworkVersionHistory engineNetworkVersion = EngineNetworkVersionHistory.HISTORY_INITIAL)
{
var reader = new NetBitReader(rawData, bitCount);
var reader = new NetBitReader(rawData, bitCount)
{
EngineNetworkVersion = engineNetworkVersion
};

reader.SerializeRepMovement(locationQuantizationLevel, rotationQuantizationLevel, velocityQuantizationLevel);
Assert.False(reader.IsError);
Assert.True(reader.AtEnd());
Expand Down
6 changes: 3 additions & 3 deletions src/Unreal.Core/BitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BitReader : FBitArchive

public override int MarkPosition { get; protected set; }

private readonly Dictionary<FBitArchiveEndIndex, int> _tempLastBit = new();
private readonly Dictionary<FBitArchiveEndIndex, int> _tempLastBit = [];


public BitReader()
Expand Down Expand Up @@ -132,7 +132,7 @@ public override ReadOnlySpan<byte> ReadBits(int bitCount)
if (!CanRead(bitCount) || bitCount < 0)
{
IsError = true;
return ReadOnlySpan<byte>.Empty;
return [];
}

var bitCountUsedInByte = Position & 7;
Expand Down Expand Up @@ -205,7 +205,7 @@ public override ReadOnlySpan<byte> ReadBytes(int byteCount)
if (!CanRead(byteCount * 8) || byteCount < 0)
{
IsError = true;
return Span<byte>.Empty;
return [];
}

var bitCountUsedInByte = Position & 7;
Expand Down
10 changes: 10 additions & 0 deletions src/Unreal.Core/Models/FRepMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public struct FRepMovement
/// </summary>
public FRotator Rotation { get; set; }

/// <summary>
/// Acceleration of component in world space. Only valid if bRepAcceleration is set.
/// </summary>
public FVector? Acceleration { get; set; }

/// <summary>
/// If set, RootComponent should be sleeping.
/// </summary>
Expand All @@ -38,6 +43,11 @@ public struct FRepMovement
/// </summary>
public bool bRepPhysics { get; set; }

/// <summary>
/// If set, additional acceleration data will be replicated.
/// </summary>
public bool bRepAcceleration { get; set; }

/// <summary>
/// Server physics step
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion src/Unreal.Core/NetBitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public FRepMovement SerializeRepMovement(
bRepPhysics = bRepPhysics,
Location = SerializePropertyQuantizedVector(locationQuantizationLevel),
Rotation = rotationQuantizationLevel == RotatorQuantization.ByteComponents ? ReadRotation() : ReadRotationShort(),

LinearVelocity = SerializePropertyQuantizedVector(velocityQuantizationLevel)
};

Expand All @@ -79,6 +78,15 @@ public FRepMovement SerializeRepMovement(
repMovement.ServerPhysicsHandle = ReadIntPacked();
}

if (EngineNetworkVersion >= EngineNetworkVersionHistory.RepMoveOptionalAcceleration)
{
repMovement.bRepAcceleration = ReadBit();
if (repMovement.bRepAcceleration)
{
repMovement.Acceleration = SerializePropertyQuantizedVector(velocityQuantizationLevel);
}
}

return repMovement;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Unreal.Core/ReplayReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,7 +1729,7 @@ public virtual bool ReceiveProperties(FBitArchive archive, NetFieldExportGroup g
#if DEBUG
Debug("failed-properties", $"Property {export.Name} (handle: {handle}, path: {group.PathName}) caused error when reading (bits: {numBits}, group: {group.PathName})");
_cmdReader.Reset();
Debug($"cmd-{export.Name}-{numBits}", "cmds", _cmdReader.ReadBytes(Math.Max((int) Math.Ceiling(_cmdReader.GetBitsLeft() / 8.0), 1)));
Debug($"cmd-{export.Name}-{numBits}", "cmds", _cmdReader.ReadBits(_cmdReader.GetBitsLeft()));
#endif
continue;
}
Expand All @@ -1742,7 +1742,7 @@ public virtual bool ReceiveProperties(FBitArchive archive, NetFieldExportGroup g
#if DEBUG
Debug("failed-properties", $"Property {export.Name} (handle: {handle}, path: {group.PathName}) didnt read proper number of bits: {(_cmdReader.LastBit - _cmdReader.GetBitsLeft())} out of {numBits}");
_cmdReader.Reset();
Debug($"cmd-{export.Name}-{numBits}", "cmds", _cmdReader.ReadBytes(Math.Max((int) Math.Ceiling(_cmdReader.GetBitsLeft() / 8.0), 1)));
Debug($"cmd-{export.Name}-{numBits}", "cmds", _cmdReader.ReadBits(_cmdReader.GetBitsLeft()));
#endif
continue;
}
Expand Down

0 comments on commit 1de9e63

Please sign in to comment.