Add event for changing mod directory.

This commit is contained in:
Ottermandias 2022-08-09 22:50:54 +02:00
parent c0542d0e94
commit 5b07245cd9
8 changed files with 75 additions and 21 deletions

View file

@ -49,6 +49,10 @@ public interface IPenumbraApi : IPenumbraApiBase
// Obtain the currently set mod directory from the configuration.
public string GetModDirectory();
// Fired whenever a mod directory change is finished.
// Gives the full path of the mod directory and whether Penumbra treats it as valid.
public event Action< string, bool >? ModDirectoryChanged;
// Obtain the entire current penumbra configuration as a json encoded string.
public string GetConfiguration();

View file

@ -28,6 +28,7 @@ public class IpcTester : IDisposable
private readonly ICallGateSubscriber< object? > _disposed;
private readonly ICallGateSubscriber< string, object? > _preSettingsDraw;
private readonly ICallGateSubscriber< string, object? > _postSettingsDraw;
private readonly ICallGateSubscriber< string, bool, object? > _modDirectoryChanged;
private readonly ICallGateSubscriber< IntPtr, int, object? > _redrawn;
private readonly ICallGateSubscriber< ModSettingChange, string, string, bool, object? > _settingChanged;
private readonly ICallGateSubscriber< IntPtr, string, IntPtr, IntPtr, IntPtr, object? > _characterBaseCreated;
@ -45,6 +46,7 @@ public class IpcTester : IDisposable
_preSettingsDraw = _pi.GetIpcSubscriber< string, object? >( PenumbraIpc.LabelProviderPreSettingsDraw );
_postSettingsDraw = _pi.GetIpcSubscriber< string, object? >( PenumbraIpc.LabelProviderPostSettingsDraw );
_settingChanged = _pi.GetIpcSubscriber< ModSettingChange, string, string, bool, object? >( PenumbraIpc.LabelProviderModSettingChanged );
_modDirectoryChanged = _pi.GetIpcSubscriber< string, bool, object? >( PenumbraIpc.LabelProviderModDirectoryChanged );
_characterBaseCreated =
_pi.GetIpcSubscriber< IntPtr, string, IntPtr, IntPtr, IntPtr, object? >( PenumbraIpc.LabelProviderCreatingCharacterBase );
_initialized.Subscribe( AddInitialized );
@ -54,6 +56,7 @@ public class IpcTester : IDisposable
_postSettingsDraw.Subscribe( UpdateLastDrawnMod );
_settingChanged.Subscribe( UpdateLastModSetting );
_characterBaseCreated.Subscribe( UpdateLastCreated );
_modDirectoryChanged.Subscribe( UpdateModDirectoryChanged );
}
public void Dispose()
@ -67,6 +70,7 @@ public class IpcTester : IDisposable
_postSettingsDraw.Unsubscribe( UpdateLastDrawnMod );
_settingChanged.Unsubscribe( UpdateLastModSetting );
_characterBaseCreated.Unsubscribe( UpdateLastCreated );
_modDirectoryChanged.Unsubscribe( UpdateModDirectoryChanged );
}
private void AddInitialized()
@ -131,11 +135,18 @@ public class IpcTester : IDisposable
private string _currentConfiguration = string.Empty;
private string _lastDrawnMod = string.Empty;
private DateTimeOffset _lastDrawnModTime;
private DateTimeOffset _lastDrawnModTime = DateTimeOffset.MinValue;
private void UpdateLastDrawnMod( string name )
=> ( _lastDrawnMod, _lastDrawnModTime ) = ( name, DateTimeOffset.Now );
private string _lastModDirectory = string.Empty;
private bool _lastModDirectoryValid = false;
private DateTimeOffset _lastModDirectoryTime = DateTimeOffset.MinValue;
private void UpdateModDirectoryChanged( string path, bool valid )
=> ( _lastModDirectory, _lastModDirectoryValid, _lastModDirectoryTime ) = ( path, valid, DateTimeOffset.Now );
private void DrawGeneral()
{
using var _ = ImRaii.TreeNode( "General IPC" );
@ -173,6 +184,10 @@ public class IpcTester : IDisposable
ImGui.TextUnformatted( $"{breaking}.{features:D4}" );
DrawIntro( PenumbraIpc.LabelProviderGetModDirectory, "Current Mod Directory" );
ImGui.TextUnformatted( _pi.GetIpcSubscriber< string >( PenumbraIpc.LabelProviderGetModDirectory ).InvokeFunc() );
DrawIntro( PenumbraIpc.LabelProviderModDirectoryChanged, "Last Mod Directory Change" );
ImGui.TextUnformatted( _lastModDirectoryTime > DateTimeOffset.MinValue
? $"{_lastModDirectory} ({( _lastModDirectoryValid ? "Valid" : "Invalid" )}) at {_lastModDirectoryTime}"
: "None" );
DrawIntro( PenumbraIpc.LabelProviderGetConfiguration, "Configuration" );
if( ImGui.Button( "Get" ) )
{

View file

@ -84,6 +84,12 @@ public class PenumbraApi : IDisposable, IPenumbraApi
return Penumbra.Config.ModDirectory;
}
public event Action< string, bool >? ModDirectoryChanged
{
add => Penumbra.ModManager.ModDirectoryChanged += value;
remove => Penumbra.ModManager.ModDirectoryChanged -= value;
}
public string GetConfiguration()
{
CheckInitialized();

View file

@ -26,6 +26,7 @@ public partial class PenumbraIpc : IDisposable
InitializeSettingProviders( pi );
InitializeTempProviders( pi );
ProviderInitialized?.SendMessage();
InvokeModDirectoryChanged( Penumbra.ModManager.BasePath.FullName, Penumbra.ModManager.Valid );
}
public void Dispose()
@ -44,20 +45,22 @@ public partial class PenumbraIpc : IDisposable
public partial class PenumbraIpc
{
public const string LabelProviderInitialized = "Penumbra.Initialized";
public const string LabelProviderDisposed = "Penumbra.Disposed";
public const string LabelProviderApiVersion = "Penumbra.ApiVersion";
public const string LabelProviderApiVersions = "Penumbra.ApiVersions";
public const string LabelProviderGetModDirectory = "Penumbra.GetModDirectory";
public const string LabelProviderGetConfiguration = "Penumbra.GetConfiguration";
public const string LabelProviderPreSettingsDraw = "Penumbra.PreSettingsDraw";
public const string LabelProviderPostSettingsDraw = "Penumbra.PostSettingsDraw";
public const string LabelProviderInitialized = "Penumbra.Initialized";
public const string LabelProviderDisposed = "Penumbra.Disposed";
public const string LabelProviderApiVersion = "Penumbra.ApiVersion";
public const string LabelProviderApiVersions = "Penumbra.ApiVersions";
public const string LabelProviderGetModDirectory = "Penumbra.GetModDirectory";
public const string LabelProviderModDirectoryChanged = "Penumbra.ModDirectoryChanged";
public const string LabelProviderGetConfiguration = "Penumbra.GetConfiguration";
public const string LabelProviderPreSettingsDraw = "Penumbra.PreSettingsDraw";
public const string LabelProviderPostSettingsDraw = "Penumbra.PostSettingsDraw";
internal ICallGateProvider< object? >? ProviderInitialized;
internal ICallGateProvider< object? >? ProviderDisposed;
internal ICallGateProvider< int >? ProviderApiVersion;
internal ICallGateProvider< (int Breaking, int Features) >? ProviderApiVersions;
internal ICallGateProvider< string >? ProviderGetModDirectory;
internal ICallGateProvider< string, bool, object? >? ProviderModDirectoryChanged;
internal ICallGateProvider< string >? ProviderGetConfiguration;
internal ICallGateProvider< string, object? >? ProviderPreSettingsDraw;
internal ICallGateProvider< string, object? >? ProviderPostSettingsDraw;
@ -116,6 +119,16 @@ public partial class PenumbraIpc
PluginLog.Error( $"Error registering IPC provider for {LabelProviderGetModDirectory}:\n{e}" );
}
try
{
ProviderModDirectoryChanged = pi.GetIpcProvider< string, bool, object? >( LabelProviderModDirectoryChanged );
Api.ModDirectoryChanged += InvokeModDirectoryChanged;
}
catch( Exception e )
{
PluginLog.Error( $"Error registering IPC provider for {LabelProviderModDirectoryChanged}:\n{e}" );
}
try
{
ProviderGetConfiguration = pi.GetIpcProvider< string >( LabelProviderGetConfiguration );
@ -155,7 +168,17 @@ public partial class PenumbraIpc
ProviderApiVersions?.UnregisterFunc();
Api.PreSettingsPanelDraw -= InvokeSettingsPreDraw;
Api.PostSettingsPanelDraw -= InvokeSettingsPostDraw;
Api.ModDirectoryChanged -= InvokeModDirectoryChanged;
}
private void InvokeSettingsPreDraw( string modDirectory )
=> ProviderPreSettingsDraw!.SendMessage( modDirectory );
private void InvokeSettingsPostDraw( string modDirectory )
=> ProviderPostSettingsDraw!.SendMessage( modDirectory );
private void InvokeModDirectoryChanged( string modDirectory, bool valid )
=> ProviderModDirectoryChanged?.SendMessage( modDirectory, valid );
}
public partial class PenumbraIpc
@ -239,12 +262,6 @@ public partial class PenumbraIpc
private void OnGameObjectRedrawn( IntPtr objectAddress, int objectTableIndex )
=> ProviderGameObjectRedrawn?.SendMessage( objectAddress, objectTableIndex );
private void InvokeSettingsPreDraw( string modDirectory )
=> ProviderPreSettingsDraw!.SendMessage( modDirectory );
private void InvokeSettingsPostDraw( string modDirectory )
=> ProviderPostSettingsDraw!.SendMessage( modDirectory );
private void DisposeRedrawProviders()
{
ProviderRedrawName?.UnregisterAction();
@ -319,7 +336,7 @@ public partial class PenumbraIpc
try
{
ProviderGetCutsceneParentIndex = pi.GetIpcProvider<int, int>( LabelProviderGetCutsceneParentIndex );
ProviderGetCutsceneParentIndex = pi.GetIpcProvider< int, int >( LabelProviderGetCutsceneParentIndex );
ProviderGetCutsceneParentIndex.RegisterFunc( Api.GetCutsceneParentIndex );
}
catch( Exception e )