Skip to content

Commit

Permalink
APIX Update (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
damolitionn authored Jul 7, 2024
1 parent 50a9842 commit 521ea9f
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Distance/BNpcAggroInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal static void Init( IDataManager dataManager, BNpcAggroInfoFile file )

// Verify entries against the english name in the sheet as a sanity check. Remove those that no longer match, or have invalid TerritoryType.
ExcelSheet<Lumina.Excel.GeneratedSheets.TerritoryType> territorySheet = dataManager.GetExcelSheet<Lumina.Excel.GeneratedSheets.TerritoryType>();
ExcelSheet<Lumina.Excel.GeneratedSheets.BNpcName> BNpcNameSheet = dataManager.GetExcelSheet<Lumina.Excel.GeneratedSheets.BNpcName>( Dalamud.ClientLanguage.English );
ExcelSheet<Lumina.Excel.GeneratedSheets.BNpcName> BNpcNameSheet = dataManager.GetExcelSheet<Lumina.Excel.GeneratedSheets.BNpcName>( Dalamud.Game.ClientLanguage.English );
for( int i = mKnownAggroEntities.Count - 1; i >= 0; i-- )
{
if( mKnownAggroEntities[i].TerritoryType < 1 || territorySheet.GetRow( mKnownAggroEntities[i].TerritoryType ) == null )
Expand Down
4 changes: 2 additions & 2 deletions Distance/Distance.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Platforms>AnyCPU</Platforms>
<Version>1.1.1.1</Version>
<Authors>PunishedPineapple</Authors>
Expand All @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.12" />
<PackageReference Include="DalamudPackager" Version="2.1.13" />
</ItemGroup>

<PropertyGroup>
Expand Down
115 changes: 25 additions & 90 deletions Distance/NameplateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
using System.Numerics;

using Dalamud.Game.ClientState.Conditions;
using Dalamud.Hooking;
using Dalamud.Game.Addon.Lifecycle.AddonArgTypes;
using Dalamud.Game.Addon.Lifecycle;

using FFXIVClientStructs.FFXIV.Client.UI;
using FFXIVClientStructs.FFXIV.Component.GUI;
Expand All @@ -17,40 +18,14 @@ internal static void Init( Configuration configuration )
// It's kinda jank to init a static class with an instance's data, but it'll never matter here, and the
// plugin service effectively already crosses this bridge this anyway, so it's not worth worrying about.
mConfiguration = configuration;
Service.AddonLifecycle.RegisterListener(AddonEvent.PostDraw, "NamePlate", NameplateDrawDetour);
}

if( Service.SigScanner == null )
{
throw new Exception( "Error in \"NameplateHandler.Init()\": A null SigScanner was passed!" );
}

// Get Function Pointers, etc.
try
{
IntPtr fpOnNameplateDraw = Service.SigScanner.ScanText( "0F B7 81 ?? ?? ?? ?? 4C 8B C1 66 C1 E0 06" ); //***** TODO: Can we hook the draw vfunc through ClientStructs? Would that be more stable?
if( fpOnNameplateDraw != IntPtr.Zero )
{
mNameplateDrawHook = Service.GameInteropProvider.HookFromAddress<NameplateDrawFuncDelegate>(fpOnNameplateDraw, mdNameplateDraw);
if( mNameplateDrawHook == null ) throw new Exception( "Unable to create nameplate draw hook." );
if( mConfiguration.NameplateDistancesConfig.ShowNameplateDistances ) mNameplateDrawHook.Enable();
}
}
catch( Exception e )
{
mNameplateDrawHook?.Disable();
mNameplateDrawHook?.Dispose();
mNameplateDrawHook = null;
Service.PluginLog.Error( $"Error in \"NameplateHandler.Init()\" while searching for required function signatures; this probably means that the plugin needs to be updated due to changes in Final Fantasy XIV.\r\n{e}" );
return;
}
}

internal static void Uninit()
internal static void Uninit()
{
mNameplateDrawHook?.Disable();
mNameplateDrawHook?.Dispose();
mNameplateDrawHook = null;
Service.AddonLifecycle.UnregisterListener(NameplateDrawDetour);

DestroyNameplateDistanceNodes();
DestroyNameplateDistanceNodes();

mNodeUpdateTimer.Reset();
mDistanceUpdateTimer.Reset();
Expand All @@ -59,41 +34,6 @@ internal static void Uninit()
mConfiguration = null;
}

internal static void EnableNameplateDistances()
{
if( mNameplateDrawHook == null ) return;
if( !mNameplateDrawHook.IsEnabled )
{
try
{
mNameplateDrawHook.Enable();
}
catch( Exception e )
{
Service.PluginLog.Error( $"Unknown error while trying to enable nameplate distances:\r\n{e}" );
}
}
}

internal static void DisableNameplateDistances()
{
if( mNameplateDrawHook == null ) return;
if( mNameplateDrawHook.IsEnabled )
{
try
{
mNameplateDrawHook.Disable();
mNodeUpdateTimer.Reset();
mDistanceUpdateTimer.Reset();
HideAllNameplateDistanceNodes();
}
catch( Exception e )
{
Service.PluginLog.Error( $"Unknown error while trying to disable nameplate distances:\r\n{e}" );
}
}
}

internal unsafe static void UpdateNameplateEntityDistanceData()
{
mDistanceUpdateTimer.Restart();
Expand All @@ -106,15 +46,15 @@ internal unsafe static void UpdateNameplateEntityDistanceData()
}

if( FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance() != null &&
FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance()->GetUiModule() != null &&
FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance()->GetUiModule()->GetUI3DModule() != null )
FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance()->GetUIModule() != null &&
FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance()->GetUIModule()->GetUI3DModule() != null )
{
var pUI3DModule = FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance()->GetUiModule()->GetUI3DModule();
var pUI3DModule = FFXIVClientStructs.FFXIV.Client.System.Framework.Framework.Instance()->GetUIModule()->GetUI3DModule();

// Update the available distance data.
for( int i = 0; i < pUI3DModule->NamePlateObjectInfoCount; ++i )
{
var pObjectInfo = ((UI3DModule.ObjectInfo**)pUI3DModule->NamePlateObjectInfoPointerArray)[i];
var pObjectInfo = pUI3DModule->NamePlateObjectInfoPointers[i].Value;
if( pObjectInfo != null &&
pObjectInfo->GameObject != null &&
pObjectInfo->NamePlateIndex >= 0 &&
Expand All @@ -125,7 +65,7 @@ internal unsafe static void UpdateNameplateEntityDistanceData()
{
mNameplateDistanceInfoArray[pObjectInfo->NamePlateIndex].IsValid = true;
mNameplateDistanceInfoArray[pObjectInfo->NamePlateIndex].TargetKind = (Dalamud.Game.ClientState.Objects.Enums.ObjectKind)pObject->ObjectKind;
mNameplateDistanceInfoArray[pObjectInfo->NamePlateIndex].ObjectID = pObject->ObjectID;
mNameplateDistanceInfoArray[pObjectInfo->NamePlateIndex].ObjectID = pObject->EntityId;
mNameplateDistanceInfoArray[pObjectInfo->NamePlateIndex].ObjectAddress = new IntPtr( pObject );
mNameplateDistanceInfoArray[pObjectInfo->NamePlateIndex].PlayerPosition = Service.ClientState.LocalPlayer?.Position ?? Vector3.Zero;
mNameplateDistanceInfoArray[pObjectInfo->NamePlateIndex].TargetPosition = new( pObject->Position.X, pObject->Position.Y, pObject->Position.Z );
Expand Down Expand Up @@ -157,7 +97,7 @@ private static bool ShouldDrawDistanceForNameplate( int i )

var distanceInfo = mNameplateDistanceInfoArray[i];

if( distanceInfo.ObjectID == Service.ClientState.LocalPlayer?.ObjectId ) return false;
if( distanceInfo.ObjectID == Service.ClientState.LocalPlayer?.EntityId ) return false;

bool filtersPermitShowing = mConfiguration.NameplateDistancesConfig.Filters.ShowDistanceForObjectKind( distanceInfo.TargetKind ) &&
mConfiguration.NameplateDistancesConfig.Filters.ShowDistanceForClassJob( Service.ClientState.LocalPlayer?.ClassJob.Id ?? 0 );
Expand Down Expand Up @@ -200,8 +140,9 @@ private static bool ShouldDrawDistanceForNameplate( int i )
}
}

private static void NameplateDrawDetour( AddonNamePlate* pThis )
private static void NameplateDrawDetour(AddonEvent type, AddonArgs args )
{
var pThis = (AddonNamePlate*)args.Addon;
try
{
if( mpNameplateAddon != pThis )
Expand All @@ -220,11 +161,6 @@ private static void NameplateDrawDetour( AddonNamePlate* pThis )
{
Service.PluginLog.Error( $"Unknown error in nameplate draw hook. Disabling nameplate distances.\r\n{e}" );
mConfiguration.NameplateDistancesConfig.ShowNameplateDistances = false;
DisableNameplateDistances();
}
finally
{
mNameplateDrawHook.Original( pThis );
}
}

Expand Down Expand Up @@ -456,8 +392,8 @@ private static bool ObjectIsNonDepthTarget( uint objectID, IntPtr pObject )
}
else
{
uint targetOID = TargetResolver.GetTarget( TargetType.Target )?.ObjectId ?? 0;
uint softTargetOID = TargetResolver.GetTarget( TargetType.SoftTarget )?.ObjectId ?? 0;
uint targetOID = TargetResolver.GetTarget( TargetType.Target )?.EntityId ?? 0;
uint softTargetOID = TargetResolver.GetTarget( TargetType.SoftTarget )?.EntityId ?? 0;
return objectID == targetOID || objectID == softTargetOID;
}
}
Expand All @@ -467,14 +403,14 @@ private static TextNodeDrawData GetNameplateNodeDrawData( int i )
var nameplateObject = GetNameplateObject( i );
if( nameplateObject == null ) return TextNodeDrawData.Default;

var pNameplateIconNode = nameplateObject.Value.ImageNode2; // Need to check this for people that are using player icon plugins with names hidden.
var pNameplateResNode = nameplateObject.Value.ResNode;
var pNameplateIconNode = nameplateObject.Value.MarkerIcon; // Need to check this for people that are using player icon plugins with names hidden.
var pNameplateResNode = nameplateObject.Value.NameContainer;
var pNameplateTextNode = nameplateObject.Value.NameText;
if( pNameplateTextNode != null && pNameplateResNode != null && pNameplateIconNode != null )
{
return new TextNodeDrawData()
{
Show = pNameplateIconNode->AtkResNode.IsVisible || ( pNameplateResNode->IsVisible && pNameplateTextNode->AtkResNode.IsVisible ),
Show = pNameplateIconNode->AtkResNode.IsVisible() || ( pNameplateResNode->IsVisible() && pNameplateTextNode->AtkResNode.IsVisible() ),
PositionX = (short)pNameplateResNode->X,
PositionY = (short)pNameplateResNode->Y,
Width = pNameplateResNode->Width,
Expand Down Expand Up @@ -511,7 +447,7 @@ private static TextNodeDrawData GetNameplateNodeDrawData( int i )
{
if( i < AddonNamePlate.NumNamePlateObjects &&
mpNameplateAddon != null &&
mpNameplateAddon->NamePlateObjectArray[i].RootNode != null )
mpNameplateAddon->NamePlateObjectArray[i].RootComponentNode != null )
{
return mpNameplateAddon->NamePlateObjectArray[i];
}
Expand All @@ -524,7 +460,7 @@ private static TextNodeDrawData GetNameplateNodeDrawData( int i )
private static AtkComponentNode* GetNameplateComponentNode( int i )
{
var nameplateObject = GetNameplateObject( i );
return nameplateObject != null ? nameplateObject.Value.RootNode : null;
return nameplateObject != null ? nameplateObject.Value.RootComponentNode : null;
}

private static void CreateNameplateDistanceNodes()
Expand All @@ -537,7 +473,7 @@ private static void CreateNameplateDistanceNodes()
Service.PluginLog.Warning( $"Unable to obtain nameplate object for index {i}" );
continue;
}
var pNameplateResNode = nameplateObject.Value.ResNode;
var pNameplateResNode = nameplateObject.Value.NameContainer;

// Make a node.
var pNewNode = AtkNodeHelpers.CreateOrphanTextNode( mNameplateDistanceNodeIDBase + (uint)i, TextFlags.Edge | TextFlags.Glare );
Expand All @@ -550,7 +486,7 @@ private static void CreateNameplateDistanceNodes()
pNewNode->AtkResNode.NextSiblingNode = pLastChild;
pNewNode->AtkResNode.ParentNode = pNameplateResNode;
pLastChild->PrevSiblingNode = (AtkResNode*)pNewNode;
nameplateObject.Value.RootNode->Component->UldManager.UpdateDrawNodeList();
nameplateObject.Value.RootComponentNode->Component->UldManager.UpdateDrawNodeList();
pNewNode->AtkResNode.SetUseDepthBasedPriority( true );

// Store it in our array.
Expand Down Expand Up @@ -662,9 +598,8 @@ private static void UpdateNameplateDistanceTextNode( int i, string str, TextNode
internal static int DEBUG_mNameplateTextFlags2 = 0;

// Delgates and Hooks
private delegate void NameplateDrawFuncDelegate( AddonNamePlate* pThis );
private delegate void NameplateDrawFuncDelegate(AddonEvent type, AddonArgs args);
private static readonly NameplateDrawFuncDelegate mdNameplateDraw = new( NameplateDrawDetour );
private static Hook<NameplateDrawFuncDelegate> mNameplateDrawHook;

// Members
private static readonly DistanceInfo[] mNameplateDistanceInfoArray = new DistanceInfo[AddonNamePlate.NumNamePlateObjects];
Expand Down
19 changes: 8 additions & 11 deletions Distance/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Distance;
public sealed class Plugin : IDalamudPlugin
{
// Initialization
public Plugin( DalamudPluginInterface pluginInterface )
public Plugin( IDalamudPluginInterface pluginInterface )
{
// API Access
pluginInterface.Create<Service>();
Expand Down Expand Up @@ -66,7 +66,7 @@ public Plugin( DalamudPluginInterface pluginInterface )
// UI Initialization
mUI = new PluginUI( this, mPluginInterface, mConfiguration );
mPluginInterface.UiBuilder.Draw += DrawUI;
mPluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
mPluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
mUI.Initialize();
NameplateHandler.Init( mConfiguration );

Expand All @@ -85,7 +85,7 @@ public void Dispose()
Service.Framework.Update -= OnGameFrameworkUpdate;
Service.ClientState.TerritoryChanged -= OnTerritoryChanged;
mPluginInterface.UiBuilder.Draw -= DrawUI;
mPluginInterface.UiBuilder.OpenConfigUi -= DrawConfigUI;
mPluginInterface.UiBuilder.OpenConfigUi -= DrawConfigUI;
mPluginInterface.LanguageChanged -= OnLanguageChanged;
Service.CommandManager.RemoveHandler( mTextCommandName );
mUI.Dispose();
Expand Down Expand Up @@ -308,9 +308,6 @@ private string ProcessTextCommand_Help( string args )
private void OnGameFrameworkUpdate( IFramework framework )
{
UpdateTargetDistanceData();

if( mConfiguration.NameplateDistancesConfig.ShowNameplateDistances ) NameplateHandler.EnableNameplateDistances();
else NameplateHandler.DisableNameplateDistances();
}

private void OnTerritoryChanged( UInt16 ID )
Expand All @@ -333,7 +330,7 @@ internal bool ShouldDrawAggroDistanceInfo()
GetDistanceInfo( mConfiguration.AggroDistanceApplicableTargetType ).IsValid &&
GetDistanceInfo( mConfiguration.AggroDistanceApplicableTargetType ).TargetKind == Dalamud.Game.ClientState.Objects.Enums.ObjectKind.BattleNpc &&
GetDistanceInfo( mConfiguration.AggroDistanceApplicableTargetType ).HasAggroRangeData &&
TargetResolver.GetTarget( mConfiguration.AggroDistanceApplicableTargetType ) is BattleChara { IsDead: false } &&
TargetResolver.GetTarget( mConfiguration.AggroDistanceApplicableTargetType ) is IBattleChara { IsDead: false } &&
!Service.Condition[ConditionFlag.Unconscious] &&
!Service.Condition[ConditionFlag.InCombat];
}
Expand All @@ -343,7 +340,7 @@ internal bool ShouldDrawDistanceInfo( DistanceWidgetConfig config )
if( Service.ClientState.IsPvP ) return false;
if( !config.Enabled ) return false;
if( !mCurrentDistanceInfoArray[(int)config.ApplicableTargetType].IsValid ) return false;
if( mCurrentDistanceInfoArray[(int)config.ApplicableTargetType].ObjectID == Service.ClientState.LocalPlayer?.ObjectId ) return false;
if( mCurrentDistanceInfoArray[(int)config.ApplicableTargetType].ObjectID == Service.ClientState.LocalPlayer?.EntityId ) return false;

return config.Filters.ShowDistanceForObjectKind( mCurrentDistanceInfoArray[(int)config.ApplicableTargetType].TargetKind ) &&
config.Filters.ShowDistanceForClassJob( Service.ClientState.LocalPlayer?.ClassJob.Id ?? 0 ) &&
Expand All @@ -369,11 +366,11 @@ private void UpdateTargetDistanceData()
{
mCurrentDistanceInfoArray[i].IsValid = true;
mCurrentDistanceInfoArray[i].TargetKind = target.ObjectKind;
mCurrentDistanceInfoArray[i].ObjectID = target.ObjectId;
mCurrentDistanceInfoArray[i].ObjectID = target.EntityId;
mCurrentDistanceInfoArray[i].PlayerPosition = Service.ClientState.LocalPlayer.Position;
mCurrentDistanceInfoArray[i].TargetPosition = target.Position;
mCurrentDistanceInfoArray[i].TargetRadius_Yalms = target.HitboxRadius;
mCurrentDistanceInfoArray[i].BNpcID = ( target as Dalamud.Game.ClientState.Objects.Types.BattleNpc )?.NameId ?? 0;
mCurrentDistanceInfoArray[i].BNpcID = ( target as Dalamud.Game.ClientState.Objects.Types.IBattleNpc )?.NameId ?? 0;
float? aggroRange = BNpcAggroInfo.GetAggroRange( mCurrentDistanceInfoArray[i].BNpcID, Service.ClientState.TerritoryType );
mCurrentDistanceInfoArray[i].HasAggroRangeData = aggroRange.HasValue;
mCurrentDistanceInfoArray[i].AggroRange_Yalms = aggroRange ?? 0;
Expand Down Expand Up @@ -401,7 +398,7 @@ private void DrawConfigUI()
internal string AggroDataPath => Path.Join( mPluginInterface.GetPluginConfigDirectory(), "AggroDistances.dat" );

private readonly DistanceInfo[] mCurrentDistanceInfoArray = new DistanceInfo[Enum.GetNames(typeof(TargetType)).Length];
private readonly DalamudPluginInterface mPluginInterface;
private readonly IDalamudPluginInterface mPluginInterface;
private readonly Configuration mConfiguration;
private readonly PluginUI mUI;
}
2 changes: 2 additions & 0 deletions Distance/Services/Service.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Dalamud.Game;
using Dalamud.Game.ClientState.Objects;
using Dalamud.IoC;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;

namespace Distance;
Expand All @@ -20,4 +21,5 @@ internal class Service
[PluginService] internal static ICondition Condition { get; private set; } = null!;
[PluginService] internal static IPluginLog PluginLog { get; private set; } = null!;
[PluginService] internal static IGameInteropProvider GameInteropProvider { get; private set; } = null!;
[PluginService] internal static IAddonLifecycle AddonLifecycle { get; private set; } = null!;
}
4 changes: 2 additions & 2 deletions Distance/Settings/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class Configuration : IPluginConfiguration
{
#region Interface

public void Initialize( DalamudPluginInterface pluginInterface )
public void Initialize( IDalamudPluginInterface pluginInterface )
{
mPluginInterface = pluginInterface;
}
Expand All @@ -26,7 +26,7 @@ public void Save()
public int Version { get; set; } = 0;

[NonSerialized]
private DalamudPluginInterface mPluginInterface;
private IDalamudPluginInterface mPluginInterface;

#endregion

Expand Down
Loading

0 comments on commit 521ea9f

Please sign in to comment.