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,18 +1,29 @@
using System;
using System.IO;
using System.Reflection;
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
{
public class PenumbraApi : IDisposable, IPenumbraApi
{
public int ApiVersion { get; } = 1;
private readonly Plugin _plugin;
public int ApiVersion { get; } = 2;
private readonly Plugin _plugin;
private readonly Lumina.GameData? _lumina;
public bool Valid { get; private set; } = false;
public PenumbraApi( Plugin penumbra )
{
_plugin = penumbra;
Valid = true;
_plugin = penumbra;
Valid = true;
_lumina = ( Lumina.GameData? )_plugin.PluginInterface.Data.GetType()
.GetField( "gameData", BindingFlags.Instance | BindingFlags.NonPublic )
?.GetValue( _plugin.PluginInterface.Data );
}
public void Dispose()
@ -61,5 +72,57 @@ namespace Penumbra.Api
_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 ) );
}
}