From 1bec0b1452f26acc60c71220371b543879e86485 Mon Sep 17 00:00:00 2001 From: TheRedDaemon <66257843+TheRedDaemon@users.noreply.github.com> Date: Fri, 19 Dec 2025 23:55:16 +0100 Subject: [PATCH 1/2] [TEMPLATES] change struct instance approach --- src/core/StructTest.h | 3 +++ src/core/ViewportRenderState.cpp | 12 ------------ src/precomp/StructResolver.h | 16 ++++++++-------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/core/StructTest.h b/src/core/StructTest.h index 25fe598ad7..fc9c53f954 100644 --- a/src/core/StructTest.h +++ b/src/core/StructTest.h @@ -3,3 +3,6 @@ #include "ViewportRenderState.h" MACRO_STRUCT_RESOLVER(ViewportRenderState, false, 0x100) ViewportRenderState_Struct; + +// in this case, it will just instantiate the struct, calling the constructor, but only if implemented +MACRO_STRUCT_INSTANCE(ViewportRenderState, 0x100); diff --git a/src/core/ViewportRenderState.cpp b/src/core/ViewportRenderState.cpp index 20146bf13a..cb6980603f 100644 --- a/src/core/ViewportRenderState.cpp +++ b/src/core/ViewportRenderState.cpp @@ -3,18 +3,6 @@ #include "windowslib.h" -// could also be placed in own file against the clutter -// PROBLEM: Needs definition of address twice, could however be helped if addresses are moved into own struct/file -MACRO_STRUCT_INSTANCE(ViewportRenderState, 0x100) -{ - // needs direct create return to properly work - // interestingly, "ViewportRenderState" here would be so big, - // a stack overflow would occur if it was not optimized to be no copy - // works with variables - // might work good together with the copy prevention - return ViewportRenderState(); -} - ViewportRenderState::ViewportRenderState() { MACRO_CALL_MEMBER(ViewportRenderState_Func::_constructor_, this)(); } // FUNCTION: STRONGHOLDCRUSADER 0x004e1fa0 diff --git a/src/precomp/StructResolver.h b/src/precomp/StructResolver.h index 3160ea3355..89309329c9 100644 --- a/src/precomp/StructResolver.h +++ b/src/precomp/StructResolver.h @@ -5,14 +5,10 @@ struct StructResolver { private: - template static T instance(); + template struct Instance; template struct InternalResolver; template struct InternalResolver { - private: - static T instance; - - public: static T* const ptr; }; template struct InternalResolver { @@ -44,9 +40,10 @@ struct StructResolver { template T* const StructResolver::InternalResolver::ptr = reinterpret_cast(gameAddress); + template -T StructResolver::InternalResolver::instance = StructResolver::instance(); -template T* const StructResolver::InternalResolver::ptr = &instance; +T* const StructResolver::InternalResolver::ptr + = &StructResolver::Instance::instance; template bool StructResolver::AddressUsageKeeper
::initialized = false; @@ -65,6 +62,9 @@ StructResolver::Resolver::Initializer::Initializer( typedef StructResolver::Resolver::Ptr #define MACRO_STRUCT_INSTANCE(STRUCT_TYPE, GAME_ADDRESS) \ - template <> STRUCT_TYPE StructResolver::instance() + template struct StructResolver::Instance { \ + static STRUCT_TYPE instance; \ + }; \ + template STRUCT_TYPE StructResolver::Instance::instance #endif // STRUCT_RESOLVER From abf741ab698285054f593edb06985dc08e4259d0 Mon Sep 17 00:00:00 2001 From: TheRedDaemon <66257843+TheRedDaemon@users.noreply.github.com> Date: Mon, 29 Dec 2025 22:40:50 +0100 Subject: [PATCH 2/2] [TEMPLATE] change instance concept to allow arrays --- src/core/StructTest.h | 2 +- src/precomp/StructResolver.h | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/StructTest.h b/src/core/StructTest.h index fc9c53f954..ab42e87845 100644 --- a/src/core/StructTest.h +++ b/src/core/StructTest.h @@ -5,4 +5,4 @@ MACRO_STRUCT_RESOLVER(ViewportRenderState, false, 0x100) ViewportRenderState_Struct; // in this case, it will just instantiate the struct, calling the constructor, but only if implemented -MACRO_STRUCT_INSTANCE(ViewportRenderState, 0x100); +MACRO_STRUCT_INSTANCE(0x100); diff --git a/src/precomp/StructResolver.h b/src/precomp/StructResolver.h index 89309329c9..8392b3beb4 100644 --- a/src/precomp/StructResolver.h +++ b/src/precomp/StructResolver.h @@ -5,7 +5,7 @@ struct StructResolver { private: - template struct Instance; + template struct Instance; template struct InternalResolver; template struct InternalResolver { @@ -61,10 +61,10 @@ StructResolver::Resolver::Initializer::Initializer( template struct StructResolver::Resolver; \ typedef StructResolver::Resolver::Ptr -#define MACRO_STRUCT_INSTANCE(STRUCT_TYPE, GAME_ADDRESS) \ - template struct StructResolver::Instance { \ - static STRUCT_TYPE instance; \ +#define MACRO_STRUCT_INSTANCE(GAME_ADDRESS) \ + template struct StructResolver::Instance { \ + static T instance; \ }; \ - template STRUCT_TYPE StructResolver::Instance::instance + template T StructResolver::Instance::instance #endif // STRUCT_RESOLVER