Plugins: Add PluginEventInfo and IPostProcessMetadata

This commit is contained in:
Katy Coe
2020-12-20 23:24:09 +01:00
parent 64a7b26134
commit c6e9e26111
6 changed files with 153 additions and 23 deletions

View File

@@ -4,13 +4,25 @@
All rights reserved.
*/
using System;
using System.IO;
namespace Il2CppInspector.PluginAPI.V100
{
/// <summary>
/// Process global-metadata.dat when it is first opened as a sequence of bytes
/// Seek cursor will be at the start of the file
/// </summary>
public interface IPreProcessMetadata
{
void PreProcessMetadata(MemoryStream stream, PluginPreProcessMetadataEventInfo data);
}
/// <summary>
/// Process global-metadata.dat after it has been loaded into a Metadata object
/// </summary>
public interface IPostProcessMetadata
{
void PostProcessMetadata(Metadata metadata);
void PostProcessMetadata(Metadata metadata, PluginPostProcessMetadataEventInfo data);
}
}

View File

@@ -0,0 +1,92 @@
/*
Copyright 2020 Katy Coe - http://www.djkaty.com - https://github.com/djkaty
All rights reserved.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Il2CppInspector.PluginAPI.V100
{
public interface IPluginEventInfo
{
public bool IsHandled { get; set; }
public bool IsInvalid { get; set; }
public bool IsDataModified { get; set; }
public bool IsStreamModified { get; set; }
public PluginErrorEventArgs Error { get; set; }
}
/// <summary>
/// Object which allows plugins to report on what has happened during a call
/// Changes made to this object propagate to the next plugin in the call chain until IsHandled is set to true
/// </summary>
public class PluginEventInfo<T> : IPluginEventInfo where T : new()
{
/// <summary>
/// A plugin should set this if it has processed the supplied data in such a way that no further processing is required by other plugins
/// Generally, this will prevent other plugins from processing the data
/// Note that this should be set even if the processed data was invalid (<seealso cref="IsInvalid"/>)
/// If this is not set, the same event will be raised on the next available plugin
/// Note that you can still do processing but set IsHandled to false to allow additional processing from other plugins
/// </summary>
public bool IsHandled { get; set; } = false;
/// <summary>
/// A plugin should set this when IsHandled = true but the data it processed was invalid, for example if the processing gave an unexpected result
/// </summary>
public bool IsInvalid { get; set; } = false;
/// <summary>
/// A plugin should set this when it has directly modified the provided data structure (object)
/// This can be set even if IsHandled = false to indicate that changes have been made but more plugins can still be called
/// Should be set to false if you have only queried (performed reads) on the data without changing it
/// </summary>
public bool IsDataModified { get; set; } = false;
/// <summary>
/// A plugin should set this when it has directly modified the supplied or underlying stream for the metadata or binary
/// This can be set even if IsHandled = false to indicate that changes have been made but more plugins can still be called
/// Should be set to false if you have only queried (performed reads) on the stream without changing it
/// </summary>
public bool IsStreamModified { get; set; } = false;
/// <summary>
/// Event-specific additional options and controls. See the documentation for each event for more details.
/// </summary>
public T AdditionalData { get; } = new T();
/// <summary>
/// This wiil be set automatically by Il2CppInspector to the last exception thrown by a plugin for the current event
/// </summary>
public PluginErrorEventArgs Error { get; set; } = null;
}
/// <summary>
/// Generic event info with no additional paramters
/// </summary>
public class PluginEventInfo : PluginEventInfo<object> { }
/// <summary>
/// Event info for PreProcessMetadata
/// </summary>
public class PluginPreProcessMetadataEventInfo : PluginEventInfo<PluginPreProcessMetadataEventData> { }
/// <summary>
/// Event info for PostProcessMetadata
/// </summary>
public class PluginPostProcessMetadataEventInfo : PluginEventInfo { }
/// <summary>
/// Additional data for PreProcessMetadata
/// </summary>
public class PluginPreProcessMetadataEventData
{
/// <summary>
/// Set to true to disable some validation checks by Il2CppInspector that the metadata is valid
/// </summary>
public bool SkipValidation { get; set; }
}
}

View File

@@ -5,6 +5,7 @@
*/
// This is the ONLY line to update when the API version changes
using System.IO;
using Il2CppInspector.PluginAPI.V100;
namespace Il2CppInspector
@@ -12,6 +13,13 @@ namespace Il2CppInspector
// Hooks we provide to plugins which can choose whether or not to provide implementations
internal static class PluginHooks
{
public static void PostProcessMetadata(Metadata metadata) => PluginManager.Try<IPostProcessMetadata>(p => p.PostProcessMetadata(metadata));
public static PluginPostProcessMetadataEventInfo PostProcessMetadata(Metadata metadata)
=> PluginManager.Try<IPostProcessMetadata, PluginPostProcessMetadataEventInfo>((p, e) => p.PostProcessMetadata(metadata, e));
public static PluginPreProcessMetadataEventInfo PreProcessMetadata(MemoryStream stream)
=> PluginManager.Try<IPreProcessMetadata, PluginPreProcessMetadataEventInfo>((p, e) => {
stream.Position = 0;
p.PreProcessMetadata(stream, e);
});
}
}

View File

@@ -209,15 +209,24 @@ namespace Il2CppInspector
// Try to cast each enabled plugin to a specific interface type, and for those supporting the interface, execute the supplied delegate
// Errors will be forwarded to the error handler
internal static void Try<I>(Action<I> action) {
internal static E Try<I, E>(Action<I, E> action) where E : IPluginEventInfo, new()
{
var eventInfo = new E();
foreach (var plugin in EnabledPlugins)
if (plugin is I p)
try {
action(p);
action(p, eventInfo);
if (eventInfo.IsHandled)
break;
}
catch (Exception ex) {
ErrorHandler?.Invoke(AsInstance, new PluginErrorEventArgs { Plugin = plugin, Exception = ex, Operation = typeof(I).Name });
eventInfo.Error = new PluginErrorEventArgs { Plugin = plugin, Exception = ex, Operation = typeof(I).Name };
ErrorHandler?.Invoke(AsInstance, eventInfo.Error);
}
return eventInfo;
}
// Process an incoming status update