Fixed map/global static handlers; map handlers should not receive WorldLoaded/WorldUnloaded on load/save anymore.

This commit is contained in:
ZZYZX 2017-01-30 08:19:38 +02:00
parent 09ca1f610d
commit fd282d3001
2 changed files with 19 additions and 11 deletions

View file

@ -121,9 +121,9 @@ static void E_InitStaticHandler(PClass* type, FString typestring, bool map)
return; return;
} }
if (!E_IsStaticType(type) && !map) if (!E_IsStaticType(type))
{ {
Printf("%cGWarning: invalid event handler class %s in MAPINFO!\nGameInfo event handlers should inherit Static* directly!\n", TEXTCOLOR_ESCAPE, typestring.GetChars()); Printf("%cGWarning: invalid event handler class %s in MAPINFO!\nMAPINFO event handlers should inherit Static* directly!\n", TEXTCOLOR_ESCAPE, typestring.GetChars());
return; return;
} }
@ -140,6 +140,7 @@ static void E_InitStaticHandler(PClass* type, FString typestring, bool map)
if (typeExists) return; if (typeExists) return;
DStaticEventHandler* handler = (DStaticEventHandler*)type->CreateNew(); DStaticEventHandler* handler = (DStaticEventHandler*)type->CreateNew();
handler->isMapScope = map;
E_RegisterHandler(handler); E_RegisterHandler(handler);
} }
@ -150,6 +151,13 @@ void E_InitStaticHandlers(bool map)
if (map) // don't initialize map handlers if restoring from savegame. if (map) // don't initialize map handlers if restoring from savegame.
{ {
// delete old map static handlers if any.
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{
if (handler->IsStatic() && handler->isMapScope)
handler->Destroy();
}
for (unsigned int i = 0; i < level.info->EventHandlers.Size(); i++) for (unsigned int i = 0; i < level.info->EventHandlers.Size(); i++)
{ {
FString typestring = level.info->EventHandlers[i]; FString typestring = level.info->EventHandlers[i];
@ -162,7 +170,7 @@ void E_InitStaticHandlers(bool map)
// delete old static handlers if any. // delete old static handlers if any.
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{ {
if (handler->IsStatic()) if (handler->IsStatic() && !handler->isMapScope)
handler->Destroy(); handler->Destroy();
} }
@ -189,8 +197,8 @@ void E_WorldLoaded()
{ {
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{ {
if (handler->IsStatic()) continue; if (handler->IsStatic() && !handler->isMapScope) continue;
if (handler->isFromSaveGame) continue; // don't execute WorldLoaded for handlers loaded from the savegame. if (handler->isMapScope && savegamerestore) continue; // don't execute WorldLoaded for handlers loaded from the savegame.
handler->WorldLoaded(); handler->WorldLoaded();
} }
} }
@ -199,7 +207,7 @@ void E_WorldUnloading()
{ {
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{ {
if (handler->IsStatic()) continue; if (handler->IsStatic() && !handler->isMapScope) continue;
handler->WorldUnloading(); handler->WorldUnloading();
} }
} }
@ -208,7 +216,7 @@ void E_WorldLoadedUnsafe()
{ {
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{ {
if (!handler->IsStatic()) continue; if (!handler->IsStatic() || handler->isMapScope) continue;
handler->WorldLoaded(); handler->WorldLoaded();
} }
} }
@ -217,7 +225,7 @@ void E_WorldUnloadingUnsafe()
{ {
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next) for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
{ {
if (!handler->IsStatic()) continue; if (!handler->IsStatic() || handler->isMapScope) continue;
handler->WorldUnloading(); handler->WorldUnloading();
} }
} }

View file

@ -39,12 +39,12 @@ public:
{ {
prev = 0; prev = 0;
next = 0; next = 0;
isFromSaveGame = false; isMapScope = false;
} }
DStaticEventHandler* prev; DStaticEventHandler* prev;
DStaticEventHandler* next; DStaticEventHandler* next;
bool isFromSaveGame; // this gets set to true if this object was received using serializator bool isMapScope;
virtual bool IsStatic() { return true; } virtual bool IsStatic() { return true; }
// serialization handler. let's keep it here so that I don't get lost in serialized/not serialized fields // serialization handler. let's keep it here so that I don't get lost in serialized/not serialized fields
@ -54,7 +54,7 @@ public:
if (arc.isReading()) if (arc.isReading())
{ {
Printf("DStaticEventHandler::Serialize: reading object %s\n", GetClass()->TypeName.GetChars()); Printf("DStaticEventHandler::Serialize: reading object %s\n", GetClass()->TypeName.GetChars());
isFromSaveGame = true; isMapScope = true; // unserialized static handler means map scope anyway. other handlers don't get serialized.
} }
else else
{ {