Skip to content

Commit

Permalink
Switch Penumbra IPC to be opportunistic
Browse files Browse the repository at this point in the history
The Penumbra IPC will behave in a more opportunistic manner, which generally should be more stable as versions change (and also be more resilient to load-time shenanigans).
  • Loading branch information
KazWolfe committed Jul 2, 2022
1 parent 231a48c commit e8f230d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
47 changes: 21 additions & 26 deletions FFXIVPlugin/IPC/Subscribers/PenumbraIPC.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Logging;
using Dalamud.Plugin.Ipc;
using Dalamud.Plugin.Ipc.Exceptions;
using XIVDeck.FFXIVPlugin.Base;
using XIVDeck.FFXIVPlugin.Game;
using XIVDeck.FFXIVPlugin.Resources.Localization;

namespace XIVDeck.FFXIVPlugin.IPC.Subscribers;

[PluginIpc]
internal class PenumbraIPC : IPluginIpcClient {
private const int PenumbraIPCCompatVersion = 5;

// note: this is *extremely fragile* and honestly bad, but this will be instantiated by the system at a higher
// level. if we want to consume the IPC, this should be a safe-ish way to do it, assuming null checks are used.
internal static PenumbraIPC? Instance;
Expand All @@ -21,7 +17,7 @@ internal class PenumbraIPC : IPluginIpcClient {
private ICallGateSubscriber<string, string>? _penumbraResolveDefaultSubscriber;

private readonly ICallGateSubscriber<object?> _penumbraRegisteredSubscriber;

internal PenumbraIPC() {
Instance = this;

Expand All @@ -42,39 +38,38 @@ public void Dispose() {
this._penumbraApiVersionSubscriber = null;
this._penumbraResolveDefaultSubscriber = null;

this.Enabled = false;

GC.SuppressFinalize(this);
}

public int Version => this._penumbraApiVersionSubscriber?.InvokeFunc() ?? 0;
public int Version => this._penumbraApiVersionSubscriber?.InvokeFunc() ?? -1;

private void _initializeIpc() {
if (!Injections.PluginInterface.PluginNames.Contains("Penumbra")) {
PluginLog.Debug("Penumbra was not found, will not create IPC at this time");
return;
}

var versionEndpoint = Injections.PluginInterface.GetIpcSubscriber<int>("Penumbra.ApiVersion");

// this line may explode with an exception, but that should be fine as we'd normally catch that.
var version = versionEndpoint.InvokeFunc();

this._penumbraApiVersionSubscriber = versionEndpoint;
this._penumbraApiVersionSubscriber = Injections.PluginInterface.GetIpcSubscriber<int>("Penumbra.ApiVersion");
this._penumbraResolveDefaultSubscriber = Injections.PluginInterface.GetIpcSubscriber<string, string>("Penumbra.ResolveDefaultPath");

if (version == PenumbraIPCCompatVersion) {
this._penumbraResolveDefaultSubscriber = Injections.PluginInterface.GetIpcSubscriber<string, string>("Penumbra.ResolveDefaultPath");

this.Enabled = true;
PluginLog.Information("Enabled Penumbra IPC connection!");
} else {
PluginLog.Warning($"Penumbra IPC detected, but version {version} is incompatible!");
DeferredChat.SendOrDeferMessage(new SeStringBuilder()
.AddUiForeground($"[{UIStrings.XIVDeck}] ", 514)
.AddText(UIStrings.PenumbraIPC_VersionMismatch)
.Build());
}
this.Enabled = true;
}

public string ResolvePenumbraPath(string path) {
return this._penumbraResolveDefaultSubscriber?.InvokeFunc(path) ?? path;
if (!this.Enabled || this._penumbraResolveDefaultSubscriber == null) return path;

try {
return this._penumbraResolveDefaultSubscriber.InvokeFunc(path);
} catch (IpcNotReadyError) {
PluginLog.Debug("Got a NotReadyError trying to call ResolveDefaultPath. Falling back to normal path");
return path;
} catch (Exception ex) {
PluginLog.Error(ex, "Failed to invoke Penumbra IPC, disabling!");
this.Enabled = false;

return path;
}
}
}
2 changes: 1 addition & 1 deletion FFXIVPlugin/XIVDeck.FFXIVPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Product>XIVDeck Game Plugin</Product>
<Authors>Kaz Wolfe</Authors>
<Company>Blacksite Technologies</Company>
<Version>0.2.6</Version>
<Version>0.2.7</Version>

<AssemblyName>XIVDeck.FFXIVPlugin</AssemblyName>

Expand Down
2 changes: 1 addition & 1 deletion SDPlugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kazwolfe/xivdeck-sdplugin",
"version": "0.2.6",
"version": "0.2.7",
"private": true,
"scripts": {
"build-release": "webpack --mode production",
Expand Down

0 comments on commit e8f230d

Please sign in to comment.