fix(core): Converts remaining player packets (#374)

- [X] Converts the remainder of the player packets
This commit is contained in:
Kamron Batman 2020-12-30 16:09:51 -08:00 • committed by GitHub
parent 1e2242bb1a
commit 540a879d63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 836 additions and 666 deletions

View file

@ -310,7 +310,7 @@ namespace Server.Network
state.SendMapPatches();
}
state.Send(SeasonChange.Instantiate(m.GetSeason(), true));
state.SendSeasonChange((byte)m.GetSeason(), true);
state.SendSupportedFeature();
@ -338,8 +338,8 @@ namespace Server.Network
state.SendMobileIncoming(m, m);
state.SendLoginComplete();
state.Send(new CurrentTime());
state.Send(SeasonChange.Instantiate(m.GetSeason(), true));
state.SendCurrentTime();
state.SendSeasonChange((byte)m.GetSeason(), true);
state.SendMapChange(m.Map);
EventSink.InvokeLogin(m);

View file

@ -483,7 +483,7 @@ namespace Server.Network
public static void PingReq(NetState state, CircularBufferReader reader)
{
state.Send(PingAck.Instantiate(reader.ReadByte()));
state.SendPingAck(reader.ReadByte());
}
public static void SetUpdateRange(NetState state, CircularBufferReader reader)

View file

@ -13,9 +13,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using Server.HuePickers;
namespace Server.Network
{
@ -31,6 +33,8 @@ namespace Server.Network
public static class OutgoingPlayerPackets
{
public const int DragEffectPacketLength = 26;
public static void SendStatLockInfo(this NetState ns, Mobile m)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
@ -122,5 +126,269 @@ namespace Server.Network
{
ns?.Send(stackalloc byte[] { 0x65, type, density, temp });
}
public static void SendServerChange(this NetState ns, Point3D p, Map map)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x76); // Packet ID
writer.Write((short)p.X);
writer.Write((short)p.Y);
writer.Write((short)p.Z);
writer.Write((byte)0);
writer.Write((short)0);
writer.Write((short)0);
writer.Write((short)map.Width);
writer.Write((short)map.Height);
ns.Send(ref buffer, writer.Position);
}
public static void SendSkillsUpdate(this NetState ns, Skills skills)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x3A); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write((byte)0x02); // type: absolute, capped
for (var i = 0; i < skills.Length; ++i)
{
var s = skills[i];
var v = s.NonRacialValue;
var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF);
writer.Write((ushort)(s.Info.SkillID + 1));
writer.Write((ushort)uv);
writer.Write((ushort)s.BaseFixedPoint);
writer.Write((byte)s.Lock);
writer.Write((ushort)s.CapFixedPoint);
}
writer.Write((short)0); // terminate
writer.WritePacketLength();
ns.Send(ref buffer, writer.Position);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendSequence(this NetState ns, byte sequence)
{
ns?.Send(stackalloc byte[] { 0x7B, sequence });
}
public static void SendSkillChange(this NetState ns, Skill skill)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x3A); // Packet ID
writer.Write((ushort)13);
var v = skill.NonRacialValue;
var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF);
writer.Write((byte)0xDF); // type: delta, capped
writer.Write((ushort)skill.Info.SkillID);
writer.Write((ushort)uv);
writer.Write((ushort)skill.BaseFixedPoint);
writer.Write((byte)skill.Lock);
writer.Write((ushort)skill.CapFixedPoint);
ns.Send(ref buffer, writer.Position);
}
public static void SendLaunchBrowser(this NetState ns, string uri)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0xA5); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.WriteAsciiNull(uri ?? "");
writer.WritePacketLength();
ns.Send(ref buffer, writer.Position);
}
public static void CreateDragEffect(
Span<byte> buffer,
Serial srcSerial, Point3D srcLocation,
Serial trgSerial, Point3D trgLocation,
int itemID, int hue, int amount
)
{
var writer = new SpanWriter(buffer);
writer.Write((byte)0x23); // Packet ID
writer.Write((short)itemID);
writer.Write((byte)0);
writer.Write((short)hue);
writer.Write((short)amount);
writer.Write(srcSerial);
writer.Write((short)srcLocation.X);
writer.Write((short)srcLocation.Y);
writer.Write((sbyte)srcLocation.Z);
writer.Write(trgSerial);
writer.Write((short)trgLocation.X);
writer.Write((short)trgLocation.Y);
writer.Write((sbyte)trgLocation.Z);
}
public static void SendDragEffect(
this NetState ns,
Serial srcSerial, Point3D srcLocation,
Serial trgSerial, Point3D trgLocation,
int itemID, int hue, int amount
)
{
if (ns == null)
{
return;
}
Span<byte> span = stackalloc byte[DragEffectPacketLength];
CreateDragEffect(span, srcSerial, srcLocation, trgSerial, trgLocation, itemID, hue, amount);
ns.Send(span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SendSeasonChange(this NetState ns, byte season, bool playSound)
{
ns?.Send(stackalloc byte[]{ 0xBC, season, *(byte*)&playSound });
}
public static void SendDisplayPaperdoll(this NetState ns, Serial m, string title, bool warmode, bool canLift)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
byte flags = 0x00;
if (warmode)
{
flags |= 0x01;
}
if (canLift)
{
flags |= 0x02;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x88); // Packet ID
writer.Write(m);
writer.WriteAscii(title, 60);
writer.Write(flags);
ns.Send(ref buffer, writer.Position);
}
public static void SendPlayMusic(this NetState ns, MusicName music)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x6D); // Packet ID
writer.Write((short)music);
ns.Send(ref buffer, writer.Position);
}
public static void SendScrollMessage(this NetState ns, int type, int tip, string text)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
text ??= "";
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0xA6); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write((byte)type);
writer.Write(tip);
writer.Write((ushort)text.Length);
writer.WriteAscii(text);
writer.WritePacketLength();
ns.Send(ref buffer, writer.Position);
}
// TODO: Use DateTime caching against core loop
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendCurrentTime(this NetState ns) => ns.SendCurrentTime(DateTime.UtcNow);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendCurrentTime(this NetState ns, DateTime date)
{
ns?.Send(stackalloc byte[] { 0x5B, (byte)date.Hour, (byte)date.Minute, (byte)date.Second });
}
public static void SendPathfindMessage(this NetState ns, Point3D p)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x38); // Packet ID
writer.Write((short)p.X);
writer.Write((short)p.Y);
writer.Write((short)p.Z);
ns.Send(ref buffer, writer.Position);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendPingAck(this NetState ns, byte ping)
{
ns?.Send(stackalloc byte[] { 0x73, ping });
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SendClearWeaponAbility(this NetState ns)
{
ns?.Send(stackalloc byte[] { 0xBF, 0x00, 0x5, 0x00, 0x21 });
}
public static void SendDisplayHuePicker(this NetState ns, Serial huePickerSerial, int huePickerItemID)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
{
return;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0x95); // Packet ID
writer.Write(huePickerSerial);
writer.Write((short)0);
writer.Write((short)huePickerItemID);
ns.Send(ref buffer, writer.Position);
}
}
}

View file

@ -1,288 +0,0 @@
using System;
using Server.HuePickers;
namespace Server.Network
{
public sealed class ServerChange : Packet
{
public ServerChange(Point3D p, Map map) : base(0x76, 16)
{
Stream.Write((short)p.X);
Stream.Write((short)p.Y);
Stream.Write((short)p.Z);
Stream.Write((byte)0);
Stream.Write((short)0);
Stream.Write((short)0);
Stream.Write((short)map.Width);
Stream.Write((short)map.Height);
}
}
public sealed class SkillUpdate : Packet
{
public SkillUpdate(Skills skills) : base(0x3A)
{
EnsureCapacity(6 + skills.Length * 9);
Stream.Write((byte)0x02); // type: absolute, capped
for (var i = 0; i < skills.Length; ++i)
{
var s = skills[i];
var v = s.NonRacialValue;
var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF);
Stream.Write((ushort)(s.Info.SkillID + 1));
Stream.Write((ushort)uv);
Stream.Write((ushort)s.BaseFixedPoint);
Stream.Write((byte)s.Lock);
Stream.Write((ushort)s.CapFixedPoint);
}
Stream.Write((short)0); // terminate
}
}
public sealed class Sequence : Packet
{
public Sequence(int num) : base(0x7B, 2)
{
Stream.Write((byte)num);
}
}
public sealed class SkillChange : Packet
{
public SkillChange(Skill skill) : base(0x3A)
{
EnsureCapacity(13);
var v = skill.NonRacialValue;
var uv = Math.Clamp((int)(v * 10), 0, 0xFFFF);
Stream.Write((byte)0xDF); // type: delta, capped
Stream.Write((ushort)skill.Info.SkillID);
Stream.Write((ushort)uv);
Stream.Write((ushort)skill.BaseFixedPoint);
Stream.Write((byte)skill.Lock);
Stream.Write((ushort)skill.CapFixedPoint);
}
}
public sealed class LaunchBrowser : Packet
{
public LaunchBrowser(string url) : base(0xA5)
{
url ??= "";
EnsureCapacity(4 + url.Length);
Stream.WriteAsciiNull(url);
}
}
public sealed class DragEffect : Packet
{
public DragEffect(IEntity src, IEntity trg, int itemID, int hue, int amount) : base(0x23, 26)
{
Stream.Write((short)itemID);
Stream.Write((byte)0);
Stream.Write((short)hue);
Stream.Write((short)amount);
Stream.Write(src.Serial);
Stream.Write((short)src.X);
Stream.Write((short)src.Y);
Stream.Write((sbyte)src.Z);
Stream.Write(trg.Serial);
Stream.Write((short)trg.X);
Stream.Write((short)trg.Y);
Stream.Write((sbyte)trg.Z);
}
}
public sealed class SeasonChange : Packet
{
private static readonly SeasonChange[][] m_Cache =
{
new SeasonChange[2],
new SeasonChange[2],
new SeasonChange[2],
new SeasonChange[2],
new SeasonChange[2]
};
public SeasonChange(int season, bool playSound = true) : base(0xBC, 3)
{
Stream.Write((byte)season);
Stream.Write(playSound);
}
public static SeasonChange Instantiate(int season) => Instantiate(season, true);
public static SeasonChange Instantiate(int season, bool playSound)
{
if (season >= 0 && season < m_Cache.Length)
{
var idx = playSound ? 1 : 0;
var p = m_Cache[season][idx];
if (p == null)
{
m_Cache[season][idx] = p = new SeasonChange(season, playSound);
p.SetStatic();
}
return p;
}
return new SeasonChange(season, playSound);
}
}
public sealed class DisplayPaperdoll : Packet
{
public DisplayPaperdoll(Serial m, string title, bool warmode, bool canLift) : base(0x88, 66)
{
byte flags = 0x00;
if (warmode)
{
flags |= 0x01;
}
if (canLift)
{
flags |= 0x02;
}
Stream.Write(m);
Stream.WriteAsciiFixed(title, 60);
Stream.Write(flags);
}
}
public sealed class PlayMusic : Packet
{
public static readonly Packet InvalidInstance = SetStatic(new PlayMusic(MusicName.Invalid));
private static readonly Packet[] m_Instances = new Packet[60];
public PlayMusic(MusicName name) : base(0x6D, 3)
{
Stream.Write((short)name);
}
public static Packet GetInstance(MusicName name)
{
if (name == MusicName.Invalid)
{
return InvalidInstance;
}
var v = (int)name;
Packet p;
if (v >= 0 && v < m_Instances.Length)
{
p = m_Instances[v];
if (p == null)
{
m_Instances[v] = p = SetStatic(new PlayMusic(name));
}
}
else
{
p = new PlayMusic(name);
}
return p;
}
}
public sealed class ScrollMessage : Packet
{
public ScrollMessage(int type, int tip, string text) : base(0xA6)
{
text ??= "";
EnsureCapacity(10 + text.Length);
Stream.Write((byte)type);
Stream.Write(tip);
Stream.Write((ushort)text.Length);
Stream.WriteAsciiFixed(text, text.Length);
}
}
public sealed class CurrentTime : Packet
{
public CurrentTime() : this(DateTime.Now)
{
}
public CurrentTime(DateTime date) : base(0x5B, 4)
{
Stream.Write((byte)date.Hour);
Stream.Write((byte)date.Minute);
Stream.Write((byte)date.Second);
}
}
public sealed class PathfindMessage : Packet
{
public PathfindMessage(Point3D p) : base(0x38, 7)
{
Stream.Write((short)p.X);
Stream.Write((short)p.Y);
Stream.Write((short)p.Z);
}
}
public sealed class PingAck : Packet
{
private static readonly PingAck[] m_Cache = new PingAck[0x100];
public PingAck(byte ping) : base(0x73, 2)
{
Stream.Write(ping);
}
public static PingAck Instantiate(byte ping)
{
var p = m_Cache[ping];
if (p == null)
{
m_Cache[ping] = p = new PingAck(ping);
p.SetStatic();
}
return p;
}
}
public sealed class ClearWeaponAbility : Packet
{
public static readonly Packet Instance = SetStatic(new ClearWeaponAbility());
public ClearWeaponAbility() : base(0xBF)
{
EnsureCapacity(5);
Stream.Write((short)0x21);
}
}
public sealed class DisplayHuePicker : Packet
{
public DisplayHuePicker(HuePicker huePicker) : base(0x95, 9)
{
Stream.Write(huePicker.Serial);
Stream.Write((short)0);
Stream.Write((short)huePicker.ItemID);
}
}
}