CommonLibVR
NativeLatentFunction.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RE/B/BSTSmartPointer.h"
5 #include "RE/I/IVirtualMachine.h"
6 #include "RE/N/NativeFunction.h"
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 
12 namespace 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:
50  using result_type = typename super::result_type;
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>
105  void IVirtualMachine::ReturnLatentResult(VMStackID a_stackID, V a_result)
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
requires is_return_convertible_v< V > 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
Base base_type
Definition: NativeFunction.h:58
R result_type
Definition: NativeFunction.h:57
F function_type
Definition: NativeFunction.h:59
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
requires(std::invocable< std::remove_reference_t< EF >>) class scope_exit
Definition: PCH.h:151
Definition: EffectArchetypes.h:65