Add single Event_UnregisterAll method, instead of having code everywhere calling Event_UnregisterXYZ

This commit is contained in:
UnknownShadow200 2020-07-29 11:48:30 +10:00
parent 9afde7d0aa
commit fd28ce7b27
18 changed files with 71 additions and 97 deletions

View File

@ -385,13 +385,7 @@ static void OnInit(void) {
Event_RegisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged);
}
static void OnFree(void) {
Animations_Clear();
Event_UnregisterVoid(&TextureEvents.PackChanged, NULL, OnPackChanged);
Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged);
}
struct IGameComponent Animations_Component = {
OnInit, /* Init */
OnFree /* Free */
OnInit, /* Init */
Animations_Clear /* Free */
};

View File

@ -977,7 +977,6 @@ static void OnFree(void) {
Sounds_Free();
Waitable_Free(music_waitable);
Audio_SysFree();
Event_UnregisterBlock(&UserEvents.BlockChanged, NULL, Audio_PlayBlockSound);
}
#endif

View File

@ -71,10 +71,7 @@ static void OnInit(void) {
Event_RegisterVoid(&GfxEvents.ContextLost, NULL, OnContextLost);
}
static void OnFree(void) {
OnContextLost(NULL);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, OnContextLost);
}
static void OnFree(void) { OnContextLost(NULL); }
struct IGameComponent AxisLinesRenderer_Component = {
OnInit, /* Init */

View File

@ -705,12 +705,8 @@ static void OnInit(void) {
Blocks.CanPlace[BLOCK_BEDROCK] = false; Blocks.CanDelete[BLOCK_BEDROCK] = false;
}
static void OnFree(void) {
Event_UnregisterVoid(&TextureEvents.AtlasChanged, NULL, OnAtlasChanged);
}
struct IGameComponent Blocks_Component = {
OnInit, /* Init */
OnFree, /* Free */
NULL, /* Free */
OnReset, /* Reset */
};

View File

@ -681,7 +681,6 @@ static void OnInit(void) {
static void OnFree(void) {
FreeFontBitmap();
fontBitmap.scan0 = NULL;
Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged);
}
struct IGameComponent Drawer2D_Component = {

View File

@ -1151,9 +1151,6 @@ static void Entities_Free(void) {
if (!Entities.List[i]) continue;
Entities_Remove((EntityID)i);
}
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, Entities_ContextLost);
Event_UnregisterVoid(&ChatEvents.FontChanged, NULL, Entities_ChatFontChanged);
Gfx_DeleteTexture(&ShadowComponent_ShadowTex);
}

View File

@ -888,15 +888,6 @@ static void OnInit(void) {
}
static void OnFree(void) {
Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged);
Event_UnregisterVoid(&TextureEvents.PackChanged, NULL, OnTexturePackChanged);
Event_UnregisterVoid(&TextureEvents.AtlasChanged, NULL, OnTerrainAtlasChanged);
Event_UnregisterVoid(&GfxEvents.ViewDistanceChanged, NULL, OnViewDistanceChanged);
Event_UnregisterInt(&WorldEvents.EnvVarChanged, NULL, OnEnvVariableChanged);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, OnContextLost);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
OnContextLost(NULL);
Mem_Free(Weather_Heightmap);
Weather_Heightmap = NULL;

View File

@ -50,6 +50,63 @@ void Event_Unregister(struct Event_Void* handlers, void* obj, Event_Void_Callbac
Logger_Abort("Attempt to unregister event handler that was not registered to begin with");
}
void Event_UnregisterAll(void) {
EntityEvents.Added.Count = 0;
EntityEvents.Removed.Count = 0;
TabListEvents.Added.Count = 0;
TabListEvents.Changed.Count = 0;
TabListEvents.Removed.Count = 0;
TextureEvents.AtlasChanged.Count = 0;
TextureEvents.PackChanged.Count = 0;
TextureEvents.FileChanged.Count = 0;
GfxEvents.ViewDistanceChanged.Count = 0;
GfxEvents.LowVRAMDetected.Count = 0;
GfxEvents.ProjectionChanged.Count = 0;
GfxEvents.ContextLost.Count = 0;
GfxEvents.ContextRecreated.Count = 0;
UserEvents.BlockChanged.Count = 0;
UserEvents.HackPermissionsChanged.Count = 0;
UserEvents.HeldBlockChanged.Count = 0;
BlockEvents.PermissionsChanged.Count = 0;
BlockEvents.BlockDefChanged.Count = 0;
WorldEvents.NewMap.Count = 0;
WorldEvents.Loading.Count = 0;
WorldEvents.MapLoaded.Count = 0;
WorldEvents.EnvVarChanged.Count = 0;
ChatEvents.FontChanged.Count = 0;
ChatEvents.ChatReceived.Count = 0;
ChatEvents.ChatSending.Count = 0;
ChatEvents.ColCodeChanged.Count = 0;
WindowEvents.Redraw.Count = 0;
WindowEvents.Resized.Count = 0;
WindowEvents.Closing.Count = 0;
WindowEvents.FocusChanged.Count = 0;
WindowEvents.StateChanged.Count = 0;
WindowEvents.Created.Count = 0;
InputEvents.Press.Count = 0;
InputEvents.Down.Count = 0;
InputEvents.Up.Count = 0;
InputEvents.Wheel.Count = 0;
InputEvents.TextChanged.Count = 0;
PointerEvents.Moved.Count = 0;
PointerEvents.Down.Count = 0;
PointerEvents.Up.Count = 0;
PointerEvents.RawMoved.Count = 0;
NetEvents.Connected.Count = 0;
NetEvents.Disconnected.Count = 0;
}
void Event_RaiseVoid(struct Event_Void* handlers) {
int i;
for (i = 0; i < handlers->Count; i++) {

View File

@ -135,6 +135,9 @@ void Event_RaiseRawMove(struct Event_RawMove* handlers, float xDelta, float yDel
#define Event_RegisterRawMove(handlers, obj, handler) Event_RegisterMacro(handlers, obj, handler)
#define Event_UnregisterRawMove(handlers, obj, handler) Event_UnregisterMacro(handlers, obj, handler)
/* NOTE: Event_UnregisterAll must be updated if events lists are changed */
void Event_UnregisterAll(void);
CC_VAR extern struct _EntityEventsList {
struct Event_Int Added; /* Entity is spawned in the current world */
struct Event_Int Removed; /* Entity is despawned from the current world */

View File

@ -610,13 +610,7 @@ void Game_Free(void* obj) {
/* Most components will call OnContextLost in their Free functions */
/* Set to false so components will always free managed textures too */
Gfx.ManagedTextures = false;
Event_UnregisterVoid(&WorldEvents.NewMap, NULL, HandleOnNewMap);
Event_UnregisterVoid(&WorldEvents.MapLoaded, NULL, HandleOnNewMapLoaded);
Event_UnregisterVoid(&GfxEvents.LowVRAMDetected, NULL, HandleLowVRAMDetected);
Event_UnregisterVoid(&WindowEvents.Resized, NULL, Game_OnResize);
Event_UnregisterVoid(&WindowEvents.Closing, NULL, Game_Free);
Event_UnregisterAll();
for (comp = comps_head; comp; comp = comp->next) {
if (comp->Free) comp->Free();

View File

@ -421,15 +421,6 @@ static void OnReset(void) {
}
static void OnFree(void) {
Event_UnregisterVoid(&ChatEvents.FontChanged, NULL, OnFontChanged);
Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, OnContextLost);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
Event_UnregisterInt(&InputEvents.Press, NULL, OnKeyPress);
#ifdef CC_BUILD_TOUCH
Event_UnregisterString(&InputEvents.TextChanged, NULL, OnTextChanged);
#endif
while (Gui_ScreensCount) Gui_Remove(Gui_Screens[0]);
OnContextLost(NULL);

View File

@ -245,13 +245,6 @@ static void OnInit(void) {
Event_RegisterBlock(&UserEvents.BlockChanged, NULL, HeldBlockRenderer_BlockChanged);
}
static void OnFree(void) {
Event_UnregisterVoid(&GfxEvents.ProjectionChanged, NULL, HeldBlockRenderer_ProjectionChanged);
Event_UnregisterVoid(&UserEvents.HeldBlockChanged, NULL, HeldBlockRenderer_DoSwitchBlockAnim);
Event_UnregisterBlock(&UserEvents.BlockChanged, NULL, HeldBlockRenderer_BlockChanged);
}
struct IGameComponent HeldBlockRenderer_Component = {
OnInit, /* Init */
OnFree /* Free */
OnInit /* Init */
};

View File

@ -791,21 +791,9 @@ static void OnInit(void) {
CalcViewDists();
}
static void OnFree(void) {
Event_UnregisterVoid(&TextureEvents.AtlasChanged, NULL, OnTerrainAtlasChanged);
Event_UnregisterInt(&WorldEvents.EnvVarChanged, NULL, OnEnvVariableChanged);
Event_UnregisterVoid(&BlockEvents.BlockDefChanged, NULL, OnBlockDefinitionChanged);
Event_UnregisterVoid(&GfxEvents.ViewDistanceChanged, NULL, OnVisibilityChanged);
Event_UnregisterVoid(&GfxEvents.ProjectionChanged, NULL, OnVisibilityChanged);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, DeleteChunks_);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, Refresh_);
OnNewMap();
}
struct IGameComponent MapRenderer_Component = {
OnInit, /* Init */
OnFree, /* Free */
OnNewMap, /* Free */
OnNewMap, /* Reset */
OnNewMap, /* OnNewMap */
OnNewMapLoaded /* OnNewMapLoaded */

View File

@ -2135,10 +2135,6 @@ static void OnInit(void) {
static void OnFree(void) {
Models_ContextLost(NULL);
CustomModel_FreeAll();
Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, Models_TextureChanged);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, Models_ContextLost);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, Models_ContextRecreated);
}
static void OnReset(void) { CustomModel_FreeAll(); }

View File

@ -608,14 +608,7 @@ static void OnInit(void) {
Event_RegisterVoid(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
}
static void OnFree(void) {
OnContextLost(NULL);
Event_UnregisterBlock(&UserEvents.BlockChanged, NULL, OnBreakBlockEffect_Handler);
Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, OnContextLost);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
}
static void OnFree(void) { OnContextLost(NULL); }
static void OnReset(void) { rain_count = 0; terrain_count = 0; custom_count = 0; }

View File

@ -118,11 +118,7 @@ static void OnInit(void) {
Event_RegisterVoid(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
}
static void OnFree(void) {
OnContextLost(NULL);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, OnContextLost);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
}
static void OnFree(void) { OnContextLost(NULL); }
struct IGameComponent PickedPosRenderer_Component = {
OnInit, /* Init */

View File

@ -219,15 +219,9 @@ static void OnInit(void) {
Event_RegisterVoid(&GfxEvents.ContextRecreated, NULL, Selections_ContextRecreated);
}
static void OnReset(void) {
selections_count = 0;
}
static void OnReset(void) { selections_count = 0; }
static void OnFree(void) {
Selections_ContextLost(NULL);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, Selections_ContextLost);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, Selections_ContextRecreated);
}
static void OnFree(void) { Selections_ContextLost(NULL); }
struct IGameComponent Selections_Component = {
OnInit, /* Init */

View File

@ -451,10 +451,6 @@ static void OnInit(void) {
}
static void OnFree(void) {
Event_UnregisterEntry(&TextureEvents.FileChanged, NULL, OnFileChanged);
Event_UnregisterVoid(&GfxEvents.ContextLost, NULL, OnContextLost);
Event_UnregisterVoid(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
OnContextLost(NULL);
Atlas2D_Free();
}