refactor(common): split classes into separate files

This commit is contained in:
Anna 2021-07-04 21:10:48 -04:00
parent c48a3ef0d8
commit 98e7889844
28 changed files with 646 additions and 636 deletions

View File

@ -1,209 +0,0 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using MessagePack;
namespace XIVChatCommon.Message.Client {
public enum ClientOperation : byte {
Ping = 1,
Message = 2,
Shutdown = 3,
Backlog = 4,
CatchUp = 5,
PlayerList = 6,
LinkshellList = 7,
Preferences = 8,
Channel = 9,
}
#region Ping
public class Ping : Encodable {
public static Ping Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte) ClientOperation.Ping;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Message
[MessagePackObject]
public class ClientMessage : Encodable {
[Key(0)]
public string Content { get; set; }
[IgnoreMember]
protected override byte Code => (byte) ClientOperation.Message;
public ClientMessage(string content) {
this.Content = content;
}
public static ClientMessage Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientMessage>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
#region Shutdown
public class ClientShutdown : Encodable {
public static ClientShutdown Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte) ClientOperation.Shutdown;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Backlog/catch-up
[MessagePackObject]
public class ClientBacklog : Encodable {
[Key(0)]
public ushort Amount { get; set; }
protected override byte Code => (byte) ClientOperation.Backlog;
public static ClientBacklog Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientBacklog>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
[MessagePackObject]
public class ClientCatchUp : Encodable {
[MessagePackFormatter(typeof(MillisecondsDateTimeFormatter))]
[Key(0)]
public DateTime After { get; set; }
protected override byte Code => (byte) ClientOperation.CatchUp;
public ClientCatchUp(DateTime after) {
this.After = after;
}
public static ClientCatchUp Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientCatchUp>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
#region Player list
[MessagePackObject]
public class ClientPlayerList : Encodable {
[Key(0)]
public PlayerListType Type { get; set; }
protected override byte Code => (byte) ClientOperation.PlayerList;
public static ClientPlayerList Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientPlayerList>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
#region Preferences
[MessagePackObject]
public class ClientPreferences : Encodable {
[Key(0)]
public Dictionary<ClientPreference, object> Preferences { get; set; } = new();
protected override byte Code => (byte) ClientOperation.Preferences;
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
public static ClientPreferences Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientPreferences>(bytes);
}
}
public enum ClientPreference {
[Preference(typeof(bool))]
BacklogNewestMessagesFirst,
}
public static class ClientPreferencesExtension {
public static bool TryGetValue<T>(this ClientPreferences prefs, ClientPreference pref, out T value) {
value = default!;
if (!prefs.Preferences.TryGetValue(pref, out var obj)) {
return false;
}
var attr = pref
.GetType()
.GetField(pref.ToString())
?.GetCustomAttribute<PreferenceAttribute>(false);
if (obj.GetType() != typeof(T) || obj.GetType() != attr?.ValueType) {
return false;
}
value = (T) obj;
return true;
}
}
public class PreferenceAttribute : Attribute {
public Type ValueType { get; }
public PreferenceAttribute(Type valueType) {
this.ValueType = valueType;
}
}
#endregion
#region Channel
[MessagePackObject]
public class ClientChannel : Encodable {
protected override byte Code => (byte) ClientOperation.Channel;
[Key(0)]
public InputChannel Channel { get; set; }
public static ClientChannel Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientChannel>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
}

View File

@ -0,0 +1,19 @@
using MessagePack;
namespace XIVChatCommon.Message.Client {
[MessagePackObject]
public class ClientBacklog : Encodable {
[Key(0)]
public ushort Amount { get; set; }
protected override byte Code => (byte) ClientOperation.Backlog;
public static ClientBacklog Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientBacklog>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,25 @@
using System;
using MessagePack;
namespace XIVChatCommon.Message.Client {
[MessagePackObject]
public class ClientCatchUp : Encodable {
[MessagePackFormatter(typeof(MillisecondsDateTimeFormatter))]
[Key(0)]
public DateTime After { get; set; }
protected override byte Code => (byte) ClientOperation.CatchUp;
public ClientCatchUp(DateTime after) {
this.After = after;
}
public static ClientCatchUp Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientCatchUp>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,19 @@
using MessagePack;
namespace XIVChatCommon.Message.Client {
[MessagePackObject]
public class ClientChannel : Encodable {
protected override byte Code => (byte) ClientOperation.Channel;
[Key(0)]
public InputChannel Channel { get; set; }
public static ClientChannel Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientChannel>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,24 @@
using MessagePack;
namespace XIVChatCommon.Message.Client {
[MessagePackObject]
public class ClientMessage : Encodable {
[Key(0)]
public string Content { get; set; }
[IgnoreMember]
protected override byte Code => (byte) ClientOperation.Message;
public ClientMessage(string content) {
this.Content = content;
}
public static ClientMessage Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientMessage>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,13 @@
namespace XIVChatCommon.Message.Client {
public enum ClientOperation : byte {
Ping = 1,
Message = 2,
Shutdown = 3,
Backlog = 4,
CatchUp = 5,
PlayerList = 6,
LinkshellList = 7,
Preferences = 8,
Channel = 9,
}
}

View File

@ -0,0 +1,19 @@
using MessagePack;
namespace XIVChatCommon.Message.Client {
[MessagePackObject]
public class ClientPlayerList : Encodable {
[Key(0)]
public PlayerListType Type { get; set; }
protected override byte Code => (byte) ClientOperation.PlayerList;
public static ClientPlayerList Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientPlayerList>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using MessagePack;
namespace XIVChatCommon.Message.Client {
[MessagePackObject]
public class ClientPreferences : Encodable {
[Key(0)]
public Dictionary<ClientPreference, object> Preferences { get; set; } = new();
protected override byte Code => (byte) ClientOperation.Preferences;
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
public static ClientPreferences Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ClientPreferences>(bytes);
}
}
public enum ClientPreference {
[Preference(typeof(bool))]
BacklogNewestMessagesFirst = 0,
[Preference(typeof(bool))]
TargetingListSupport = 1,
}
public static class ClientPreferencesExtension {
public static bool TryGetValue<T>(this ClientPreferences prefs, ClientPreference pref, out T value) {
value = default!;
if (!prefs.Preferences.TryGetValue(pref, out var obj)) {
return false;
}
var attr = pref
.GetType()
.GetField(pref.ToString())
?.GetCustomAttribute<PreferenceAttribute>(false);
if (obj.GetType() != typeof(T) || obj.GetType() != attr?.ValueType) {
return false;
}
value = (T) obj;
return true;
}
}
public class PreferenceAttribute : Attribute {
public Type ValueType { get; }
public PreferenceAttribute(Type valueType) {
this.ValueType = valueType;
}
}
}

View File

@ -0,0 +1,14 @@
using MessagePack;
namespace XIVChatCommon.Message.Client {
public class ClientShutdown : Encodable {
public static ClientShutdown Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte) ClientOperation.Shutdown;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
}

View File

@ -0,0 +1,14 @@
using MessagePack;
namespace XIVChatCommon.Message.Client {
public class Ping : Encodable {
public static Ping Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte) ClientOperation.Ping;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
}

View File

@ -50,10 +50,10 @@ namespace XIVChatCommon.Message {
public ushort Raw { get; }
public ChatType Type => (ChatType)(this.Raw & Clear7);
public ChatType Type => (ChatType) (this.Raw & Clear7);
public ChatSource Source => this.SourceFrom(11);
public ChatSource Target => this.SourceFrom(7);
private ChatSource SourceFrom(ushort shift) => (ChatSource)(1 << ((this.Raw >> shift) & 0xF));
private ChatSource SourceFrom(ushort shift) => (ChatSource) (1 << ((this.Raw >> shift) & 0xF));
public ChatCode(ushort raw) {
this.Raw = raw;
@ -371,9 +371,9 @@ namespace XIVChatCommon.Message {
}
private static uint Rgba(byte red, byte green, byte blue, byte alpha = 0xFF) => alpha
| (uint)(red << 24)
| (uint)(green << 16)
| (uint)(blue << 8);
| (uint) (red << 24)
| (uint) (green << 16)
| (uint) (blue << 8);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1028:Enum Storage should be Int32")]
@ -616,13 +616,14 @@ namespace XIVChatCommon.Message {
InputChannel.CrossLinkshell8 => 7,
_ => 0,
};
}
}
public enum PlayerListType : byte {
Party = 1,
Friend = 2,
Linkshell = 3,
CrossLinkshell = 4,
Targeting = 5,
}
[MessagePackObject]
@ -672,7 +673,7 @@ namespace XIVChatCommon.Message {
[Key(14)]
public byte MainLanguage { get; set; }
public bool HasStatus(PlayerStatus status) => (this.Status & ((ulong)1 << (int)status)) > 0;
public bool HasStatus(PlayerStatus status) => (this.Status & ((ulong) 1 << (int) status)) > 0;
}
public enum PlayerStatus {
@ -755,7 +756,7 @@ namespace XIVChatCommon.Message {
}
public void Serialize(ref MessagePackWriter writer, DateTime value, MessagePackSerializerOptions options) {
var millis = (long)(value.ToUniversalTime() - Epoch).TotalMilliseconds;
var millis = (long) (value.ToUniversalTime() - Epoch).TotalMilliseconds;
writer.WriteInt64(millis);
}
}

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using MessagePack;
using MessagePack;
namespace XIVChatCommon.Message.Relay {
[Union(1, typeof(RelayRegister))]
@ -8,52 +7,10 @@ namespace XIVChatCommon.Message.Relay {
public interface IToRelay {
}
[MessagePackObject]
public class RelayRegister : IToRelay {
[Key(0)]
public string AuthToken { get; set; }
[Key(1)]
public byte[] PublicKey { get; set; }
}
[Union(1, typeof(RelaySuccess))]
[Union(2, typeof(RelayNewClient))]
[Union(3, typeof(RelayedMessage))]
[Union(4, typeof(RelayClientDisconnect))]
public interface IFromRelay {
}
[MessagePackObject]
public class RelaySuccess : IFromRelay {
[Key(0)]
public bool Success { get; set; }
[Key(1)]
public string? Info { get; set; }
}
[MessagePackObject]
public class RelayNewClient : IFromRelay {
[Key(0)]
public List<byte> PublicKey { get; set; }
[Key(1)]
public string Address { get; set; }
}
[MessagePackObject]
public class RelayedMessage : IFromRelay, IToRelay {
[Key(0)]
public List<byte> PublicKey { get; set; }
[Key(1)]
public List<byte> Message { get; set; }
}
[MessagePackObject]
public class RelayClientDisconnect : IFromRelay, IToRelay {
[Key(0)]
public List<byte> PublicKey { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using MessagePack;
namespace XIVChatCommon.Message.Relay {
[MessagePackObject]
public class RelayClientDisconnect : IFromRelay, IToRelay {
[Key(0)]
public List<byte> PublicKey { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System.Collections.Generic;
using MessagePack;
namespace XIVChatCommon.Message.Relay {
[MessagePackObject]
public class RelayNewClient : IFromRelay {
[Key(0)]
public List<byte> PublicKey { get; set; }
[Key(1)]
public string Address { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using MessagePack;
namespace XIVChatCommon.Message.Relay {
[MessagePackObject]
public class RelayRegister : IToRelay {
[Key(0)]
public string AuthToken { get; set; }
[Key(1)]
public byte[] PublicKey { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using MessagePack;
namespace XIVChatCommon.Message.Relay {
[MessagePackObject]
public class RelaySuccess : IFromRelay {
[Key(0)]
public bool Success { get; set; }
[Key(1)]
public string? Info { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System.Collections.Generic;
using MessagePack;
namespace XIVChatCommon.Message.Relay {
[MessagePackObject]
public class RelayedMessage : IFromRelay, IToRelay {
[Key(0)]
public List<byte> PublicKey { get; set; }
[Key(1)]
public List<byte> Message { get; set; }
}
}

View File

@ -0,0 +1,24 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
[MessagePackObject]
public class Availability : Encodable {
[Key(0)]
public readonly bool available;
public Availability(bool available) {
this.available = available;
}
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Availability;
public static Availability Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<Availability>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,15 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
[MessagePackObject]
public class EmptyPlayerData : Encodable {
public static EmptyPlayerData Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.PlayerData;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
}

View File

@ -0,0 +1,36 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
[MessagePackObject]
public class PlayerData : Encodable {
[Key(0)]
public readonly string homeWorld;
[Key(1)]
public readonly string currentWorld;
[Key(2)]
public readonly string location;
[Key(3)]
public readonly string name;
public PlayerData(string homeWorld, string currentWorld, string location, string name) {
this.homeWorld = homeWorld;
this.currentWorld = currentWorld;
this.location = location;
this.name = name;
}
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.PlayerData;
public static PlayerData Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<PlayerData>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,14 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
public class Pong : Encodable {
public static Pong Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Pong;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
}

View File

@ -1,375 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MessagePack;
namespace XIVChatCommon.Message.Server {
public enum ServerOperation : byte {
Pong = 1,
Message = 2,
Shutdown = 3,
PlayerData = 4,
Availability = 5,
Channel = 6,
Backlog = 7,
PlayerList = 8,
LinkshellList = 9,
}
#region Pong
public class Pong : Encodable {
public static Pong Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Pong;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Message
[MessagePackObject]
public class ServerMessage : Encodable {
[MessagePackFormatter(typeof(MillisecondsDateTimeFormatter))]
[Key(0)]
public DateTime Timestamp { get; set; }
[Key(1)]
public ChatType Channel { get; set; }
[Key(2)]
public byte[] Sender { get; set; }
[Key(3)]
public byte[] Content { get; set; }
[Key(4)]
public List<Chunk> Chunks { get; set; }
[IgnoreMember]
public string ContentText => XivString.GetText(this.Content);
[IgnoreMember]
public string SenderText => XivString.GetText(this.Sender);
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Message;
public ServerMessage(DateTime timestamp, ChatType channel, byte[] sender, byte[] content, List<Chunk> chunks) {
this.Timestamp = timestamp;
this.Channel = channel;
this.Sender = sender;
this.Content = content;
this.Chunks = chunks;
}
public static ServerMessage Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerMessage>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
public SenderPlayer? GetSenderPlayer() {
using var stream = new MemoryStream(this.Sender);
using var reader = new BinaryReader(stream);
var text = new List<byte>();
while (reader.BaseStream.Position < reader.BaseStream.Length) {
var b = reader.ReadByte();
// read payloads
if (b == 2) {
var chunkType = reader.ReadByte();
var chunkLen = XivString.GetInteger(reader);
// interactive
if (chunkType == 0x27) {
var subType = reader.ReadByte();
// player name
if (subType == 0x01) {
// unk
reader.ReadByte();
var serverId = (ushort)XivString.GetInteger(reader);
// unk
reader.ReadBytes(2);
var nameLen = (int)XivString.GetInteger(reader);
var playerName = Encoding.UTF8.GetString(reader.ReadBytes(nameLen));
return new SenderPlayer(playerName, serverId);
}
}
reader.ReadBytes((int)chunkLen);
continue;
}
// read text
text.Add(b);
}
if (text.Count == 0) {
return null;
}
var name = Encoding.UTF8.GetString(text.ToArray());
// remove the party position if present
var chars = name.ToCharArray();
if (chars.Length > 0 && PartyChars.Contains((chars[0]))) {
name = name.Substring(1);
}
return new SenderPlayer(name, 0);
}
private static readonly char[] PartyChars = {
'\ue090', '\ue091', '\ue092', '\ue093', '\ue094', '\ue095', '\ue096', '\ue097',
};
public class SenderPlayer : IComparable<SenderPlayer>, IComparable {
public string Name { get; }
public ushort Server { get; }
public SenderPlayer(string name, ushort server) {
this.Name = name;
this.Server = server;
}
protected bool Equals(SenderPlayer other) {
return this.Name == other.Name && this.Server == other.Server;
}
public override bool Equals(object? obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
return obj.GetType() == this.GetType() && this.Equals((SenderPlayer)obj);
}
public override int GetHashCode() {
unchecked {
return (this.Name.GetHashCode() * 397) ^ (int)this.Server;
}
}
public int CompareTo(SenderPlayer? other) {
if (ReferenceEquals(this, other)) {
return 0;
}
if (ReferenceEquals(null, other)) {
return 1;
}
var nameComparison = string.Compare(this.Name, other.Name, StringComparison.Ordinal);
return nameComparison != 0 ? nameComparison : this.Server.CompareTo(other.Server);
}
public int CompareTo(object? obj) {
if (ReferenceEquals(null, obj)) {
return 1;
}
if (ReferenceEquals(this, obj)) {
return 0;
}
return obj is SenderPlayer other ? this.CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(SenderPlayer)}");
}
}
}
#endregion
#region Shutdown
public class ServerShutdown : Encodable {
public static ServerShutdown Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Shutdown;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Player data
[MessagePackObject]
public class PlayerData : Encodable {
[Key(0)]
public readonly string homeWorld;
[Key(1)]
public readonly string currentWorld;
[Key(2)]
public readonly string location;
[Key(3)]
public readonly string name;
public PlayerData(string homeWorld, string currentWorld, string location, string name) {
this.homeWorld = homeWorld;
this.currentWorld = currentWorld;
this.location = location;
this.name = name;
}
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.PlayerData;
public static PlayerData Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<PlayerData>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
[MessagePackObject]
public class EmptyPlayerData : Encodable {
public static EmptyPlayerData Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.PlayerData;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Availability
[MessagePackObject]
public class Availability : Encodable {
[Key(0)]
public readonly bool available;
public Availability(bool available) {
this.available = available;
}
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Availability;
public static Availability Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<Availability>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
#region Channel
[MessagePackObject]
public class ServerChannel : Encodable {
[Key(0)]
public readonly byte channel;
[Key(1)]
public readonly string name;
[IgnoreMember]
public InputChannel InputChannel => (InputChannel)this.channel;
protected override byte Code => (byte)ServerOperation.Channel;
public ServerChannel(InputChannel channel, string name) : this((byte)channel, name) { }
public ServerChannel(byte channel, string name) {
this.channel = channel;
this.name = name;
}
public static ServerChannel Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerChannel>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
#region Backlog
[MessagePackObject]
public class ServerBacklog : Encodable {
[Key(0)]
public readonly ServerMessage[] messages;
protected override byte Code => (byte)ServerOperation.Backlog;
public ServerBacklog(ServerMessage[] messages) {
this.messages = messages;
}
public static ServerBacklog Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerBacklog>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
#region Player list
[MessagePackObject]
public class ServerPlayerList : Encodable {
[Key(0)]
public PlayerListType Type { get; set; }
[Key(1)]
public Player[] Players { get; set; }
protected override byte Code => (byte)ServerOperation.PlayerList;
public ServerPlayerList(PlayerListType type, Player[] players) {
this.Type = type;
this.Players = players;
}
public static ServerPlayerList Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerPlayerList>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
#endregion
}

View File

@ -0,0 +1,23 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
[MessagePackObject]
public class ServerBacklog : Encodable {
[Key(0)]
public readonly ServerMessage[] messages;
protected override byte Code => (byte)ServerOperation.Backlog;
public ServerBacklog(ServerMessage[] messages) {
this.messages = messages;
}
public static ServerBacklog Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerBacklog>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,32 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
[MessagePackObject]
public class ServerChannel : Encodable {
[Key(0)]
public readonly byte channel;
[Key(1)]
public readonly string name;
[IgnoreMember]
public InputChannel InputChannel => (InputChannel)this.channel;
protected override byte Code => (byte)ServerOperation.Channel;
public ServerChannel(InputChannel channel, string name) : this((byte)channel, name) { }
public ServerChannel(byte channel, string name) {
this.channel = channel;
this.name = name;
}
public static ServerChannel Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerChannel>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,170 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MessagePack;
namespace XIVChatCommon.Message.Server {
[MessagePackObject]
public class ServerMessage : Encodable {
[MessagePackFormatter(typeof(MillisecondsDateTimeFormatter))]
[Key(0)]
public DateTime Timestamp { get; set; }
[Key(1)]
public ChatType Channel { get; set; }
[Key(2)]
public byte[] Sender { get; set; }
[Key(3)]
public byte[] Content { get; set; }
[Key(4)]
public List<Chunk> Chunks { get; set; }
[IgnoreMember]
public string ContentText => XivString.GetText(this.Content);
[IgnoreMember]
public string SenderText => XivString.GetText(this.Sender);
[IgnoreMember]
protected override byte Code => (byte) ServerOperation.Message;
public ServerMessage(DateTime timestamp, ChatType channel, byte[] sender, byte[] content, List<Chunk> chunks) {
this.Timestamp = timestamp;
this.Channel = channel;
this.Sender = sender;
this.Content = content;
this.Chunks = chunks;
}
public static ServerMessage Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerMessage>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
public SenderPlayer? GetSenderPlayer() {
using var stream = new MemoryStream(this.Sender);
using var reader = new BinaryReader(stream);
var text = new List<byte>();
while (reader.BaseStream.Position < reader.BaseStream.Length) {
var b = reader.ReadByte();
// read payloads
if (b == 2) {
var chunkType = reader.ReadByte();
var chunkLen = XivString.GetInteger(reader);
// interactive
if (chunkType == 0x27) {
var subType = reader.ReadByte();
// player name
if (subType == 0x01) {
// unk
reader.ReadByte();
var serverId = (ushort) XivString.GetInteger(reader);
// unk
reader.ReadBytes(2);
var nameLen = (int) XivString.GetInteger(reader);
var playerName = Encoding.UTF8.GetString(reader.ReadBytes(nameLen));
return new SenderPlayer(playerName, serverId);
}
}
reader.ReadBytes((int) chunkLen);
continue;
}
// read text
text.Add(b);
}
if (text.Count == 0) {
return null;
}
var name = Encoding.UTF8.GetString(text.ToArray());
// remove the party position if present
var chars = name.ToCharArray();
if (chars.Length > 0 && PartyChars.Contains(chars[0])) {
name = name.Substring(1);
}
return new SenderPlayer(name, 0);
}
private static readonly char[] PartyChars = {
'\ue090', '\ue091', '\ue092', '\ue093', '\ue094', '\ue095', '\ue096', '\ue097',
};
public class SenderPlayer : IComparable<SenderPlayer>, IComparable {
public string Name { get; }
public ushort Server { get; }
public SenderPlayer(string name, ushort server) {
this.Name = name;
this.Server = server;
}
protected bool Equals(SenderPlayer other) {
return this.Name == other.Name && this.Server == other.Server;
}
public override bool Equals(object? obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
return obj.GetType() == this.GetType() && this.Equals((SenderPlayer) obj);
}
public override int GetHashCode() {
unchecked {
return (this.Name.GetHashCode() * 397) ^ this.Server;
}
}
public int CompareTo(SenderPlayer? other) {
if (ReferenceEquals(this, other)) {
return 0;
}
if (ReferenceEquals(null, other)) {
return 1;
}
var nameComparison = string.Compare(this.Name, other.Name, StringComparison.Ordinal);
return nameComparison != 0 ? nameComparison : this.Server.CompareTo(other.Server);
}
public int CompareTo(object? obj) {
if (ReferenceEquals(null, obj)) {
return 1;
}
if (ReferenceEquals(this, obj)) {
return 0;
}
return obj is SenderPlayer other ? this.CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(SenderPlayer)}");
}
}
}
}

View File

@ -0,0 +1,14 @@
namespace XIVChatCommon.Message.Server {
public enum ServerOperation : byte {
Pong = 1,
Message = 2,
Shutdown = 3,
PlayerData = 4,
Availability = 5,
Channel = 6,
Backlog = 7,
PlayerList = 8,
LinkshellList = 9,
TargetingList = 10,
}
}

View File

@ -0,0 +1,27 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
[MessagePackObject]
public class ServerPlayerList : Encodable {
[Key(0)]
public PlayerListType Type { get; set; }
[Key(1)]
public Player[] Players { get; set; }
protected override byte Code => (byte) ServerOperation.PlayerList;
public ServerPlayerList(PlayerListType type, Player[] players) {
this.Type = type;
this.Players = players;
}
public static ServerPlayerList Decode(byte[] bytes) {
return MessagePackSerializer.Deserialize<ServerPlayerList>(bytes);
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
}

View File

@ -0,0 +1,14 @@
using MessagePack;
namespace XIVChatCommon.Message.Server {
public class ServerShutdown : Encodable {
public static ServerShutdown Instance { get; } = new();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Shutdown;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
}