BNM 2.5.0
 
Loading...
Searching...
No Matches
Example 02: Other Structures

It shows how to work with different Unity and Mono structures.

#include <BNM/UserSettings/GlobalSettings.hpp>
#include <BNM/Class.hpp>
#include <BNM/Field.hpp>
#include <BNM/Method.hpp>
#include <BNM/Property.hpp>
#include <BNM/Operators.hpp>
#include <BNM/BasicMonoStructures.hpp>
#include <BNM/ComplexMonoStructures.hpp>
#include <BNM/ClassesManagement.hpp>
using namespace BNM::Structures::Unity; // Vector3, Vector2 etc.
using namespace BNM::Structures; // Mono::String, Mono::Array etc.
void MonoArray() {
// Type[] - an array of any objects
Mono::Array<int> *array = nullptr;
//! It can be created in two ways
// * Array<Тип>::Create(размер или std::vector<Тип>) - does not get into the garbage collector
// * LoadClass().NewArray<Тип>(размер) - gets into the garbage collector
// Let's create it in 1 way
//! You can get the data using
auto dataPtr = array->GetData(); // Pointer to the C array
// or
auto dataVec = array->ToVector(); // std::vector<int>
// or
auto firstData = array->At(0); // First element of the array
}
void MonoList() {
// System.Collections.Generic.List<Тип> - a list of any objects
Mono::List<int> *list = nullptr;
//! It can only be created through a class:
// LoadClass().NewList<Type>(size) - gets into the garbage collector
// LoadClass().NewListUnsafe<Type>(size) - gets into the garbage collector, but all methods are handled by BNM
//! To avoid searching for the System.Int32 class (the int value class in C#)
//! You can use BNM::Defaults::Get<Type>()
//! BNM::Defaults::Get supports only basic types
auto intClass = BNM::Defaults::Get<int>().ToClass();
list = intClass.NewList<int>();
//! You can get the data using
auto dataPtr = list->GetData(); // Pointer to the C array
// or
auto dataVec = list->ToVector(); // std::vector<int>
// or
auto firstData = list->At(0); // The first element of the array
}
void MonoDictionary() {
// System.Collections.Generic.Dictionary<Key type, Value type> - dictionary
// For more information about generic, see example 03
auto dictionaryClass = BNM::Class(BNM_OBFUSCATE("System.Collections.Generic"), BNM_OBFUSCATE("Dictionary`2"), BNM::Image(BNM_OBFUSCATE("mscorlib.dll")));
auto dictionary_int_int_Class = dictionaryClass.GetGeneric({BNM::Defaults::Get<int>(), BNM::Defaults::Get<int>()});
dictionary = (Mono::Dictionary<int, int> *) dictionary_int_int_Class.CreateNewObjectParameters();
//! You can get the data using
auto keys = dictionary->GetKeys(); // std::vector<Key type>
// or
auto values = dictionary->GetValues(); // std::vector<Value type>
// or
auto map = dictionary->ToMap(); // std::map<Key type, Value type>
// or
int value = 0;
if (dictionary->TryGet(1, &value))
; // Value found
}
// The example below can be compiled in Unity and it will work
namespace DelegatesAndActions {
// C# class
/*
public class Delegates : MonoBehaviour {
public delegate int JustDelegate(int x, int y);
public JustDelegate justDelegateDef;
public UnityAction<int, int> JustUnityAction;
public Action<int, int> JustAction;
public UnityEvent<int, int> JustEvent;
void Start() {
justDelegateDef += delegate(int x, int y) {
Log($"justDelegateDef(1) x: {x}, y: {y}");
return 1;
};
justDelegateDef += delegate(int x, int y) {
Log($"justDelegateDef(3) x: {x}, y: {y}");
return 3;
};
justDelegateDef += delegate(int x, int y) {
Log($"justDelegateDef(500) x: {x}, y: {y}");
return 500;
};
JustAction += delegate(int x, int y) {
Log($"JustAction x: {x}, y: {y}");
return;
};
JustUnityAction += delegate(int x, int y) {
Log($"JustUnityAction x: {x}, y: {y}");
return;
};
JustEvent.AddListener(delegate(int x, int y) {
Log($"JustEvent x: {x}, y: {y}");
return;
});
// Just output of messages
logClass = new AndroidJavaClass("android.util.Log");
}
// Just output of messages
private AndroidJavaClass logClass;
void Log(string s) { logClass.CallStatic<int>("e", "BNM_TargetApp", s); }
}
*/
// ClassesManagement is used here. It is described in more detail in Example 05.
struct Delegates : BNM::UnityEngine::MonoBehaviour {
BNM::MulticastDelegate<int> *justDelegateDef;
BNM::UnityEngine::UnityAction<int, int> *JustUnityAction;
BNM::Structures::Mono::Action<int, int> *JustAction;
BNM::UnityEngine::UnityEvent<int, int> *JustEvent;
void *logClass;
BNM_CustomClass(Delegates, BNM::CompileTimeClassBuilder(nullptr, BNM_OBFUSCATE_TMP("Delegates")).Build(), {}, {});
void Start() {
BNM_LOG_DEBUG("justDelegateDef: %p", justDelegateDef);
BNM_LOG_DEBUG("JustUnityAction: %p", JustUnityAction);
BNM_LOG_DEBUG("JustAction: %p", JustAction);
BNM_LOG_DEBUG("JustEvent: %p", JustEvent);
if (justDelegateDef) justDelegateDef->Invoke(10, 60);
if (JustUnityAction) JustUnityAction->Invoke(70, 9);
if (JustAction) JustAction->Invoke(30, 42);
if (JustEvent) JustEvent->Invoke(7, 234);
}
BNM_CustomMethod(Start, false, BNM::Defaults::Get<void>(), "Start");
};
// You will see something like this in log:
/*
ByNameModding justDelegateDef: 0x7986ef6900
ByNameModding JustUnityAction: 0x7986ef67e0
ByNameModding JustAction: 0x7986ef6870
ByNameModding JustEvent: 0x7986eea480
BNM_TargetApp justDelegateDef(1) x: 10, y: 60
BNM_TargetApp justDelegateDef(3) x: 10, y: 60
BNM_TargetApp justDelegateDef(500) x: 10, y: 60
BNM_TargetApp JustUnityAction x: 70, y: 9
BNM_TargetApp JustAction x: 30, y: 42
BNM_TargetApp JustEvent x: 7, y: 234
*/
}
void OnLoaded_Example_02() {
using namespace BNM;
//! Unity structures
// Mathematical structures
// Mathematical operations similar to those in Unity can be performed on these structures
Vector2 vector2;
Vector3 vector3;
Vector4 vector4;
Matrix3x3 matrix3x3;
Matrix4x4 matrix4x4;
Quaternion quaternion;
// Structures for Raycast
Ray ray;
RaycastHit raycastHit;
//! Mono structures
//! System.String, for more information, see Example 01
Mono::String *string;
//! Method describes Array
MonoArray();
//! Method describes List
MonoList();
//! Method describes Dictionary
MonoDictionary();
//! Namespace describes Delegates and Actions
using namespace DelegatesAndActions;
}
#define BNM_CustomMethod(_method_, _isStatic_, _type_, _name_,...)
Define info about C++ method for il2cpp.
Definition ClassesManagement.hpp:309
#define BNM_CustomClass(_class_, _targetType_, _baseType_, _owner_,...)
Define info of C++ class for il2cpp.
Definition ClassesManagement.hpp:250
#define BNM_CustomMethodMarkAsInvokeHook(_method_)
Mark method to prefer InvokeHook.
Definition ClassesManagement.hpp:330
#define BNM_CallCustomMethodOrigin(_method_,...)
Call method origin, if it exists.
Definition ClassesManagement.hpp:358
#define BNM_CustomMethodSkipTypeMatch(_method_)
Skip method parameters type matching.
Definition ClassesManagement.hpp:348
constexpr DefaultTypeRef Get()
Method that helps to get il2cpp class type from C++ and BNM types.
Definition Defaults.hpp:132
Namespace that holds Unity math and helper structs.
Definition Defaults.hpp:14
Main BNM namespace.
Definition BasicMonoStructures.hpp:16
Class for working with il2cpp classes.
Definition Class.hpp:29
Class for working with il2cpp images.
Definition Image.hpp:19
Ret Invoke(Parameters ...parameters)
Invoke delegate.
Definition Delegates.hpp:140
Array type implementation.
Definition BasicMonoStructures.hpp:157
T * GetData() const
Get array data pointer.
Definition BasicMonoStructures.hpp:181
Utils::DataIterator< T > At(IL2CPP::il2cpp_array_size_t index) const
Get element at index.
Definition BasicMonoStructures.hpp:225
static Array< T > * Create(size_t capacity, bool _forceUseAlloc=false)
Creates new empty array of set capacity.
Definition BasicMonoStructures.hpp:242
std::vector< T > ToVector() const
Convert array to std::vector.
Definition BasicMonoStructures.hpp:187
System.Generic.Dictionary type implementation.
Definition ComplexMonoStructures.hpp:20
bool TryGet(TKey key, TValue *value) const
Try to get value by key.
Definition ComplexMonoStructures.hpp:142
std::vector< TKey > GetKeys() const
Convert to std::vector.
Definition ComplexMonoStructures.hpp:107
std::map< TKey, TValue > ToMap() const
Convert to std::map.
Definition ComplexMonoStructures.hpp:97
std::vector< TValue > GetValues() const
Convert to std::vector.
Definition ComplexMonoStructures.hpp:117
System.Generic.List type implementation.
Definition BasicMonoStructures.hpp:325
Utils::DataIterator< T > At(int index) const
Get element at index.
Definition BasicMonoStructures.hpp:452
T * GetData() const
Get list data pointer.
Definition BasicMonoStructures.hpp:355
std::vector< T > ToVector() const
Convert list to std::vector.
Definition BasicMonoStructures.hpp:379
String type implementation.
Definition BasicMonoStructures.hpp:95
Definition Matrix3x3.hpp:12
Definition Matrix4x4.hpp:26
Definition Quaternion.hpp:6
Definition Ray.hpp:6
Definition RaycastHit.hpp:9
Definition Vector2.hpp:7
Definition Vector3.hpp:10
Definition Vector4.hpp:10
void Invoke(Parameters ...parameters) const
Invoke event.
Definition UnityStructures.hpp:215