Skip to content

Commit

Permalink
Removed obsolete penumbra methods.
Browse files Browse the repository at this point in the history
Moved UpdateWindow into abstract class.
  • Loading branch information
Kurochi51 committed Jun 19, 2024
1 parent 6d7038f commit f5e4dbc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 199 deletions.
10 changes: 9 additions & 1 deletion TickTracker/Helpers/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,22 @@ public bool WindowCondition(WindowType window)
/// <summary>
/// Saves the size and position for the indicated <paramref name="window"/>.
/// </summary>
public void UpdateWindowConfig(Vector2 currentPos, Vector2 currentSize, WindowType window)
public bool UpdateWindowConfig(Vector2 currentPos, Vector2 currentSize, WindowType window)
{
var configChanged = false;
if (window is WindowType.HpWindow)
{
if (!currentPos.Equals(config.HPBarPosition))
{
config.HPBarPosition = currentPos;
config.Save(pluginInterface);
configChanged = true;
}
if (!currentSize.Equals(config.HPBarSize))
{
config.HPBarSize = currentSize;
config.Save(pluginInterface);
configChanged = true;
}
}
if (window is WindowType.MpWindow)
Expand All @@ -116,11 +119,13 @@ public void UpdateWindowConfig(Vector2 currentPos, Vector2 currentSize, WindowTy
{
config.MPBarPosition = currentPos;
config.Save(pluginInterface);
configChanged = true;
}
if (!currentSize.Equals(config.MPBarSize))
{
config.MPBarSize = currentSize;
config.Save(pluginInterface);
configChanged = true;
}
}
if (window is WindowType.GpWindow)
Expand All @@ -129,13 +134,16 @@ public void UpdateWindowConfig(Vector2 currentPos, Vector2 currentSize, WindowTy
{
config.GPBarPosition = currentPos;
config.Save(pluginInterface);
configChanged = true;
}
if (!currentSize.Equals(config.GPBarSize))
{
config.GPBarSize = currentSize;
config.Save(pluginInterface);
configChanged = true;
}
}
return configChanged;
}

/// <summary>
Expand Down
19 changes: 17 additions & 2 deletions TickTracker/IPC/PenumbraEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,29 @@ public enum PenumbraApiEc

public enum ModSettingChange
{
/// <summary> It was set to inherit from other collections or not to inherit anymore. </summary>
Inheritance,

/// <summary> It was enabled or disabled. </summary>
EnableState,

/// <summary> Its priority was changed. </summary>
Priority,

/// <summary> A specific setting for an option group was changed. </summary>
Setting,

/// <summary> Multiple mods were set to inherit from other collections or not inherit anymore at once. </summary>
MultiInheritance,

/// <summary> Multiple mods were enabled or disabled at once. </summary>
MultiEnableState,

/// <summary> A temporary mod was enabled or disabled. </summary>
TemporaryMod,

/// <summary> A mod was edited. Only invoked on edits affecting the current players collection and for that for now. </summary>
Edited,
}

public enum ApiCollectionType : byte
Expand Down Expand Up @@ -132,5 +149,3 @@ public enum ApiCollectionType : byte
Interface = 0xE1,
Current = 0xE2,
}


111 changes: 14 additions & 97 deletions TickTracker/IPC/PenumbraIpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ public sealed class PenumbraIpc : IDisposable
private readonly ICallGateSubscriber<ModSettingChange, Guid, string, bool, Action?> modSettingsChanged;
private readonly (Guid Id, string Name) interfaceCollection;

[Obsolete("Changed with Penumbra API rework")]
private readonly ICallGateSubscriber<string> oldInterfaceCollection;
[Obsolete("Changed with Penumbra API rework")]
private readonly ICallGateSubscriber<IList<(string modDirectory, string modName)>> oldModList;
[Obsolete("Changed with Penumbra API rework")]
private readonly ICallGateSubscriber<string, string, string, bool, (PenumbraApiEc status, (bool modEnabled, int priority, IDictionary<string, IList<string>> optionDetails, bool settingsInherited)? settings)> oldModSettings;
[Obsolete("Changed with Penumbra API rework")]
private readonly ICallGateSubscriber<ModSettingChange, string, string, bool, Action?> oldModSettingsChanged;

public bool NativeUiBanned { get; private set; }

private bool penumbraModsEnabled
Expand Down Expand Up @@ -68,21 +59,12 @@ private bool penumbraModsEnabled
}
}

#pragma warning disable CS8618
public PenumbraIpc(DalamudPluginInterface _pluginInterface, IPluginLog _pluginLog)
{
log = _pluginLog;
apiVersions = _pluginInterface.GetIpcSubscriber<(int, int)>("Penumbra.ApiVersion.V5");

try
{
apiVersions = _pluginInterface.GetIpcSubscriber<(int, int)>("Penumbra.ApiVersions");
}
catch
{
apiVersions = _pluginInterface.GetIpcSubscriber<(int, int)>("Penumbra.ApiVersion");
}

if (penumbraApiVersion.Breaking is not 4 and not 5)
if (penumbraApiVersion.Breaking is not 5)
{
throw new NotSupportedException("Penumbra API out of date. Version " + penumbraApiVersion.Breaking.ToString(CultureInfo.InvariantCulture));
}
Expand All @@ -91,42 +73,27 @@ public PenumbraIpc(DalamudPluginInterface _pluginInterface, IPluginLog _pluginLo
penumbraInit = _pluginInterface.GetIpcSubscriber<object>("Penumbra.Initialized");
penumbraDispose = _pluginInterface.GetIpcSubscriber<object>("Penumbra.Disposed");
penumbraEnabledChange = _pluginInterface.GetIpcSubscriber<bool, Action<bool>?>("Penumbra.EnabledChange");
if (penumbraApiVersion.Breaking is 4)
{
oldInterfaceCollection = _pluginInterface.GetIpcSubscriber<string>("Penumbra.GetInterfaceCollectionName");
oldModList = _pluginInterface.GetIpcSubscriber<IList<(string modDirectory, string modName)>>("Penumbra.GetMods");
oldModSettings = _pluginInterface.GetIpcSubscriber<string, string, string, bool, (PenumbraApiEc status, (bool modEnabled, int priority, IDictionary<string, IList<string>> optionDetails, bool settingsInherited)? settings)>("Penumbra.GetCurrentModSettings");
oldModSettingsChanged = _pluginInterface.GetIpcSubscriber<ModSettingChange, string, string, bool, Action?>("Penumbra.ModSettingChanged");
oldModSettingsChanged.Subscribe(OldCheckModChanges);
}
else if (penumbraApiVersion.Breaking is 5)
{
interfaceCollection = _pluginInterface.GetIpcSubscriber<ApiCollectionType, (Guid id, string Name)>("Penumbra.GetCollection").InvokeFunc(ApiCollectionType.Interface);
modList = _pluginInterface.GetIpcSubscriber<Dictionary<string, string>>("Penumbra.GetModList");
modSettings = _pluginInterface.GetIpcSubscriber<Guid, string, string, bool,
(PenumbraApiEc status, (bool modEnabled, int priority, Dictionary<string, List<string>> optionDetails, bool ignoreInheritance)? settings)>
("Penumbra.GetCurrentModSettings");
modSettingsChanged = _pluginInterface.GetIpcSubscriber<ModSettingChange, Guid, string, bool, Action?>("Penumbra.ModSettingChanged");
modSettingsChanged.Subscribe(CheckModChanges);
}
interfaceCollection = _pluginInterface.GetIpcSubscriber<ApiCollectionType, (Guid id, string Name)>("Penumbra.GetCollection").InvokeFunc(ApiCollectionType.Interface);
modList = _pluginInterface.GetIpcSubscriber<Dictionary<string, string>>("Penumbra.GetModList");
modSettings = _pluginInterface.GetIpcSubscriber<Guid, string, string, bool,
(PenumbraApiEc status, (bool modEnabled, int priority, Dictionary<string, List<string>> optionDetails, bool ignoreInheritance)? settings)>
("Penumbra.GetCurrentModSettings.V5");
modSettingsChanged = _pluginInterface.GetIpcSubscriber<ModSettingChange, Guid, string, bool, Action?>("Penumbra.ModSettingChanged.V5");
modSettingsChanged.Subscribe(CheckModChanges);

penumbraEnabledChange.Subscribe(CheckState);
penumbraInit.Subscribe(PenumbraInit);
penumbraDispose.Subscribe(PenumbraDispose);

if (penumbraModsEnabled)
{
NativeUiBanned = penumbraApiVersion.Breaking is 4 ? OldCheckMUIPresence(oldModList!.InvokeFunc(), oldInterfaceCollection!.InvokeFunc())
: CheckMUIPresence(modList!.InvokeFunc(), interfaceCollection.Id);
NativeUiBanned = CheckMUIPresence(modList!.InvokeFunc(), interfaceCollection.Id);
}
}
#pragma warning restore CS8618

private void CheckState(bool penumbraEnabled)
{
NativeUiBanned = penumbraEnabled
&& penumbraApiVersion.Breaking == 4 ? OldCheckMUIPresence(oldModList.InvokeFunc(), oldInterfaceCollection.InvokeFunc())
: CheckMUIPresence(modList.InvokeFunc(), interfaceCollection.Id);
NativeUiBanned = penumbraEnabled && CheckMUIPresence(modList.InvokeFunc(), interfaceCollection.Id);
}

private void CheckModChanges(ModSettingChange type, Guid collectionId, string modDirectory, bool inherited)
Expand Down Expand Up @@ -163,70 +130,20 @@ private bool CheckMUIPresence(Dictionary<string, string> modList, Guid collectio
return false;
}


private void OldCheckModChanges(ModSettingChange type, string collectionName, string modDirectory, bool inherited)
{
if ((type is not ModSettingChange.EnableState && type is not ModSettingChange.Inheritance) || !oldInterfaceCollection.InvokeFunc().Equals(collectionName, StringComparison.Ordinal))
{
return;
}
var currentMods = oldModList.InvokeFunc().Where(currentMod => currentMod.modDirectory.Equals(modDirectory, StringComparison.Ordinal));
NativeUiBanned = OldCheckMUIPresence(currentMods, collectionName);
}

private bool OldCheckMUIPresence(IEnumerable<(string modDirectory, string modName)> modList, string collection)
{
if (!modList.Any())
{
return false;
}
foreach (var mod in modList)
{
if (!mod.modName.Contains("Material UI", StringComparison.OrdinalIgnoreCase) && !mod.modDirectory.Contains("Material UI", StringComparison.OrdinalIgnoreCase))
{
continue;
}
var modDetails = oldModSettings.InvokeFunc(collection, mod.modDirectory, mod.modName, arg4: false);
if (modDetails.status is not PenumbraApiEc.Success || !modDetails.settings.HasValue)
{
log.Error("Failed to retrieve mod details. {stat}", modDetails.status);
continue;
}
return modDetails.settings.Value.modEnabled;
}
return false;
}


private void PenumbraInit()
{
penumbraEnabledChange.Subscribe(CheckState);
if (penumbraApiVersion.Breaking is 4)
{
oldModSettingsChanged.Subscribe(OldCheckModChanges);
}
else if (penumbraApiVersion.Breaking is 5)
{
modSettingsChanged.Subscribe(CheckModChanges);
}
modSettingsChanged.Subscribe(CheckModChanges);
if (penumbraModsEnabled)
{
NativeUiBanned = penumbraApiVersion.Breaking is 4 ? OldCheckMUIPresence(oldModList.InvokeFunc(), oldInterfaceCollection.InvokeFunc())
: CheckMUIPresence(modList.InvokeFunc(), interfaceCollection.Id);
NativeUiBanned = CheckMUIPresence(modList.InvokeFunc(), interfaceCollection.Id);
}
}

private void PenumbraDispose()
{
penumbraEnabledChange.Unsubscribe(CheckState);
if (penumbraApiVersion.Breaking is 4)
{
oldModSettingsChanged.Unsubscribe(OldCheckModChanges);
}
else if (penumbraApiVersion.Breaking is 5)
{
modSettingsChanged.Unsubscribe(CheckModChanges);
}
modSettingsChanged.Unsubscribe(CheckModChanges);
NativeUiBanned = false;
}

Expand Down
35 changes: 35 additions & 0 deletions TickTracker/Windows/BarWindowBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public abstract class BarWindowBase : Window
public double PreviousProgress { get; set; }
public Vector2 WindowPosition { get; protected set; }
public Vector2 WindowSize { get; protected set; }
protected Vector2 ConfigPos { get; set; }
protected Vector2 ConfigSize { get; set; }

private const FontAwesomeIcon RegenIcon = FontAwesomeIcon.Forward;
private const FontAwesomeIcon FastIcon = FontAwesomeIcon.FastForward;
Expand Down Expand Up @@ -144,4 +146,37 @@ protected void DrawProgress(double progress, Vector4 backgroundColor, Vector4 fi
pos = iconPair.Value == invalidSize ? pos : pos - (iconPair.Value / 2);
drawList.AddText(UiBuilder.IconFont, currentFontSize * 0.85f, pos, color, iconPair.Key);
}

protected void UpdateWindow()
{
var windowPos = ImGui.GetWindowPos();
var windowSize = ImGui.GetWindowSize();
if (config.LockBar)
{
WindowPosition = windowPos;
WindowSize = windowSize;
return;
}
if (IsFocused)
{
if (utilities.UpdateWindowConfig(windowPos, windowSize, WindowType))
{
ConfigSize = windowSize;
ConfigPos = windowPos;
}
}
else
{
if (windowPos != ConfigPos)
{
ImGui.SetWindowPos(ConfigPos);
}
if (windowSize != ConfigSize)
{
ImGui.SetWindowSize(ConfigSize);
}
}
WindowPosition = windowPos;
WindowSize = windowSize;
}
}
35 changes: 2 additions & 33 deletions TickTracker/Windows/GPBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,16 @@ namespace TickTracker.Windows;
public class GPBar : BarWindowBase
{
private readonly Configuration config;
private readonly Utilities utilities;
public GPBar(IClientState _clientState, IPluginLog _pluginLog, Utilities _utilities, Configuration _config) : base(_clientState, _pluginLog, _utilities, _config, WindowType.GpWindow, "GPBarWindow")
{
config = _config;
utilities = _utilities;
Size = config.GPBarSize;
Position = config.GPBarPosition;
Size = ConfigSize = config.GPBarSize;
Position = ConfigPos = config.GPBarPosition;
}

public override void Draw()
{
UpdateWindow();
DrawProgress(Progress, config.GPBarBackgroundColor, config.GPBarFillColor, config.GPBarBorderColor, config.GPIconColor);
}

private void UpdateWindow()
{
var windowPos = ImGui.GetWindowPos();
var windowSize = ImGui.GetWindowSize();
if (config.LockBar)
{
WindowPosition = windowPos;
WindowSize = windowSize;
return;
}
if (IsFocused)
{
utilities.UpdateWindowConfig(windowPos, windowSize, WindowType);
}
else
{
if (windowPos != config.GPBarPosition)
{
ImGui.SetWindowPos(config.GPBarPosition);
}
if (windowSize != config.GPBarSize)
{
ImGui.SetWindowSize(config.GPBarSize);
}
}
WindowPosition = windowPos;
WindowSize = windowSize;
}
}
Loading

0 comments on commit f5e4dbc

Please sign in to comment.