BNM 2.5.0
 
Loading...
Searching...
No Matches
Example 05: Classes Management

It shows how to use classes management.

#include <BNM/UserSettings/GlobalSettings.hpp>
#include <BNM/ClassesManagement.hpp>
#include <BNM/BasicMonoStructures.hpp>
#include <BNM/UnityStructures.hpp>
#include <BNM/Field.hpp>
#include "BNM/ComplexMonoStructures.hpp"
#ifdef BNM_CLASSES_MANAGEMENT
using namespace BNM::Structures::Unity;
using namespace BNM::Structures::Mono;
// Let's add a class to the game
/*
namespace BNM_Example_05 {
public class BNM_ExampleObject : UnityEngine.MonoBehaviour, UnityEngine.IExposedPropertyTable {
int Value;
void Start();
Object GetReferenceValue(PropertyName id, out bool idValid);
};
};
*/
//! BNM::IL2CPP::Il2CppObject is needed if the object inherits System.Object or nothing
//! BNM::UnityEngine::Object is needed if the object inherits UnityEngine.ScriptableObject
//! BNM::UnityEngine::MonoBehaviour is needed if the object inherits UnityEngine.MonoBehaviour
struct BNM_ExampleObject : public BNM::UnityEngine::MonoBehaviour {
BNM_CustomClass(BNM_ExampleObject,
BNM::CompileTimeClassBuilder(BNM_OBFUSCATE_TMP("BNM_Example_05"), BNM_OBFUSCATE_TMP("BNM_ExampleObject")).Build(),
BNM::CompileTimeClassBuilder(BNM_OBFUSCATE_TMP("UnityEngine"), BNM_OBFUSCATE_TMP("MonoBehaviour"), BNM_OBFUSCATE_TMP("UnityEngine.CoreModule")).Build(),
// Here need specify base class, if you need to create inner class
{},
BNM::CompileTimeClassBuilder(BNM_OBFUSCATE_TMP("UnityEngine"), BNM_OBFUSCATE_TMP("IExposedPropertyTable")).Build(),
);
// To set fields, for example `veryImportantValue`
// Otherwise it will have garbage from memory
void Constructor() {
*this = BNM_ExampleObject();
}
int Value{};
uintptr_t veryImportantValue{0x424E4D};
void Start() {
BNM_LOG_INFO("BNM_ExampleObject::Start! Верен ли veryImportantValue (Is veryImportantValue true): %d", veryImportantValue == 0x424E4D);
}
void *GetReferenceValue(int id, bool *isValid) {
*isValid = false;
BNM_LOG_INFO("BNM_ExampleObject::GetReferenceValue(%d)!", id);
return nullptr;
}
// To override virtual methods or replace methods, the types must be exactly the same
BNM_CustomMethod(GetReferenceValue, false, BNM::Defaults::Get<BNM::IL2CPP::Il2CppObject *>(), "GetReferenceValue",
BNM::CompileTimeClassBuilder(BNM_OBFUSCATE_TMP("UnityEngine"), BNM_OBFUSCATE_TMP("IExposedPropertyTable")).Build(),
BNM_CustomMethod(Start, false, BNM::Defaults::Get<void>(), "Start");
BNM_CustomMethod(Constructor, false, BNM::Defaults::Get<void>(), ".ctor");
};
//! Let's look at an example from example 02
// This class was created to replace Start and use class' fields
struct Delegates :
// Since we want to use the fields directly, we need to specify parent to match addresses of fields themselves.
// Here we specify fields. It is not necessary to specify them before or after BNM_CustomClass.
BNM::MulticastDelegate<int> *justDelegateDef;
BNM::UnityEngine::UnityAction<int, int> *JustUnityAction;
BNM::Structures::Mono::Action<int, int> *JustAction;
BNM::UnityEngine::UnityEvent<int, int> *JustEvent;
// If the field is not needed and you do not create this class in C++ using new or any other methods, you can omit all fields that come after the necessary ones.
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);
}
// We specify all information about method
BNM_CustomMethod(Start, false, BNM::Defaults::Get<void>(), "Start");
// Used to speed up the search for methods. Tells BNM not to compare the types of fields, but simply check their number.
// Specifies BNM to use method hook via Invoke. It doesn't have to be specified, it just makes it easier for BNM to work.
// Also BNM have:
//! BNM_CustomMethodMarkAsBasicHook()
// It works the same way as BNM_CustomMethodMarkAsInvokeHook, but says to use hook using hooking software.
};
void OnLoaded_Example_05() {
using namespace BNM;
//! auto BNM_ExampleObjectClass = LoadClass(BNM_OBFUSCATE("BNM_Example_03"), BNM_OBFUSCATE("BNM_ExampleObject"))
// or
Class BNM_ExampleObjectClass = BNM_ExampleObject::BNMCustomClass.myClass;
}
#endif
#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
#define BNM_CustomField(_field_, _type_, _name_)
Define info about C++ field for il2cpp.
Definition ClassesManagement.hpp:280
constexpr DefaultTypeRef Get()
Method that helps to get il2cpp class type from C++ and BNM types.
Definition Defaults.hpp:132
Namespace that holds some basic and the most commonly used C# classes.
Definition BasicMonoStructures.hpp:70
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
Struct for building CompileTimeClass.
Definition Class.hpp:639
CompileTimeClass Build()
Build CompileTimeClass.
Definition Class.hpp:715
Ret Invoke(Parameters ...parameters)
Invoke delegate.
Definition Delegates.hpp:140
UnityEngine.MonoBehaviour implementation.
Definition UnityStructures.hpp:72
void Invoke(Parameters ...parameters) const
Invoke event.
Definition UnityStructures.hpp:215