Skip to content

Commit

Permalink
Optimise packet reader and writer to use RecycledMemoryStream, optimi…
Browse files Browse the repository at this point in the history
…se packet buffers
  • Loading branch information
Kaioru committed Oct 12, 2023
1 parent 45e8a57 commit 5582dbf
Show file tree
Hide file tree
Showing 73 changed files with 414 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Edelstein.Protocol.Gameplay.Game.Combat;
using Edelstein.Protocol.Gameplay.Game.Objects.AffectedArea;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob;
using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Stats;
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Edelstein.Common.Constants;
using Edelstein.Protocol.Gameplay.Game.Combat;
using Edelstein.Protocol.Gameplay.Game.Objects;
using Edelstein.Protocol.Gameplay.Game.Objects.AffectedArea;
using Edelstein.Protocol.Gameplay.Game.Objects.Summoned;
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats;
Expand Down
41 changes: 19 additions & 22 deletions src/common/Edelstein.Common.Gameplay.Game/Continents/ContiMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,24 @@ IContiMoveTemplate template
.Configure(ContiMoveState.Move)
.OnEntryFromAsync(ContiMoveStateTrigger.Start, async () =>
{
using var packet = new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetStartShipMoveField)
.WriteByte((byte)ContiMoveStateTrigger.Start);

await Move(WaitField, MoveField);
await StartShipMoveField.Dispatch(
new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetStartShipMoveField)
.WriteByte((byte)ContiMoveStateTrigger.Start)
.Build()
);
await StartShipMoveField.Dispatch(packet.Build());
})
.OnExitAsync(async () =>
{
using var packet = new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetEndShipMoveField)
.WriteByte((byte)ContiMoveStateTrigger.End);

await Move(MoveField, EndField);
if (CabinField != null)
await Move(CabinField, EndField);

await EndShipMoveField.Dispatch(
new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetEndShipMoveField)
.WriteByte((byte)ContiMoveStateTrigger.End)
.Build()
);
await EndShipMoveField.Dispatch(packet.Build());

NextBoarding = NextBoarding.AddMinutes(Template.Term);
ResetEvent();
Expand All @@ -102,12 +100,11 @@ await EndShipMoveField.Dispatch(
);

// TODO: Mobspawns
using var packet = new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetMoveField)
.WriteByte((byte)ContiMoveStateTrigger.MobGen);

await MoveField.Dispatch(
new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetMoveField)
.WriteByte((byte)ContiMoveStateTrigger.MobGen)
.Build()
await MoveField.Dispatch(packet.Build()
);
})
.OnExitAsync(async () =>
Expand All @@ -118,12 +115,12 @@ await MoveField.Dispatch(
);

// TODO: Mobspawns

using var packet = new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetMoveField)
.WriteByte((byte)ContiMoveStateTrigger.MobDestroy);

await MoveField.Dispatch(
new PacketWriter(PacketSendOperations.CONTIMOVE)
.WriteByte((byte)ContiMoveTarget.TargetMoveField)
.WriteByte((byte)ContiMoveStateTrigger.MobDestroy)
.Build()
await MoveField.Dispatch(packet.Build()
);
})
.Permit(ContiMoveStateTrigger.MobDestroy, ContiMoveState.Move);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public ConversationContext(IAdapter adapter)

public async Task<T> Request<T>(IConversationMessageRequest<T> messageRequest)
{
await _adapter.Dispatch(new PacketWriter(PacketSendOperations.ScriptMessage)
.Write(messageRequest)
.Build());
using var packet = new PacketWriter(PacketSendOperations.ScriptMessage)
.Write(messageRequest);

await _adapter.Dispatch(packet.Build());

if (await _channel.Reader.ReadAsync(TokenSource.Token) is not IConversationMessageResponse<T> response)
throw new InvalidDataException("Invalid response");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ public class DialogueNPCShop : IDialogueNPCShop

public async Task<bool> HandleEnter(IFieldUser user)
{
var p = new PacketWriter(PacketSendOperations.OpenShopDlg);
using var packet = new PacketWriter(PacketSendOperations.OpenShopDlg);

p.WriteInt(Shop.ID);
p.WriteShort((short)Shop.Items.Count);
packet.WriteInt(Shop.ID);
packet.WriteShort((short)Shop.Items.Count);

foreach (var item in Shop.Items)
{
p.WriteInt(item.TemplateID);
packet.WriteInt(item.TemplateID);

p.WriteInt(item.Price);
p.WriteByte(item.DiscountRate);
packet.WriteInt(item.Price);
packet.WriteByte(item.DiscountRate);

p.WriteInt(item.TokenTemplateID);
p.WriteInt(item.TokenPrice);
packet.WriteInt(item.TokenTemplateID);
packet.WriteInt(item.TokenPrice);

p.WriteInt(item.ItemPeriod);
p.WriteInt(item.LevelLimited);
packet.WriteInt(item.ItemPeriod);
packet.WriteInt(item.LevelLimited);

if (ItemConstants.IsRechargeableItem(item.TemplateID))
p.WriteDouble(item.UnitPrice);
packet.WriteDouble(item.UnitPrice);
else
p.WriteShort((short)item.Quantity);
packet.WriteShort((short)item.Quantity);

p.WriteShort(item.MaxPerSlot);
packet.WriteShort(item.MaxPerSlot);
}

await user.Dispatch(p.Build());
await user.Dispatch(packet.Build());
return true;
}
public Task<bool> HandleLeave(IFieldUser user) => Task.FromResult(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,83 @@ public static class GameStageUserExtensions
{
public static Task DispatchInitFuncKeys(this IGameStageUser user)
{
var p = new PacketWriter(PacketSendOperations.FuncKeyMappedInit);
using var packet = new PacketWriter(PacketSendOperations.FuncKeyMappedInit);

p.WriteBool(user.Character?.FuncKeys.Records.Count == 0);
packet.WriteBool(user.Character?.FuncKeys.Records.Count == 0);
if (user.Character?.FuncKeys.Records.Count > 0)
{
for (byte i = 0; i < 90; i++)
{
if (user.Character.FuncKeys.Records.TryGetValue(i, out var value))
{
p.WriteByte(value.Type);
p.WriteInt(value.Action);
packet.WriteByte(value.Type);
packet.WriteInt(value.Action);
}
else
{
p.WriteByte(0);
p.WriteInt(0);
packet.WriteByte(0);
packet.WriteInt(0);
}
}
}

return user.Dispatch(p.Build());
return user.Dispatch(packet.Build());
}

public static Task DispatchInitQuickSlotKeys(this IGameStageUser user)
{
var p = new PacketWriter(PacketSendOperations.QuickslotMappedInit);
using var packet = new PacketWriter(PacketSendOperations.QuickslotMappedInit);

p.WriteBool(user.Character?.QuickslotKeys.Records.Count > 0);
packet.WriteBool(user.Character?.QuickslotKeys.Records.Count > 0);
if (user.Character?.QuickslotKeys.Records.Count > 0)
for (byte i = 0; i < 8; i++)
p.WriteInt(user.Character.QuickslotKeys.Records.TryGetValue(i, out var value)
packet.WriteInt(user.Character.QuickslotKeys.Records.TryGetValue(i, out var value)
? value
: 0
);
return user.Dispatch(p.Build());
return user.Dispatch(packet.Build());
}

public async static Task DispatchInitFriends(this IGameStageUser user)
{
if (user.Friends != null)
{
var p = new PacketWriter(PacketSendOperations.FriendResult);
using var packet = new PacketWriter(PacketSendOperations.FriendResult);

p.WriteByte((byte)FriendResultOperations.LoadFriend_Done);
p.WriteByte((byte)user.Friends.Records.Count);
packet.WriteByte((byte)FriendResultOperations.LoadFriend_Done);
packet.WriteByte((byte)user.Friends.Records.Count);
foreach (var record in user.Friends.Records.Values)
p.WriteFriendInfo(record);
packet.WriteFriendInfo(record);
foreach (var _ in user.Friends.Records.Values)
p.WriteInt(0);
await user.Dispatch(p.Build());
packet.WriteInt(0);
await user.Dispatch(packet.Build());
}
}

public async static Task DispatchInitParty(this IGameStageUser user)
{
if (user.Party != null)
{
var p = new PacketWriter(PacketSendOperations.PartyResult);
p.WriteByte((byte)PartyResultOperations.LoadPartyDone);
p.WriteInt(user.Party.ID);
p.WritePartyInfo(user.Party);
await user.Dispatch(p.Build());
using var packet = new PacketWriter(PacketSendOperations.PartyResult);
packet.WriteByte((byte)PartyResultOperations.LoadPartyDone);
packet.WriteInt(user.Party.ID);
packet.WritePartyInfo(user.Party);
await user.Dispatch(packet.Build());
}
}

public async static Task DispatchInitQuestTime(this IGameStageUser user)
{
var records = await user.Context.Managers.QuestTime.RetrieveAll();
var p = new PacketWriter(PacketSendOperations.SetQuestTime);
using var packet = new PacketWriter(PacketSendOperations.SetQuestTime);

p.WriteByte((byte)records.Count);
packet.WriteByte((byte)records.Count);
foreach (var record in records)
{
p.WriteInt(record.ID);
p.WriteDateTime(record.DateStart);
p.WriteDateTime(record.DateEnd);
packet.WriteInt(record.ID);
packet.WriteDateTime(record.DateStart);
packet.WriteDateTime(record.DateEnd);
}
await user.Dispatch(p.Build());
await user.Dispatch(packet.Build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ protected override async Task Handle(IFieldUser user, IPacketReader reader)
if (canUseCommonCommand)
{
var hidden = reader.ReadBool();
using var packet = new PacketWriter(PacketSendOperations.AdminResult)
.WriteByte(0x12)
.WriteBool(hidden);

await user.Hide(hidden);
await user.Dispatch(new PacketWriter(PacketSendOperations.AdminResult)
.WriteByte(0x12)
.WriteBool(hidden)
.Build());
await user.Dispatch(packet.Build());
}
break;
case 0x1F: // /job <arg1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketFriendAcceptRequest

if (response.Result == FriendResult.Success) return;

var p = new PacketWriter(PacketSendOperations.FriendResult);
p.WriteByte((byte)FriendResultOperations.AcceptFriendUnknown);
p.WriteBool(false);
await message.User.Dispatch(p.Build());
using var packet = new PacketWriter(PacketSendOperations.FriendResult);
packet.WriteByte((byte)FriendResultOperations.AcceptFriendUnknown);
packet.WriteBool(false);
await message.User.Dispatch(packet.Build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketFriendDeleteRequest

if (response.Result == FriendResult.Success) return;

var p = new PacketWriter(PacketSendOperations.FriendResult);
p.WriteByte((byte)FriendResultOperations.DeleteFriendUnknown);
p.WriteBool(false);
await message.User.Dispatch(p.Build());
using var packet = new PacketWriter(PacketSendOperations.FriendResult);
packet.WriteByte((byte)FriendResultOperations.DeleteFriendUnknown);
packet.WriteBool(false);
await message.User.Dispatch(packet.Build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketFriendSetRequest mes
FriendResult.FailedCharacterNotFound => FriendResultOperations.SetFriendUnknownUser,
_ => FriendResultOperations.SetFriendUnknown
};
var p = new PacketWriter(PacketSendOperations.FriendResult);
p.WriteByte((byte)result);
using var packet = new PacketWriter(PacketSendOperations.FriendResult);
packet.WriteByte((byte)result);
if (result == FriendResultOperations.SetFriendUnknown)
p.WriteBool(false);
await message.User.Dispatch(p.Build());
packet.WriteBool(false);
await message.User.Dispatch(packet.Build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ public class FieldOnPacketMobMovePlug : IPipelinePlug<FieldOnPacketMobMove>
{
public async Task Handle(IPipelineContext ctx, FieldOnPacketMobMove message)
{
using var p = new PacketWriter(PacketSendOperations.MobCtrlAck);
using var packet = new PacketWriter(PacketSendOperations.MobCtrlAck);

p.WriteInt(message.Mob.ObjectID ?? 0);
p.WriteShort(message.Path.MobCtrlSN);
p.WriteBool(message.Path.NextAttackPossible);
p.WriteShort(0); // nMP
p.WriteByte(0); // SkillCommand
p.WriteByte(0); // SLV
packet.WriteInt(message.Mob.ObjectID ?? 0);
packet.WriteShort(message.Path.MobCtrlSN);
packet.WriteBool(message.Path.NextAttackPossible);
packet.WriteShort(0); // nMP
packet.WriteByte(0); // SkillCommand
packet.WriteByte(0); // SLV

await message.User.Dispatch(p.Build());
await message.User.Dispatch(packet.Build());
await message.Mob.Move(message.Path, message.User);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketPartyChangeLeaderReq
{
_ => PartyResultOperations.ChangePartyBossUnknown
};
var p = new PacketWriter(PacketSendOperations.PartyResult);
p.WriteByte((byte)result);
await message.User.Dispatch(p.Build());
using var packet = new PacketWriter(PacketSendOperations.PartyResult);
packet.WriteByte((byte)result);
await message.User.Dispatch(packet.Build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketPartyCreateRequest m
PartyResult.FailedAlreadyInParty => PartyResultOperations.CreateNewPartyAlreayJoined,
_ => PartyResultOperations.CreateNewPartyUnknown
};
var p = new PacketWriter(PacketSendOperations.PartyResult);
p.WriteByte((byte)result);
await message.User.Dispatch(p.Build());
using var packet = new PacketWriter(PacketSendOperations.PartyResult);
packet.WriteByte((byte)result);
await message.User.Dispatch(packet.Build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketPartyInviteAcceptRes
PartyResult.FailedAlreadyInParty => PartyResultOperations.JoinPartyAlreadyJoined,
_ => PartyResultOperations.JoinPartyUnknown
};
var p = new PacketWriter(PacketSendOperations.PartyResult);
p.WriteByte((byte)result);
await message.User.Dispatch(p.Build());
using var packet = new PacketWriter(PacketSendOperations.PartyResult);
packet.WriteByte((byte)result);
await message.User.Dispatch(packet.Build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public async Task Handle(IPipelineContext ctx, FieldOnPacketPartyInviteRejectRes
{
_ => PartyResultOperations.JoinPartyUnknown
};
var p = new PacketWriter(PacketSendOperations.PartyResult);
p.WriteByte((byte)result);
await message.User.Dispatch(p.Build());
using var packet = new PacketWriter(PacketSendOperations.PartyResult);
packet.WriteByte((byte)result);
await message.User.Dispatch(packet.Build());
}
}
Loading

0 comments on commit 5582dbf

Please sign in to comment.