Helper methods – Top level helpers
← 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 bool
TryGetTerrariaType
(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 bool
TryGetReLogicType
(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 bool
TryGetXNAType
(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 bool
IsTypeInTerrariaAssembly
(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 bool
IsTypeInReLogicAssembly
(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 bool
IsTypeInAnyXNAAssembly
(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 object
ActivateInstanceUsingFirstConstructor
(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 object
GetFieldValueWithReflection
(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:
-
object
GetFieldValueWithReflection
(Type type, string fieldName, object sourceObject)
- Finds the specified
fieldName
on the specifiedtype
, instead of ontypeof(sourceObject)
- Finds the specified
-
object
GetFieldValueWithReflection
(FieldInfo field, object sourceObject)
- Uses the provided
FieldInfo field
to accesssourceObject
- Uses the provided
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 void
SetFieldValueWithReflection
(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:
-
void
SetFieldValueWithReflection
(Type type, string fieldName, object sourceObject, object newFieldValue)
- Finds the specified
fieldName
on the specifiedtype
, instead of ontypeof(sourceObject)
- Finds the specified
-
void
SetFieldValueWithReflection
(FieldInfo field, object sourceObject, object newFieldValue)
- Uses the provided
FieldInfo field
to operate onsourceObject
- Uses the provided
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 object
GetPropertyValueWithReflection
(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:
-
object
GetPropertyValueWithReflection
(Type type, string propertyName, object sourceObject)
- Finds the specified
propertyName
on the specifiedtype
, instead of ontypeof(sourceObject)
- Finds the specified
-
object
GetPropertyValueWithReflection
(PropertyInfo property, object sourceObject)
- Uses the provided
PropertyInfo field
to accesssourceObject
- Uses the provided
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 void
SetPropertyValueWithReflection
(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:
-
object
SetPropertyValueWithReflection
(Type type, string propertyName, object sourceObject, object newPropertyValue)
- Finds the specified
propertyName
on the specifiedtype
, instead of ontypeof(sourceObject)
- Finds the specified
-
object
SetPropertyValueWithReflection
(PropertyInfo property, object sourceObject, object newPropertyValue)
- Uses the provided
PropertyInfo field
to accesssourceObject
- Uses the provided
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 Assembly
s. 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.
◆ Main Topics
Creating a new blank plugin
Plugin class structure
Helpful inherited members
Writing stub patch methods
Using persistent savedata
Helper methods
Top-level helpers
AssetHandling helpers
StringDrawing helpers
InputReading helpers
UIState helpers
Security level compliance
◆ Advanced Topics
Precompiling your plugin
Developing with Visual Studio
Creating a project
Embedding resources
Source code debugging