Implement service locator

This commit is contained in:
Raymond 2021-08-20 11:59:35 -04:00
parent 06b1163a52
commit ff1d7f2829
101 changed files with 1614 additions and 1436 deletions

View file

@ -27,9 +27,9 @@ namespace Dalamud.Game.Text.SeStringHandling
private byte[] encodedData;
/// <summary>
/// Gets or sets the Lumina instance to use for any necessary data lookups.
/// Gets the Lumina instance to use for any necessary data lookups.
/// </summary>
public DataManager DataResolver { get; set; }
public DataManager DataResolver => Service<DataManager>.Get();
/// <summary>
/// Gets the type of this payload.
@ -45,9 +45,8 @@ namespace Dalamud.Game.Text.SeStringHandling
/// Decodes a binary representation of a payload into its corresponding nice object payload.
/// </summary>
/// <param name="reader">A reader positioned at the start of the payload, and containing at least one entire payload.</param>
/// <param name="data">The DataManager instance.</param>
/// <returns>The constructed Payload-derived object that was decoded from the binary data.</returns>
public static Payload Decode(BinaryReader reader, DataManager data)
public static Payload Decode(BinaryReader reader)
{
var payloadStartPos = reader.BaseStream.Position;
@ -64,8 +63,6 @@ namespace Dalamud.Game.Text.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

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
using Serilog;
@ -27,17 +26,15 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="AutoTranslatePayload"/> class.
/// Creates a new auto-translate payload.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="group">The group id for this message.</param>
/// <param name="key">The key/row id for this message. Which table this is in depends on the group id and details the Completion table.</param>
/// <remarks>
/// This table is somewhat complicated in structure, and so using this constructor may not be very nice.
/// There is probably little use to create one of these, however.
/// </remarks>
public AutoTranslatePayload(DataManager data, uint group, uint key)
public AutoTranslatePayload(uint group, uint key)
{
// TODO: friendlier ctor? not sure how to handle that given how weird the tables are
this.DataResolver = data;
this.group = group;
this.key = key;
}

View file

@ -3,7 +3,6 @@ using System.IO;
using System.Linq;
using System.Text;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
@ -29,15 +28,13 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="ItemPayload"/> class.
/// Creates a payload representing an interactable item link for the specified item.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="itemId">The id of the item.</param>
/// <param name="isHQ">Whether or not the link should be for the high-quality variant of the item.</param>
/// <param name="displayNameOverride">An optional name to include in the item link. Typically this should
/// be left as null, or set to the normal item name. Actual overrides are better done with the subsequent
/// TextPayload that is a part of a full item link in chat.</param>
public ItemPayload(DataManager data, uint itemId, bool isHQ, string displayNameOverride = null)
public ItemPayload(uint itemId, bool isHQ, string displayNameOverride = null)
{
this.DataResolver = data;
this.itemId = itemId;
this.IsHQ = isHQ;
this.displayName = displayNameOverride;

View file

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
@ -28,15 +27,13 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="MapLinkPayload"/> class.
/// Creates an interactable MapLinkPayload from a human-readable position.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="territoryTypeId">The id of the TerritoryType entry for this link.</param>
/// <param name="mapId">The id of the Map entry for this link.</param>
/// <param name="niceXCoord">The human-readable x-coordinate for this link.</param>
/// <param name="niceYCoord">The human-readable y-coordinate for this link.</param>
/// <param name="fudgeFactor">An optional offset to account for rounding and truncation errors; it is best to leave this untouched in most cases.</param>
public MapLinkPayload(DataManager data, uint territoryTypeId, uint mapId, float niceXCoord, float niceYCoord, float fudgeFactor = 0.05f)
public MapLinkPayload(uint territoryTypeId, uint mapId, float niceXCoord, float niceYCoord, float fudgeFactor = 0.05f)
{
this.DataResolver = data;
this.territoryTypeId = territoryTypeId;
this.mapId = mapId;
// this fudge is necessary basically to ensure we don't shift down a full tenth
@ -50,14 +47,12 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="MapLinkPayload"/> class.
/// Creates an interactable MapLinkPayload from a raw position.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="territoryTypeId">The id of the TerritoryType entry for this link.</param>
/// <param name="mapId">The id of the Map entry for this link.</param>
/// <param name="rawX">The internal raw x-coordinate for this link.</param>
/// <param name="rawY">The internal raw y-coordinate for this link.</param>
public MapLinkPayload(DataManager data, uint territoryTypeId, uint mapId, int rawX, int rawY)
public MapLinkPayload(uint territoryTypeId, uint mapId, int rawX, int rawY)
{
this.DataResolver = data;
this.territoryTypeId = territoryTypeId;
this.mapId = mapId;
this.RawX = rawX;

View file

@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
@ -25,12 +24,10 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="PlayerPayload"/> class.
/// Create a PlayerPayload link for the specified player.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="playerName">The player's displayed name.</param>
/// <param name="serverId">The player's home server id.</param>
public PlayerPayload(DataManager data, string playerName, uint serverId)
public PlayerPayload(string playerName, uint serverId)
{
this.DataResolver = data;
this.playerName = playerName;
this.serverId = serverId;
}

View file

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
@ -21,11 +20,9 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="QuestPayload"/> class.
/// Creates a payload representing an interactable quest link for the specified quest.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="questId">The id of the quest.</param>
public QuestPayload(DataManager data, uint questId)
public QuestPayload(uint questId)
{
this.DataResolver = data;
this.questId = questId;
}

View file

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
@ -21,11 +20,9 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="StatusPayload"/> class.
/// Creates a new StatusPayload for the given status id.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="statusId">The id of the Status for this link.</param>
public StatusPayload(DataManager data, uint statusId)
public StatusPayload(uint statusId)
{
this.DataResolver = data;
this.statusId = statusId;
}

View file

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
@ -21,11 +20,9 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="UIForegroundPayload"/> class.
/// Creates a new UIForegroundPayload for the given UIColor key.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="colorKey">A UIColor key.</param>
public UIForegroundPayload(DataManager data, ushort colorKey)
public UIForegroundPayload(ushort colorKey)
{
this.DataResolver = data;
this.colorKey = colorKey;
}
@ -41,7 +38,7 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Gets a payload representing disabling foreground color on following text.
/// </summary>
// TODO Make this work with DI
public static UIForegroundPayload UIForegroundOff => new(null, 0);
public static UIForegroundPayload UIForegroundOff => new(0);
/// <inheritdoc/>
public override PayloadType Type => PayloadType.UIForeground;

View file

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using Dalamud.Data;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json;
@ -21,11 +20,9 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Initializes a new instance of the <see cref="UIGlowPayload"/> class.
/// Creates a new UIForegroundPayload for the given UIColor key.
/// </summary>
/// <param name="data">DataManager instance needed to resolve game data.</param>
/// <param name="colorKey">A UIColor key.</param>
public UIGlowPayload(DataManager data, ushort colorKey)
public UIGlowPayload(ushort colorKey)
{
this.DataResolver = data;
this.colorKey = colorKey;
}
@ -41,7 +38,7 @@ namespace Dalamud.Game.Text.SeStringHandling.Payloads
/// Gets a payload representing disabling glow color on following text.
/// </summary>
// TODO Make this work with DI
public static UIGlowPayload UIGlowOff => new(null, 0);
public static UIGlowPayload UIGlowOff => new(0);
/// <inheritdoc/>
public override PayloadType Type => PayloadType.UIGlow;

View file

@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dalamud.Data;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Newtonsoft.Json;
@ -76,9 +75,8 @@ namespace Dalamud.Game.Text.SeStringHandling
/// Creates a SeString from a json. (For testing - not recommended for production use.)
/// </summary>
/// <param name="json">A serialized SeString produced by ToJson() <see cref="ToJson"/>.</param>
/// <param name="dataManager">An initialized instance of DataManager for Lumina queries.</param>
/// <returns>A SeString initialized with values from the json.</returns>
public static SeString FromJson(string json, DataManager dataManager)
public static SeString FromJson(string json)
{
var s = JsonConvert.DeserializeObject<SeString>(json, new JsonSerializerSettings
{
@ -87,11 +85,6 @@ namespace Dalamud.Game.Text.SeStringHandling
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
});
foreach (var payload in s.Payloads)
{
payload.DataResolver = dataManager;
}
return s;
}

View file

@ -4,6 +4,8 @@ using System.Linq;
using Dalamud.Data;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
using Lumina.Excel.GeneratedSheets;
namespace Dalamud.Game.Text.SeStringHandling
@ -11,17 +13,15 @@ namespace Dalamud.Game.Text.SeStringHandling
/// <summary>
/// This class facilitates creating new SeStrings and breaking down existing ones into their individual payload components.
/// </summary>
public class SeStringManager
[PluginInterface]
[InterfaceVersion("1.0")]
public sealed class SeStringManager
{
private readonly DataManager data;
/// <summary>
/// Initializes a new instance of the <see cref="SeStringManager"/> class.
/// </summary>
/// <param name="data">The DataManager instance.</param>
public SeStringManager(DataManager data)
internal SeStringManager()
{
this.data = data;
}
/// <summary>
@ -38,7 +38,7 @@ namespace Dalamud.Game.Text.SeStringHandling
{
while (stream.Position < bytes.Length)
{
var payload = Payload.Decode(reader, this.data);
var payload = Payload.Decode(reader);
if (payload != null)
payloads.Add(payload);
}
@ -56,7 +56,9 @@ namespace Dalamud.Game.Text.SeStringHandling
/// <returns>An SeString containing all the payloads necessary to display an item link in the chat log.</returns>
public SeString CreateItemLink(uint itemId, bool isHQ, string displayNameOverride = null)
{
var displayName = displayNameOverride ?? this.data.GetExcelSheet<Item>().GetRow(itemId).Name;
var data = Service<DataManager>.Get();
var displayName = displayNameOverride ?? data.GetExcelSheet<Item>().GetRow(itemId).Name;
if (isHQ)
{
displayName += $" {(char)SeIconChar.HighQuality}";
@ -65,9 +67,9 @@ namespace Dalamud.Game.Text.SeStringHandling
// TODO: probably a cleaner way to build these than doing the bulk+insert
var payloads = new List<Payload>(new Payload[]
{
new UIForegroundPayload(this.data, 0x0225),
new UIGlowPayload(this.data, 0x0226),
new ItemPayload(this.data, itemId, isHQ),
new UIForegroundPayload(0x0225),
new UIGlowPayload(0x0226),
new ItemPayload(itemId, isHQ),
// arrow goes here
new TextPayload(displayName),
RawPayload.LinkTerminator,
@ -101,7 +103,7 @@ namespace Dalamud.Game.Text.SeStringHandling
/// <returns>An SeString containing all of the payloads necessary to display a map link in the chat log.</returns>
public SeString CreateMapLink(uint territoryId, uint mapId, int rawX, int rawY)
{
var mapPayload = new MapLinkPayload(this.data, territoryId, mapId, rawX, rawY);
var mapPayload = new MapLinkPayload(territoryId, mapId, rawX, rawY);
var nameString = $"{mapPayload.PlaceName} {mapPayload.CoordinateString}";
var payloads = new List<Payload>(new Payload[]
@ -127,7 +129,7 @@ namespace Dalamud.Game.Text.SeStringHandling
/// <returns>An SeString containing all of the payloads necessary to display a map link in the chat log.</returns>
public SeString CreateMapLink(uint territoryId, uint mapId, float xCoord, float yCoord, float fudgeFactor = 0.05f)
{
var mapPayload = new MapLinkPayload(this.data, territoryId, mapId, xCoord, yCoord, fudgeFactor);
var mapPayload = new MapLinkPayload(territoryId, mapId, xCoord, yCoord, fudgeFactor);
var nameString = $"{mapPayload.PlaceName} {mapPayload.CoordinateString}";
var payloads = new List<Payload>(new Payload[]
@ -152,9 +154,11 @@ namespace Dalamud.Game.Text.SeStringHandling
/// <returns>An SeString containing all of the payloads necessary to display a map link in the chat log.</returns>
public SeString CreateMapLink(string placeName, float xCoord, float yCoord, float fudgeFactor = 0.05f)
{
var mapSheet = this.data.GetExcelSheet<Map>();
var data = Service<DataManager>.Get();
var matches = this.data.GetExcelSheet<PlaceName>()
var mapSheet = data.GetExcelSheet<Map>();
var matches = data.GetExcelSheet<PlaceName>()
.Where(row => row.Name.ToString().ToLowerInvariant() == placeName.ToLowerInvariant())
.ToArray();
@ -180,8 +184,8 @@ namespace Dalamud.Game.Text.SeStringHandling
{
return new List<Payload>(new Payload[]
{
new UIForegroundPayload(this.data, 0x01F4),
new UIGlowPayload(this.data, 0x01F5),
new UIForegroundPayload(0x01F4),
new UIGlowPayload(0x01F5),
new TextPayload($"{(char)SeIconChar.LinkMarker}"),
UIGlowPayload.UIGlowOff,
UIForegroundPayload.UIForegroundOff,