Skip to content

Commit

Permalink
ActivitySource/otel
Browse files Browse the repository at this point in the history
- may have been causing issues with drawtimes on layout window
- move character resolution to framework tick
  • Loading branch information
PassiveModding committed Oct 6, 2024
1 parent 553e121 commit 17f68f1
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 175 deletions.
4 changes: 0 additions & 4 deletions Meddle/Meddle.Plugin/Meddle.Plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
<PackageReference Include="DalamudPackager" Version="2.1.13"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0"/>
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0"/>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.7" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0"/>
<PackageReference Include="Vortice.Direct3D11" Version="3.5.0"/>
<Reference Include="Dalamud">
<HintPath>$(DalamudLibPath)Dalamud.dll</HintPath>
Expand Down
4 changes: 2 additions & 2 deletions Meddle/Meddle.Plugin/Models/Layout/ParsedInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void Resolve(ResolverService resolver)
if (IsResolved) return;
try
{
resolver.ResolveParsedTerrainInstance(this);
resolver.ResolveInstances(this);
}
finally
{
Expand Down Expand Up @@ -363,7 +363,7 @@ public void Resolve(ResolverService resolver)
if (IsResolved) return;
try
{
resolver.ResolveParsedCharacterInstance(this);
resolver.ResolveInstances(this);
}
finally
{
Expand Down
22 changes: 0 additions & 22 deletions Meddle/Meddle.Plugin/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using Dalamud.Configuration;
using Dalamud.IoC;
Expand All @@ -12,10 +11,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OtterTex;

namespace Meddle.Plugin;
Expand Down Expand Up @@ -53,23 +48,6 @@ public Plugin(IDalamudPluginInterface pluginInterface)
.AddSingleton(config)
.AddUi()
.AddSingleton(new SqPack(Environment.CurrentDirectory));

#if DEBUG
services.AddOpenTelemetry()
.ConfigureResource(x => { x.AddService("Meddle"); })
.WithTracing(x =>
{
x.AddSource("Meddle.*");
})
.WithMetrics(x =>
{
x.AddProcessInstrumentation();
x.AddRuntimeInstrumentation();
x.AddMeter("Meddle.*");
})
.WithLogging()
.UseOtlpExporter();
#endif
});

app = host.Build();
Expand Down
2 changes: 0 additions & 2 deletions Meddle/Meddle.Plugin/Services/AnimationExportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Meddle.Plugin.Services;

public class AnimationExportService : IDisposable, IService
{
private static readonly ActivitySource ActivitySource = new("Meddle.Plugin.Utils.ExportUtil");
private readonly Configuration configuration;
private readonly ILogger<AnimationExportService> logger;

Expand All @@ -31,7 +30,6 @@ public void ExportAnimation(List<(DateTime, AttachSet[])> frames, bool includePo
{
try
{
using var activity = ActivitySource.StartActivity();
var boneSets = SkeletonUtils.GetAnimatedBoneMap(frames.ToArray(), configuration.PoseMode);
var startTime = frames.Min(x => x.Item1);
//var folder = GetPathForOutput();
Expand Down
28 changes: 9 additions & 19 deletions Meddle/Meddle.Plugin/Services/LayoutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,43 @@ public LayoutService(
this.framework.Update += Update;
}

public DateTime LastDrawTime { get; set; }
public bool RequestUpdate { get; set; }

public ParsedInstance[]? LastState { get; private set; }

private void Update(IFramework framework)
private void Update(IFramework _)
{
// if last draw time longer than 5sec ago, skip
if (LastDrawTime != default && DateTime.Now - LastDrawTime > TimeSpan.FromSeconds(5))
if (RequestUpdate == false)
return;

var worldState = GetWorldState();
LastState = worldState;
RequestUpdate = false;
}

public static ActivitySource ActivitySource { get; } = new("Meddle.LayoutService");

public unsafe ParsedInstance[]? GetWorldState()
{
using var activity = ActivitySource.StartActivity();
var layoutWorld = sigUtil.GetLayoutWorld();
if (layoutWorld == null)
return null;

var layers = new List<ParsedInstance>();
var objects = ParseObjects();
layers.AddRange(objects);

var currentTerritory = GetCurrentTerritory();
var housingItems = ParseTerritoryFurniture(currentTerritory);
var parseCtx = new ParseContext(housingItems);
var objects = ParseObjects();

var loadedLayouts = layoutWorld->LoadedLayouts.ToArray();
var loadedLayers = loadedLayouts
.Select(layout => ParseLayout(layout.Value, parseCtx))
.SelectMany(x => x).ToArray();
var globalLayers = ParseLayout(layoutWorld->GlobalLayout, parseCtx);

var layers = new List<ParsedInstance>();

layers.AddRange(loadedLayers.SelectMany(x => x.Instances));
layers.AddRange(globalLayers.SelectMany(x => x.Instances));
layers.AddRange(objects);

return layers.ToArray();
}
Expand All @@ -101,7 +100,6 @@ private void Update(IFramework framework)
private unsafe ParsedInstanceSet[] ParseLayout(LayoutManager* activeLayout, ParseContext context)
{
if (activeLayout == null) return [];
using var activity = ActivitySource.StartActivity();
var layers = new List<ParsedInstanceSet>();
foreach (var (_, layerPtr) in activeLayout->Layers)
{
Expand Down Expand Up @@ -132,8 +130,6 @@ private unsafe ParsedInstanceSet[] ParseLayout(LayoutManager* activeLayout, Pars
if (terrainPtr == null || terrainPtr.Value == null)
return null;

using var activity = ActivitySource.StartActivity();

var terrainManager = terrainPtr.Value;
var path = terrainManager->PathString;
return new ParsedTerrainInstance((nint)terrainManager, new Transform(Vector3.Zero, Quaternion.Identity, Vector3.One), path);
Expand All @@ -144,7 +140,6 @@ private unsafe ParsedInstanceSet[] ParseLayout(LayoutManager* activeLayout, Pars
if (layerManagerPtr == null || layerManagerPtr.Value == null)
return null;

using var activity = ActivitySource.StartActivity();
var layerManager = layerManagerPtr.Value;
var instances = new List<ParsedInstance>();
foreach (var (_, instancePtr) in layerManager->Instances)
Expand All @@ -171,10 +166,7 @@ private unsafe ParsedInstanceSet[] ParseLayout(LayoutManager* activeLayout, Pars
if (instancePtr == null || instancePtr.Value == null)
return null;

using var activity = ActivitySource.StartActivity();
var instanceLayout = instancePtr.Value;
activity?.SetTag("instanceType", instanceLayout->Id.Type.ToString());
activity?.SetTag("instancePtr", (nint)instanceLayout);
switch (instanceLayout->Id.Type)
{
case InstanceType.BgPart:
Expand Down Expand Up @@ -319,7 +311,6 @@ private unsafe ParsedInstanceSet[] ParseLayout(LayoutManager* activeLayout, Pars

public unsafe ParsedInstance[] ParseObjects()
{
using var activity = ActivitySource.StartActivity();
var gameObjectManager = sigUtil.GetGameObjectManager();

var objects = new List<ParsedInstance>();
Expand Down Expand Up @@ -365,7 +356,6 @@ private unsafe Furniture[] ParseTerritoryFurniture(HousingTerritory* territory)
if (furniture == null || objectManager == null)
return [];

using var activity = ActivitySource.StartActivity();
var items = new List<Furniture>();
foreach (var item in furniture)
{
Expand Down
3 changes: 0 additions & 3 deletions Meddle/Meddle.Plugin/Services/ParseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Meddle.Plugin.Services;

public class ParseService : IDisposable, IService
{
private static readonly ActivitySource ActivitySource = new("Meddle.Plugin.Utils.ParseUtil");
private readonly EventLogger<ParseService> logger;
private readonly SqPack pack;
private readonly PbdHooks pbdHooks;
Expand Down Expand Up @@ -60,7 +59,6 @@ private void OnLog(LogLevel logLevel, string message)

public unsafe Dictionary<int, IColorTableSet> ParseColorTableTextures(CharacterBase* characterBase)
{
using var activity = ActivitySource.StartActivity();
var colorTableTextures = new Dictionary<int, IColorTableSet>();
for (var i = 0; i < characterBase->ColorTableTexturesSpan.Length; i++)
{
Expand All @@ -81,7 +79,6 @@ public unsafe Dictionary<int, IColorTableSet> ParseColorTableTextures(CharacterB
// Only call from main thread or you will probably crash
public unsafe IColorTableSet ParseColorTableTexture(Texture* colorTableTexture)
{
using var activity = ActivitySource.StartActivity();
var (colorTableRes, stride) = DXHelper.ExportTextureResource(colorTableTexture);
if ((TexFile.TextureFormat)colorTableTexture->TextureFormat != TexFile.TextureFormat.R16G16B16A16F)
{
Expand Down
21 changes: 14 additions & 7 deletions Meddle/Meddle.Plugin/Services/ResolverService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Game.Character;
using FFXIVClientStructs.FFXIV.Client.Game.Object;
using FFXIVClientStructs.FFXIV.Client.Graphics.Scene;
Expand Down Expand Up @@ -26,29 +27,35 @@ public class ResolverService : IService
private readonly LayoutService layoutService;
private readonly SqPack pack;
private readonly ParseService parseService;
private readonly IFramework framework;
private readonly PbdHooks pbdHooks;

public ResolverService(
ILogger<ResolverService> logger,
LayoutService layoutService,
SqPack pack,
ParseService parseService,
IFramework framework,
PbdHooks pbdHooks)
{
this.logger = logger;
this.layoutService = layoutService;
this.pack = pack;
this.parseService = parseService;
this.framework = framework;
this.pbdHooks = pbdHooks;
}


public void ResolveInstances(ParsedInstance[] instances)
public void ResolveInstances(params ParsedInstance[] instances)
{
foreach (var instance in instances)
framework.RunOnTick(() =>
{
ResolveInstance(instance);
}
foreach (var instance in instances)
{
ResolveInstance(instance);
}
}).GetAwaiter().GetResult();
}

private bool IsCharacterKind(ObjectKind kind)
Expand All @@ -66,7 +73,7 @@ private bool IsCharacterKind(ObjectKind kind)
};
}

public unsafe void ResolveParsedCharacterInstance(ParsedCharacterInstance characterInstance)
private unsafe void ResolveParsedCharacterInstance(ParsedCharacterInstance characterInstance)
{
var objects = layoutService.ParseObjects();
// check to ensure the character instance is still valid
Expand All @@ -90,7 +97,7 @@ public unsafe void ResolveParsedCharacterInstance(ParsedCharacterInstance charac
}
}

public void ResolveParsedTerrainInstance(ParsedTerrainInstance terrainInstance)
private void ResolveParsedTerrainInstance(ParsedTerrainInstance terrainInstance)
{
var path = terrainInstance.Path;
var teraPath = $"{terrainInstance.Path.GamePath}/bgplate/terrain.tera";
Expand All @@ -106,7 +113,7 @@ public void ResolveParsedTerrainInstance(ParsedTerrainInstance terrainInstance)
terrainInstance.Data = new ParsedTerrainInstanceData(terrain);
}

public void ResolveInstance(ParsedInstance instance)
private void ResolveInstance(ParsedInstance instance)
{
if (instance is ParsedCharacterInstance {IsResolved: false} characterInstance)
{
Expand Down
3 changes: 1 addition & 2 deletions Meddle/Meddle.Plugin/UI/Layout/LayoutWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Dalamud.Interface;
using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Interface.Windowing;
using Dalamud.Plugin.Services;
using ImGuiNET;
using Meddle.Plugin.Models;
Expand Down Expand Up @@ -72,6 +71,7 @@ public LayoutWindow(

private void SetupCurrentState()
{
layoutService.RequestUpdate = true;
currentLayout = layoutService.LastState ?? [];
currentPos = sigUtil.GetLocalPosition();
}
Expand Down Expand Up @@ -110,7 +110,6 @@ public void Draw()

if (shouldUpdateState)
{
layoutService.LastDrawTime = DateTime.Now;
SetupCurrentState();
shouldUpdateState = false;
}
Expand Down
Loading

0 comments on commit 17f68f1

Please sign in to comment.