Add resolving file paths and obtaining files through Penumbra to the API.

This commit is contained in:
Ottermandias 2021-08-12 14:47:07 +02:00
parent 9f5a72d21a
commit a1504046c2
3 changed files with 94 additions and 4 deletions

View file

@ -1,4 +1,5 @@
using Dalamud.Game.ClientState.Actors.Types; using Dalamud.Game.ClientState.Actors.Types;
using Lumina.Data;
namespace Penumbra.Api namespace Penumbra.Api
{ {
@ -21,11 +22,33 @@ namespace Penumbra.Api
public interface IPenumbraApi : IPenumbraApiBase public interface IPenumbraApi : IPenumbraApiBase
{ {
// Triggered when the user hovers over a listed changed object in a mod tab.
// Can be used to append tooltips.
public event ChangedItemHover? ChangedItemTooltip; public event ChangedItemHover? ChangedItemTooltip;
// Triggered when the user clicks a listed changed object in a mod tab.
public event ChangedItemClick? ChangedItemClicked; public event ChangedItemClick? ChangedItemClicked;
// Queue redrawing of all actors of the given name with the given RedrawType.
public void RedrawActor( string name, RedrawType setting ); public void RedrawActor( string name, RedrawType setting );
// Queue redrawing of the specific actor with the given RedrawType. Should only be used when the actor is sure to be valid.
public void RedrawActor( Actor actor, RedrawType setting ); public void RedrawActor( Actor actor, RedrawType setting );
// Queue redrawing of all currently available actors with the given RedrawType.
public void RedrawAll( RedrawType setting ); public void RedrawAll( RedrawType setting );
// Resolve a given gamePath via Penumbra using the Default and Forced collections.
// Returns the given gamePath if penumbra would not manipulate it.
public string ResolvePath(string gamePath);
// Resolve a given gamePath via Penumbra using the character collection for the given name (if it exists) and the Forced collections.
// Returns the given gamePath if penumbra would not manipulate it.
public string ResolvePath( string gamePath, string characterName );
// Try to load a given gamePath with the resolved path from Penumbra.
public T? GetFile< T >( string gamePath ) where T : FileResource;
// Try to load a given gamePath with the resolved path from Penumbra.
public T? GetFile<T>( string gamePath, string characterName ) where T : FileResource;
} }
} }

View file

@ -33,5 +33,9 @@
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Dalamud.dll</HintPath> <HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Dalamud.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="Lumina">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Lumina.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,18 +1,29 @@
using System; using System;
using System.IO;
using System.Reflection;
using Dalamud.Game.ClientState.Actors.Types; using Dalamud.Game.ClientState.Actors.Types;
using Dalamud.Plugin;
using Lumina.Data;
using Penumbra.GameData.Util;
using Penumbra.Mods;
using Penumbra.Util;
namespace Penumbra.Api namespace Penumbra.Api
{ {
public class PenumbraApi : IDisposable, IPenumbraApi public class PenumbraApi : IDisposable, IPenumbraApi
{ {
public int ApiVersion { get; } = 1; public int ApiVersion { get; } = 2;
private readonly Plugin _plugin; private readonly Plugin _plugin;
private readonly Lumina.GameData? _lumina;
public bool Valid { get; private set; } = false; public bool Valid { get; private set; } = false;
public PenumbraApi( Plugin penumbra ) public PenumbraApi( Plugin penumbra )
{ {
_plugin = penumbra; _plugin = penumbra;
Valid = true; Valid = true;
_lumina = ( Lumina.GameData? )_plugin.PluginInterface.Data.GetType()
.GetField( "gameData", BindingFlags.Instance | BindingFlags.NonPublic )
?.GetValue( _plugin.PluginInterface.Data );
} }
public void Dispose() public void Dispose()
@ -61,5 +72,57 @@ namespace Penumbra.Api
_plugin.ActorRefresher.RedrawAll( setting ); _plugin.ActorRefresher.RedrawAll( setting );
} }
private string ResolvePath( string path, ModManager manager, ModCollection collection )
{
if( !_plugin.Configuration.IsEnabled )
{
return path;
}
var gamePath = new GamePath( path );
var ret = collection.Cache?.ResolveSwappedOrReplacementPath( gamePath );
ret ??= manager.Collections.ForcedCollection.Cache?.ResolveSwappedOrReplacementPath( gamePath );
ret ??= path;
return ret;
}
public string ResolvePath( string path )
{
var modManager = Service< ModManager >.Get();
return ResolvePath( path, modManager, modManager.Collections.DefaultCollection );
}
public string ResolvePath( string path, string characterName )
{
var modManager = Service< ModManager >.Get();
return ResolvePath( path, modManager,
modManager.Collections.CharacterCollection.TryGetValue( characterName, out var collection )
? collection
: ModCollection.Empty );
}
private T? GetFileIntern< T >( string resolvedPath ) where T : FileResource
{
try
{
if( Path.IsPathRooted( resolvedPath ) )
{
return _lumina?.GetFileFromDisk< T >( resolvedPath );
}
return _plugin.PluginInterface.Data.GetFile< T >( resolvedPath );
}
catch( Exception e)
{
PluginLog.Warning( $"Could not load file {resolvedPath}:\n{e}" );
return null;
}
}
public T? GetFile< T >( string gamePath ) where T : FileResource
=> GetFileIntern< T >( ResolvePath( gamePath ) );
public T? GetFile< T >( string gamePath, string characterName ) where T : FileResource
=> GetFileIntern< T >( ResolvePath( gamePath, characterName ) );
} }
} }