BNM 2.5.0
 
Loading...
Searching...
No Matches
Example 08: Exceptions

It shows how to handle il2cpp exceptions.

#include <BNM/Exceptions.hpp>
#include <BNM/Method.hpp>
// For example game has "danger" method, that can throw C# exception
// And we want to catch it, to continue code execution even if method thrown exception
BNM::Method<int> DangerMethod;
void (*old_Start)(void *);
void Start(void *instance) {
old_Start(instance);
int result;
// To catch you can use defines
result = DangerMethod[instance]();
BNM_catch(exception /*exception object name*/) // You can skip catch block
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;
// Or you can use BNM::TryInvoke:
auto exception = BNM::TryInvoke(/*You can pass anything without args and returning void (lambdas, methods) */ [&]{
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;
}
if (result == -1) {/*Do sth*/}
}
void OnLoaded_Example_08() {
auto cls = BNM::Class(BNM_OBFUSCATE("Example"), BNM_OBFUSCATE("Example"));
DangerMethod = cls.GetMethod(BNM_OBFUSCATE("DangerMethod"));
BNM::InvokeHook(cls.GetMethod(BNM_OBFUSCATE("Start")), Start, old_Start);
}
#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
Exception TryInvoke(const std::function< void()> &func)
Helper function for catching il2cpp errors.
bool InvokeHook(const BNM::MethodBase &targetMethod, T_NEW newMet, T_OLD &oldMet)
Hook method by changing MethodInfo.
Definition MethodBase.hpp:172
Class for working with il2cpp classes.
Definition Class.hpp:29
Typed class for working with il2cpp methods.
Definition Method.hpp:20