Add Byte String stuff, remove Services, cleanup and refactor interop stuff, disable path resolver for the moment

This commit is contained in:
Ottermandias 2022-03-06 00:40:42 +01:00
parent 0e8f839471
commit c3454f1d16
65 changed files with 4707 additions and 3371 deletions

View file

@ -3,47 +3,42 @@ using System.Linq;
using EmbedIO;
using EmbedIO.Routing;
using EmbedIO.WebApi;
using Penumbra.Mods;
using Penumbra.Util;
namespace Penumbra.Api
namespace Penumbra.Api;
public class ModsController : WebApiController
{
public class ModsController : WebApiController
private readonly Penumbra _penumbra;
public ModsController( Penumbra penumbra )
=> _penumbra = penumbra;
[Route( HttpVerbs.Get, "/mods" )]
public object? GetMods()
{
private readonly Penumbra _penumbra;
return Penumbra.ModManager.Collections.CurrentCollection.Cache?.AvailableMods.Values.Select( x => new
{
x.Settings.Enabled,
x.Settings.Priority,
x.Data.BasePath.Name,
x.Data.Meta,
BasePath = x.Data.BasePath.FullName,
Files = x.Data.Resources.ModFiles.Select( fi => fi.FullName ),
} )
?? null;
}
public ModsController( Penumbra penumbra )
=> _penumbra = penumbra;
[Route( HttpVerbs.Post, "/mods" )]
public object CreateMod()
=> new { };
[Route( HttpVerbs.Get, "/mods" )]
public object? GetMods()
{
var modManager = Service< ModManager >.Get();
return modManager.Collections.CurrentCollection.Cache?.AvailableMods.Values.Select( x => new
{
x.Settings.Enabled,
x.Settings.Priority,
x.Data.BasePath.Name,
x.Data.Meta,
BasePath = x.Data.BasePath.FullName,
Files = x.Data.Resources.ModFiles.Select( fi => fi.FullName ),
} )
?? null;
}
[Route( HttpVerbs.Post, "/mods" )]
public object CreateMod()
=> new { };
[Route( HttpVerbs.Get, "/files" )]
public object GetFiles()
{
var modManager = Service< ModManager >.Get();
return modManager.Collections.CurrentCollection.Cache?.ResolvedFiles.ToDictionary(
o => ( string )o.Key,
o => o.Value.FullName
)
?? new Dictionary< string, string >();
}
[Route( HttpVerbs.Get, "/files" )]
public object GetFiles()
{
return Penumbra.ModManager.Collections.CurrentCollection.Cache?.ResolvedFiles.ToDictionary(
o => ( string )o.Key,
o => o.Value.FullName
)
?? new Dictionary< string, string >();
}
}

View file

@ -1,166 +1,157 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Dalamud.Game.ClientState.Objects.Types;
using Dalamud.Logging;
using Lumina.Data;
using Lumina.Data.Parsing;
using Lumina.Excel.GeneratedSheets;
using Penumbra.GameData.Enums;
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; } = 3;
private Penumbra? _penumbra;
private Lumina.GameData? _lumina;
public bool Valid
=> _penumbra != null;
public PenumbraApi( Penumbra penumbra )
{
public int ApiVersion { get; } = 3;
private Penumbra? _penumbra;
private Lumina.GameData? _lumina;
_penumbra = penumbra;
_lumina = ( Lumina.GameData? )Dalamud.GameData.GetType()
.GetField( "gameData", BindingFlags.Instance | BindingFlags.NonPublic )
?.GetValue( Dalamud.GameData );
}
public bool Valid
=> _penumbra != null;
public void Dispose()
{
_penumbra = null;
_lumina = null;
}
public PenumbraApi( Penumbra penumbra )
public event ChangedItemClick? ChangedItemClicked;
public event ChangedItemHover? ChangedItemTooltip;
internal bool HasTooltip
=> ChangedItemTooltip != null;
internal void InvokeTooltip( object? it )
=> ChangedItemTooltip?.Invoke( it );
internal void InvokeClick( MouseButton button, object? it )
=> ChangedItemClicked?.Invoke( button, it );
private void CheckInitialized()
{
if( !Valid )
{
_penumbra = penumbra;
_lumina = ( Lumina.GameData? )Dalamud.GameData.GetType()
.GetField( "gameData", BindingFlags.Instance | BindingFlags.NonPublic )
?.GetValue( Dalamud.GameData );
throw new Exception( "PluginShare is not initialized." );
}
}
public void RedrawObject( string name, RedrawType setting )
{
CheckInitialized();
_penumbra!.ObjectReloader.RedrawObject( name, setting );
}
public void RedrawObject( GameObject? gameObject, RedrawType setting )
{
CheckInitialized();
_penumbra!.ObjectReloader.RedrawObject( gameObject, setting );
}
public void RedrawAll( RedrawType setting )
{
CheckInitialized();
_penumbra!.ObjectReloader.RedrawAll( setting );
}
private static string ResolvePath( string path, ModManager manager, ModCollection collection )
{
if( !Penumbra.Config.IsEnabled )
{
return path;
}
public void Dispose()
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 )
{
CheckInitialized();
return ResolvePath( path, Penumbra.ModManager, Penumbra.ModManager.Collections.DefaultCollection );
}
public string ResolvePath( string path, string characterName )
{
CheckInitialized();
return ResolvePath( path, Penumbra.ModManager,
Penumbra.ModManager.Collections.CharacterCollection.TryGetValue( characterName, out var collection )
? collection
: ModCollection.Empty );
}
private T? GetFileIntern< T >( string resolvedPath ) where T : FileResource
{
CheckInitialized();
try
{
_penumbra = null;
_lumina = null;
}
public event ChangedItemClick? ChangedItemClicked;
public event ChangedItemHover? ChangedItemTooltip;
internal bool HasTooltip
=> ChangedItemTooltip != null;
internal void InvokeTooltip( object? it )
=> ChangedItemTooltip?.Invoke( it );
internal void InvokeClick( MouseButton button, object? it )
=> ChangedItemClicked?.Invoke( button, it );
private void CheckInitialized()
{
if( !Valid )
if( Path.IsPathRooted( resolvedPath ) )
{
throw new Exception( "PluginShare is not initialized." );
}
}
public void RedrawObject( string name, RedrawType setting )
{
CheckInitialized();
_penumbra!.ObjectReloader.RedrawObject( name, setting );
}
public void RedrawObject( GameObject? gameObject, RedrawType setting )
{
CheckInitialized();
_penumbra!.ObjectReloader.RedrawObject( gameObject, setting );
}
public void RedrawAll( RedrawType setting )
{
CheckInitialized();
_penumbra!.ObjectReloader.RedrawAll( setting );
}
private static string ResolvePath( string path, ModManager manager, ModCollection collection )
{
if( !Penumbra.Config.IsEnabled )
{
return path;
return _lumina?.GetFileFromDisk< T >( resolvedPath );
}
var gamePath = new GamePath( path );
var ret = collection.Cache?.ResolveSwappedOrReplacementPath( gamePath );
ret ??= manager.Collections.ForcedCollection.Cache?.ResolveSwappedOrReplacementPath( gamePath );
ret ??= path;
return ret;
return Dalamud.GameData.GetFile< T >( resolvedPath );
}
public string ResolvePath( string path )
catch( Exception e )
{
CheckInitialized();
var modManager = Service< ModManager >.Get();
return ResolvePath( path, modManager, modManager.Collections.DefaultCollection );
PluginLog.Warning( $"Could not load file {resolvedPath}:\n{e}" );
return null;
}
}
public string ResolvePath( string path, string characterName )
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 ) );
public IReadOnlyDictionary< string, object? > GetChangedItemsForCollection( string collectionName )
{
CheckInitialized();
try
{
CheckInitialized();
var modManager = Service< ModManager >.Get();
return ResolvePath( path, modManager,
modManager.Collections.CharacterCollection.TryGetValue( characterName, out var collection )
? collection
: ModCollection.Empty );
if( !Penumbra.ModManager.Collections.Collections.TryGetValue( collectionName, out var collection ) )
{
collection = ModCollection.Empty;
}
if( collection.Cache != null )
{
return collection.Cache.ChangedItems;
}
PluginLog.Warning( $"Collection {collectionName} does not exist or is not loaded." );
return new Dictionary< string, object? >();
}
private T? GetFileIntern< T >( string resolvedPath ) where T : FileResource
catch( Exception e )
{
CheckInitialized();
try
{
if( Path.IsPathRooted( resolvedPath ) )
{
return _lumina?.GetFileFromDisk< T >( resolvedPath );
}
return Dalamud.GameData.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 ) );
public IReadOnlyDictionary< string, object? > GetChangedItemsForCollection( string collectionName )
{
CheckInitialized();
try
{
var modManager = Service< ModManager >.Get();
if( !modManager.Collections.Collections.TryGetValue( collectionName, out var collection ) )
{
collection = ModCollection.Empty;
}
if( collection.Cache != null )
{
return collection.Cache.ChangedItems;
}
PluginLog.Warning( $"Collection {collectionName} does not exist or is not loaded." );
return new Dictionary< string, object? >();
}
catch( Exception e )
{
PluginLog.Error( $"Could not obtain Changed Items for {collectionName}:\n{e}" );
throw;
}
PluginLog.Error( $"Could not obtain Changed Items for {collectionName}:\n{e}" );
throw;
}
}
}