CommonLibVR
Loading...
Searching...
No Matches
NativeLatentFunction.h
Go to the documentation of this file.
1#pragma once
2
7#include "RE/P/PackUnpack.h"
8#include "RE/T/TypeTraits.h"
9#include "RE/V/Variable.h"
10#include "RE/V/VirtualMachine.h"
11
12namespace RE
13{
14 namespace BSScript
15 {
16 template <bool IS_LONG, class F, class callbackR, class latentR, class Base, class... Args>
17 class NativeLatentFunction : public NativeFunction<IS_LONG, F, std::underlying_type_t<callbackR>, Base, Args...>
18 {
19 public:
20 using result_type = callbackR;
21 using base_type = Base;
22 using function_type = F;
23
27
28 NativeLatentFunction(std::string_view a_fnName, std::string_view a_className, function_type a_callback) :
29 NativeFunction<IS_LONG, F, std::underlying_type_t<callbackR>, Base, Args...>(a_fnName, a_className, a_callback)
30 {
31 this->_retType = GetRawType<latentR>();
32 this->_isLatent = true;
33 }
34
35 ~NativeLatentFunction() override = default; // 00
36 };
37 }
38
39 template <class R, class F, class = void>
41
42 template <class R, class Int, class F, class Cls, class... Args>
43 class NativeLatentFunction<R, F(BSScript::Internal::VirtualMachine*, Int, Cls, Args...), std::enable_if_t<BSScript::is_valid_latent_long_sig_v<R, Int, F, Cls, Args...>>> :
44 public BSScript::NativeLatentFunction<true, F(BSScript::Internal::VirtualMachine*, Int, Cls, Args...), F, R, Cls, Args...>
45 {
46 private:
47 using super = BSScript::NativeLatentFunction<true, F(BSScript::Internal::VirtualMachine*, Int, Cls, Args...), F, R, Cls, Args...>;
48
49 public:
51 using base_type = typename super::base_type;
53
54 using super::super;
55 };
56
57 namespace BSScript
58 {
59 enum LatentStatus : bool
60 {
61 kFailed, // Failed to start latent funciton, return NONE and log error
62 kStarted, // Latent function started, script will pause until latent function finishes
63 };
64
87 template <class R, class F>
88 void IVirtualMachine::RegisterLatentFunction(std::string_view a_fnName, std::string_view a_className, F a_callback, bool a_callableFromTasklets)
89 {
90 BindNativeMethod(new RE::NativeLatentFunction<R, std::remove_pointer_t<F>>(a_fnName, a_className, a_callback));
91 if (a_callableFromTasklets) {
92 SetCallableFromTasklets(a_className.data(), a_fnName.data(), true);
93 }
94 }
95
103 template <class V>
104 requires is_return_convertible_v<V>
106 {
108 var.Pack(a_result);
109 ReturnFromLatent(a_stackID, var);
110 }
111 }
112}
void SetCallableFromTasklets(const char *a_className, const char *a_stateName, const char *a_fnName, bool a_callable)
virtual void ReturnFromLatent(VMStackID a_stackID, const Variable &a_val)=0
void ReturnLatentResult(VMStackID a_stackID, V result)
Returns a result to the stack if it is waiting for a latent function to return. This is a wrapper ove...
Definition NativeLatentFunction.h:105
virtual bool BindNativeMethod(IFunction *a_fn)=0
void RegisterLatentFunction(std::string_view a_fnName, std::string_view a_className, F a_callback, bool a_callableFromTasklets=false)
Registers a latent function. Unlike normal native functions, latent functions do not return a result ...
Definition NativeLatentFunction.h:88
Definition VirtualMachine.h:46
TypeInfo _retType
Definition NativeFunctionBase.h:65
bool _isLatent
Definition NativeFunctionBase.h:69
Definition NativeFunction.h:55
Definition NativeLatentFunction.h:18
NativeLatentFunction(NativeLatentFunction &&)=delete
~NativeLatentFunction() override=default
F function_type
Definition NativeLatentFunction.h:22
Base base_type
Definition NativeLatentFunction.h:21
NativeLatentFunction(const NativeLatentFunction &)=delete
callbackR result_type
Definition NativeLatentFunction.h:20
NativeLatentFunction(std::string_view a_fnName, std::string_view a_className, function_type a_callback)
Definition NativeLatentFunction.h:28
Definition NativeLatentFunction.h:40
LatentStatus
Definition NativeLatentFunction.h:60
@ kStarted
Definition NativeLatentFunction.h:62
@ kFailed
Definition NativeLatentFunction.h:61
Definition AbsorbEffect.h:6
std::uint32_t VMStackID
Definition BSCoreTypes.h:8
Definition EffectArchetypes.h:65