Fixed crash when event handler class isn't derived from StaticEventHandler

This commit is contained in:
alexey.lysiuk 2018-03-29 16:41:29 +03:00
parent 9feccac8cd
commit cbad9ac219

View file

@ -174,8 +174,9 @@ bool E_CheckHandler(DStaticEventHandler* handler)
bool E_IsStaticType(PClass* type) bool E_IsStaticType(PClass* type)
{ {
return (type->IsDescendantOf(RUNTIME_CLASS(DStaticEventHandler)) && // make sure it's from our hierarchy at all. assert(type != nullptr);
!type->IsDescendantOf(RUNTIME_CLASS(DEventHandler))); assert(type->IsDescendantOf(RUNTIME_CLASS(DStaticEventHandler)));
return !type->IsDescendantOf(RUNTIME_CLASS(DEventHandler));
} }
void E_SerializeEvents(FSerializer& arc) void E_SerializeEvents(FSerializer& arc)
@ -230,27 +231,24 @@ void E_SerializeEvents(FSerializer& arc)
} }
} }
static void E_InitStaticHandler(PClass* type, FString typestring, bool map) static PClass* E_GetHandlerClass(const FString& typeName)
{ {
PClass* type = PClass::FindClass(typeName);
if (type == nullptr) if (type == nullptr)
{ {
I_Error("Fatal: unknown event handler class %s in MAPINFO!\n", typestring.GetChars()); I_Error("Fatal: unknown event handler class %s", typeName.GetChars());
return; }
else if (!type->IsDescendantOf(RUNTIME_CLASS(DStaticEventHandler)))
{
I_Error("Fatal: event handler class %s is not derived from StaticEventHandler", typeName.GetChars());
} }
if (E_IsStaticType(type) && map) return type;
{
I_Error("Fatal: invalid event handler class %s in MAPINFO!\nMap-specific event handlers cannot be static.\n", typestring.GetChars());
return;
} }
/*
if (!E_IsStaticType(type) && !map)
{
Printf("%cGWarning: invalid event handler class %s in MAPINFO!\nMAPINFO event handlers should inherit Static* directly!\n", TEXTCOLOR_ESCAPE, typestring.GetChars());
return;
}*/
static void E_InitHandler(PClass* type)
{
// check if type already exists, don't add twice. // check if type already exists, don't add twice.
bool typeExists = false; bool typeExists = false;
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
@ -269,41 +267,34 @@ static void E_InitStaticHandler(PClass* type, FString typestring, bool map)
void E_InitStaticHandlers(bool map) void E_InitStaticHandlers(bool map)
{ {
// don't initialize map handlers if restoring from savegame.
if (savegamerestore) if (savegamerestore)
return; return;
// just make sure // just make sure
E_Shutdown(map); E_Shutdown(map);
if (map) // don't initialize map handlers if restoring from savegame. // initialize event handlers from gameinfo
for (const FString& typeName : gameinfo.EventHandlers)
{ {
// load non-static handlers from gameinfo PClass* type = E_GetHandlerClass(typeName);
for (unsigned int i = 0; i < gameinfo.EventHandlers.Size(); i++) // don't init the really global stuff here on startup initialization.
{ // don't init map-local global stuff here on level setup.
FString typestring = gameinfo.EventHandlers[i]; if (map == E_IsStaticType(type))
PClass* type = PClass::FindClass(typestring);
if (!type || E_IsStaticType(type)) // don't init the really global stuff here.
continue; continue;
E_InitStaticHandler(type, typestring, false); E_InitHandler(type);
} }
for (unsigned int i = 0; i < level.info->EventHandlers.Size(); i++) if (!map)
return;
// initialize event handlers from mapinfo
for (const FString& typeName : level.info->EventHandlers)
{ {
FString typestring = level.info->EventHandlers[i]; PClass* type = E_GetHandlerClass(typeName);
PClass* type = PClass::FindClass(typestring); if (E_IsStaticType(type))
E_InitStaticHandler(type, typestring, true); I_Error("Fatal: invalid event handler class %s in MAPINFO!\nMap-specific event handlers cannot be static.\n", typeName.GetChars());
} E_InitHandler(type);
}
else
{
for (unsigned int i = 0; i < gameinfo.EventHandlers.Size(); i++)
{
FString typestring = gameinfo.EventHandlers[i];
PClass* type = PClass::FindClass(typestring);
if (!type || !E_IsStaticType(type)) // don't init map-local global stuff here.
continue;
E_InitStaticHandler(type, typestring, false);
}
} }
} }