UnrealModFramework
Mod framework API
Loading...
Searching...
No Matches
ModExportMacros.h
Go to the documentation of this file.
1// Copyright (c) 2026 Nelaric Contributors
10#pragma once
11
12#if defined(EXPORTMETADATA_API)
13#include "ModSourceFailure.h"
14
15#include <type_traits>
16#endif
17
18// Only generated source copies use this entry point. A metadata bootstrap executes their original bodies.
19#if defined(MOD_API_BOOTSTRAP) && MOD_API_BOOTSTRAP
20namespace Mod::ExportMetadata::Private
21{
22template <typename Object, typename Original, typename... Arguments>
23decltype(auto) CallOriginal(Object&, Original&& Callable, Arguments&&... Args)
24{
25 return Callable(static_cast<Arguments&&>(Args)...);
26}
27} // namespace Mod::ExportMetadata::Private
28#define UMF_SOURCE_HOOK_CALL(Dispatcher) ::Mod::ExportMetadata::Private::CallOriginal
29#else
30#if __has_include("Host/ModRuntimeHookDispatch.h")
31#include "Host/ModRuntimeHookDispatch.h"
32#endif
33#define UMF_SOURCE_HOOK_CALL(Dispatcher) Dispatcher
34#endif
35
40#define MOD_EXPORT_API(...)
41
46#define MOD_EXPORT_EVENT(...)
47
48namespace Mod::ExportMetadata
49{
51template <typename T> constexpr T SourceFailure(const char*, [[maybe_unused]] unsigned int Code, T Value) noexcept
52{
53#if defined(EXPORTMETADATA_API)
54 if (!std::is_constant_evaluated())
55 {
57 }
58#endif
59 return Value;
60}
61} // namespace Mod::ExportMetadata
62
68#define MOD_SOURCE_FAILURE(Name, Code, Value) ::Mod::ExportMetadata::SourceFailure(#Name, Code, Value)
69
70namespace Mod::ExportMetadata::Private
71{
72// The preprocessor folds whitespace in stringified macro arguments to single spaces.
73constexpr bool ConsumeHookToken(const char*& Text, const char* Token)
74{
75 const char* Cursor = Text;
76 while (*Token)
77 {
78 if (*Cursor++ != *Token++)
79 {
80 return false;
81 }
82 }
83 Text = *Cursor == ' ' ? Cursor + 1 : Cursor;
84 return true;
85}
86
87// Bootstrap builds check field spelling and duplication. MHT validates names and IDs.
88constexpr bool HasValidHookFields(const char* Text)
89{
90 unsigned Seen = 0;
91 while (*Text)
92 {
93 unsigned Field = 0;
94 if (ConsumeHookToken(Text, "CompatibleNames"))
95 {
96 Field = 1;
97 if (!ConsumeHookToken(Text, "=") || !ConsumeHookToken(Text, "("))
98 {
99 return false;
100 }
101 int Depth = 1;
102 while (*Text && Depth)
103 {
104 Depth += (*Text == '(') - (*Text == ')');
105 ++Text;
106 }
107 if (Depth)
108 {
109 return false;
110 }
111 if (*Text == ' ')
112 {
113 ++Text;
114 }
115 }
116 else
117 {
118 constexpr const char* Flags[]{"NoPre", "NoOn", "NoPost"};
119 unsigned Bit = 2;
120 for (const char* Flag : Flags)
121 {
122 if (ConsumeHookToken(Text, Flag))
123 {
124 Field = Bit;
125 break;
126 }
127 Bit <<= 1;
128 }
129 }
130 if (!Field || (Seen & Field))
131 {
132 return false;
133 }
134 Seen |= Field;
135 if (*Text && (!ConsumeHookToken(Text, ",") || !*Text))
136 {
137 return false;
138 }
139 }
140 return true;
141}
142} // namespace Mod::ExportMetadata::Private
143
149#define MOD_EXPORT_HOOK(...) \
150 static_assert(::Mod::ExportMetadata::Private::HasValidHookFields("" #__VA_ARGS__), \
151 "MOD_EXPORT_HOOK accepts NoPre, NoOn, NoPost and CompatibleNames = (...) once each");
constexpr T SourceFailure(const char *, unsigned int Code, T Value) noexcept
Returns the native value and records the explicit business error.
Definition ModExportMacros.h:51
static EXPORTMETADATA_API void Record(unsigned int Code) noexcept
Records an explicit source error for the active native invocation.