fix(core): Converts to ref struct enumerators (#426)

- [X] Converts boat moving entities to ref struct enumerator
- [X] Converts skills to a ref struct enumerator
- [X] Converts type cache to ref struct enumerator
This commit is contained in:
Kamron Batman 2021-01-22 19:41:44 -08:00 committed by GitHub
parent c6ef6ff80a
commit 9ef7752beb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 352 additions and 198 deletions

View file

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Moq;
using Server;
using Server.Items;
using Server.Multis;
using Server.Multis.Boats;
using Server.Network;
@ -18,33 +18,45 @@ namespace UOContent.Tests
[InlineData(Direction.West, 10, 100, 200)]
public void TestMoveBoatHS(Direction d, int speed, int xOffset, int yOffset)
{
var boat = new Mock<BaseBoat>((Serial)0x2);
boat.Object.Location = new Point3D(10, 20, 15);
boat.Object.Facing = Direction.Right;
var item1 = new Item((Serial)0x1000)
{
ItemID = 0x13B9,
Location = new Point3D(11, 21, 16),
Map = Map.Felucca,
Visible = true
};
var item2 = new Item((Serial)0x2000)
{
ItemID = 0x13B9,
Location = new Point3D(100, 200, 16),
Map = Map.Felucca,
Visible = true
};
// Item on the boat
var item1 = new Mock<BaseWeapon>((Serial)0x2);
item1.Setup(m => m.ItemID).Returns(0x13B9);
item1.Object.Location = new Point3D(10, 20, 15);
// Item not on the boat
var item2 = new Mock<BaseWeapon>((Serial)0x2);
item2.Setup(m => m.ItemID).Returns(0x13B9);
item2.Object.Location = new Point3D(100, 200, 15);
var beholder = new Mock<Mobile>((Serial)0x1024u);
var beholder = new Mock<Mobile>((Serial)0x100);
beholder.Object.DefaultMobileInit();
beholder.Object.Location = new Point3D(10, 20, 15);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == boat.Object))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item1.Object))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item2.Object))).Returns(false);
beholder.Object.Map = Map.Felucca;
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == beholder.Object))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item1))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item2))).Returns(false);
var list = new List<IEntity>(4) { item1.Object, item2.Object, boat.Object, beholder.Object };
var list = new List<IEntity> { item1, beholder.Object };
var notContained = new List<IEntity> { item2 };
var boat = new TestBoat(0x3000, list, notContained)
{
Location = new Point3D(10, 20, 15),
Facing = Direction.Right,
Map = Map.Felucca
};
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == boat))).Returns(true);
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = ProtocolChanges.HighSeas;
var expected = new MoveBoatHS(beholder.Object, boat.Object, d, speed, list, xOffset, yOffset).Compile();
var expected = new MoveBoatHS(beholder.Object, boat, d, speed, list, xOffset, yOffset).Compile();
ns.SendMoveBoatHS(beholder.Object, boat.Object, d, speed, list, xOffset, yOffset);
ns.SendMoveBoatHS(beholder.Object, boat, d, speed, boat.GetMovingEntities(true), xOffset, yOffset);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
@ -53,8 +65,18 @@ namespace UOContent.Tests
[Fact]
public void TestDisplayBoatHS()
{
var item1 = new Item((Serial)0x1000) { ItemID = 0x13B9, Location = new Point3D(11, 21, 16), Map = Map.Felucca };
var item2 = new Item((Serial)0x2000) { ItemID = 0x13B9, Location = new Point3D(100, 200, 16), Map = Map.Felucca };
var item1 = new Item((Serial)0x1000)
{
ItemID = 0x13B9,
Location = new Point3D(11, 21, 16),
Map = Map.Felucca
};
var item2 = new Item((Serial)0x2000)
{
ItemID = 0x13B9,
Location = new Point3D(100, 200, 16),
Map = Map.Felucca
};
var beholder = new Mock<Mobile>((Serial)0x100);
beholder.Object.DefaultMobileInit();
@ -63,22 +85,45 @@ namespace UOContent.Tests
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item1))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == item2))).Returns(false);
var boat = new Mock<BaseBoat>((Serial)0x3000);
boat.Setup(b => b.GetMovingEntities()).Returns(() => new List<IEntity>{ item1, beholder.Object });
boat.Setup(b => b.Location).Returns(new Point3D(10, 20, 15));
boat.Object.Facing = Direction.Right;
var list = new List<IEntity> { item1, beholder.Object };
var notContained = new List<IEntity> { item2 };
var boat = new TestBoat(0x3000, list, notContained)
{
Location = new Point3D(10, 20, 15),
Facing = Direction.Right
};
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == boat.Object))).Returns(true);
beholder.Setup(m => m.CanSee(It.Is<IEntity>(e => e == boat))).Returns(true);
using var ns = PacketTestUtilities.CreateTestNetState();
ns.ProtocolChanges = ProtocolChanges.HighSeas;
var expected = new DisplayBoatHS(beholder.Object, boat.Object).Compile();
var expected = new DisplayBoatHS(beholder.Object, boat).Compile();
ns.SendDisplayBoatHS(beholder.Object, boat.Object);
ns.SendDisplayBoatHS(beholder.Object, boat);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
public class TestBoat : BaseBoat
{
private readonly List<IEntity> components;
private readonly List<IEntity> notContained;
public TestBoat(Serial serial, List<IEntity> list, List<IEntity> notContainedList) : base(serial)
{
components = list;
notContained = notContainedList;
Components = new MultiComponentList(new List<MultiTileEntry>());
}
public override MultiComponentList Components { get; }
public override bool Contains(int x, int y) => !notContained.Any(e => e.X == x && e.Y == y);
public override MovingEntitiesEnumerable GetMovingEntities(bool includeBoat = false) =>
new(this, true, new Map.PooledEnumerable<IEntity>(components));
}
}
}

View file

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Server;
using Server.Collections;
using Server.Items;
@ -52,16 +53,9 @@ namespace UOContent.Tests
{
public DisplayBoatHS(Mobile beholder, BaseBoat boat) : base(0xF7)
{
var ents = boat.GetMovingEntities();
var ents = boat.GetMovingEntities(true);
ents.AddNotNull(boat.TillerMan);
ents.AddNotNull(boat.Hold);
ents.AddNotNull(boat.PPlank);
ents.AddNotNull(boat.SPlank);
ents.Add(boat);
EnsureCapacity(3 + 2 + ents.Count * 26);
EnsureCapacity(3 + 2 + 5 * 26);
Stream.Write((short)0); // count placeholder