This section provides some basics and information about the most often used BNM's API.
Main API
BNM provides some high level API to work with il2cpp.
Struct that allows you to find image (dll):
auto assemblyCShrap =
BNM::Image(
"Assembly-CShrap.dll");
auto assemblyCShrap =
BNM::Image(
"Assembly-CShrap");
Class for working with il2cpp images.
Definition Image.hpp:19
Struct that allows you to find classes:
auto gameObject =
BNM::Class(
"UnityEngine",
"GameObject");
Class for working with il2cpp classes.
Definition Class.hpp:29
Struct that allows you to get/set instance and static fields:
int v = staticInt;
auto v = staticInt();
auto v = staticInt.
Get();
staticInt = v + 1;
void *someClassInstance = ...;
auto v = instanceInt[someClassInstance]();
auto v = instanceInt();
FieldBase & SetInstance(IL2CPP::Il2CppObject *val)
Set field instance if it's non-static.
Typed class for working with il2cpp fields.
Definition Field.hpp:20
T Get() const
Get field value.
Definition Field.hpp:60
void Set(T value) const
Set field value.
Definition Field.hpp:86
Struct that allows you to call instance and static methods.
auto v = staticMethod((BNM::Types::byte)5);
auto v = staticMethod.
Call((uint8_t)5);
void *someClassInstance = ...;
auto v = instanceMethod[someClassInstance]();
auto v = instanceMethod();
MethodBase & SetInstance(IL2CPP::Il2CppObject *val)
Set method instance if it's non-static.
Typed class for working with il2cpp methods.
Definition Method.hpp:20
Ret Call(Parameters ...parameters) const
Call method.
Definition Method.hpp:80
Struct that allows you to get/set properties.
int v = staticInt;
auto v = staticInt();
auto v = staticInt.
Get();
staticInt = v + 1;
void *someClassInstance = ...;
auto v = instanceInt[someClassInstance]();
auto v = instanceInt();
PropertyBase & SetInstance(IL2CPP::Il2CppObject *val)
Set property instance if it's non-static.
Typed class for working with il2cpp properties.
Definition Property.hpp:20
T Get() const
Call getter.
Definition Property.hpp:71
void Set(T value)
Call setter.
Definition Property.hpp:91
Method for getting some frequently used C# types.
auto intClass = intType.ToClass();
constexpr DefaultTypeRef Get()
Method that helps to get il2cpp class type from C++ and BNM types.
Definition Defaults.hpp:132
Unity and mono related structs
1. Unity structures
BNM provides some unity structures that imitate operations similar to those in Unity.
Vector2 vector2;
Vector3 vector3;
Vector4 vector4;
Matrix3x3 matrix3x3;
Matrix4x4 matrix4x4;
Quaternion quaternion;
Ray ray;
RaycastHit raycastHit;
2. Mono structures
BNM provides some mono (C#) structures
- String
BNM::Structures::Mono::String - implementation of C# System.String.
std::string cppString = newString->str();
Structures::Mono::String * CreateMonoString(const std::string_view &str)
Macro function for creating C# strings (BNM::Structures::Mono::String).
- Array
BNM::Structures::Mono::Array - implementation of C# System.Array.
Mono::Array<int> *array = nullptr;
array = Mono::Array<int>::Create(10);
auto dataPtr = array->GetData();
auto dataVec = array->ToVector();
auto firstData = array->At(0);
- List
BNM::Structures::Mono::List - implementation of C# System.Collections.Generic.List.
Mono::List<int> *list = nullptr;
auto dataPtr = list->GetData();
auto dataVec = list->ToVector();
auto firstData = list->At(0);
Structures::Mono::List< T > * NewList() const
Create list of this type.
Definition Class.hpp:720
- Dictionary
BNM::Structures::Mono::List - implementation of C# System.Collections.Generic.Dictionary.
Mono::Dictionary<int, int> *dictionary;
auto dictionaryClass =
BNM::Class(
"System.Collections.Generic",
"Dictionary`2",
BNM::Image(
"mscorlib.dll"));
auto dictionary_int_int_Class = dictionaryClass.GetGeneric({intClass, intClass});
dictionary = (Mono::Dictionary<int, int> *) dictionary_int_int_Class.CreateNewObjectParameters();
auto keys = dictionary->GetKeys();
auto values = dictionary->GetValues();
auto map = dictionary->ToMap();
int value = 0;
if (dictionary->TryGet(1 , &value))
;
- Delegate
BNM::Delegate - implementation of C# System.Delegate.
BNM::MulticastDelegate - implementation of C# System.MulticastDelegate.
auto delegate = demoDelegate();
auto result = delegate->Invoke(1, 2);
- Action/UnityAction
BNM::Structures::Mono::Action - implementation of C# System.Action. It works the same way as delegate, because it's inherited from it.
BNM::UnityEngine::UnityAction - implementation of C# UnityEngine.Events.UnityAction. It works the same way as delegate, because it's inherited from it.
auto action = demoAction();
auto unityAction = demoUnityAction();
action->Invoke(1, 2);
unityAction->Invoke(1, 2);
- Unity event
BNM::UnityEngine::UnityEvent - implementation of C# UnityEngine.Events.UnityEvent.
auto event = demoEvent();
event->Invoke(1, 2);
Generics
BNM provides API for getting typed versions of generic classes and methods.
- Generic methods
auto GetComponent = gameObject.GetMethod("GetComponent", 0);
- Generic classes
auto dictionaryClass =
BNM::Class(
"System.Collections.Generic",
"Dictionary`2",
BNM::Image(
"mscorlib.dll"));
auto dictionary_int_int_Class = dictionaryClass.GetGeneric({intClass, intClass});
Exceptions
BNM provides simple API to handle il2cpp's exceptions.
Macros
You can use BNM_try/BNM_catch macros:
int result = 0;
result = DangerMethod[instance]();
auto className = exception.ClassName();
auto message = exception.Message();
BNM_LOG_WARN("DangerMethod returned exception (in try catch) [%s]: %s", className.c_str(), message.c_str());
result = -1;
#define BNM_catch(ex)
Define block to handle exceptions.
Definition Exceptions.hpp:100
#define BNM_end_try
End BNM_try or BNM_try&BNM_catch blocks.
Definition Exceptions.hpp:108
#define BNM_try
Define danger code block.
Definition Exceptions.hpp:90
API
Or you can use BNM::TryInvoke method:
int result = 0;
result = DangerMethod[instance]();
});
if (exception.IsValid()) {
auto className = exception.ClassName();
auto message = exception.Message();
BNM_LOG_WARN("DangerMethod returned exception (in TryInvoke) [%s]: %s", className.c_str(), message.c_str());
result = -1;
}
Exception TryInvoke(const std::function< void()> &func)
Helper function for catching il2cpp errors.