refactor: break message file into parts

This commit is contained in:
Anna 2020-11-17 23:29:34 -05:00
parent 4f4f188832
commit 01f4634505
18 changed files with 702 additions and 588 deletions

View File

@ -10,7 +10,8 @@ using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using XIVChatCommon;
using XIVChatCommon.Message;
using XIVChatCommon.Message.Server;
namespace XIVChat_Desktop {
[JsonObject]

View File

@ -7,8 +7,11 @@ using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using XIVChatCommon;
using DispatcherPriority = System.Windows.Threading.DispatcherPriority;
using XIVChatCommon.Message;
using XIVChatCommon.Message.Client;
using XIVChatCommon.Message.Server;
namespace XIVChat_Desktop {
public class Connection : INotifyPropertyChanged {
@ -268,7 +271,13 @@ namespace XIVChat_Desktop {
this.Disconnect();
break;
case ServerOperation.PlayerData:
var playerData = payload.Length == 0 ? null : PlayerData.Decode(payload);
var rawPlayerData = payload.Length == 0 ? null : PlayerData.Decode(payload);
var playerData = rawPlayerData switch {
PlayerData data => data,
EmptyPlayerData _ => null,
_ => throw new Exception("Bad player data"),
};
this.SetPlayerData(playerData);
break;

View File

@ -1,6 +1,6 @@
using System.Windows;
using System.Windows.Data;
using XIVChatCommon;
using XIVChatCommon.Message.Server;
namespace XIVChat_Desktop.Controls {
public class MessageTextBlock : SelectableTextBlock {

View File

@ -4,7 +4,7 @@ using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;
using XIVChatCommon;
using XIVChatCommon.Message.Server;
namespace XIVChat_Desktop {
public class DoubleConverter : IValueConverter {

View File

@ -11,7 +11,7 @@ using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Threading;
using Microsoft.Win32;
using XIVChatCommon;
using XIVChatCommon.Message.Server;
namespace XIVChat_Desktop {
public partial class Export : INotifyPropertyChanged {

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using XIVChatCommon;
using XIVChatCommon.Message;
namespace XIVChat_Desktop {
public enum FilterCategory {
@ -102,9 +102,14 @@ namespace XIVChat_Desktop {
// NOTE: Changing the order of these is a breaking change
public enum FilterType {
[Filter("Say", ChatType.Say)] Say,
[Filter("Shout", ChatType.Shout)] Shout,
[Filter("Yell", ChatType.Yell)] Yell,
[Filter("Say", ChatType.Say)]
Say,
[Filter("Shout", ChatType.Shout)]
Shout,
[Filter("Yell", ChatType.Yell)]
Yell,
[Filter("Tell", ChatType.TellOutgoing, ChatType.TellIncoming)]
Tell,
@ -117,7 +122,9 @@ namespace XIVChat_Desktop {
[Filter("Free Company", ChatType.FreeCompany)]
FreeCompany,
[Filter("PvP Team", ChatType.PvpTeam)] PvpTeam,
[Filter("PvP Team", ChatType.PvpTeam)]
PvpTeam,
[Filter("Cross-world Linkshell [1]", ChatType.CrossLinkshell1)]
CrossLinkshell1,
@ -190,9 +197,14 @@ namespace XIVChat_Desktop {
)]
Battle,
[Filter("Debug", ChatType.Debug)] Debug,
[Filter("Urgent", ChatType.Urgent)] Urgent,
[Filter("Notice", ChatType.Notice)] Notice,
[Filter("Debug", ChatType.Debug)]
Debug,
[Filter("Urgent", ChatType.Urgent)]
Urgent,
[Filter("Notice", ChatType.Notice)]
Notice,
[Filter("System Messages", ChatType.System)]
SystemMessages,
@ -208,7 +220,9 @@ namespace XIVChat_Desktop {
[Filter("Error Messages", ChatType.Error)]
ErrorMessages,
[Filter("Echo", ChatType.Echo)] Echo,
[Filter("Echo", ChatType.Echo)]
Echo,
[Filter("Novice Network Notifications", ChatType.NoviceNetworkSystem)]
NoviceNetworkAnnouncements,

View File

@ -5,7 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:XIVChat_Desktop"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:common="clr-namespace:XIVChatCommon;assembly=XIVChatCommon"
xmlns:message="clr-namespace:XIVChatCommon.Message;assembly=XIVChatCommon"
ui:WindowHelper.UseModernWindowStyle="True"
mc:Ignorable="d"
Title="Friend list"
@ -43,8 +43,8 @@
<ContextMenu x:Key="RowMenu"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"
d:DataContext="{d:DesignInstance common:Player}">
<MenuItem Header="Send tell"
d:DataContext="{d:DesignInstance message:Player}">
<MenuItem Header="Send /tell"
Command="local:FriendList.SendTell"
CommandParameter="{Binding}" />
</ContextMenu>
@ -59,7 +59,7 @@
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn d:DataContext="{d:DesignInstance common:Player}">
<DataGridTemplateColumn d:DataContext="{d:DesignInstance message:Player}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image MaxHeight="32"

View File

@ -7,7 +7,7 @@ using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using XIVChatCommon;
using XIVChatCommon.Message;
namespace XIVChat_Desktop {
public partial class FriendList : INotifyPropertyChanged {

View File

@ -6,6 +6,7 @@
xmlns:local="clr-namespace:XIVChat_Desktop"
xmlns:cc="clr-namespace:XIVChat_Desktop.Controls"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:server="clr-namespace:XIVChatCommon.Message.Server;assembly=XIVChatCommon"
ui:WindowHelper.UseModernWindowStyle="True"
ui:TitleBar.ExtendViewIntoTitleBar="{Binding App.Config.CompactMode}"
mc:Ignorable="d"

View File

@ -8,7 +8,8 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using XIVChatCommon;
using XIVChatCommon.Message;
using XIVChatCommon.Message.Server;
namespace XIVChat_Desktop {
/// <summary>
@ -70,6 +71,30 @@ namespace XIVChat_Desktop {
new ManageTabs(this).Show();
}
public static readonly RoutedUICommand MessageSendTell = new RoutedUICommand(
"MessageSendTell",
"MessageSendTell",
typeof(MainWindow)
);
private void MessageSendTell_OnExecuted(object eventSender, ExecutedRoutedEventArgs e) {
if (!(e.Parameter is ServerMessage message)) {
return;
}
var sender = message.GetSenderPlayer();
if (sender == null) {
return;
}
var input = this.GetCurrentInputBox();
if (input == null) {
return;
}
input.Text.Insert(0, $"/tell {sender.Name}@{sender.Server} ");
}
#endregion
public App App => (App)Application.Current;

View File

@ -6,8 +6,8 @@ using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using XIVChatCommon;
using Inline = System.Windows.Documents.Inline;
using XIVChatCommon.Message;
using XIVChatCommon.Message.Server;
namespace XIVChat_Desktop {
public class MessageFormatter {

View File

@ -0,0 +1,188 @@
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,
}
#region Ping
public class Ping : IEncodable {
public static Ping Instance { get; } = new Ping();
[IgnoreMember]
protected override byte Code => (byte)ClientOperation.Ping;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Message
[MessagePackObject]
public class ClientMessage : IEncodable {
[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 : IEncodable {
public static ClientShutdown Instance { get; } = new ClientShutdown();
[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 : IEncodable {
[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 : IEncodable {
[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 : IEncodable {
[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 : IEncodable {
[Key(0)]
public Dictionary<ClientPreference, object> Preferences { get; set; } = new Dictionary<ClientPreference, object>();
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
}

View File

@ -1,182 +1,8 @@
using MessagePack;
using System;
using MessagePack;
using MessagePack.Formatters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedType.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable NotAccessedField.Global
namespace XIVChatCommon {
[MessagePackObject]
public class ServerMessage : IEncodable {
[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)}");
}
}
}
namespace XIVChatCommon.Message {
[Union(1, typeof(TextChunk))]
[Union(2, typeof(IconChunk))]
[MessagePackObject]
@ -215,7 +41,8 @@ namespace XIVChatCommon {
[MessagePackObject]
public class IconChunk : Chunk {
[Key(0)] public byte index;
[Key(0)]
public byte index;
}
public class NameFormatting {
@ -726,133 +553,6 @@ namespace XIVChatCommon {
OtherPet = 2048,
}
[MessagePackObject]
public class ClientMessage : IEncodable {
[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);
}
}
public enum ServerOperation : byte {
/// <summary>
/// Sent in response to a client ping. Has no payload.
/// </summary>
Pong = 1,
/// <summary>
/// A message was sent in game and is being relayed to the client.
/// </summary>
Message = 2,
/// <summary>
/// The server is shutting down. Clients should send no response and close their sockets. Has no payload.
/// </summary>
Shutdown = 3,
PlayerData = 4,
Availability = 5,
Channel = 6,
Backlog = 7,
PlayerList = 8,
LinkshellList = 9,
}
[MessagePackObject]
public class PlayerData : IEncodable {
[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);
}
}
public class EmptyPlayerData : IEncodable {
public static EmptyPlayerData Instance { get; } = new EmptyPlayerData();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.PlayerData;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
[MessagePackObject]
public class Availability : IEncodable {
[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);
}
}
[MessagePackObject]
public class ServerChannel : IEncodable {
[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);
}
}
public enum InputChannel : byte {
Tell = 0,
Say = 1,
@ -884,146 +584,6 @@ namespace XIVChatCommon {
Linkshell8 = 26,
}
public class Pong : IEncodable {
public static Pong Instance { get; } = new Pong();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Pong;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
public class Ping : IEncodable {
public static Ping Instance { get; } = new Ping();
[IgnoreMember]
protected override byte Code => (byte)ClientOperation.Ping;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
public class ServerShutdown : IEncodable {
public static ServerShutdown Instance { get; } = new ServerShutdown();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Shutdown;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
public class ClientShutdown : IEncodable {
public static ClientShutdown Instance { get; } = new ClientShutdown();
[IgnoreMember]
protected override byte Code => (byte)ClientOperation.Shutdown;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
[MessagePackObject]
public class ServerBacklog : IEncodable {
[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);
}
}
[MessagePackObject]
public class ClientBacklog : IEncodable {
[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 : IEncodable {
[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);
}
}
[MessagePackObject]
public class ServerPlayerList : IEncodable {
[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);
}
}
[MessagePackObject]
public class ClientPlayerList : IEncodable {
[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);
}
}
public enum PlayerListType : byte {
Party = 1,
Friend = 2,
@ -1131,56 +691,6 @@ namespace XIVChatCommon {
Online = 47,
}
[MessagePackObject]
public class ClientPreferences : IEncodable {
[Key(0)]
public Dictionary<ClientPreference, object> Preferences { get; set; } = new Dictionary<ClientPreference, object>();
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 class PreferenceAttribute : Attribute {
public Type ValueType { get; }
public PreferenceAttribute(Type valueType) {
this.ValueType = valueType;
}
}
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 abstract class IEncodable {
protected abstract byte Code { get; }
protected abstract byte[] PayloadEncode();
@ -1201,28 +711,6 @@ namespace XIVChatCommon {
}
}
public enum ClientOperation : byte {
/// <summary>
/// The client is sending data to the server to keep the socket alive. Has no payload.
/// </summary>
Ping = 1,
/// <summary>
/// The client has a message to be sent in the game and is relaying it to the server.
/// </summary>
Message = 2,
/// <summary>
/// The client is shutting down. Clients should send this and close their socket for a clean shutdown.
/// </summary>
Shutdown = 3,
Backlog = 4,
CatchUp = 5,
PlayerList = 6,
LinkshellList = 7,
Preferences = 8,
}
public class MillisecondsDateTimeFormatter : IMessagePackFormatter<DateTime> {
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

View File

@ -0,0 +1,379 @@
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 : IEncodable {
public static Pong Instance { get; } = new Pong();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Pong;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Message
[MessagePackObject]
public class ServerMessage : IEncodable {
[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 : IEncodable {
public static ServerShutdown Instance { get; } = new ServerShutdown();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.Shutdown;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Player data
[MessagePackObject]
public class PlayerData : IEncodable {
[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 object Decode(byte[] bytes) {
try {
return MessagePackSerializer.Deserialize<PlayerData>(bytes);
} catch (Exception) {
return MessagePackSerializer.Deserialize<EmptyPlayerData>(bytes);
}
}
protected override byte[] PayloadEncode() {
return MessagePackSerializer.Serialize(this);
}
}
[MessagePackObject]
public class EmptyPlayerData : IEncodable {
public static EmptyPlayerData Instance { get; } = new EmptyPlayerData();
[IgnoreMember]
protected override byte Code => (byte)ServerOperation.PlayerData;
protected override byte[] PayloadEncode() {
return new byte[0];
}
}
#endregion
#region Availability
[MessagePackObject]
public class Availability : IEncodable {
[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 : IEncodable {
[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 : IEncodable {
[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 : IEncodable {
[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

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using XIVChatCommon.Message;
namespace XIVChatCommon {
public static class SecretMessage {

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using XIVChatCommon.Message;
namespace XIVChatCommon {
public static class XivString {
@ -68,8 +69,10 @@ namespace XIVChatCommon {
case 0x49:
break;
}
continue;
}
stringBytes.Add(b);
}
@ -90,8 +93,10 @@ namespace XIVChatCommon {
if (end != End) {
throw new ArgumentException("Input was not a valid XivString");
}
continue;
}
stringBytes.Add(b);
}
@ -108,15 +113,15 @@ namespace XIVChatCommon {
ByteTimes256 = 0xF1,
Int16 = 0xF2,
ByteShl16 = 0xF3,
Int16Packed = 0xF4, // seen in map links, seemingly 2 8-bit values packed into 2 bytes with only one marker
Int16Packed = 0xF4, // seen in map links, seemingly 2 8-bit values packed into 2 bytes with only one marker
Int16Shl8 = 0xF5,
Int24Special = 0xF6, // unsure how different form Int24 - used for hq items that add 1 million, also used for normal 24-bit values in map links
Int24Special = 0xF6, // unsure how different form Int24 - used for hq items that add 1 million, also used for normal 24-bit values in map links
Int8Shl24 = 0xF7,
Int8Shl8Int8 = 0xF8,
Int8Shl8Int8Shl8 = 0xF9,
Int24 = 0xFA,
Int16Shl16 = 0xFB,
Int24Packed = 0xFC, // used in map links- sometimes short+byte, sometimes... not??
Int24Packed = 0xFC, // used in map links- sometimes short+byte, sometimes... not??
Int16Int8Shl8 = 0xFD,
Int32 = 0xFE,
}
@ -146,66 +151,66 @@ namespace XIVChatCommon {
case IntegerType.Int8Shl24:
return (uint)(input.ReadByte() << 24);
case IntegerType.Int8Shl8Int8: {
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte();
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte();
return (uint)v;
}
case IntegerType.Int8Shl8Int8Shl8: {
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 8;
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 8;
return (uint)v;
}
case IntegerType.Int16:
// fallthrough - same logic
case IntegerType.Int16Packed: {
var v = 0;
v |= input.ReadByte() << 8;
v |= input.ReadByte();
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 8;
v |= input.ReadByte();
return (uint)v;
}
case IntegerType.Int16Shl8: {
var v = 0;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
return (uint)v;
}
case IntegerType.Int16Shl16: {
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 16;
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 16;
return (uint)v;
}
case IntegerType.Int24Special:
// Fallthrough - same logic
case IntegerType.Int24Packed:
// fallthrough again
case IntegerType.Int24: {
var v = 0;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
v |= input.ReadByte();
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
v |= input.ReadByte();
return (uint)v;
}
case IntegerType.Int16Int8Shl8: {
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
return (uint)v;
}
case IntegerType.Int32: {
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
v |= input.ReadByte();
return (uint)v;
}
var v = 0;
v |= input.ReadByte() << 24;
v |= input.ReadByte() << 16;
v |= input.ReadByte() << 8;
v |= input.ReadByte();
return (uint)v;
}
default:
throw new NotSupportedException();

View File

@ -6,7 +6,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using XIVChatCommon;
using XIVChatCommon.Message;
namespace XIVChatPlugin {
public class GameFunctions : IDisposable {

View File

@ -18,6 +18,9 @@ using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using XIVChatCommon;
using XIVChatCommon.Message;
using XIVChatCommon.Message.Client;
using XIVChatCommon.Message.Server;
namespace XIVChatPlugin {
public class Server : IDisposable {