Use GcArmyManager instead of addon/atk parsing

This commit is contained in:
Liza 2024-03-02 22:15:39 +01:00
parent 6891846716
commit cf0e72ba4d
Signed by: liza
GPG Key ID: 7199F8D727D55F67
6 changed files with 83 additions and 60 deletions

View File

@ -37,4 +37,9 @@ internal class Attributes : IEquatable<Attributes>
{ {
return !Equals(left, right); return !Equals(left, right);
} }
public override string ToString()
{
return $"{PhysicalAbility} / {MentalAbility} / {TacticalAbility}";
}
} }

View File

@ -10,12 +10,12 @@ internal sealed class SquadronMember : IEquatable<SquadronMember>
{ {
public required string Name { get; init; } public required string Name { get; init; }
public required int Level { get; init; } public required int Level { get; init; }
public required uint ClassJob { get; init; }
// TODO
public required Race Race { get; init; }
// TODO public required uint ClassJob { get; init; }
public int Experience { get; init; }
public required Race Race { get; init; }
public required uint EnlistmentTimestamp { get; init; }
public uint Experience { get; init; }
public int PhysicalAbility => GrowthParams[Level].PhysicalAbility; public int PhysicalAbility => GrowthParams[Level].PhysicalAbility;
public int MentalAbility => GrowthParams[Level].MentalAbility; public int MentalAbility => GrowthParams[Level].MentalAbility;
public int TacticalAbility => GrowthParams[Level].TacticalAbility; public int TacticalAbility => GrowthParams[Level].TacticalAbility;
@ -34,6 +34,7 @@ internal sealed class SquadronMember : IEquatable<SquadronMember>
{ {
growthAsList.Add((growth.Physical[i], growth.Mental[i], growth.Tactical[i])); growthAsList.Add((growth.Physical[i], growth.Mental[i], growth.Tactical[i]));
} }
growthAsList.Add((growth.Unknown123, growth.Unknown184, growth.Unknown245)); growthAsList.Add((growth.Unknown123, growth.Unknown184, growth.Unknown245));
GrowthParams = growthAsList; GrowthParams = growthAsList;
return this; return this;
@ -43,7 +44,8 @@ internal sealed class SquadronMember : IEquatable<SquadronMember>
{ {
if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true; if (ReferenceEquals(this, other)) return true;
return Name == other.Name && Level == other.Level && ClassJob == other.ClassJob && Race == other.Race && Experience == other.Experience; return Name == other.Name && Level == other.Level && ClassJob == other.ClassJob && Race == other.Race &&
Experience == other.Experience && PhysicalAbility == other.PhysicalAbility && MentalAbility == other.MentalAbility && TacticalAbility == other.TacticalAbility;
} }
public override bool Equals(object? obj) public override bool Equals(object? obj)
@ -65,4 +67,11 @@ internal sealed class SquadronMember : IEquatable<SquadronMember>
{ {
return !Equals(left, right); return !Equals(left, right);
} }
public Attributes ToAttributes() => new()
{
PhysicalAbility = PhysicalAbility,
MentalAbility = MentalAbility,
TacticalAbility = TacticalAbility,
};
} }

View File

@ -7,9 +7,9 @@ internal sealed class SquadronState
{ {
private readonly Dictionary<SquadronMission, Task<SquadronSolver.CalculationResults>> _calculationResults = new(); private readonly Dictionary<SquadronMission, Task<SquadronSolver.CalculationResults>> _calculationResults = new();
public required byte Rank { get; init; }
public required IReadOnlyList<SquadronMember> Members { get; init; } public required IReadOnlyList<SquadronMember> Members { get; init; }
public required BonusAttributes Bonus { get; set; } public required BonusAttributes Bonus { get; set; }
public required uint CurrentTraining { get; set; }
public Task<SquadronSolver.CalculationResults>? GetCalculation(SquadronMission mission) public Task<SquadronSolver.CalculationResults>? GetCalculation(SquadronMission mission)
=> _calculationResults.TryGetValue(mission, out var task) ? task : null; => _calculationResults.TryGetValue(mission, out var task) ? task : null;

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<Version>1.1</Version> <Version>2.0</Version>
<LangVersion>11.0</LangVersion> <LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

View File

@ -1,15 +1,13 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using Dalamud.Game.Addon.Lifecycle; using Dalamud.Game.Addon.Lifecycle;
using Dalamud.Game.Addon.Lifecycle.AddonArgTypes; using Dalamud.Game.Addon.Lifecycle.AddonArgTypes;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using Dalamud.Memory;
using Dalamud.Plugin; using Dalamud.Plugin;
using Dalamud.Plugin.Services; using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Game;
using FFXIVClientStructs.FFXIV.Client.UI; using FFXIVClientStructs.FFXIV.Client.UI;
using FFXIVClientStructs.FFXIV.Component.GUI;
using Lumina.Excel.GeneratedSheets; using Lumina.Excel.GeneratedSheets;
using Squadronista.Solver; using Squadronista.Solver;
using Squadronista.Windows; using Squadronista.Windows;
@ -26,12 +24,11 @@ public class SquadronistaPlugin : IDalamudPlugin
private readonly IPluginLog _pluginLog; private readonly IPluginLog _pluginLog;
private readonly IDataManager _dataManager; private readonly IDataManager _dataManager;
private readonly IAddonLifecycle _addonLifecycle; private readonly IAddonLifecycle _addonLifecycle;
private readonly IReadOnlyDictionary<string, uint> _classNamesToId;
private readonly IReadOnlyList<SquadronMission> _allMissions; private readonly IReadOnlyList<SquadronMission> _allMissions;
private readonly MainWindow _mainWindow; private readonly MainWindow _mainWindow;
public SquadronistaPlugin(DalamudPluginInterface pluginInterface, IClientState clientState, IPluginLog pluginLog, public SquadronistaPlugin(DalamudPluginInterface pluginInterface, IClientState clientState, IPluginLog pluginLog,
IDataManager dataManager, IAddonLifecycle addonLifecycle, IGameGui gameGui, IFramework framework) IDataManager dataManager, IAddonLifecycle addonLifecycle, IGameGui gameGui)
{ {
_pluginInterface = pluginInterface; _pluginInterface = pluginInterface;
_clientState = clientState; _clientState = clientState;
@ -39,12 +36,6 @@ public class SquadronistaPlugin : IDalamudPlugin
_dataManager = dataManager; _dataManager = dataManager;
_addonLifecycle = addonLifecycle; _addonLifecycle = addonLifecycle;
_classNamesToId = dataManager.GetExcelSheet<ClassJob>()!
.Where(x => x.RowId > 0)
.Where(x => x.Name.ToString().Length > 0)
.ToDictionary(x => x.Name.ToString().ToLower(), x => x.RowId)
.AsReadOnly();
_allMissions = dataManager.GetExcelSheet<GcArmyExpedition>()! _allMissions = dataManager.GetExcelSheet<GcArmyExpedition>()!
.Where(x => x.RowId > 0) .Where(x => x.RowId > 0)
.Select(x => new SquadronMission .Select(x => new SquadronMission
@ -98,68 +89,81 @@ public class SquadronistaPlugin : IDalamudPlugin
private unsafe void UpdateSquadronState(AddonEvent type, AddonArgs args) private unsafe void UpdateSquadronState(AddonEvent type, AddonArgs args)
{ {
_pluginLog.Information("Updating squadron state from member list"); _pluginLog.Information("Updating squadron state from member list");
var gcArmyManager = GcArmyManager.Instance();
AtkUnitBase* addon = (AtkUnitBase*)args.Addon; if (gcArmyManager == null)
if (addon->AtkValuesCount != 133)
{ {
_pluginLog.Error("Unexpected AddonGcArmyMemberList atkvalues count"); _pluginLog.Warning("No GcArmyManager");
ResetCharacterSpecificData(); ResetCharacterSpecificData();
return; return;
} }
var atkValues = addon->AtkValues; if (gcArmyManager->Data == null)
uint memberCount = atkValues[4].UInt;
// can't do any missions like this...
if (memberCount < 4)
{ {
_pluginLog.Warning("No GcArmyManager->Data");
ResetCharacterSpecificData(); ResetCharacterSpecificData();
return; return;
} }
IReadOnlyList<SquadronMember> members = Enumerable.Range(0, (int)memberCount) if (gcArmyManager->GetMemberCount() < 4)
.Select(i => new SquadronMember {
_pluginLog.Warning($"Not enough squadron members to send on missions, only got {gcArmyManager->GetMemberCount()} members");
ResetCharacterSpecificData();
return;
}
IReadOnlyList<SquadronMember> members = Enumerable.Range(0, (int)gcArmyManager->GetMemberCount())
.Select(i =>
{ {
Name = ReadAtkString(atkValues[6 + 15 * i])!, var member = gcArmyManager->GetMember((uint)i);
Level = atkValues[10 + 15 * i].Int, if (member == null)
ClassJob = _classNamesToId[ReadAtkString(atkValues[7 + 15 * i])!.ToLower()], return null;
Race = Race.Lalafell, // TODO
Experience = 0, // TODO return new SquadronMember
}.InitializeFrom(_dataManager)) {
Name = _dataManager.GetExcelSheet<ENpcResident>()!.GetRow(member->ENpcResidentId)!
.Singular.ToString(),
Level = member->Level,
ClassJob = member->ClassJob,
Race = (Race)member->Race,
Experience = member->Experience,
EnlistmentTimestamp = member->EnlistmentTimestamp,
}.InitializeFrom(_dataManager);
})
.Where(x => x != null)
.Cast<SquadronMember>()
.OrderBy(x => x.EnlistmentTimestamp)
.ToList() .ToList()
.AsReadOnly(); .AsReadOnly();
byte rank = byte.Parse(ReadAtkString(atkValues[0])!.Split(":")[1].Trim());
int[] attributes = ReadAtkString(atkValues[1])!.Split(":")[1].Trim()
.Split("/")
.Select(int.Parse)
.ToArray();
var bonus = new BonusAttributes var bonus = new BonusAttributes
{ {
PhysicalAbility = attributes[0], PhysicalAbility = gcArmyManager->Data->BonusPhysical,
MentalAbility = attributes[1], MentalAbility = gcArmyManager->Data->BonusMental,
TacticalAbility = attributes[2], TacticalAbility = gcArmyManager->Data->BonusTactical,
Cap = attributes.Sum() Cap = gcArmyManager->Data->BonusPhysical + gcArmyManager->Data->BonusMental + gcArmyManager->Data->BonusTactical,
}; };
if (SquadronState != null && if (SquadronState != null &&
members.SequenceEqual(SquadronState.Members) && members.SequenceEqual(SquadronState.Members) &&
rank == SquadronState.Rank &&
bonus == SquadronState.Bonus) bonus == SquadronState.Bonus)
{ {
// nothing changed... // nothing changed...
_pluginLog.Verbose("Not updating SquadronState, not changed");
return; return;
} }
SquadronState = new SquadronState SquadronState = new SquadronState
{ {
Members = members, Members = members,
Rank = rank,
Bonus = bonus, Bonus = bonus,
CurrentTraining = ((ExtendedGcArmyData*)gcArmyManager->Data)->CurrentTraining,
}; };
_pluginLog.Verbose(
$"Bonus stats: {bonus} (Cap: {bonus.Cap})");
foreach (var member in members) foreach (var member in members)
_pluginLog.Verbose($"Squadron Member {member.Name}: ClassJob {member.ClassJob}, Lv{member.Level}"); _pluginLog.Verbose(
$"Squadron Member {member.Name}: ClassJob {member.ClassJob}, Lv{member.Level} → {member.ToAttributes()}");
} }
private unsafe void UpdateExpeditionState(AddonEvent type, AddonArgs args) private unsafe void UpdateExpeditionState(AddonEvent type, AddonArgs args)
@ -184,13 +188,6 @@ public class SquadronistaPlugin : IDalamudPlugin
.ToList(); .ToList();
} }
private unsafe string? ReadAtkString(AtkValue atkValue)
{
if (atkValue.String != null)
return MemoryHelper.ReadSeStringNullTerminated(new nint(atkValue.String)).ToString();
return null;
}
private void ResetCharacterSpecificData() private void ResetCharacterSpecificData()
{ {
SquadronState = null; SquadronState = null;
@ -207,4 +204,9 @@ public class SquadronistaPlugin : IDalamudPlugin
_clientState.Logout -= ResetCharacterSpecificData; _clientState.Logout -= ResetCharacterSpecificData;
_pluginInterface.UiBuilder.Draw -= _windowSystem.Draw; _pluginInterface.UiBuilder.Draw -= _windowSystem.Draw;
} }
[StructLayout(LayoutKind.Explicit, Size = 0xB18)]
private struct ExtendedGcArmyData {
[FieldOffset(0x284)] public ushort CurrentTraining;
}
} }

View File

@ -97,13 +97,20 @@ internal sealed class MainWindow : LImGui.LWindow, IDisposable
} }
var selectedMission = _plugin.AvailableMissions[agentExpedition->SelectedRow]; var selectedMission = _plugin.AvailableMissions[agentExpedition->SelectedRow];
ImGui.Text($"{selectedMission.Name}"); if (selectedMission.CurrentAttributes != null)
ImGui.Text($"{selectedMission.Name} ({selectedMission.CurrentAttributes})");
else
ImGui.Text($"{selectedMission.Name}");
var state = _plugin.SquadronState; var state = _plugin.SquadronState;
if (state == null) if (state == null)
{ {
ImGui.TextColored(ImGuiColors.DalamudYellow, "Open Squadron Member list to continue."); ImGui.TextColored(ImGuiColors.DalamudYellow, "Open Squadron Member list to continue.");
} }
else if (state.CurrentTraining != 0)
{
ImGui.TextColored(ImGuiColors.DalamudRed, "Your squadron is currently in training.");
}
else else
{ {
var task = state.GetCalculation(selectedMission); var task = state.GetCalculation(selectedMission);
@ -188,7 +195,7 @@ internal sealed class MainWindow : LImGui.LWindow, IDisposable
} }
else else
{ {
ImGui.TextColored(ImGuiColors.DalamudRed, $"No combination of members/trainings can achieve\n{(selectedMission.IsFlaggedMission ? "all" : "2 out of 3")} attributes for {selectedMission.CurrentAttributes!.PhysicalAbility} / {selectedMission.CurrentAttributes!.MentalAbility} / {selectedMission.CurrentAttributes!.TacticalAbility}."); ImGui.TextColored(ImGuiColors.DalamudRed, $"No combination of members/trainings can achieve\n{(selectedMission.IsFlaggedMission ? "all" : "2 out of 3")} attributes for {selectedMission.CurrentAttributes}.");
ImGui.Text("Level the squadron further and check again."); ImGui.Text("Level the squadron further and check again.");
} }
} }
@ -319,7 +326,7 @@ internal sealed class MainWindow : LImGui.LWindow, IDisposable
if (selectedMission.PossibleAttributes.Contains(newAttributes)) if (selectedMission.PossibleAttributes.Contains(newAttributes))
selectedMission.CurrentAttributes = newAttributes; selectedMission.CurrentAttributes = newAttributes;
else else
_pluginLog.Warning($"Wrong attributes for {selectedMission.Name}: {physical} / {mental} / {tactical}"); _pluginLog.Warning($"Wrong attributes for {selectedMission.Name}: {newAttributes}");
} }
private static unsafe T* GetNodeById<T>(AtkUldManager uldManager, uint nodeId, NodeType? type = null) private static unsafe T* GetNodeById<T>(AtkUldManager uldManager, uint nodeId, NodeType? type = null)