Helper methods – Top level helpers - TiberiumFusion/TTPlugins GitHub Wiki

Back to the Helper methods overview article.


These helpers are located directly in the HHelpers class and are mostly methods which use Reflection to find, access, and manipulate types & members in a way that is compliant with all plugin security levels.

All helper methods are static.


Method boolTryGetTerrariaType(string typeFullName, out Type result)

Retrieves the Type inside the Terraria assembly with the specified full name. If the Type was found, it is assigned to result and the method returns true. If the Type was not found, result is set to null and the method returns false.

Example usage:

Type typePlayer = null;
bool wasFound = HHelpers.TryGetTerrariaType("Terraria.Player", out typePlayer);

Method boolTryGetReLogicType(string typeFullName, out Type result)

Retrieves the Type inside the proprietary ReLogic assembly with the specified full name. If the Type was found, it is assigned to result and the method returns true. If the Type was not found, result is set to null and the method returns false.

Example usage:

Type typeDynamicSpriteFont = null;
bool wasFound = HHelpers.TryGetReLogicType("ReLogic.Graphics.DynamicSpriteFont", out typeDynamicSpriteFont);

Method boolTryGetXNAType(string typeFullName, out Type result)

Retrieves the Type inside the any of the loaded XNA assemblies with the specified full name. If the Type was found, it is assigned to result and the method returns true. If the Type was not found, result is set to null and the method returns false.

Example usage:

Type typeVector4 = null;
bool wasFound = HHelpers.TryGetXNAType("Microsoft.Xna.Framework.Vector4", out typeVector4);

Method boolIsTypeInTerrariaAssembly(Type type)

Checks if the specified Type is declared inside the Terraria assembly. Returns true or false.

Example usage:

HHelpers.IsTypeInTerrariaAssembly(typeof(Terraria.Player)); // true

Method boolIsTypeInReLogicAssembly(Type type)

Checks if the specified Type is declared inside the proprietary ReLogic assembly. Returns true or false.

Example usage:

HHelpers.IsTypeInReLogicAssembly(typeof(ReLogic.Graphics.DynamicSpriteFont)); // true

Method boolIsTypeInAnyXNAAssembly(Type type)

Checks if the specified Type is declared inside any of the loaded XNA assemblies. Returns true or false.

Example usage:

HHelpers.IsTypeInAnyXNAAssembly(typeof(Microsoft.Xna.Framework.Vector4)); // true

Method objectActivateInstanceUsingFirstConstructor(Type type, object[] ctorParams = null)

Creates and returns an instance of the specified Type using the first found constructor. If the specified Type is not in the Terraria, ReLogic, or XNA assemblies, an exception is thrown. The ctorParams parameter is optional and allows you to supply parameters for the constructor via an object[]. Parameter count, order, and type must exactly match the constructor's parameters. This method is particularly useful for instantiating types that have a non-public constructor.

Example usage:

var localizedText = HHelpers.ActivateInstanceUsingFirstConstructor("Terraria.Localization.LocalizedText", new object[] { "ItemName_001", "I am an item." });

Method objectGetFieldValueWithReflection(string fieldName, object sourceObject)

Gets the value of the field specified by fieldName belonging to the provided sourceObject using Reflection. If the type of sourceObject is not defined in the Terraria, ReLogic, or XNA assemblies, an exception will be thrown. This helper is particularly useful if you want your plugin to be compliant with plugin Security Level 4, which prohibits plugin code from directly using Reflection.

Example usage:

Terraria.Player player = /* code */;
int numberOfTorchAttacks = HHelpers.GetFieldValueWithReflection("numberOfTorchAttacks", player);

This method has overloads:

  • objectGetFieldValueWithReflection(Type type, string fieldName, object sourceObject)
    • Finds the specified fieldName on the specified type, instead of on typeof(sourceObject)
  • objectGetFieldValueWithReflection(FieldInfo field, object sourceObject)
    • Uses the provided FieldInfo field to access sourceObject

TIP: You can use the Type type, string fieldName, object sourceObject overload to get the value of static fields, like this:

int toolTipDistance = HHelpers.GetFieldValueWithReflection(typeof(Terraria.Main), "toolTipDistance", null);

Method voidSetFieldValueWithReflection(string fieldName, object sourceObject, object newFieldValue)

Sets the value of the field specified by fieldName to the value specified by newFieldValue for the provided sourceObject using Reflection. If the type of sourceObject is not defined in the Terraria, ReLogic, or XNA assemblies, an exception will be thrown. This helper is particularly useful if you want your plugin to be compliant with plugin Security Level 4, which prohibits plugin code from directly using Reflection.

Example usage:

Terraria.Player player = /* code */;
HHelpers.SetFieldValueWithReflection("numberOfTorchAttacks", player, 100);

This method has overloads:

  • voidSetFieldValueWithReflection(Type type, string fieldName, object sourceObject, object newFieldValue)
    • Finds the specified fieldName on the specified type, instead of on typeof(sourceObject)
  • voidSetFieldValueWithReflection(FieldInfo field, object sourceObject, object newFieldValue)
    • Uses the provided FieldInfo field to operate on sourceObject

TIP: You can use the Type type, string fieldName, object sourceObject, object newFieldValue overload to set the value of static fields, like this:

HHelpers.SetFieldValueWithReflection(typeof(Terraria.Main), "toolTipDistance", null, 10);

Method objectGetPropertyValueWithReflection(string propertyName, object sourceObject)

Gets the value of the property specified by propertyName belonging to the provided sourceObject using Reflection. If the type of sourceObject is not defined in the Terraria, ReLogic, or XNA assemblies, an exception will be thrown. This helper is particularly useful if you want your plugin to be compliant with plugin Security Level 4, which prohibits plugin code from directly using Reflection.

Example usage:

Terraria.Localization.LocalizedText localizedText = /* code */;
string localizedText = HHelpers.GetPropertyValueWithReflection("Value", localizedText);

This method has overloads:

  • objectGetPropertyValueWithReflection(Type type, string propertyName, object sourceObject)
    • Finds the specified propertyName on the specified type, instead of on typeof(sourceObject)
  • objectGetPropertyValueWithReflection(PropertyInfo property, object sourceObject)
    • Uses the provided PropertyInfo field to access sourceObject

TIP: You can use the Type type, string propertyName, object sourceObject overload to get the value of static properties, like this:

int GameMode = HHelpers.GetFieldValueWithReflection(typeof(Terraria.Main), "GameMode", null);

Method voidSetPropertyValueWithReflection(string propertyName, object sourceObject, object newPropertyValue)

Sets the value of the property specified by propertyName to the value specified by newPropertyValue for the provided sourceObject using Reflection. If the type of sourceObject is not defined in the Terraria, ReLogic, or XNA assemblies, an exception will be thrown. This helper is particularly useful if you want your plugin to be compliant with plugin Security Level 4, which prohibits plugin code from directly using Reflection.

Example usage:

Terraria.Localization.LocalizedText localizedText = /* code */;
HHelpers.SetPropertyValueWithReflection("Value", localizedText, "I am some text");

This method has overloads:

  • objectSetPropertyValueWithReflection(Type type, string propertyName, object sourceObject, object newPropertyValue)
    • Finds the specified propertyName on the specified type, instead of on typeof(sourceObject)
  • objectSetPropertyValueWithReflection(PropertyInfo property, object sourceObject, object newPropertyValue)
    • Uses the provided PropertyInfo field to access sourceObject

TIP: You can use the Type type, string propertyName, object sourceObject, object newPropertyValue overload to set the value of static properties, like this:

HHelpers.SetPropertyValueWithReflection(typeof(Terraria.Main), "GameMode", null, 1);

Method Assembly[]GetAssembliesInCurrentAppDomain()

Returns an array of all loaded Assemblys. This method simply wraps AppDomain.CurrentDomain.GetAssemblies() and is particularly useful under plugin Security Level 2 or greater, which prohibits plugins from directly accessing AppDomain.

Example usage:

foreach (Assembly asm in HHelpers.GetAssembliesInCurrentAppDomain())
	Debug.WriteLine("Assembly: " + asm.GetName().FullName);

Back to the Helper methods overview article.