From 740ccfde1150d987dd3fb9559ef28f98c1bf66a1 Mon Sep 17 00:00:00 2001
From: goat <16760685+goaaats@users.noreply.github.com>
Date: Mon, 29 Jun 2020 23:41:33 +0200
Subject: [PATCH] fix: crash when chat is processed before init, use DI for
SeString stuff
---
Dalamud/Dalamud.cs | 11 ++--
Dalamud/DiscordBot/DiscordBotManager.cs | 16 ++---
Dalamud/Game/Chat/SeStringHandling/Payload.cs | 16 ++---
.../Payloads/AutoTranslatePayload.cs | 38 +++++------
.../SeStringHandling/Payloads/ItemPayload.cs | 2 +-
.../Payloads/MapLinkPayload.cs | 4 +-
.../Payloads/PlayerPayload.cs | 2 +-
.../Payloads/StatusPayload.cs | 2 +-
.../Payloads/UIForegroundPayload.cs | 2 +-
.../Payloads/UIGlowPayload.cs | 2 +-
.../Game/Chat/SeStringHandling/SeString.cs | 27 +-------
.../{SeStringUtils.cs => SeStringManager.cs} | 63 ++++++++++++++-----
Dalamud/Game/ChatHandlers.cs | 32 +++++-----
Dalamud/Game/Internal/Gui/ChatGui.cs | 4 +-
14 files changed, 110 insertions(+), 111 deletions(-)
rename Dalamud/Game/Chat/SeStringHandling/{SeStringUtils.cs => SeStringManager.cs} (75%)
diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs
index 9cf47d043..c4cf16a72 100644
--- a/Dalamud/Dalamud.cs
+++ b/Dalamud/Dalamud.cs
@@ -61,6 +61,8 @@ namespace Dalamud {
public DataManager Data { get; private set; }
+ internal SeStringManager SeStringManager { get; private set; }
+
internal Localization LocalizationManager;
@@ -122,8 +124,7 @@ namespace Dalamud {
this.Data = new DataManager(this.StartInfo.Language);
await this.Data.Initialize(this.baseDirectory);
- // TODO: better way to do this? basically for lumina injection
- SeString.Dalamud = this;
+ SeStringManager = new SeStringManager(Data);
this.NetworkHandlers = new NetworkHandlers(this, this.Configuration.OptOutMbCollection);
@@ -145,6 +146,9 @@ namespace Dalamud {
Log.Error(ex, "Plugin load failed.");
}
+ this.Framework.Enable();
+ this.ClientState.Enable();
+
IsReady = true;
});
}
@@ -153,9 +157,6 @@ namespace Dalamud {
#if DEBUG
ReplaceExceptionHandler();
#endif
-
- this.Framework.Enable();
- this.ClientState.Enable();
}
public void Unload() {
diff --git a/Dalamud/DiscordBot/DiscordBotManager.cs b/Dalamud/DiscordBot/DiscordBotManager.cs
index 2aff48585..ff2edef0d 100644
--- a/Dalamud/DiscordBot/DiscordBotManager.cs
+++ b/Dalamud/DiscordBot/DiscordBotManager.cs
@@ -162,7 +162,10 @@ namespace Dalamud.DiscordBot {
await channel.SendMessageAsync(embed: embedBuilder.Build());
}
- public async Task ProcessChatMessage(XivChatType type, StdString message, StdString sender) {
+ public async Task ProcessChatMessage(XivChatType type, SeString message, SeString sender) {
+ if (this.dalamud.SeStringManager == null)
+ return;
+
// Special case for outgoing tells, these should be sent under Incoming tells
var wasOutgoingTell = false;
if (type == XivChatType.TellOutgoing) {
@@ -180,8 +183,7 @@ namespace Dalamud.DiscordBot {
var channels = chatTypeConfigs.Select(c => GetChannel(c.Channel).GetAwaiter().GetResult());
- var parsedSender = SeString.Parse(sender.RawData);
- var playerLink = parsedSender.Payloads.FirstOrDefault(x => x.Type == PayloadType.Player) as PlayerPayload;
+ var playerLink = sender.Payloads.FirstOrDefault(x => x.Type == PayloadType.Player) as PlayerPayload;
string senderName;
string senderWorld;
@@ -192,15 +194,15 @@ namespace Dalamud.DiscordBot {
// Special case 2 - When the local player talks in party/alliance, the name comes through as raw text,
// but prefixed by their position number in the party (which for local player may always be 1)
- if (parsedSender.TextValue.EndsWith(this.dalamud.ClientState.LocalPlayer.Name))
+ if (sender.TextValue.EndsWith(this.dalamud.ClientState.LocalPlayer.Name))
{
senderName = this.dalamud.ClientState.LocalPlayer.Name;
}
else
{
- Log.Error("playerLink was null. Sender: {0}", BitConverter.ToString(sender.RawData));
+ Log.Error("playerLink was null. Sender: {0}", BitConverter.ToString(sender.Encode()));
- senderName = wasOutgoingTell ? this.dalamud.ClientState.LocalPlayer.Name : parsedSender.TextValue;
+ senderName = wasOutgoingTell ? this.dalamud.ClientState.LocalPlayer.Name : sender.TextValue;
}
senderWorld = this.dalamud.ClientState.LocalPlayer.HomeWorld.GameData.Name;
@@ -209,7 +211,7 @@ namespace Dalamud.DiscordBot {
senderWorld = playerLink.World.Name;
}
- var rawMessage = SeString.Parse(message.RawData).TextValue;
+ var rawMessage = message.TextValue;
var avatarUrl = string.Empty;
var lodestoneId = string.Empty;
diff --git a/Dalamud/Game/Chat/SeStringHandling/Payload.cs b/Dalamud/Game/Chat/SeStringHandling/Payload.cs
index 2fc1080cd..89f6c8bd4 100644
--- a/Dalamud/Game/Chat/SeStringHandling/Payload.cs
+++ b/Dalamud/Game/Chat/SeStringHandling/Payload.cs
@@ -51,22 +51,12 @@ namespace Dalamud.Game.Chat.SeStringHandling
///
/// The Lumina instance to use for any necessary data lookups.
///
- protected DataManager dataResolver;
+ protected DataManager DataResolver;
// private for now, since subclasses shouldn't interact with this
// To force-invalidate it, Dirty can be set to true
private byte[] encodedData;
- protected Payload()
- {
- // this is not a good way to do this, but I don't want to have to include a dalamud
- // reference on multiple methods in every payload class
- // We could also just directly reference this static where we use it, but this at least
- // allows for more easily changing how this is injected later, without affecting code
- // that makes use of it
- this.dataResolver = SeString.Dalamud.Data;
- }
-
///
/// Encode this payload object into a byte[] useable in-game for things like the chat log.
///
@@ -88,7 +78,7 @@ namespace Dalamud.Game.Chat.SeStringHandling
///
/// A reader positioned at the start of the payload, and containing at least one entire payload.
/// The constructed Payload-derived object that was decoded from the binary data.
- public static Payload Decode(BinaryReader reader)
+ public static Payload Decode(BinaryReader reader, DataManager data)
{
var payloadStartPos = reader.BaseStream.Position;
@@ -105,6 +95,8 @@ namespace Dalamud.Game.Chat.SeStringHandling
payload = DecodeChunk(reader);
}
+ payload.DataResolver = data;
+
// for now, cache off the actual binary data for this payload, so we don't have to
// regenerate it if the payload isn't modified
// TODO: probably better ways to handle this
diff --git a/Dalamud/Game/Chat/SeStringHandling/Payloads/AutoTranslatePayload.cs b/Dalamud/Game/Chat/SeStringHandling/Payloads/AutoTranslatePayload.cs
index f7a675ccd..c31c4c026 100644
--- a/Dalamud/Game/Chat/SeStringHandling/Payloads/AutoTranslatePayload.cs
+++ b/Dalamud/Game/Chat/SeStringHandling/Payloads/AutoTranslatePayload.cs
@@ -90,7 +90,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{
string value = null;
- var sheet = this.dataResolver.GetExcelSheet();
+ var sheet = this.DataResolver.GetExcelSheet();
Completion row = null;
try
@@ -119,24 +119,24 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
var name = actualTableName switch
{
- "Action" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "ActionComboRoute" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "BuddyAction" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "ClassJob" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "Companion" => this.dataResolver.GetExcelSheet().GetRow(this.key).Singular,
- "CraftAction" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "GeneralAction" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "GuardianDeity" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "MainCommand" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "Mount" => this.dataResolver.GetExcelSheet().GetRow(this.key).Singular,
- "Pet" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "PetAction" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "PetMirage" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "PlaceName" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
- "Race" => this.dataResolver.GetExcelSheet().GetRow(this.key).Masculine,
- "TextCommand" => this.dataResolver.GetExcelSheet().GetRow(this.key).Command,
- "Tribe" => this.dataResolver.GetExcelSheet().GetRow(this.key).Masculine,
- "Weather" => this.dataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "Action" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "ActionComboRoute" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "BuddyAction" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "ClassJob" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "Companion" => this.DataResolver.GetExcelSheet().GetRow(this.key).Singular,
+ "CraftAction" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "GeneralAction" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "GuardianDeity" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "MainCommand" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "Mount" => this.DataResolver.GetExcelSheet().GetRow(this.key).Singular,
+ "Pet" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "PetAction" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "PetMirage" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "PlaceName" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
+ "Race" => this.DataResolver.GetExcelSheet().GetRow(this.key).Masculine,
+ "TextCommand" => this.DataResolver.GetExcelSheet().GetRow(this.key).Command,
+ "Tribe" => this.DataResolver.GetExcelSheet().GetRow(this.key).Masculine,
+ "Weather" => this.DataResolver.GetExcelSheet().GetRow(this.key).Name,
_ => throw new Exception(actualTableName)
};
diff --git a/Dalamud/Game/Chat/SeStringHandling/Payloads/ItemPayload.cs b/Dalamud/Game/Chat/SeStringHandling/Payloads/ItemPayload.cs
index c723e5d6a..e0ca4b481 100644
--- a/Dalamud/Game/Chat/SeStringHandling/Payloads/ItemPayload.cs
+++ b/Dalamud/Game/Chat/SeStringHandling/Payloads/ItemPayload.cs
@@ -25,7 +25,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{
get
{
- this.item ??= this.dataResolver.GetExcelSheet- ().GetRow(this.itemId);
+ this.item ??= this.DataResolver.GetExcelSheet
- ().GetRow(this.itemId);
return this.item;
}
}
diff --git a/Dalamud/Game/Chat/SeStringHandling/Payloads/MapLinkPayload.cs b/Dalamud/Game/Chat/SeStringHandling/Payloads/MapLinkPayload.cs
index c222c4101..41bf4565b 100644
--- a/Dalamud/Game/Chat/SeStringHandling/Payloads/MapLinkPayload.cs
+++ b/Dalamud/Game/Chat/SeStringHandling/Payloads/MapLinkPayload.cs
@@ -23,7 +23,7 @@ namespace Dalamud.Game.Chat.SeStringHandling.Payloads
{
get
{
- this.map ??= this.dataResolver.GetExcelSheet