Skip to content

Commit 32de795

Browse files
committed
more map cleanup impl
1 parent f4aa2e7 commit 32de795

6 files changed

Lines changed: 57 additions & 15 deletions

File tree

‎Game.Server/AI_DynamicLink.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,18 @@ public static void ResetDynamicLinks() {
273273
}
274274
}
275275

276+
public static void PurgeDynamicLinks() {
277+
AI_DynamicLink? dynamicLink = AllDynamicLinks;
278+
279+
while (dynamicLink != null) {
280+
// todo: Editor_DeleteNodeLink
281+
if (!dynamicLink.IsLinkValid())
282+
Util.Remove(dynamicLink);
283+
284+
dynamicLink = dynamicLink.NextDynamicLink;
285+
}
286+
}
287+
276288
public bool IsLinkValid() {
277289
AI_Node node = g_pBigAINet!.GetNode(SrcID)!;
278290
return !Unsafe.IsNullRef(ref node.GetLink(DestID));

‎Game.Server/BaseEntity.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ public virtual bool IsViewable() {
934934
}
935935

936936
public virtual void UpdateOnRemove() {
937+
Util.g_bRemovingEntity++;
937938
Util.g_bReceivedChainedUpdateOnRemove = true;
938939

939940
#if GMOD_DLL
@@ -1013,6 +1014,7 @@ public virtual void UpdateOnRemove() {
10131014
modelinfo.ReleaseDynamicModel(ModelIndex); // no-op if not dynamic
10141015
ModelIndex = -1;
10151016
}
1017+
Util.g_bRemovingEntity--;
10161018
}
10171019

10181020
public string GetDebugName() {
@@ -3143,6 +3145,7 @@ public static void PrecacheModelComponents(int modelIndex) {
31433145
/// see CBaseEntity::~CBaseEntity() etc
31443146
/// </summary>
31453147
public virtual void Term() {
3148+
Util.g_bRemovingEntity++;
31463149
VPhysicsDestroyObject();
31473150

31483151
// Need to remove references to this entity before EHANDLES go null
@@ -3163,6 +3166,7 @@ public virtual void Term() {
31633166
LuaTableObject?.UnReference();
31643167
LuaTableObject = null;
31653168
LuaCalcAbsolutePosition.UnReference();
3169+
Util.g_bRemovingEntity--;
31663170

31673171
CollisionProp().DestroyPartitionHandle();
31683172

‎Game.Server/Util.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ public static void EmitAmbientSound(int entindex, in Vector3 vecOrigin, ReadOnly
344344
}
345345

346346
public static bool g_bDisableEhandleAccess = false;
347+
public static int g_bRemovingEntity = 0;
347348
public static bool g_bReceivedChainedUpdateOnRemove = false;
348349
public static void LogPrintf(ReadOnlySpan<char> text) {
349350
engine.LogPrint(text);

‎Game.Shared/GarrysMod/GMODGameRules.cs‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -878,16 +878,19 @@ static bool CleanupFilterContains(ILuaObject? filters, ReadOnlySpan<char> classN
878878

879879
ILuaInterface lua = g_Lua!;
880880
filters.Push();
881-
lua.PushNil();
882-
while (lua.Next(-2)) {
883-
if (lua.GetString(-1) is string value && className.SequenceEqual(value)) {
884-
lua.Pop(3);
881+
for (int i = 1; ; i++) {
882+
lua.PushNumber(i);
883+
lua.GetTable(-2);
884+
if (lua.GetType(-1) != LuaType.String) {
885+
lua.Pop(2);
886+
return false;
887+
}
888+
if (strcmp(lua.GetString(-1), className) == 0) {
889+
lua.Pop(2);
885890
return true;
886891
}
887892
lua.Pop(1);
888893
}
889-
lua.Pop(1);
890-
return false;
891894
}
892895

893896
sealed class GMODMapEntityFilter(ILuaObject? filters) : IMapEntityFilter
@@ -917,8 +920,8 @@ public bool ShouldCreateEntity(ReadOnlySpan<char> className) {
917920
public void CleanUpMap(ILuaObject? filters) {
918921
BaseEntity? ent = gEntList.FirstEnt();
919922
AI_DynamicLink.gm_bInitialized = false;
920-
// todo: AI_DynamicLink.PurgeDynamicLinks();
921-
// todo: AI_DynamicLink.ResetStates();
923+
AI_DynamicLink.PurgeDynamicLinks();
924+
AI_DynamicLink.ResetDynamicLinks();
922925

923926
CleaningUpMap = true;
924927
while (ent != null) {
@@ -943,7 +946,7 @@ public void CleanUpMap(ILuaObject? filters) {
943946
MapEntity_ParseAllEntities(engine.GetMapEntitiesString(), new GMODMapEntityFilter(filters), true);
944947

945948
AI_DynamicLink.InitDynamicLinks();
946-
// todo: unidentified reset call that follows InitDynamicLinks
949+
Game.Server.NavMesh.NavMesh.Instance?.Load();
947950
}
948951
#endif
949952
}

‎Game.Shared/GarrysMod/LuaGame.cs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ static int SetGlobalCounter(ILuaInterface lua) {
149149

150150
[LuaFunction]
151151
static int CleanUpMap(ILuaInterface lua) {
152-
// todo: refuse with "Cannot run game.CleanUpMap while deleting entities!\n" while entities are being deleted
152+
if (Util.g_bRemovingEntity > 0 || Util.g_bDisableEhandleAccess) {
153+
lua.ErrorFromLua("Cannot run game.CleanUpMap while deleting entities!\n");
154+
return 0;
155+
}
156+
153157
if (MapCleanupRunning || MapCleanupPending) {
154158
lua.ErrorFromLua("Cannot run game.CleanUpMap while it is already running!\n");
155159
return 0;
@@ -198,13 +202,18 @@ static void DoCleanUpMap(bool dontSendToClients, ILuaObject filters) {
198202
GarrysMod.BroadcastLua(filter, "game.CleanUpMap()");
199203
}
200204

201-
// todo: unidentified step between the client broadcast and AllowImmediateEdictReuse
205+
// todo: s_RagdollLRU.RemoveAll()
202206
engine.AllowImmediateEdictReuse();
203207

204208
gGM?.Call((int)LUA_POOLEDSTRING.PostCleanupMap);
205209
MapCleanupRunning = false;
206210
}
207211

212+
public static void ResetMapCleanupSchedule() {
213+
MapCleanupPending = false;
214+
MapCleanupDontSendToClients = false;
215+
}
216+
208217
public static void RunMapCleanupIfNeeded() {
209218
if (!MapCleanupPending)
210219
return;
@@ -259,12 +268,17 @@ static void DoCleanUpMap() {
259268
MapCleanupRunning = true;
260269
gGM?.Call((int)LUA_POOLEDSTRING.PreCleanupMap);
261270

262-
// todo: remove the Lua-created clientside entities
271+
// todo: C_PhysPropClientside.RecreateAll()
263272
engine.ClientCmd("r_cleardecals\n");
264273
enginesound.StopAllSounds(true);
265-
// todo: unidentified call that follows StopAllSounds
266-
// todo: release every C_Hairball in the client entity list
267-
// todo: unidentified step before the PostCleanupMap hook
274+
Soundscape_OnStopAllSounds();
275+
List<C_BaseEntity> hairballs = [];
276+
for (C_BaseEntity? ent = cl_entitylist.FirstBaseEntity(); ent != null; ent = cl_entitylist.NextBaseEntity(ent))
277+
if (!ent.IsEFlagSet(EFL.KillMe) && strcmp(ent.GetClassname(), "class C_Hairball") == 0)
278+
hairballs.Add(ent);
279+
foreach (C_BaseEntity ent in hairballs)
280+
ent.Release();
281+
// todo: s_RagdollLRU.RemoveAll()
268282

269283
gGM?.Call((int)LUA_POOLEDSTRING.PostCleanupMap);
270284
MapCleanupRunning = false;

‎Game.Shared/GarrysMod/LuaGameSystem.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,15 @@ public override bool Init() {
128128
return true;
129129
}
130130

131+
#if GAME_DLL
132+
public override void LevelShutdownPostEntity() {
133+
// todo: EmptyLoopingSounds();
134+
LuaGame.ResetMapCleanupSchedule();
135+
// todo: RemoveQueuedRecipientFilters();
136+
}
137+
#else
131138
// todo LevelShutdownPostEntity
139+
#endif
132140

133141
public void FireGameEvent(IGameEvent ev) {
134142
#if CLIENT_DLL

0 commit comments

Comments
 (0)