aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/core/structs.h
diff options
context:
space:
mode:
authorEmma Miler <emma.pi@protonmail.com>2022-12-19 19:32:16 +0100
committerGitHub <noreply@github.com>2022-12-19 19:32:16 +0100
commite04f3b36accccb590a2d51b4829256b9964ac3fd (patch)
tree20ee30c82e6f53e6e772be2e1b9613eebca12bf3 /NorthstarDLL/core/structs.h
parent33f18a735986dcd136bf8ba70ad8331306c28227 (diff)
downloadNorthstarLauncher-e04f3b36accccb590a2d51b4829256b9964ac3fd.tar.gz
NorthstarLauncher-e04f3b36accccb590a2d51b4829256b9964ac3fd.zip
Restructuring (#365)
* Remove launcher proxy * Restructuring * More restructuring * Fix include dirs * Fix merge * Remove clang thing * Filters * Oops
Diffstat (limited to 'NorthstarDLL/core/structs.h')
-rw-r--r--NorthstarDLL/core/structs.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/NorthstarDLL/core/structs.h b/NorthstarDLL/core/structs.h
new file mode 100644
index 00000000..ac29f711
--- /dev/null
+++ b/NorthstarDLL/core/structs.h
@@ -0,0 +1,62 @@
+#pragma once
+//clang-format off
+// About this file:
+// This file contains several macros used to define reversed structs
+// The reason we use these macros is to make it easier to update existing structs
+// when new fields are reversed
+// This means we dont have to manually add padding, and recalculate when updating
+
+// Technical note:
+// While functionally, these structs act like a regular struct, they are actually
+// defined as unions with anonymous structs in them.
+// This means that each field is essentially an offset into a union.
+// We acknowledge that this goes against C++'s active-member guideline for unions
+// However, this is not such a big deal here as these structs will not be constructed
+
+// Usage:
+// To use these macros, define a struct like so:
+/*
+OFFSET_STRUCT(Name)
+{
+ STRUCT_SIZE(0x100) // Total in-memory struct size
+ FIELD(0x0, int field) // offset, signature
+}
+*/
+
+#define OFFSET_STRUCT(name) union name
+#define STRUCT_SIZE(size) char __size[size];
+#define STRUCT_FIELD_OFFSET(offset, signature) \
+ struct \
+ { \
+ char CONCAT2(pad, __LINE__)[offset]; \
+ signature; \
+ };
+
+// Special case for a 0-offset field
+#define STRUCT_FIELD_NOOFFSET(offset, signature) signature;
+
+// Based on: https://stackoverflow.com/questions/11632219/c-preprocessor-macro-specialisation-based-on-an-argument
+// Yes, this is hacky, but it works quite well actually
+// This basically makes sure that when the offset is 0x0, no padding field gets generated
+#define OFFSET_0x0 ()
+
+#define IIF(c) CONCAT2(IIF_, c)
+#define IIF_0(t, ...) __VA_ARGS__
+#define IIF_1(t, ...) t
+
+#define PROBE(x) x, 1
+#define MSVC_VA_ARGS_WORKAROUND(define, args) define args
+#define CHECK(...) MSVC_VA_ARGS_WORKAROUND(CHECK_N, (__VA_ARGS__, 0))
+#define DO_PROBE(offset) PROBE_PROXY(OFFSET_##offset) // concatenate prefix with offset
+#define PROBE_PROXY(...) PROBE_PRIMITIVE(__VA_ARGS__) // expand arguments
+#define PROBE_PRIMITIVE(x) PROBE_COMBINE_##x // merge
+#define PROBE_COMBINE_(...) PROBE(~) // if merge successful, expand to probe
+
+#define CHECK_N(x, n, ...) n
+
+#define IS_0(offset) CHECK(DO_PROBE(offset))
+
+#define FIELD(offset, signature) IIF(IS_0(offset))(STRUCT_FIELD_NOOFFSET, STRUCT_FIELD_OFFSET)(offset, signature)
+#define FIELDS FIELD
+
+//clang-format on