LordAshes-ReflectionPlugin icon

ReflectionPlugin

Provides reflection helpers to operate using types without needing to reference their assemblies at runtime.

Last updated 3 hours ago
Total downloads 0
Total rating 0 
Categories Integration
Dependency string LordAshes-ReflectionPlugin-1.0.0
Dependants 1 other package depends on this package

This mod requires the following mods to function

bbepisTaleSpire-BepInExPack-5.4.10 icon
bbepisTaleSpire-BepInExPack

Unified BepInEx all-in-one modding pack - plugin framework, detour library

Preferred version: 5.4.10
brcoding-SetInjectionFlagPlugin-2.3.0 icon
brcoding-SetInjectionFlagPlugin

Allows players to flag mods are installed

Preferred version: 2.3.0
LordAshes-LoggingPlugin-2.0.1 icon
LordAshes-LoggingPlugin

Provides unified logging functionality

Preferred version: 2.0.1

README

Generic Class Plugin

This unofficial TaleSpire dependency plugin which allows plugins to access types, methods, fields, properties and events using reflection. This allows plugins to access BR libraries without needing to add the BR assembly as a reference which makes the plugin more resilient if BR changes the namespace or location of the type.

This is a succesor to the short lived GenClassPlugin. It uses Reflection styles operations but provides helpers functions for easily getting references to types, methods, field, properties and events.

Install

Use R2ModMan or similar installer to install this asset pack.

Generally this plugin will be added automatically as a dependency of another plugin that uses it.

Usage

Type(string typeName, string[] has = null, string[] hasType = null, int resultIndex = 0)

Creates a System.Type based on the string typeName. If the "has" array is provided the type must have properties or fields that match the names provide in the array. If the "hasType" array is provided, the type must have properties of the type specified (using string names). Lastly if the match returns multiple results, the resultIndex can be used to chose which Type to actually use.

If a type will be used frequently, defining a variable to hold it will be more efficient since the code will not have to try to find the corresponding type each time.

For example:

System.Type _LocalClient = Type("_LocalClient");
object cid = ReflectionPlugin.Reflection.Property(_LocalClient, "SelectedCreatureId").GetValue(null);

New(Type type, object[] constructorParamaters)

Creates a new instance of the given type using the constructor parameters provided. Usually chained with Type (see above). For example: object counter = New(Type("Counter"), new object[] { 0 });

HasMethod(Type type, string methodName, string[] parameterTypes = null)

Check if the given type has the specified method. If the parameterTypes array is provided, the method must match the parameter types specified (using string names). A null parameter in the parameterTypes array means any type. Usually chained with Type (see above). For example: HasMethod(Type("Counter"),"Increment", new string[] { "System.Single" });

Method(Type type, string methodName, string[] parameterTypes = null)

Returns the specified method on the specified type. If the parameterTypes array is provided, the method must match the parameter types specified (using string names). A null parameter in the parameterTypes array means any type. Used first method found if search results in multiple matching methods. Usually chained with Type (see above). For example: Method(Type("Counter"),"Increment", new string[] { "System.Single" });

Methods(Type type, string methodName, string[] parameterTypes = null)

Returns the specified methods on the specified type. If the parameterTypes array is provided, the method must match the parameter types specified (using string names). A null parameter in the parameterTypes array means any type. Returns all methods matching the search parameters. Usually chained with Type (see above). For example: Methods(Type("Counter"),"Increment");

Execute(MethodInfo method, object instance, object[] parameters, int resultIndex = 0)

Invokes the specified method using the specified instance and the specified parameters. Normally the method returns the return value of the invoked method. Any out parameters are updated in the array of input parameters. However, to make it easier to access a single out parameter directly, the resultIndex can be used to indicated the result to be returned. If the invoked method returns a value then it corresponds to resultIndex 0. The rest of the resultIndex refer to the input parameters. Thus resultIndex 1 would be the first input parameter, resultIndex 2 would be the second, and so on. If the invoked method does not retutrn a value then resultIndex 0 refers to the first parameter.

For example:

object[] parameters = new object[] { ReflectionPlugin.Reflection.Property(_LocalClient, "SelectedCreatureId").GetValue(null), null };
ReflectionPlugin.Reflection.Execute(ReflectionPlugin.Reflection.Method(_CreaturePresenter, "TryGetAsset"), null, parameters);
object asset = parameters[1];

can be abbreviated by:

object[] parameters = new object[] { ReflectionPlugin.Reflection.Property(_LocalClient, "SelectedCreatureId").GetValue(null), null };
object asset = ReflectionPlugin.Reflection.Execute(ReflectionPlugin.Reflection.Method(_CreaturePresenter, "TryGetAsset"), null, parameters, 2);

Since TryGetAsset returns a bool that return value becomes resultIndex 0. The first input parameter is a guid and the second parameter is the out parameter. As such to access the out parameter, a resultIndex of 2 needs to be used.

HasField(Type type, string fieldName, string fieldType = null)

Check if the given type has the specified field. If the fieldType parameter is provided, the field must match the parameter type specified (using string names). Usually chained with Type (see above). For example: HasField(Type("Counter"),"Count", new string[] { "System.Single" });

Field(Type type, string methodName, string[] parameterTypes = null)

Returns the specified field on the specified type. If the parameterTypes parameter is provided, the field must match the parameter type specified (using string names). Note: This method obtains the field (Reflection.FieldInfo) and not the value of the field. The GetValue() needs to be used to obtain the field value specifying a instance object if the type is not static. Usually chained with Type (see above). For example: Field(Type("Counter"),"Count", new string[] { "System.Single" }).GetValue(counter1);

Fields(Type type, string methodName, string[] parameterTypes = null)

Same a Field except returns an array of fields that match the specifications.

HasProperty(Type type, string fieldName, string propertyType = null)

Check if the given type has the specified property. If the propertyType parameter is provided, the property must match the parameter type specified (using string names). Usually chained with Type (see above). For example: HasProperty(Type("Counter"),"Count", new string[] { "System.Single" });

Property(Type type, string methodName, string[] parameterTypes = null)

Returns the specified propety on the specified type. If the parameterTypes parameter is provided, the field must match the parameter type specified (using string names). Note: This method obtains the property (Reflection.PropertyInfo) and not the value of the property. The GetValue() needs to be used to obtain the property value specifying a instance object if the type is not static. Usually chained with Type (see above). For example: Property(Type("Counter"),"Count", new string[] { "System.Single" }).GetValue(counter1);

Properties(Type type, string methodName, string[] parameterTypes = null)

Same a Property except returns an array of properties that match the specifications.

HasEvent(Type type, string eventName)

Check if the given type has the specified event. Usually chained with Type (see above). For example: HasEvent(Type("UIButton"),"Click");

Event(Type type, string eventName, string eventType = null)

Returns the specified event on the specified type. If the eventType parameter is provided, the event must match the parameter type specified (using string names). Usually chained with Type (see above). For example: Event(Type("UIButton"),"Click", new string[] { "System.String" });

Events(Type type, string eventName)

Same a Event except returns an array of properties that match the specifications.

AddEvent(Type type, string eventName, Delegate handler, object instance)

Adds an event to the specified type or type instance. The handler typically requires complex objects to be referenced as object.

Info(Type type, object instance)

Provides an analysis of the given type and optional instance (use null for static types). The analysis includes things like name, if the type is static, what assembly it is defined in, its methods, its fields, and its properties.

**CatchFullError(Exception exception)

Logs a complete exception diffing down into InnerExceptions.