refactor: add constructors for messages

This commit is contained in:
Anna 2020-11-01 10:31:56 -05:00
parent 0e675d95c4
commit d8ef1265ee
7 changed files with 81 additions and 68 deletions

View File

@ -90,9 +90,7 @@ namespace XIVChat_Desktop {
// catch-up
var lastRealMessage = this.app.Window.Messages.FirstOrDefault(msg => msg.Channel != 0);
if (lastRealMessage != null) {
var catchUp = new ClientCatchUp {
After = lastRealMessage.Timestamp,
};
var catchUp = new ClientCatchUp(lastRealMessage.Timestamp);
await SecretMessage.SendSecretMessage(stream, handshake.Keys.tx, catchUp, this.cancel.Token);
}
} else if (this.app.Config.BacklogMessages > 0) {
@ -151,9 +149,7 @@ namespace XIVChat_Desktop {
var toSend = await outgoing;
outgoing = this.outgoing.Reader.ReadAsync().AsTask();
var message = new ClientMessage {
Content = toSend,
};
var message = new ClientMessage(toSend);
try {
await SecretMessage.SendSecretMessage(stream, handshake.Keys.tx, message, this.cancel.Token);
} catch (Exception ex) {

View File

@ -53,17 +53,17 @@ namespace XIVChat_Desktop {
}
public void AddSystemMessage(string content) {
var message = new ServerMessage {
Channel = 0,
Content = Encoding.UTF8.GetBytes(content),
Timestamp = DateTime.UtcNow,
Chunks = new List<Chunk> {
new TextChunk {
var message = new ServerMessage(
DateTime.UtcNow,
0,
new byte[0],
Encoding.UTF8.GetBytes(content),
new List<Chunk> {
new TextChunk(content) {
Foreground = 0xb38cffff,
Content = content,
},
},
};
}
);
this.AddMessage(message);
}

View File

@ -97,7 +97,7 @@ namespace XIVChat_Desktop {
});
break;
case IconChunk iconChunk:
var bounds = GetBounds(iconChunk.Index);
var bounds = GetBounds(iconChunk.index);
if (bounds == null) {
break;
}

View File

@ -2,6 +2,12 @@
using MessagePack.Formatters;
using System;
using System.Collections.Generic;
// 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]
@ -30,6 +36,14 @@ namespace XIVChatCommon {
[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);
@ -62,11 +76,15 @@ namespace XIVChatCommon {
[Key(4)]
public string Content { get; set; }
public TextChunk(string content) {
this.Content = content;
}
}
[MessagePackObject]
public class IconChunk : Chunk {
[Key(0)] public byte Index;
[Key(0)] public byte index;
}
public class NameFormatting {
@ -96,11 +114,11 @@ namespace XIVChatCommon {
}
public class ChatCode {
private const ushort CLEAR_7 = ~(~0 << 7);
private const ushort Clear7 = ~(~0 << 7);
private readonly ushort code;
public ChatType Type => (ChatType)(this.code & CLEAR_7);
public ChatType Type => (ChatType)(this.code & Clear7);
public ChatSource Source => this.SourceFrom(11);
public ChatSource Target => this.SourceFrom(7);
private ChatSource SourceFrom(ushort shift) => (ChatSource)(1 << ((this.code >> shift) & 0xF));
@ -298,7 +316,7 @@ namespace XIVChatCommon {
// }
//}
public NameFormatting NameFormat() => this.Type switch {
public NameFormatting? NameFormat() => this.Type switch {
ChatType.Say => NameFormatting.Of("", ": "),
ChatType.Shout => NameFormatting.Of("", ": "),
ChatType.Yell => NameFormatting.Of("", ": "),
@ -345,7 +363,7 @@ namespace XIVChatCommon {
ChatType.CrossLinkshell7 => NameFormatting.Of("[CWLS7]<", "> "),
ChatType.CrossLinkshell8 => NameFormatting.Of("[CWLS8]<", "> "),
ChatType.NoviceNetwork => NameFormatting.Of("[NOVICE]", ": "),
_ => null
_ => null,
};
public bool IsBattle() {
@ -500,7 +518,6 @@ namespace XIVChatCommon {
CustomEmote = 28,
StandardEmote = 29,
Yell = 30,
// 31 - also party?
CrossParty = 32,
PvpTeam = 36,
@ -584,6 +601,10 @@ namespace XIVChatCommon {
[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);
@ -599,12 +620,10 @@ namespace XIVChatCommon {
/// 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>
@ -817,6 +836,10 @@ namespace XIVChatCommon {
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);
}
@ -835,6 +858,11 @@ namespace XIVChatCommon {
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);
@ -871,10 +899,10 @@ namespace XIVChatCommon {
[MessagePackObject]
public class Player {
[Key(0)]
public string Name { get; set; }
public string? Name { get; set; }
[Key(1)]
public string FreeCompany { get; set; }
public string? FreeCompany { get; set; }
[Key(2)]
public ulong Status { get; set; }
@ -883,31 +911,31 @@ namespace XIVChatCommon {
public ushort CurrentWorld { get; set; }
[Key(4)]
public string CurrentWorldName { get; set; }
public string? CurrentWorldName { get; set; }
[Key(5)]
public ushort HomeWorld { get; set; }
[Key(6)]
public string HomeWorldName { get; set; }
public string? HomeWorldName { get; set; }
[Key(7)]
public ushort Territory { get; set; }
[Key(8)]
public string TerritoryName { get; set; }
public string? TerritoryName { get; set; }
[Key(9)]
public byte Job { get; set; }
[Key(10)]
public string JobName { get; set; }
public string? JobName { get; set; }
[Key(11)]
public byte GrandCompany { get; set; }
[Key(12)]
public string GrandCompanyName { get; set; }
public string? GrandCompanyName { get; set; }
[Key(13)]
public byte Languages { get; set; }

View File

@ -12,6 +12,7 @@
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>

View File

@ -18,11 +18,10 @@ namespace XIVChatCommon {
void AppendCurrent(bool clear) {
var text = Encoding.UTF8.GetString(stringBytes.ToArray());
chunks.Add(new TextChunk {
chunks.Add(new TextChunk(text) {
Foreground = foreground,
Glow = glow,
Italic = italic,
Content = text,
});
if (clear) {
stringBytes.Clear();
@ -46,7 +45,7 @@ namespace XIVChatCommon {
case 0x12:
var spriteIndex = GetInteger(data);
chunks.Add(new IconChunk {
Index = (byte)spriteIndex,
index = (byte)spriteIndex,
});
break;
// italics processing
@ -119,7 +118,7 @@ namespace XIVChatCommon {
Int16Shl16 = 0xFB,
Int24Packed = 0xFC, // used in map links- sometimes short+byte, sometimes... not??
Int16Int8Shl8 = 0xFD,
Int32 = 0xFE
Int32 = 0xFE,
}
private static uint GetInteger(BinaryReader input) {

View File

@ -128,10 +128,7 @@ namespace XIVChatPlugin {
}
private async void OnReceiveFriendList(List<Player> friends) {
var msg = new ServerPlayerList {
Type = PlayerListType.Friend,
Players = friends.ToArray(),
};
var msg = new ServerPlayerList(PlayerListType.Friend, friends.ToArray());
foreach (var id in this.waitingForFriendList) {
if (!this.Clients.TryGetValue(id, out var client)) {
@ -193,27 +190,25 @@ namespace XIVChatPlugin {
// var format = this.FormatFor(chatCode.Type);
var format = chatCode.NameFormat();
if (format != null && format.IsPresent) {
chunks.Add(new TextChunk {
chunks.Add(new TextChunk(format.Before) {
FallbackColour = colour,
Content = format.Before,
});
chunks.AddRange(ToChunks(sender, colour));
chunks.Add(new TextChunk {
chunks.Add(new TextChunk(format.After) {
FallbackColour = colour,
Content = format.After,
});
}
}
chunks.AddRange(ToChunks(message, colour));
var msg = new ServerMessage {
Timestamp = DateTime.UtcNow,
Channel = (ChatType)type,
Sender = sender.Encode(),
Content = message.Encode(),
Chunks = chunks,
};
var msg = new ServerMessage(
DateTime.UtcNow,
(ChatType)type,
sender.Encode(),
message.Encode(),
chunks
);
this.backlog.AddLast(msg);
while (this.backlog.Count > this.plugin.Config.BacklogCount) {
@ -273,7 +268,7 @@ namespace XIVChatPlugin {
return;
}
var handshake = await KeyExchange.ServerHandshake(this.plugin.Config.KeyPair, stream);
var handshake = await KeyExchange.ServerHandshake(this.plugin.Config.KeyPair!, stream);
var newClient = new Client(conn) {
Handshake = handshake,
};
@ -521,12 +516,11 @@ namespace XIVChatPlugin {
var glow = new Stack<uint>();
void Append(string text) {
chunks.Add(new TextChunk {
chunks.Add(new TextChunk(text) {
FallbackColour = defaultColour,
Foreground = foreground.Count > 0 ? foreground.Peek() : (uint?)null,
Glow = glow.Count > 0 ? glow.Peek() : (uint?)null,
Italic = italic,
Content = text,
});
}
@ -556,24 +550,20 @@ namespace XIVChatPlugin {
break;
case PayloadType.AutoTranslateText:
chunks.Add(new IconChunk {
Index = 54,
index = 54,
});
var autoText = ((AutoTranslatePayload)payload).Text;
Append(autoText.Substring(2, autoText.Length - 4));
chunks.Add(new IconChunk {
Index = 55,
index = 55,
});
break;
case PayloadType.Icon:
var index = ((IconPayload)payload).IconIndex;
chunks.Add(new IconChunk {
Index = (byte)index,
index = (byte)index,
});
break;
// FIXME: use ITextProvider directly once it's exposed
case PayloadType.RawText:
Append(((TextPayload)payload).Text);
break;
case PayloadType.Unknown:
var rawPayload = (RawPayload)payload;
if (rawPayload.Data[1] == 0x13) {
@ -582,12 +572,12 @@ namespace XIVChatPlugin {
}
break;
//default:
// var textProviderType = typeof(SeString).Assembly.GetType("Dalamud.Game.Chat.SeStringHandling.ITextProvider");
// var textProp = textProviderType.GetProperty("Text", BindingFlags.NonPublic | BindingFlags.Instance);
// var text = (string)textProp.GetValue(payload);
// append(text);
// break;
default:
if (payload is ITextProvider textProvider) {
Append(textProvider.Text);
}
break;
}
}
@ -732,9 +722,8 @@ namespace XIVChatPlugin {
var homeWorld = player.HomeWorld.GameData.Name;
var currentWorld = player.CurrentWorld.GameData.Name;
// FIXME: NPE if injected late
var territory = this.plugin.Interface.Data.GetExcelSheet<TerritoryType>().GetRow(this.plugin.Interface.ClientState.TerritoryType);
var location = territory.PlaceName.Value.Name;
var location = territory?.PlaceName?.Value?.Name ?? "???";
var name = player.Name;
return new PlayerData(homeWorld, currentWorld, location, name);