Skip to content

Commit

Permalink
IPC cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
awgil committed Feb 24, 2024
1 parent 20bd35f commit 36ea9fc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 116 deletions.
2 changes: 1 addition & 1 deletion vnavmesh/Debug/DebugNavmeshManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Draw()
ImGui.TextUnformatted($"Current target: {_target}");

ImGui.Checkbox("Allow movement", ref _path.MovementAllowed);
ImGui.Checkbox("Auto mesh on TerritoryChanged", ref _manager.AutoMesh);
ImGui.Checkbox("Auto load mesh when changing zone", ref _manager.AutoLoad);
ImGui.Checkbox("Use raycasts", ref _path.UseRaycasts);
ImGui.Checkbox("Use string pulling", ref _path.UseStringPulling);
if (ImGui.Button("Pathfind to target using navmesh"))
Expand Down
62 changes: 62 additions & 0 deletions vnavmesh/IPCProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Navmesh.Movement;
using System;
using System.Collections.Generic;
using System.Numerics;

namespace Navmesh
{
class IPCProvider : IDisposable
{
private List<Action> _disposeActions = new();

public IPCProvider(NavmeshManager navmeshManager, FollowPath followPath, MainWindow mainWindow)
{
Register("Nav.IsReady", () => navmeshManager.Navmesh != null);
Register("Nav.BuildProgress", () => navmeshManager.TaskProgress);
Register("Nav.Reload", () => navmeshManager.Reload(true));
Register("Nav.Rebuild", () => navmeshManager.Reload(false));
Register("Nav.IsAutoLoad", () => navmeshManager.AutoLoad);
Register<bool>("Nav.SetAutoLoad", v => navmeshManager.AutoLoad = v);

Register<Vector3>("Path.MoveTo", followPath.MoveTo);
Register<Vector3>("Path.FlyTo", followPath.FlyTo);
Register("Path.Stop", followPath.Stop);
Register("Path.IsRunning", () => followPath.Waypoints.Count > 0);
Register("Path.NumWaypoints", () => followPath.Waypoints.Count);
Register("Path.GetMovementAllowed", () => followPath.MovementAllowed);
Register<bool>("Path.SetMovementAllowed", v => followPath.MovementAllowed = v);
Register("Path.GetTolerance", () => followPath.Tolerance);
Register<float>("Path.SetTolerance", v => followPath.Tolerance = v);

Register("Window.IsOpen", () => mainWindow.IsOpen);
Register<bool>("Window.SetOpen", v => mainWindow.IsOpen = v);
}

public void Dispose()
{
foreach (var a in _disposeActions)
a();
}

private void Register<TRet>(string name, Func<TRet> func)
{
var p = Service.PluginInterface.GetIpcProvider<TRet>("vnavmesh." + name);
p.RegisterFunc(func);
_disposeActions.Add(p.UnregisterFunc);
}

private void Register(string name, Action func)
{
var p = Service.PluginInterface.GetIpcProvider<object>("vnavmesh." + name);
p.RegisterAction(func);
_disposeActions.Add(p.UnregisterAction);
}

private void Register<T1>(string name, Action<T1> func)
{
var p = Service.PluginInterface.GetIpcProvider<T1, object>("vnavmesh." + name);
p.RegisterAction(func);
_disposeActions.Add(p.UnregisterAction);
}
}
}
13 changes: 9 additions & 4 deletions vnavmesh/NavmeshManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Navmesh;
// manager that loads navmesh matching current zone
public class NavmeshManager : IDisposable
{
public bool AutoMesh = true;
public bool AutoLoad = true; // whether we load/build mesh automatically when changing zone
public event Action<Navmesh?>? OnNavmeshChanged;
public Navmesh? Navmesh => _navmesh;
public float TaskProgress => _task != null ? _taskProgress : -1; // returns negative value if task is not running
Expand Down Expand Up @@ -42,8 +42,6 @@ public void Dispose()

public void Update()
{
if (!AutoMesh)
return;
if (_task != null)
{
if (!_task.IsCompleted)
Expand All @@ -67,6 +65,13 @@ public void Update()
if (curKey == _lastKey)
return; // everything up-to-date

if (!AutoLoad)
{
if (_lastKey.Length == 0)
return; // nothing is loaded, and auto-load is forbidden
curKey = ""; // just unload existing mesh
}

Service.Log.Info($"Starting transition from '{_lastKey}' to '{curKey}'");
_lastKey = curKey;
Reload(true);
Expand All @@ -79,7 +84,7 @@ public bool Reload(bool allowLoadFromCache)
Service.Log.Error($"Can't initiate reload - another task is already in progress");
return false; // some task is already in progress...
}
AutoMesh = true;

ClearState();
if (_lastKey.Length > 0)
{
Expand Down
15 changes: 7 additions & 8 deletions vnavmesh/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public sealed class Plugin : IDalamudPlugin
private NavmeshManager _navmeshManager;
private FollowPath _followPath;
private MainWindow _wndMain;

private IPCProvider _ipcProvider;

public Plugin(DalamudPluginInterface dalamud)
{
if (!dalamud.ConfigDirectory.Exists)
Expand All @@ -32,9 +33,11 @@ public Plugin(DalamudPluginInterface dalamud)

_navmeshManager = new(new($"{dalamud.ConfigDirectory.FullName}/meshcache"));
_followPath = new(_navmeshManager);

_wndMain = new(_navmeshManager, _followPath);
_ipcProvider = new(_navmeshManager, _followPath, _wndMain);

WindowSystem.AddWindow(_wndMain);
//_wndMain.IsOpen = true;

dalamud.UiBuilder.Draw += WindowSystem.Draw;
dalamud.UiBuilder.OpenConfigUi += () => _wndMain.IsOpen = true;
Expand All @@ -53,11 +56,7 @@ public Plugin(DalamudPluginInterface dalamud)
ShowInHelp = true,
});

_wndMain.IsOpen = true;

Service.Framework.Update += OnUpdate;

VNavmeshIPC.Initialize(_navmeshManager, _followPath, _wndMain);
}

public void Dispose()
Expand All @@ -66,11 +65,11 @@ public void Dispose()

Service.CommandManager.RemoveHandler("/vnavmesh");
WindowSystem.RemoveAllWindows();
_wndMain.Dispose();

_ipcProvider.Dispose();
_wndMain.Dispose();
_followPath.Dispose();
_navmeshManager.Dispose();
VNavmeshIPC.Dispose();
}

private void OnUpdate(IFramework fwk)
Expand Down
2 changes: 1 addition & 1 deletion vnavmesh/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Service
[PluginService] public static ITextureProvider TextureProvider { get; private set; } = null!;
[PluginService] public static IAddonLifecycle AddonLifecycle { get; private set; } = null!;
[PluginService] public static IFramework Framework { get; private set; } = null!;
[PluginService] public static DalamudPluginInterface PluginInterface { get; private set; }
[PluginService] public static DalamudPluginInterface PluginInterface { get; private set; } = null!;

public static Lumina.GameData LuminaGameData => DataManager.GameData;
public static T? LuminaRow<T>(uint row) where T : Lumina.Excel.ExcelRow => LuminaGameData.GetExcelSheet<T>(Lumina.Data.Language.English)?.GetRow(row);
Expand Down
102 changes: 0 additions & 102 deletions vnavmesh/VNavmeshIPC.cs

This file was deleted.

0 comments on commit 36ea9fc

Please sign in to comment.