ChatTwo/ChatTwo/Message.cs

142 lines
5.0 KiB
C#
Raw Normal View History

2022-02-15 18:02:46 +00:00
using ChatTwo.Code;
2022-02-13 09:36:08 +00:00
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
2022-02-13 09:36:08 +00:00
using LiteDB;
2021-12-29 19:31:45 +00:00
2021-12-30 02:53:44 +00:00
namespace ChatTwo;
2021-12-29 19:31:45 +00:00
2022-02-13 09:36:08 +00:00
internal class SortCode {
internal ChatType Type { get; set; }
internal ChatSource Source { get; set; }
internal SortCode(ChatType type, ChatSource source) {
this.Type = type;
this.Source = source;
}
public SortCode() {
}
2022-06-14 16:33:06 +00:00
private bool Equals(SortCode other) {
return this.Type == other.Type && this.Source == other.Source;
}
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((SortCode) obj);
}
public override int GetHashCode() {
unchecked {
return ((int) this.Type * 397) ^ (int) this.Source;
}
}
2022-02-13 09:36:08 +00:00
}
2021-12-29 19:31:45 +00:00
internal class Message {
2022-02-13 09:36:08 +00:00
// ReSharper disable once UnusedMember.Global
internal ObjectId Id { get; } = ObjectId.NewObjectId();
internal ulong Receiver { get; }
internal ulong ContentId { get; set; }
[BsonIgnore]
2022-01-31 08:04:50 +00:00
internal float? Height;
2022-02-13 09:36:08 +00:00
[BsonIgnore]
2022-01-31 08:04:50 +00:00
internal bool IsVisible;
2022-01-06 20:33:35 +00:00
2021-12-29 19:31:45 +00:00
internal DateTime Date { get; }
internal ChatCode Code { get; }
internal List<Chunk> Sender { get; }
internal List<Chunk> Content { get; }
2022-02-13 09:36:08 +00:00
internal SeString SenderSource { get; }
internal SeString ContentSource { get; }
internal SortCode SortCode { get; }
internal Guid ExtraChatChannel { get; }
2022-06-14 16:34:02 +00:00
2022-06-14 16:33:06 +00:00
internal int Hash { get; }
2022-02-13 09:36:08 +00:00
internal Message(ulong receiver, ChatCode code, List<Chunk> sender, List<Chunk> content, SeString senderSource, SeString contentSource) {
this.Receiver = receiver;
2021-12-29 19:31:45 +00:00
this.Date = DateTime.UtcNow;
this.Code = code;
this.Sender = sender;
this.Content = content;
2022-02-13 09:36:08 +00:00
this.SenderSource = senderSource;
this.ContentSource = contentSource;
this.SortCode = new SortCode(this.Code.Type, this.Code.Source);
this.ExtraChatChannel = this.ExtractExtraChatChannel();
2022-06-14 16:33:06 +00:00
this.Hash = this.GenerateHash();
2022-01-14 18:25:33 +00:00
foreach (var chunk in sender.Concat(content)) {
chunk.Message = this;
}
2021-12-29 19:31:45 +00:00
}
2022-02-13 09:36:08 +00:00
2022-02-16 06:52:13 +00:00
internal Message(ObjectId id, ulong receiver, ulong contentId, DateTime date, BsonDocument code, BsonArray sender, BsonArray content, BsonValue senderSource, BsonValue contentSource, BsonDocument sortCode) {
2022-02-13 09:36:08 +00:00
this.Id = id;
this.Receiver = receiver;
this.ContentId = contentId;
this.Date = date;
this.Code = BsonMapper.Global.ToObject<ChatCode>(code);
this.Sender = BsonMapper.Global.Deserialize<List<Chunk>>(sender);
this.Content = BsonMapper.Global.Deserialize<List<Chunk>>(content);
2022-02-16 06:52:13 +00:00
this.SenderSource = BsonMapper.Global.Deserialize<SeString>(senderSource);
this.ContentSource = BsonMapper.Global.Deserialize<SeString>(contentSource);
2022-02-13 09:36:08 +00:00
this.SortCode = BsonMapper.Global.ToObject<SortCode>(sortCode);
this.ExtraChatChannel = this.ExtractExtraChatChannel();
this.Hash = this.GenerateHash();
foreach (var chunk in this.Sender.Concat(this.Content)) {
chunk.Message = this;
}
}
internal Message(ObjectId id, ulong receiver, ulong contentId, DateTime date, BsonDocument code, BsonArray sender, BsonArray content, BsonValue senderSource, BsonValue contentSource, BsonDocument sortCode, BsonValue extraChatChannel) {
this.Id = id;
this.Receiver = receiver;
this.ContentId = contentId;
this.Date = date;
this.Code = BsonMapper.Global.ToObject<ChatCode>(code);
this.Sender = BsonMapper.Global.Deserialize<List<Chunk>>(sender);
this.Content = BsonMapper.Global.Deserialize<List<Chunk>>(content);
this.SenderSource = BsonMapper.Global.Deserialize<SeString>(senderSource);
this.ContentSource = BsonMapper.Global.Deserialize<SeString>(contentSource);
this.SortCode = BsonMapper.Global.ToObject<SortCode>(sortCode);
this.ExtraChatChannel = BsonMapper.Global.Deserialize<Guid>(extraChatChannel);
2022-06-14 16:33:06 +00:00
this.Hash = this.GenerateHash();
2022-02-13 09:36:08 +00:00
foreach (var chunk in this.Sender.Concat(this.Content)) {
chunk.Message = this;
}
}
2022-06-14 16:33:06 +00:00
private int GenerateHash() {
2022-06-14 16:34:02 +00:00
return this.SortCode.GetHashCode()
^ this.ExtraChatChannel.GetHashCode()
2022-06-14 16:34:02 +00:00
^ string.Join("", this.Sender.Select(c => c.StringValue())).GetHashCode()
^ string.Join("", this.Content.Select(c => c.StringValue())).GetHashCode();
2022-06-14 16:33:06 +00:00
}
private Guid ExtractExtraChatChannel() {
if (this.ContentSource.Payloads.Count > 0 && this.ContentSource.Payloads[0] is RawPayload raw) {
// this does an encode and clone every time it's accessed, so cache
var data = raw.Data;
if (data[1] == 0x27 && data[2] == 18 && data[3] == 0x20) {
return new Guid(data[4..^1]);
}
}
return Guid.Empty;
}
2021-12-29 19:31:45 +00:00
}