From db474652eedeec11e0ff45fb00503272b983b30e Mon Sep 17 00:00:00 2001 From: goat Date: Tue, 24 Mar 2020 03:16:49 +0900 Subject: [PATCH] feat: use Lumina for client state resolvers --- Dalamud/Dalamud.cs | 2 +- Dalamud/DiscordBot/DiscordBotManager.cs | 6 +++--- Dalamud/Game/ClientState/Actors/ActorTable.cs | 10 ++++++---- .../Actors/Resolvers/BaseResolver.cs | 17 +++++++++++++++++ .../ClientState/Actors/Resolvers/ClassJob.cs | 9 +++++---- .../Game/ClientState/Actors/Resolvers/World.cs | 9 +++++---- Dalamud/Game/ClientState/Actors/Types/Actor.cs | 6 +++++- Dalamud/Game/ClientState/Actors/Types/Chara.cs | 5 +++-- .../Actors/Types/NonPlayer/BattleNpc.cs | 3 ++- .../ClientState/Actors/Types/NonPlayer/Npc.cs | 3 ++- .../ClientState/Actors/Types/PlayerCharacter.cs | 7 ++++--- Dalamud/Game/ClientState/ClientState.cs | 5 ++--- Dalamud/Interface/DalamudDataWindow.cs | 6 +++--- 13 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 Dalamud/Game/ClientState/Actors/Resolvers/BaseResolver.cs diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 68a80f744..3f6af4d24 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -85,7 +85,7 @@ namespace Dalamud { this.Data = new DataManager(this.StartInfo.Language); this.Data.Initialize(); - this.ClientState = new ClientState(this, info, this.SigScanner, this.targetModule); + this.ClientState = new ClientState(this, info, this.SigScanner); this.BotManager = new DiscordBotManager(this, this.Configuration.DiscordFeatureConfig); diff --git a/Dalamud/DiscordBot/DiscordBotManager.cs b/Dalamud/DiscordBot/DiscordBotManager.cs index 4943c9f06..4dae26982 100644 --- a/Dalamud/DiscordBot/DiscordBotManager.cs +++ b/Dalamud/DiscordBot/DiscordBotManager.cs @@ -119,7 +119,7 @@ namespace Dalamud.DiscordBot { var world = string.Empty; if (this.dalamud.ClientState.Actors.Length > 0) - world = this.dalamud.ClientState.LocalPlayer.CurrentWorld.Name; + world = this.dalamud.ClientState.LocalPlayer.CurrentWorld.GameData.Name; var embedBuilder = new EmbedBuilder { @@ -144,7 +144,7 @@ namespace Dalamud.DiscordBot { dynamic item = XivApi.GetItem(itemId).GetAwaiter().GetResult(); var character = this.dalamud.ClientState.LocalPlayer; - var characterInfo = await GetCharacterInfo(character.Name, character.HomeWorld.Name); + var characterInfo = await GetCharacterInfo(character.Name, character.HomeWorld.GameData.Name); var embedBuilder = new EmbedBuilder { Title = (isHq ? "<:hq:593406013651156994> " : "") + item.Name, @@ -203,7 +203,7 @@ namespace Dalamud.DiscordBot { senderName = wasOutgoingTell ? this.dalamud.ClientState.LocalPlayer.Name : parsedSender.TextValue; } - senderWorld = this.dalamud.ClientState.LocalPlayer.HomeWorld.Name; + senderWorld = this.dalamud.ClientState.LocalPlayer.HomeWorld.GameData.Name; } else { playerLink.Resolve(); diff --git a/Dalamud/Game/ClientState/Actors/ActorTable.cs b/Dalamud/Game/ClientState/Actors/ActorTable.cs index 586404fd1..b1100065e 100644 --- a/Dalamud/Game/ClientState/Actors/ActorTable.cs +++ b/Dalamud/Game/ClientState/Actors/ActorTable.cs @@ -11,13 +11,15 @@ namespace Dalamud.Game.ClientState.Actors { /// public class ActorTable : ICollection { private ClientStateAddressResolver Address { get; } + private Dalamud dalamud; /// /// Set up the actor table collection. /// /// Client state address resolver. - public ActorTable(ClientStateAddressResolver addressResolver) { + public ActorTable(Dalamud dalamud, ClientStateAddressResolver addressResolver) { Address = addressResolver; + this.dalamud = dalamud; Log.Verbose("Actor table address {ActorTable}", Address.ActorTable); } @@ -50,9 +52,9 @@ namespace Dalamud.Game.ClientState.Actors { switch (actorStruct.ObjectKind) { - case ObjectKind.Player: return new PlayerCharacter(actorStruct); - case ObjectKind.BattleNpc: return new BattleNpc(actorStruct); - default: return new Actor(actorStruct); + case ObjectKind.Player: return new PlayerCharacter(actorStruct, this.dalamud); + case ObjectKind.BattleNpc: return new BattleNpc(actorStruct, this.dalamud); + default: return new Actor(actorStruct, this.dalamud); } } catch (AccessViolationException) { return null; diff --git a/Dalamud/Game/ClientState/Actors/Resolvers/BaseResolver.cs b/Dalamud/Game/ClientState/Actors/Resolvers/BaseResolver.cs new file mode 100644 index 000000000..af55336a1 --- /dev/null +++ b/Dalamud/Game/ClientState/Actors/Resolvers/BaseResolver.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace Dalamud.Game.ClientState.Actors.Resolvers +{ + public abstract class BaseResolver { + protected Dalamud dalamud; + + public BaseResolver(Dalamud dalamud) { + this.dalamud = dalamud; + } + } +} diff --git a/Dalamud/Game/ClientState/Actors/Resolvers/ClassJob.cs b/Dalamud/Game/ClientState/Actors/Resolvers/ClassJob.cs index 82e4b1c42..ecb8cbcc4 100644 --- a/Dalamud/Game/ClientState/Actors/Resolvers/ClassJob.cs +++ b/Dalamud/Game/ClientState/Actors/Resolvers/ClassJob.cs @@ -9,22 +9,23 @@ namespace Dalamud.Game.ClientState.Actors.Resolvers /// /// This object represents a class or job. /// - public class ClassJob { + public class ClassJob : BaseResolver { /// /// ID of the ClassJob. /// public readonly int Id; /// - /// Name of the ClassJob. + /// GameData linked to this ClassJob. /// - public string Name => (string) XivApi.GetClassJob(this.Id).GetAwaiter().GetResult()["Name"]; + public Lumina.Excel.GeneratedSheets.ClassJob GameData => + this.dalamud.Data.GetExcelSheet().GetRow(this.Id); /// /// Set up the ClassJob resolver with the provided ID. /// /// The ID of the world. - public ClassJob(byte id) { + public ClassJob(byte id, Dalamud dalamud) : base(dalamud) { this.Id = id; } } diff --git a/Dalamud/Game/ClientState/Actors/Resolvers/World.cs b/Dalamud/Game/ClientState/Actors/Resolvers/World.cs index 53d23cdec..4ba4d9396 100644 --- a/Dalamud/Game/ClientState/Actors/Resolvers/World.cs +++ b/Dalamud/Game/ClientState/Actors/Resolvers/World.cs @@ -9,22 +9,23 @@ namespace Dalamud.Game.ClientState.Actors.Resolvers /// /// This object represents a world a character can reside on. /// - public class World { + public class World : BaseResolver { /// /// ID of the world. /// public readonly int Id; /// - /// Name of the world. + /// GameData linked to this world. /// - public string Name => (string) XivApi.GetWorld(this.Id).GetAwaiter().GetResult()["Name"]; + public Lumina.Excel.GeneratedSheets.World GameData => + this.dalamud.Data.GetExcelSheet().GetRow(this.Id); /// /// Set up the world resolver with the provided ID. /// /// The ID of the world. - public World(byte id) { + public World(byte id, Dalamud dalamud) : base(dalamud) { this.Id = id; } } diff --git a/Dalamud/Game/ClientState/Actors/Types/Actor.cs b/Dalamud/Game/ClientState/Actors/Types/Actor.cs index ae327211f..fd9a06584 100644 --- a/Dalamud/Game/ClientState/Actors/Types/Actor.cs +++ b/Dalamud/Game/ClientState/Actors/Types/Actor.cs @@ -8,12 +8,16 @@ namespace Dalamud.Game.ClientState.Actors.Types { /// protected Structs.Actor actorStruct; + protected Dalamud dalamud; + /// /// Initialize a representation of a basic FFXIV actor. /// /// The memory representation of the base actor. - public Actor(Structs.Actor actorStruct) { + /// A dalamud reference needed to access game data in Resolvers. + public Actor(Structs.Actor actorStruct, Dalamud dalamud) { this.actorStruct = actorStruct; + this.dalamud = dalamud; } /// diff --git a/Dalamud/Game/ClientState/Actors/Types/Chara.cs b/Dalamud/Game/ClientState/Actors/Types/Chara.cs index 60ca0549c..4c01753db 100644 --- a/Dalamud/Game/ClientState/Actors/Types/Chara.cs +++ b/Dalamud/Game/ClientState/Actors/Types/Chara.cs @@ -9,7 +9,8 @@ namespace Dalamud.Game.ClientState.Actors.Types { /// Set up a new Chara with the provided memory representation. /// /// The memory representation of the base actor. - public Chara(Structs.Actor actorStruct) : base(actorStruct) { } + /// A dalamud reference needed to access game data in Resolvers. + protected Chara(Structs.Actor actorStruct, Dalamud dalamud) : base(actorStruct, dalamud) { } /// /// The level of this Chara. @@ -19,7 +20,7 @@ namespace Dalamud.Game.ClientState.Actors.Types { /// /// The ClassJob of this Chara. /// - public ClassJob ClassJob => new ClassJob(this.actorStruct.ClassJob); + public ClassJob ClassJob => new ClassJob(this.actorStruct.ClassJob, this.dalamud); /// /// The current HP of this Chara. diff --git a/Dalamud/Game/ClientState/Actors/Types/NonPlayer/BattleNpc.cs b/Dalamud/Game/ClientState/Actors/Types/NonPlayer/BattleNpc.cs index ea8bf9fcc..d316cb730 100644 --- a/Dalamud/Game/ClientState/Actors/Types/NonPlayer/BattleNpc.cs +++ b/Dalamud/Game/ClientState/Actors/Types/NonPlayer/BattleNpc.cs @@ -7,7 +7,8 @@ namespace Dalamud.Game.ClientState.Actors.Types.NonPlayer { /// Set up a new BattleNpc with the provided memory representation. /// /// The memory representation of the base actor. - public BattleNpc(Structs.Actor actorStruct) : base(actorStruct) { } + /// A dalamud reference needed to access game data in Resolvers. + public BattleNpc(Structs.Actor actorStruct, Dalamud dalamud) : base(actorStruct, dalamud) { } /// /// The BattleNpc of this BattleNpc. diff --git a/Dalamud/Game/ClientState/Actors/Types/NonPlayer/Npc.cs b/Dalamud/Game/ClientState/Actors/Types/NonPlayer/Npc.cs index ef0ce3b7c..2bd31b1e5 100644 --- a/Dalamud/Game/ClientState/Actors/Types/NonPlayer/Npc.cs +++ b/Dalamud/Game/ClientState/Actors/Types/NonPlayer/Npc.cs @@ -7,7 +7,8 @@ namespace Dalamud.Game.ClientState.Actors.Types.NonPlayer { /// Set up a new NPC with the provided memory representation. /// /// The memory representation of the base actor. - public Npc(Structs.Actor actorStruct) : base(actorStruct) { } + /// A dalamud reference needed to access game data in Resolvers. + protected Npc(Structs.Actor actorStruct, Dalamud dalamud) : base(actorStruct, dalamud) { } /// /// The data ID of the NPC linking to their respective game data. diff --git a/Dalamud/Game/ClientState/Actors/Types/PlayerCharacter.cs b/Dalamud/Game/ClientState/Actors/Types/PlayerCharacter.cs index ed09601de..b1c9238a1 100644 --- a/Dalamud/Game/ClientState/Actors/Types/PlayerCharacter.cs +++ b/Dalamud/Game/ClientState/Actors/Types/PlayerCharacter.cs @@ -9,16 +9,17 @@ namespace Dalamud.Game.ClientState.Actors.Types { /// Set up a new player character with the provided memory representation. /// /// The memory representation of the base actor. - public PlayerCharacter(Structs.Actor actorStruct) : base(actorStruct) { } + /// A dalamud reference needed to access game data in Resolvers. + public PlayerCharacter(Structs.Actor actorStruct, Dalamud dalamud) : base(actorStruct, dalamud) { } /// /// The current world of the character. /// - public World CurrentWorld => new World(this.actorStruct.CurrentWorld); + public World CurrentWorld => new World(this.actorStruct.CurrentWorld, this.dalamud); /// /// The home world of the character. /// - public World HomeWorld => new World(this.actorStruct.HomeWorld); + public World HomeWorld => new World(this.actorStruct.HomeWorld, this.dalamud); } } diff --git a/Dalamud/Game/ClientState/ClientState.cs b/Dalamud/Game/ClientState/ClientState.cs index ade048517..5ecfb75b2 100644 --- a/Dalamud/Game/ClientState/ClientState.cs +++ b/Dalamud/Game/ClientState/ClientState.cs @@ -61,8 +61,7 @@ namespace Dalamud.Game.ClientState /// Dalamud instance /// /// StartInfo of the current Dalamud launch /// Sig scanner - /// Game process module - public ClientState(Dalamud dalamud, DalamudStartInfo startInfo, SigScanner scanner, ProcessModule targetModule) { + public ClientState(Dalamud dalamud, DalamudStartInfo startInfo, SigScanner scanner) { Address = new ClientStateAddressResolver(); Address.Setup(scanner); @@ -70,7 +69,7 @@ namespace Dalamud.Game.ClientState this.ClientLanguage = startInfo.Language; - this.Actors = new ActorTable(Address); + this.Actors = new ActorTable(dalamud, Address); this.JobGauges = new JobGauges(Address); diff --git a/Dalamud/Interface/DalamudDataWindow.cs b/Dalamud/Interface/DalamudDataWindow.cs index d7e6168de..4965e7f34 100644 --- a/Dalamud/Interface/DalamudDataWindow.cs +++ b/Dalamud/Interface/DalamudDataWindow.cs @@ -69,8 +69,8 @@ namespace Dalamud.Interface stateString += $"ActorTableLen: {this.dalamud.ClientState.Actors.Length}\n"; stateString += $"LocalPlayerName: {this.dalamud.ClientState.LocalPlayer.Name}\n"; - stateString += $"CurrentWorldName: {this.dalamud.ClientState.LocalPlayer.CurrentWorld.Name}\n"; - stateString += $"HomeWorldName: {this.dalamud.ClientState.LocalPlayer.HomeWorld.Name}\n"; + stateString += $"CurrentWorldName: {this.dalamud.ClientState.LocalPlayer.CurrentWorld.GameData.Name}\n"; + stateString += $"HomeWorldName: {this.dalamud.ClientState.LocalPlayer.HomeWorld.GameData.Name}\n"; stateString += $"LocalCID: {this.dalamud.ClientState.LocalContentId:X}\n"; stateString += $"LastLinkedItem: {this.dalamud.Framework.Gui.Chat.LastLinkedItemId.ToString()}\n"; @@ -85,7 +85,7 @@ namespace Dalamud.Interface if (actor is Chara chara) stateString += - $" Level: {chara.Level} ClassJob: {chara.ClassJob.Name} CHP: {chara.CurrentHp} MHP: {chara.MaxHp} CMP: {chara.CurrentMp} MMP: {chara.MaxMp}\n"; + $" Level: {chara.Level} ClassJob: {chara.ClassJob.GameData.Name} CHP: {chara.CurrentHp} MHP: {chara.MaxHp} CMP: {chara.CurrentMp} MMP: {chara.MaxMp}\n"; ; }