It shows how to use coroutines.
#include <BNM/Coroutine.hpp>
#include <BNM/UnityStructures.hpp>
#include <BNM/ClassesManagement.hpp>
#include "BNM/Field.hpp"
#ifdef BNM_CLASSES_MANAGEMENT
#ifdef BNM_COROUTINE
struct CustomYieldInstruction : BNM::IL2CPP::Il2CppObject {
BNM::CompileTimeClassBuilder(BNM_OBFUSCATE_TMP(
"System.Collections"), BNM_OBFUSCATE_TMP(
"IEnumerator"), BNM_OBFUSCATE_TMP(
"mscorlib.dll")).
Build());
std::chrono::time_point<std::chrono::system_clock> waitUntilTime;
void Finalize() { this->~CustomYieldInstruction(); }
bool MoveNext() { return waitUntilTime > std::chrono::system_clock::now(); }
void Reset() { waitUntilTime = {}; }
Il2CppObject *Current() { return nullptr; }
void Setup(long long seconds) {
Reset();
waitUntilTime = std::chrono::system_clock::now() + std::chrono::seconds(seconds);
}
static BNM::IL2CPP::Il2CppObject *New(long long seconds) {
auto instance = (CustomYieldInstruction *) BNM::Class(BNMCustomClass.myClass).CreateNewInstance();
instance->Setup(seconds);
return instance;
}
};
BNM_LOG_DEBUG("IEnumeratorExample 1");
BNM_LOG_DEBUG("IEnumeratorExample 2 (WaitForEndOfFrame)");
BNM_LOG_DEBUG("IEnumeratorExample 3 (WaitForFixedUpdate)");
BNM_LOG_DEBUG("IEnumeratorExample 4 (WaitForSeconds)");
BNM_LOG_DEBUG("IEnumeratorExample 5 (WaitForSecondsRealtime)");
co_yield CustomYieldInstruction::New(5);
BNM_LOG_DEBUG("IEnumeratorExample 6 (CustomYieldInstruction)");
return thing->Alive();
});
BNM_LOG_DEBUG("'thing' мертва (dead) :_(");
co_return;
}
BNM_CustomClass(EnumeratorTests, BNM::CompileTimeClassBuilder(
nullptr, BNM_OBFUSCATE_TMP(
"EnumeratorTests")).Build(), {}, {});
void Start() {
auto enumerator = IEnumeratorExample().Get();
auto r = StartCoroutine[this](enumerator);
BNM_LOG_DEBUG("Invoked: %p", r);
}
inline static int frames = 0;
void Update() {
frames++;
if (frames != 200) return;
BNM_LOG_DEBUG("Killing 'thing'");
Destory(thing);
}
};
void OnLoaded_Example_06() {
auto coreModule =
BNM::Image(BNM_OBFUSCATE(
"UnityEngine.CoreModule.dll"));
GameObject =
BNM::Class(BNM_OBFUSCATE(
"UnityEngine"), BNM_OBFUSCATE(
"GameObject"), coreModule);
StartCoroutine =
BNM::Class(BNM_OBFUSCATE(
"UnityEngine"), BNM_OBFUSCATE(
"MonoBehaviour"), coreModule).
GetMethod(BNM_OBFUSCATE(
"StartCoroutine"), {BNM_OBFUSCATE(
"routine")});
}
#endif
#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_CallCustomMethodOrigin(_method_,...)
Call method origin, if it exists.
Definition ClassesManagement.hpp:358
constexpr DefaultTypeRef Get()
Method that helps to get il2cpp class type from C++ and BNM types.
Definition Defaults.hpp:132
Main BNM namespace.
Definition BasicMonoStructures.hpp:16
Class for working with il2cpp classes.
Definition Class.hpp:29
BNM::MethodBase GetMethod(const std::string_view &name, int parameters=-1) const
Get method by name and parameters count.
BNM::IL2CPP::Il2CppObject * CreateNewObjectParameters(Parameters ...parameters) const
Create class instance.
Definition Method.hpp:127
BNM::Class GetParent() const
Get parent.
Struct for building CompileTimeClass.
Definition Class.hpp:639
CompileTimeClass Build()
Build CompileTimeClass.
Definition Class.hpp:715
Analog of C# IEnumerator that is based on C++20 coroutines to emulate work of Unity's coroutines.
Definition Coroutine.hpp:97
BNM's custom wrapper for UnityEngine.WaitForEndOfFrame.
Definition Coroutine.hpp:53
BNM's custom wrapper for UnityEngine.WaitForFixedUpdate.
Definition Coroutine.hpp:60
BNM's custom wrapper for UnityEngine.WaitForSecondsRealtime.
Definition Coroutine.hpp:74
BNM's custom wrapper for UnityEngine.WaitForSeconds.
Definition Coroutine.hpp:67
BNM's custom implementation of UnityEngine.WaitUntil.
Definition Coroutine.hpp:88
Class for working with il2cpp images.
Definition Image.hpp:19
Typed class for working with il2cpp methods.
Definition Method.hpp:20
UnityEngine.MonoBehaviour implementation.
Definition UnityStructures.hpp:72
UnityEngine.Object implementation.
Definition UnityStructures.hpp:33