OrangeGuidanceTomestone/client/Util/ActorManager.cs

228 lines
8.0 KiB
C#
Raw Normal View History

2024-07-22 05:44:50 +00:00
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Game.Character;
using FFXIVClientStructs.FFXIV.Client.Game.Object;
2024-07-22 19:06:51 +00:00
using FFXIVClientStructs.Interop;
2024-07-22 07:44:30 +00:00
using Lumina.Excel.GeneratedSheets;
2024-07-22 05:44:50 +00:00
namespace OrangeGuidanceTomestone.Util;
internal class ActorManager : IDisposable {
private Plugin Plugin { get; }
2024-07-22 19:06:51 +00:00
private readonly Stack<uint> _idx = [];
2024-07-22 06:22:54 +00:00
private readonly Queue<BaseActorAction> _tasks = [];
2024-07-22 05:44:50 +00:00
internal ActorManager(Plugin plugin) {
this.Plugin = plugin;
this.Plugin.Framework.Update += this.OnFramework;
this.Plugin.ClientState.TerritoryChanged += this.OnTerritoryChange;
2024-07-22 05:44:50 +00:00
this.Plugin.Ui.Viewer.View += this.OnView;
}
public void Dispose() {
this.Plugin.Ui.Viewer.View -= this.OnView;
this.Plugin.ClientState.TerritoryChanged -= this.OnTerritoryChange;
2024-07-22 05:44:50 +00:00
this.Plugin.Framework.Update -= this.OnFramework;
2024-07-22 09:49:51 +00:00
2024-07-22 19:06:51 +00:00
if (this._idx.Count > 0) {
2024-07-22 09:49:51 +00:00
unsafe {
var objMan = ClientObjectManager.Instance();
new DeleteAction().Run(this, objMan);
}
}
2024-07-22 05:44:50 +00:00
}
private unsafe void OnFramework(IFramework framework) {
2024-07-22 06:22:54 +00:00
if (!this._tasks.TryPeek(out var actorAction)) {
2024-07-22 05:44:50 +00:00
return;
}
var objMan = ClientObjectManager.Instance();
2024-07-22 06:22:54 +00:00
var success = false;
2024-07-22 05:44:50 +00:00
2024-07-22 06:22:54 +00:00
if (actorAction.Tries < 10) {
try {
actorAction.Tries += 1;
success = actorAction.Run(this, objMan);
} catch (Exception ex) {
Plugin.Log.Error(ex, "Error in actor action queue");
2024-07-22 05:44:50 +00:00
}
2024-07-22 06:22:54 +00:00
} else {
Plugin.Log.Warning("too many retries, skipping");
success = true;
2024-07-22 05:44:50 +00:00
}
if (success) {
2024-07-22 06:22:54 +00:00
this._tasks.Dequeue();
2024-07-22 05:44:50 +00:00
}
}
private void OnTerritoryChange(ushort obj) {
2024-07-22 19:06:51 +00:00
this._idx.Clear();
}
2024-07-22 05:44:50 +00:00
private void OnView(Message? message) {
2024-07-22 06:07:05 +00:00
var msg = message == null ? "null" : "not null";
Plugin.Log.Debug($"OnView message is {msg}");
2024-07-22 05:44:50 +00:00
this.Despawn();
2024-07-22 09:49:51 +00:00
if (this.Plugin.Config.ShowEmotes && message?.Emote != null) {
2024-07-22 05:44:50 +00:00
this.Spawn(message);
}
}
internal void Spawn(Message message) {
this._tasks.Enqueue(new SpawnAction(message));
2024-07-22 06:22:54 +00:00
}
internal void Despawn() {
this._tasks.Enqueue(new DeleteAction());
}
private abstract unsafe class BaseActorAction {
2024-07-22 06:22:54 +00:00
/// <summary>
/// Run this action.
/// </summary>
/// <returns>true if the action is finished, false if it should be run again</returns>
public abstract bool Run(ActorManager manager, ClientObjectManager* objMan);
public int Tries { get; set; }
2024-07-22 19:06:51 +00:00
protected IEnumerable<Pointer<BattleChara>> GetBattleCharas(
2024-07-22 06:22:54 +00:00
ActorManager manager,
2024-07-22 19:06:51 +00:00
Pointer<ClientObjectManager> objMan
2024-07-22 06:22:54 +00:00
) {
2024-07-22 19:06:51 +00:00
foreach (var idx in manager._idx) {
Pointer<BattleChara> ptr;
unsafe {
var obj = (BattleChara*) objMan.Value->GetObjectByIndex((ushort) idx);
if (obj == null) {
continue;
}
ptr = obj;
}
2024-07-22 06:22:54 +00:00
2024-07-22 19:06:51 +00:00
yield return ptr;
2024-07-22 06:22:54 +00:00
}
}
}
private unsafe class SpawnAction(Message message) : BaseActorAction {
public override bool Run(ActorManager manager, ClientObjectManager* objMan) {
2024-07-22 07:44:30 +00:00
if (message.Emote == null) {
Plugin.Log.Warning("refusing to spawn an actor for a message without an emote");
return true;
}
2024-07-22 06:22:54 +00:00
var idx = objMan->CreateBattleCharacter();
if (idx == 0xFFFFFFFF) {
Plugin.Log.Debug("actor could not be spawned");
return true;
}
2024-07-22 19:06:51 +00:00
manager._idx.Push(idx);
2024-07-22 07:44:30 +00:00
var emote = message.Emote;
2024-07-22 09:49:51 +00:00
var emoteRow = manager.GetValidEmote(emote.Id);
2024-07-22 05:44:50 +00:00
2024-07-22 06:22:54 +00:00
var chara = (BattleChara*) objMan->GetObjectByIndex((ushort) idx);
2024-07-22 05:44:50 +00:00
2024-07-22 06:22:54 +00:00
chara->ObjectKind = ObjectKind.BattleNpc;
chara->TargetableStatus = 0;
2024-07-22 06:22:54 +00:00
chara->Position = message.Position;
chara->Rotation = message.Yaw;
var drawData = &chara->DrawData;
2024-07-22 07:44:30 +00:00
2024-07-22 09:49:51 +00:00
var maxLen = Math.Min(sizeof(CustomizeData), emote.Customise.Count);
2024-07-22 07:44:30 +00:00
var rawCustomise = (byte*) &drawData->CustomizeData;
for (var i = 0; i < maxLen; i++) {
rawCustomise[i] = emote.Customise[i];
}
// check if data is valid to prevent crashes
if (!(&drawData->CustomizeData)->NormalizeCustomizeData(&drawData->CustomizeData)) {
drawData->CustomizeData = new CustomizeData();
}
// weapon and equipment values don't cause crashes, just transparent body parts
2024-07-22 07:44:30 +00:00
for (var i = 0; i < Math.Min(drawData->EquipmentModelIds.Length, emote.Equipment.Length); i++) {
var equip = emote.Equipment[i];
drawData->Equipment((DrawDataContainer.EquipmentSlot) i) = new EquipmentModelId {
Id = equip.Id,
Variant = equip.Variant,
Stain0 = equip.Stain0,
Stain1 = equip.Stain1,
};
}
2024-07-22 09:49:51 +00:00
if (emoteRow is { DrawsWeapon: true }) {
for (var i = 0; i < Math.Min(drawData->WeaponData.Length, emote.Weapon.Length); i++) {
var weapon = emote.Weapon[i];
drawData->Weapon((DrawDataContainer.WeaponSlot) i).ModelId = new FFXIVClientStructs.FFXIV.Client.Game.Character.WeaponModelId {
2024-07-22 07:44:30 +00:00
Id = weapon.ModelId.Id,
Type = weapon.ModelId.Kind,
Variant = weapon.ModelId.Variant,
Stain0 = weapon.ModelId.Stain0,
Stain1 = weapon.ModelId.Stain1,
2024-07-22 09:49:51 +00:00
};
drawData->Weapon((DrawDataContainer.WeaponSlot) i).Flags1 = weapon.Flags1;
drawData->Weapon((DrawDataContainer.WeaponSlot) i).Flags2 = weapon.Flags2;
drawData->Weapon((DrawDataContainer.WeaponSlot) i).State = weapon.State;
}
2024-07-22 07:44:30 +00:00
}
drawData->IsHatHidden = emote.HatHidden;
drawData->IsVisorToggled = emote.VisorToggled;
drawData->IsWeaponHidden = emote.WeaponHidden;
drawData->SetGlasses(0, (ushort) emote.Glasses);
2024-07-22 05:44:50 +00:00
chara->Alpha = Math.Clamp(manager.Plugin.Config.EmoteAlpha / 100, 0, 1);
2024-07-22 06:22:54 +00:00
chara->SetMode(CharacterModes.AnimLock, 0);
2024-07-22 09:49:51 +00:00
if (emoteRow != null) {
chara->Timeline.BaseOverride = (ushort) emoteRow.ActionTimeline[0].Row;
2024-07-22 07:44:30 +00:00
}
2024-07-22 05:44:50 +00:00
2024-07-22 06:22:54 +00:00
manager._tasks.Enqueue(new EnableAction());
return true;
}
2024-07-22 05:44:50 +00:00
}
2024-07-22 09:49:51 +00:00
private Emote? GetValidEmote(uint rowId) {
var emote = this.Plugin.DataManager.GetExcelSheet<Emote>()?.GetRow(rowId);
if (emote == null) {
return null;
}
return emote.TextCommand.Row == 0 ? null : emote;
}
2024-07-22 06:22:54 +00:00
private unsafe class EnableAction : BaseActorAction {
public override bool Run(ActorManager manager, ClientObjectManager* objMan) {
2024-07-22 19:06:51 +00:00
var allReady = true;
foreach (var chara in this.GetBattleCharas(manager, objMan)) {
if (!chara.Value->IsReadyToDraw()) {
allReady = false;
continue;
}
2024-07-22 06:22:54 +00:00
2024-07-22 19:06:51 +00:00
chara.Value->EnableDraw();
2024-07-22 06:22:54 +00:00
}
2024-07-22 19:06:51 +00:00
return allReady;
2024-07-22 06:07:05 +00:00
}
2024-07-22 06:22:54 +00:00
}
private unsafe class DeleteAction : BaseActorAction {
public override bool Run(ActorManager manager, ClientObjectManager* objMan) {
2024-07-22 19:06:51 +00:00
foreach (var wrapper in this.GetBattleCharas(manager, objMan)) {
wrapper.Value->DisableDraw();
var idx = objMan->GetIndexByObject((GameObject*) wrapper.Value);
objMan->DeleteObjectByIndex((ushort) idx, 0);
2024-07-22 06:22:54 +00:00
}
2024-07-22 19:06:51 +00:00
manager._idx.Clear();
2024-07-22 06:22:54 +00:00
return true;
}
2024-07-22 05:44:50 +00:00
}
}