UnrealModFramework
Mod framework API
Loading...
Searching...
No Matches
ModNativeHook.h
Go to the documentation of this file.
1// Copyright (c) 2026 Nelaric Contributors
3#pragma once
4
5#include "CoreGlobals.h"
6#include "Logging/LogMacros.h"
7#include "Math/UnrealMathUtility.h"
9
10#include <tuple>
11#include <type_traits>
12
13namespace Mod::Extension
14{
16template <class TResult, class... TArguments> struct TNativeHook
17{
18 using FArguments = std::tuple<TArguments...>;
19 using FWireResult = std::conditional_t<std::is_void_v<TResult>, uint8, TResult>;
20 static constexpr int32 InputSize = (0 + ... + sizeof(TArguments));
21 static constexpr int32 Size = InputSize + sizeof(FWireResult);
22
23 static TResult Dispatch(UObject& Source, FName Id, TFunctionRef<TResult(TArguments...)> Original,
24 TArguments... Arguments)
25 {
26 TArray<uint8> Bytes;
27 Bytes.SetNumZeroed(Size);
28 int32 Offset = 0;
29 const auto WriteInput = [&](const auto& Value)
30 {
31 Write(Bytes.GetData() + Offset, Value);
32 Offset += sizeof(Value);
33 };
34 (WriteInput(Arguments), ...);
35 const auto ValidateInput = [](const FPayload& Packet)
36 {
37 FArguments Inputs;
38 return ReadInputs(Packet, Inputs);
39 };
40 const auto ValidateResult = [](const FPayload& Packet)
41 {
42 FWireResult Value{};
43 return ReadResult(Packet, Value);
44 };
45 auto Packet = FPayload::FromNativeCall(MoveTemp(Bytes));
46 if (!ValidateInput(Packet))
47 {
48 return Original(Arguments...);
49 }
50 const auto Core = [&Original](const FPayload& Input) -> TCoreResult<FPayload>
51 {
52 FArguments Effective;
53 if (!ReadInputs(Input, Effective))
54 {
56 }
57 if constexpr (std::is_void_v<TResult>)
58 {
59 std::apply(Original, Effective);
60 return {Input, Mod::API::EError::None, true};
61 }
62 else
63 {
64 const auto Value = std::apply(Original, Effective);
65 const auto InputBytes = Input.GetNativeCall();
66 TArray<uint8> Output(InputBytes.GetData(), InputBytes.Num());
67 Write(Output.GetData() + InputSize, Value);
68 return {FPayload::FromNativeCall(MoveTemp(Output)), Mod::API::EError::None, true};
69 }
70 };
71 FEndpoint Endpoint;
72 const auto Result = UModExtensionSubsystem::InvokeScalar<FPayload>(Source, Id, Endpoint, MoveTemp(Packet), Core,
73 ValidateInput, ValidateResult);
74 if (Result.Error != Mod::API::EError::None)
75 {
76 UE_LOG(LogTemp, Warning, TEXT("%s failed (%d), committed=%d."), *Id.ToString(),
77 static_cast<int32>(Result.Error), Result.bCommitted);
78 }
79 if constexpr (!std::is_void_v<TResult>)
80 {
81 TResult Value{};
82 return (Result.Error == Mod::API::EError::None || Result.bCommitted) && ReadResult(Result.Value, Value)
83 ? Value
84 : TResult{};
85 }
86 }
87
88private:
89 template <class T> static void Write(uint8* Data, T Value)
90 {
91 if constexpr (std::is_same_v<T, bool>)
92 {
93 *Data = Value ? 1 : 0;
94 }
95 else
96 {
97 FMemory::Memcpy(Data, &Value, sizeof(Value));
98 }
99 }
100
101 template <class T> static bool Read(const uint8* Data, T& Value)
102 {
103 if constexpr (std::is_same_v<T, bool>)
104 {
105 Value = *Data != 0;
106 return *Data <= 1;
107 }
108 else
109 {
110 FMemory::Memcpy(&Value, Data, sizeof(Value));
111 if constexpr (std::is_floating_point_v<T>)
112 {
113 return FMath::IsFinite(Value);
114 }
115 else
116 {
117 return true;
118 }
119 }
120 }
121
122 static bool ReadInputs(const FPayload& Packet, FArguments& Inputs)
123 {
124 const auto Bytes = Packet.GetNativeCall();
125 if (Bytes.Num() != Size)
126 {
127 return false;
128 }
129 int32 Offset = 0;
130 const auto ReadInput = [&](auto& Value)
131 {
132 const bool bValid = Read(Bytes.GetData() + Offset, Value);
133 Offset += sizeof(Value);
134 return bValid;
135 };
136 return std::apply([&](auto&... Values) { return (ReadInput(Values) && ...); }, Inputs);
137 }
138
139 static bool ReadResult(const FPayload& Packet, FWireResult& Result)
140 {
141 const auto Bytes = Packet.GetNativeCall();
142 return Bytes.Num() == Size && Read(Bytes.GetData() + InputSize, Result);
143 }
144};
145} // namespace Mod::Extension
@ InvalidArgument
A registration or scope argument is invalid or already in use.
Definition ModAPIResult.h:31
@ None
The operation succeeded.
Definition ModAPIResult.h:13
One World payload with owned struct storage or a weak object identity.
Definition ModExtensionPayload.h:18
static FPayload FromNativeCall(TArray< uint8 > Value)
Owns the packed inputs and result of a C++ Hook.
TConstArrayView< uint8 > GetNativeCall() const
Reads the bytes of a C++ Hook value.
Definition ModExtensionPayload.h:34
A cached link from a host call site to the current World's channel.
Definition ModExtension.h:345
Result of the host's original operation.
Definition ModExtension.h:165
Calls a native Hook with an independently typed argument list and result.
Definition ModNativeHook.h:17