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)
{
return (type->IsDescendantOf(RUNTIME_CLASS(DStaticEventHandler)) && // make sure it's from our hierarchy at all.
!type->IsDescendantOf(RUNTIME_CLASS(DEventHandler)));
assert(type != nullptr);
assert(type->IsDescendantOf(RUNTIME_CLASS(DStaticEventHandler)));
return !type->IsDescendantOf(RUNTIME_CLASS(DEventHandler));
}
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)
{
I_Error("Fatal: unknown event handler class %s in MAPINFO!\n", typestring.GetChars());
return;
I_Error("Fatal: unknown event handler class %s", typeName.GetChars());
}
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)
{
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;
}*/
return type;
}
static void E_InitHandler(PClass* type)
{
// check if type already exists, don't add twice.
bool typeExists = false;
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)
{
// don't initialize map handlers if restoring from savegame.
if (savegamerestore)
return;
// just make sure
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
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 the really global stuff here.
continue;
E_InitStaticHandler(type, typestring, false);
}
for (unsigned int i = 0; i < level.info->EventHandlers.Size(); i++)
{
FString typestring = level.info->EventHandlers[i];
PClass* type = PClass::FindClass(typestring);
E_InitStaticHandler(type, typestring, true);
}
PClass* type = E_GetHandlerClass(typeName);
// don't init the really global stuff here on startup initialization.
// don't init map-local global stuff here on level setup.
if (map == E_IsStaticType(type))
continue;
E_InitHandler(type);
}
else
if (!map)
return;
// initialize event handlers from mapinfo
for (const FString& typeName : level.info->EventHandlers)
{
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);
}
PClass* type = E_GetHandlerClass(typeName);
if (E_IsStaticType(type))
I_Error("Fatal: invalid event handler class %s in MAPINFO!\nMap-specific event handlers cannot be static.\n", typeName.GetChars());
E_InitHandler(type);
}
}