From efb1e5d33abb340e19ec4ca4271636ace38d0215 Mon Sep 17 00:00:00 2001 From: ZZYZX Date: Sun, 22 Jan 2017 09:58:48 +0200 Subject: [PATCH] Implemented global EventHandlers in MAPINFO --- src/events.cpp | 68 ++++++++++++++++++++++++++++--------------------- src/g_level.cpp | 2 ++ src/gi.cpp | 1 + src/gi.h | 1 + src/info.cpp | 4 +++ src/p_spec.cpp | 2 -- 6 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/events.cpp b/src/events.cpp index 8927649f4..504b2667f 100755 --- a/src/events.cpp +++ b/src/events.cpp @@ -2,6 +2,7 @@ #include "virtual.h" #include "r_utility.h" #include "g_levellocals.h" +#include "gi.h" #include "v_text.h" DStaticEventHandler* E_FirstEventHandler = nullptr; @@ -56,6 +57,37 @@ bool E_IsStaticType(PClass* type) !type->IsDescendantOf(RUNTIME_CLASS(DRenderEventHandler))); } +static void E_InitStaticHandler(PClass* type, FString typestring, bool map) +{ + if (type == nullptr) + { + Printf("%cGWarning: unknown event handler class %s in MAPINFO!", TEXTCOLOR_ESCAPE, typestring.GetChars()); + return; + } + + if (!E_IsStaticType(type)) + { + Printf("%cGWarning: invalid event handler class %s in MAPINFO!\nMAPINFO event handlers should inherit Static* directly!", TEXTCOLOR_ESCAPE, typestring.GetChars()); + return; + } + + // check if type already exists, don't add twice. + bool typeExists = false; + for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) + { + if (handler->IsA(type)) + { + typeExists = true; + break; + } + } + + if (typeExists) return; + DStaticEventHandler* handler = (DStaticEventHandler*)type->CreateNew(); + handler->isMapScope = map; + E_RegisterHandler(handler); +} + void E_InitStaticHandlers(bool map) { // remove existing @@ -72,39 +104,17 @@ void E_InitStaticHandlers(bool map) { FString typestring = level.info->EventHandlers[i]; PClass* type = PClass::FindClass(typestring); - - if (type == nullptr) - { - Printf("%cGWarning: unknown event handler class %s in MAPINFO!", TEXTCOLOR_ESCAPE, typestring.GetChars()); - continue; - } - - if (!E_IsStaticType(type)) - { - Printf("%cGWarning: invalid event handler class %s in MAPINFO!\nMAPINFO event handlers should inherit Static* directly!", TEXTCOLOR_ESCAPE, typestring.GetChars()); - continue; - } - - // check if type already exists, don't add twice. - bool typeExists = false; - for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) - { - if (handler->IsA(type)) - { - typeExists = true; - break; - } - } - - if (typeExists) continue; - DStaticEventHandler* handler = (DStaticEventHandler*)type->CreateNew(); - handler->isMapScope = true; - E_RegisterHandler(handler); + E_InitStaticHandler(type, typestring, true); } } else { - + for (unsigned int i = 0; i < gameinfo.EventHandlers.Size(); i++) + { + FString typestring = gameinfo.EventHandlers[i]; + PClass* type = PClass::FindClass(typestring); + E_InitStaticHandler(type, typestring, false); + } } } diff --git a/src/g_level.cpp b/src/g_level.cpp index 8ab1f2d48..ec4e3049e 100644 --- a/src/g_level.cpp +++ b/src/g_level.cpp @@ -1068,6 +1068,8 @@ void G_DoLoadLevel (int position, bool autosave) StatusBar->AttachToPlayer (&players[consoleplayer]); // [ZZ] init per-map static handlers E_InitStaticHandlers(true); + // call map load hook + E_MapLoaded(); P_DoDeferedScripts (); // [RH] Do script actions that were triggered on another map. if (demoplayback || oldgs == GS_STARTUP || oldgs == GS_TITLELEVEL) diff --git a/src/gi.cpp b/src/gi.cpp index 8d8ff9da7..b1e0b1cf2 100644 --- a/src/gi.cpp +++ b/src/gi.cpp @@ -317,6 +317,7 @@ void FMapInfoParser::ParseGameInfo() GAMEINFOKEY_STRINGARRAY(PrecachedClasses, "precacheclasses", 0, false) GAMEINFOKEY_STRINGARRAY(PrecachedTextures, "precachetextures", 0, false) GAMEINFOKEY_STRINGARRAY(PrecachedSounds, "precachesounds", 0, false) + GAMEINFOKEY_STRINGARRAY(EventHandlers, "eventhandlers", 0, false) GAMEINFOKEY_STRING(PauseSign, "pausesign") GAMEINFOKEY_STRING(quitSound, "quitSound") GAMEINFOKEY_STRING(BorderFlat, "borderFlat") diff --git a/src/gi.h b/src/gi.h index bd5e84363..1bab4e9a7 100644 --- a/src/gi.h +++ b/src/gi.h @@ -124,6 +124,7 @@ struct gameinfo_t TArray PrecachedClasses; TArray PrecachedTextures; TArray PrecachedSounds; + TArray EventHandlers; FString titleMusic; int titleOrder; diff --git a/src/info.cpp b/src/info.cpp index a424c890f..4167eea04 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -54,6 +54,7 @@ #include "thingdef.h" #include "d_player.h" #include "doomerrors.h" +#include "events.h" extern void LoadActors (); extern void InitBotStuff(); @@ -214,6 +215,9 @@ void PClassActor::StaticInit() ClearStrifeTypes(); LoadActors (); InitBotStuff(); + + // reinit GLOBAL static stuff from gameinfo, once classes are loaded. + E_InitStaticHandlers(false); } //========================================================================== diff --git a/src/p_spec.cpp b/src/p_spec.cpp index 89f920b2f..0eb619723 100644 --- a/src/p_spec.cpp +++ b/src/p_spec.cpp @@ -1443,8 +1443,6 @@ void P_SpawnSpecials (void) break; } } - // [ZZ] Loading event hook - E_MapLoaded(); // [RH] Start running any open scripts on this map FBehavior::StaticStartTypedScripts (SCRIPT_Open, NULL, false); }