fix(core): Converts mobile status packets (#368)

- [X] Combines compact/extended statuses
This commit is contained in:
Kamron Batman 2020-12-28 01:13:02 -08:00 committed by GitHub
parent 8f8b650ab5
commit 3d98e6e193
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 621 additions and 537 deletions

View file

@ -174,150 +174,86 @@ namespace Server.Tests.Network
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]
public void TestMobileStatusCompact()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void TestMobileStatusCompact(bool canBeRenamed)
{
var m = new Mobile(0x1);
var m = new Mobile(0x1) { Name = "Random Mobile 1" };
m.DefaultMobileInit();
m.Str = 50;
m.Hits = 100;
m.Int = 75;
m.Mana = 100;
m.Dex = 25;
m.Stam = 100;
var canBeRenamed = false;
var expected = new MobileStatusCompact(canBeRenamed, m).Compile();
var data = new MobileStatusCompact(canBeRenamed, m).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendMobileStatusCompact(m, canBeRenamed);
Span<byte> expectedData = stackalloc byte[43];
var pos = 0;
expectedData.Write(ref pos, (byte)0x11); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, m.Serial);
expectedData.WriteAsciiFixed(ref pos, m.Name ?? "", 30);
expectedData.WriteReverseAttribute(ref pos, m.Hits, m.HitsMax, true);
expectedData.Write(ref pos, canBeRenamed);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0); // type
#endif
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Theory, InlineData(ProtocolChanges.Version70610), InlineData(ProtocolChanges.Version400a),
InlineData(ProtocolChanges.Version502b)]
public void TestMobileStatusExtended(ProtocolChanges changes)
[Theory]
[InlineData(ProtocolChanges.Version70610)]
[InlineData(ProtocolChanges.Version400a)]
[InlineData(ProtocolChanges.Version502b)]
public void TestMobileStatus(ProtocolChanges changes)
{
var beholder = new Mobile(0x1)
{
Name = "Random Mobile 1"
};
var beholder = new Mobile(0x1) { Name = "Random Mobile 1" };
beholder.DefaultMobileInit();
beholder.Str = 50;
beholder.Hits = 100;
beholder.Int = 75;
beholder.Mana = 100;
beholder.Dex = 25;
beholder.Stam = 100;
var beheld = new Mobile(0x2)
{
Name = "Random Mobile 2"
};
var beheld = new Mobile(0x2) { Name = "Random Mobile 2" };
beheld.DefaultMobileInit();
beheld.Str = 50;
beheld.Hits = 100;
beheld.Int = 75;
beheld.Mana = 100;
beheld.Dex = 25;
beheld.Stam = 100;
var ns = new NetState(null)
{
ProtocolChanges = changes
};
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
var data = new MobileStatus(beholder, beheld, ns).Compile();
var expected = new MobileStatus(beholder, beheld, ns).Compile();
ns.SendMobileStatus(beholder, beheld);
Span<byte> expectedData = stackalloc byte[121]; // Max Size
var pos = 0;
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
expectedData.Write(ref pos, (byte)0x11);
pos += 2; // Length
[Theory]
[InlineData(ProtocolChanges.Version70610)]
[InlineData(ProtocolChanges.Version400a)]
[InlineData(ProtocolChanges.Version502b)]
public void TestMobileStatusExtendedSelf(ProtocolChanges changes)
{
var m = new Mobile(0x1) { Name = "Random Mobile 1" };
m.DefaultMobileInit();
m.Str = 50;
m.Hits = 100;
m.Int = 75;
m.Mana = 100;
m.Dex = 25;
m.Stam = 100;
int type;
var notSelf = beholder != beheld;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = changes;
if (notSelf)
{
type = 0;
}
else if (Core.HS && ns.ExtendedStatus)
{
type = 6;
}
else if (Core.ML && ns.SupportsExpansion(Expansion.ML))
{
type = 5;
}
else
{
type = Core.AOS ? 4 : 3;
}
var expected = new MobileStatusExtended(m, ns).Compile();
ns.SendMobileStatus(m, m);
expectedData.Write(ref pos, beheld.Serial);
expectedData.WriteAsciiFixed(ref pos, beheld.Name, 30);
expectedData.WriteReverseAttribute(ref pos, beheld.Hits, beheld.HitsMax, notSelf);
expectedData.Write(ref pos, beheld.CanBeRenamedBy(beheld));
expectedData.Write(ref pos, (byte)type);
if (type > 0)
{
expectedData.Write(ref pos, beheld.Female);
expectedData.Write(ref pos, (ushort)beheld.Str);
expectedData.Write(ref pos, (ushort)beheld.Dex);
expectedData.Write(ref pos, (ushort)beheld.Int);
expectedData.WriteReverseAttribute(ref pos, beheld.Stam, beheld.StamMax, notSelf);
expectedData.WriteReverseAttribute(ref pos, beheld.Mana, beheld.ManaMax, notSelf);
expectedData.Write(ref pos, beheld.TotalGold);
expectedData.Write(
ref pos,
(ushort)(Core.AOS ? beheld.PhysicalResistance : (int)(beheld.ArmorRating + 0.5))
);
expectedData.Write(ref pos, (ushort)(Mobile.BodyWeight + beheld.TotalWeight));
if (type >= 5)
{
expectedData.Write(ref pos, (ushort)beheld.MaxWeight);
expectedData.Write(ref pos, (byte)(beheld.Race.RaceID + 1)); // 0x00 for a non-ML enabled account
}
expectedData.Write(ref pos, (ushort)beheld.StatCap);
expectedData.Write(ref pos, (byte)beheld.Followers);
expectedData.Write(ref pos, (byte)beheld.FollowersMax);
if (type >= 4)
{
expectedData.Write(ref pos, (ushort)beheld.FireResistance);
expectedData.Write(ref pos, (ushort)beheld.ColdResistance);
expectedData.Write(ref pos, (ushort)beheld.PoisonResistance);
expectedData.Write(ref pos, (ushort)beheld.EnergyResistance);
expectedData.Write(ref pos, (ushort)beheld.Luck);
}
var min = 0;
var max = 0;
beheld.Weapon?.GetStatusDamage(beheld, out min, out max);
expectedData.Write(ref pos, (ushort)min);
expectedData.Write(ref pos, (ushort)max);
expectedData.Write(ref pos, beheld.TithingPoints);
if (type >= 6)
{
for (var i = 0; i < 15; ++i)
{
expectedData.Write(ref pos, (ushort)beheld.GetAOSStatus(i));
}
}
}
expectedData.Slice(1, 2).Write((ushort)pos); // Length
expectedData = expectedData.Slice(0, pos);
AssertThat.Equal(data, expectedData);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]

View file

@ -220,4 +220,246 @@ namespace Server.Tests.Network
}
}
}
public sealed class MobileStatusCompact : Packet
{
public MobileStatusCompact(bool canBeRenamed, Mobile m) : base(0x11)
{
EnsureCapacity(43);
Stream.Write(m.Serial);
Stream.WriteAsciiFixed(m.Name ?? "", 30);
AttributeNormalizer.WriteReverse(Stream, m.Hits, m.HitsMax);
Stream.Write(canBeRenamed);
Stream.Write((byte)0); // type
}
}
public sealed class MobileStatusExtended : Packet
{
public MobileStatusExtended(Mobile m) : this(m, m.NetState)
{
}
public MobileStatusExtended(Mobile m, NetState ns) : base(0x11)
{
var name = m.Name ?? "";
int type;
if (Core.HS && ns?.ExtendedStatus == true)
{
type = 6;
EnsureCapacity(121);
}
else if (Core.ML && ns?.SupportsExpansion(Expansion.ML) == true)
{
type = 5;
EnsureCapacity(91);
}
else
{
type = Core.AOS ? 4 : 3;
EnsureCapacity(88);
}
Stream.Write(m.Serial);
Stream.WriteAsciiFixed(name, 30);
Stream.Write((short)m.Hits);
Stream.Write((short)m.HitsMax);
Stream.Write(m.CanBeRenamedBy(m));
Stream.Write((byte)type);
Stream.Write(m.Female);
Stream.Write((short)m.Str);
Stream.Write((short)m.Dex);
Stream.Write((short)m.Int);
Stream.Write((short)m.Stam);
Stream.Write((short)m.StamMax);
Stream.Write((short)m.Mana);
Stream.Write((short)m.ManaMax);
Stream.Write(m.TotalGold);
Stream.Write((short)(Core.AOS ? m.PhysicalResistance : (int)(m.ArmorRating + 0.5)));
Stream.Write((short)(Mobile.BodyWeight + m.TotalWeight));
if (type >= 5)
{
Stream.Write((short)m.MaxWeight);
Stream.Write((byte)(m.Race?.RaceID + 1 ?? 0)); // Would be 0x00 if it's a non-ML enabled account but...
}
Stream.Write((short)m.StatCap);
Stream.Write((byte)m.Followers);
Stream.Write((byte)m.FollowersMax);
if (type >= 4)
{
Stream.Write((short)m.FireResistance); // Fire
Stream.Write((short)m.ColdResistance); // Cold
Stream.Write((short)m.PoisonResistance); // Poison
Stream.Write((short)m.EnergyResistance); // Energy
Stream.Write((short)m.Luck); // Luck
var weapon = m.Weapon;
if (weapon != null)
{
weapon.GetStatusDamage(m, out var min, out var max);
Stream.Write((short)min); // Damage min
Stream.Write((short)max); // Damage max
}
else
{
Stream.Write((short)0); // Damage min
Stream.Write((short)0); // Damage max
}
Stream.Write(m.TithingPoints);
}
if (type >= 6)
{
for (var i = 0; i < 15; ++i)
{
Stream.Write((short)m.GetAOSStatus(i));
}
}
}
}
public sealed class MobileStatus : Packet
{
public MobileStatus(Mobile beholder, Mobile beheld) : this(beholder, beheld, beheld.NetState)
{
}
public MobileStatus(Mobile beholder, Mobile beheld, NetState ns) : base(0x11)
{
var name = beheld.Name ?? "";
int type;
if (beholder != beheld)
{
type = 0;
EnsureCapacity(43);
}
else if (Core.HS && ns?.ExtendedStatus == true)
{
type = 6;
EnsureCapacity(121);
}
else if (Core.ML && ns?.SupportsExpansion(Expansion.ML) == true)
{
type = 5;
EnsureCapacity(91);
}
else
{
type = Core.AOS ? 4 : 3;
EnsureCapacity(88);
}
Stream.Write(beheld.Serial);
Stream.WriteAsciiFixed(name, 30);
if (beholder == beheld)
{
WriteAttr(beheld.Hits, beheld.HitsMax);
}
else
{
WriteAttrNorm(beheld.Hits, beheld.HitsMax);
}
Stream.Write(beheld.CanBeRenamedBy(beholder));
Stream.Write((byte)type);
if (type <= 0)
{
return;
}
Stream.Write(beheld.Female);
Stream.Write((short)beheld.Str);
Stream.Write((short)beheld.Dex);
Stream.Write((short)beheld.Int);
WriteAttr(beheld.Stam, beheld.StamMax);
WriteAttr(beheld.Mana, beheld.ManaMax);
Stream.Write(beheld.TotalGold);
Stream.Write((short)(Core.AOS ? beheld.PhysicalResistance : (int)(beheld.ArmorRating + 0.5)));
Stream.Write((short)(Mobile.BodyWeight + beheld.TotalWeight));
if (type >= 5)
{
Stream.Write((short)beheld.MaxWeight);
Stream.Write((byte)(beheld.Race.RaceID + 1)); // Would be 0x00 if it's a non-ML enabled account but...
}
Stream.Write((short)beheld.StatCap);
Stream.Write((byte)beheld.Followers);
Stream.Write((byte)beheld.FollowersMax);
if (type >= 4)
{
Stream.Write((short)beheld.FireResistance); // Fire
Stream.Write((short)beheld.ColdResistance); // Cold
Stream.Write((short)beheld.PoisonResistance); // Poison
Stream.Write((short)beheld.EnergyResistance); // Energy
Stream.Write((short)beheld.Luck); // Luck
var weapon = beheld.Weapon;
if (weapon != null)
{
weapon.GetStatusDamage(beheld, out var min, out var max);
Stream.Write((short)min); // Damage min
Stream.Write((short)max); // Damage max
}
else
{
Stream.Write((short)0); // Damage min
Stream.Write((short)0); // Damage max
}
Stream.Write(beheld.TithingPoints);
}
if (type >= 6)
{
for (var i = 0; i < 15; ++i)
{
Stream.Write((short)beheld.GetAOSStatus(i));
}
}
}
private void WriteAttr(int current, int maximum)
{
Stream.Write((short)current);
Stream.Write((short)maximum);
}
private void WriteAttrNorm(int current, int maximum)
{
AttributeNormalizer.WriteReverse(Stream, current, maximum);
}
}
}