From b9ba2807e27ab535afd7006ef3ea1ea5b42c8ee4 Mon Sep 17 00:00:00 2001 From: Martin Taibr Date: Sun, 11 Nov 2018 21:01:02 +0100 Subject: [PATCH] move deglob stuff to it's own file --- qcsrc/dpdefs/csprogsdefs.qh | 4 +-- qcsrc/dpdefs/post.qh | 70 ------------------------------------ qcsrc/lib/_all.inc | 1 + qcsrc/lib/deglobalization.qh | 56 +++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 72 deletions(-) create mode 100644 qcsrc/lib/deglobalization.qh diff --git a/qcsrc/dpdefs/csprogsdefs.qh b/qcsrc/dpdefs/csprogsdefs.qh index 267737c2c..0909ede7a 100644 --- a/qcsrc/dpdefs/csprogsdefs.qh +++ b/qcsrc/dpdefs/csprogsdefs.qh @@ -44,5 +44,5 @@ .void(entity this, entity actor, entity trigger) use; #define touch move_touch -void(vector dir) vectorvectors_broken = #432; -#define vectorvectors DO_NOT_USE_GLOBALS +void(vector dir) _vectorvectors_hidden = #432; +#define vectorvectors DO_NOT_USE_GLOBALS_PREFER_VECTOR_VECTORS_MACRO_INSTEAD diff --git a/qcsrc/dpdefs/post.qh b/qcsrc/dpdefs/post.qh index c33695e73..70e5f3784 100644 --- a/qcsrc/dpdefs/post.qh +++ b/qcsrc/dpdefs/post.qh @@ -16,73 +16,3 @@ #else #define NULL (RVALUE, world) #endif - -#include "lib/accumulate.qh" -#include "lib/misc.qh" -#include "lib/static.qh" -#include "lib/vector.qh" - -//void(vector) _vectorvectors; - -#ifdef GAMEQC -STATIC_INIT(globals) { - //_vectorvectors = vectorvectors; - - // set to NaN to more easily detect uninitialized use - v_forward = VEC_NAN; - v_right = VEC_NAN; - v_up = VEC_NAN; -} - -/// Same as the `makevectors` builtin but uses the provided locals instead of the `v_*` globals. -/// Always use this instead of raw `makevectors` to make the data flow clear. -/// It's 2018, they even teach that globals are bad at my uni... though for some reason they never explained why. Sigh. -#define MAKEVECTORS(angles, forward, right, up) MACRO_BEGIN { \ - makevectors(angles); \ - forward = v_forward; \ - right = v_right; \ - up = v_up; \ - v_forward = VEC_NAN; \ - v_right = VEC_NAN; \ - v_up = VEC_NAN; \ -} MACRO_END - -// Same as `MAKEVECTORS` but also creates the locals for convenience. -#define MAKEVECTORS_NEW(angles, forward, right, up) \ - vector forward = '0 0 0'; \ - vector right = '0 0 0'; \ - vector up = '0 0 0'; \ - MAKEVECTORS(angles, forward, right, up); - -#define VECTOR_VECTORS(forward_in, forward, right, up) MACRO_BEGIN { \ - vectorvectors_broken(forward_in); \ - forward = v_forward; \ - right = v_right; \ - up = v_up; \ - v_forward = VEC_NAN; \ - v_right = VEC_NAN; \ - v_up = VEC_NAN; \ -} MACRO_END - -#define VECTOR_VECTORS_NEW(forward_in, forward, right, up) \ - vector forward = '0 0 0'; \ - vector right = '0 0 0'; \ - vector up = '0 0 0'; \ - VECTOR_VECTORS(forward_in, forward, right, up); - -#define vectorvectors DO_NOT_USE_GLOBALS - -// FIXME find a good place for this -// FIXME MAKE_VECTORS because current naming sucks -// FIXME ban vectorvectors - -// TODO when raw makevectors and similar functions are not used anywhere else anymore, -// assert that the global vectors are NaN before calling makevectors in MAKEVECTORS -// to make sure nobody (even builtins) is accidentally using them - NaN is the most liekly value to expose values clearly -// also uncomment these: -//#define makevectors DO_NOT_USE_GLOBALS -//#define v_forward DO_NOT_USE_GLOBALS -//#define v_right DO_NOT_USE_GLOBALS -//#define v_up DO_NOT_USE_GLOBALS -// FIXME ^ won't work -#endif diff --git a/qcsrc/lib/_all.inc b/qcsrc/lib/_all.inc index 0bed40bbf..cd35c3436 100644 --- a/qcsrc/lib/_all.inc +++ b/qcsrc/lib/_all.inc @@ -112,6 +112,7 @@ #include "counting.qh" #include "cvar.qh" #include "defer.qh" +#include "deglobalization.qh" #include "draw.qh" #include "enumclass.qh" #include "file.qh" diff --git a/qcsrc/lib/deglobalization.qh b/qcsrc/lib/deglobalization.qh new file mode 100644 index 000000000..ac37f7c9f --- /dev/null +++ b/qcsrc/lib/deglobalization.qh @@ -0,0 +1,56 @@ +#include "lib/misc.qh" +#include "lib/static.qh" +#include "lib/vector.qh" + +// These macros wrap functions which use globals so mutation only occurs inside them and is not visible from outside. +// Functions for which all usages are replaced with these macros can be hidden inside our `*defs.qh` files +// to prevent anyone from using them accidentally. + +// FIXME MAKE_VECTORS because current naming sucks + +#ifdef GAMEQC +STATIC_INIT(globals) { + // set to NaN to more easily detect uninitialized use + // TODO when all functions are wrapped and the raw functions are not used anymore, + // assert that the global vectors are NaN before calling the raw functions + // to make sure nobody (even builtins) is accidentally using them - NaN is the most likely value to expose remaining usages + v_forward = VEC_NAN; + v_right = VEC_NAN; + v_up = VEC_NAN; +} +#endif + +/// Same as the `makevectors` builtin but uses the provided locals instead of the `v_*` globals. +/// Always use this instead of raw `makevectors` to make the data flow clear. +#define MAKEVECTORS(angles, forward, right, up) MACRO_BEGIN { \ + makevectors(angles); \ + forward = v_forward; \ + right = v_right; \ + up = v_up; \ + v_forward = VEC_NAN; \ + v_right = VEC_NAN; \ + v_up = VEC_NAN; \ +} MACRO_END + +/// Same as `MAKEVECTORS` but also creates the locals for convenience. +#define MAKEVECTORS_NEW(angles, forward, right, up) \ + vector forward = '0 0 0'; \ + vector right = '0 0 0'; \ + vector up = '0 0 0'; \ + MAKEVECTORS(angles, forward, right, up); + +#define VECTOR_VECTORS(forward_in, forward, right, up) MACRO_BEGIN { \ + _vectorvectors_hidden(forward_in); \ + forward = v_forward; \ + right = v_right; \ + up = v_up; \ + v_forward = VEC_NAN; \ + v_right = VEC_NAN; \ + v_up = VEC_NAN; \ +} MACRO_END + +#define VECTOR_VECTORS_NEW(forward_in, forward, right, up) \ + vector forward = '0 0 0'; \ + vector right = '0 0 0'; \ + vector up = '0 0 0'; \ + VECTOR_VECTORS(forward_in, forward, right, up); -- 2.39.2