Skip to content

Commit 6968bfa

Browse files
Add OnAdd, OnRemove to BaseObjectPool and improve mock id and ptr free
1 parent 843b404 commit 6968bfa

File tree

7 files changed

+61
-1
lines changed

7 files changed

+61
-1
lines changed

api/AltV.Net.Mock/MockBlipPool.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ public class MockBlipPool : BlipPool
88
public MockBlipPool(IBaseObjectFactory<IBlip> blipFactory) : base(blipFactory)
99
{
1010
}
11+
12+
public override void OnRemove(IBlip entity)
13+
{
14+
MockEntities.FreeNoId(entity.NativePointer);
15+
}
1116
}
1217
}

api/AltV.Net.Mock/MockCheckpointPool.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ public class MockCheckpointPool : CheckpointPool
88
public MockCheckpointPool(IBaseObjectFactory<ICheckpoint> checkpointFactory) : base(checkpointFactory)
99
{
1010
}
11+
12+
public override void OnRemove(ICheckpoint entity)
13+
{
14+
MockEntities.FreeNoId(entity.NativePointer);
15+
}
1116
}
1217
}

api/AltV.Net.Mock/MockEntities.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace AltV.Net.Mock
45
{
@@ -7,16 +8,40 @@ public static class MockEntities
78
private static IntPtr _ptr = IntPtr.Zero;
89

910
private static ushort _id = 0;
11+
12+
private static readonly Stack<IntPtr> FreePointers = new Stack<IntPtr>();
13+
14+
private static readonly Stack<ushort> FreeIds = new Stack<ushort>();
1015

1116
public static IntPtr GetNextPtr(out ushort id)
1217
{
18+
if (FreeIds.Count > 0 && FreePointers.Count > 0)
19+
{
20+
id = FreeIds.Pop();
21+
return FreePointers.Pop();
22+
}
1323
_ptr += 1;
1424
id = ++_id;
1525
return _ptr;
1626
}
27+
28+
public static void Free(IntPtr intPtr, ushort id)
29+
{
30+
FreeIds.Push(id);
31+
FreePointers.Push(intPtr);
32+
}
33+
34+
public static void FreeNoId(IntPtr intPtr)
35+
{
36+
FreePointers.Push(intPtr);
37+
}
1738

1839
public static IntPtr GetNextPtrNoId()
1940
{
41+
if (FreePointers.Count > 0)
42+
{
43+
return FreePointers.Pop();
44+
}
2045
_ptr += 1;
2146
return _ptr;
2247
}

api/AltV.Net.Mock/MockPlayerPool.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ public override ushort GetId(IntPtr entityPointer)
1414
{
1515
return 0;
1616
}
17+
18+
public override void OnRemove(IPlayer entity)
19+
{
20+
MockEntities.Free(entity.NativePointer, entity.Id);
21+
}
1722
}
1823
}

api/AltV.Net.Mock/MockVehiclePool.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ public override ushort GetId(IntPtr entityPointer)
1414
{
1515
return 0;
1616
}
17+
18+
public override void OnRemove(IVehicle entity)
19+
{
20+
MockEntities.Free(entity.NativePointer, entity.Id);
21+
}
1722
}
1823
}

api/AltV.Net.Mock/MockVoiceChannelPool.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ public class MockVoiceChannelPool : VoiceChannelPool
88
public MockVoiceChannelPool(IBaseObjectFactory<IVoiceChannel> voiceChannelFactory) : base(voiceChannelFactory)
99
{
1010
}
11+
12+
public override void OnRemove(IVoiceChannel entity)
13+
{
14+
MockEntities.FreeNoId(entity.NativePointer);
15+
}
1116
}
1217
}

api/AltV.Net/Elements/Pools/BaseObjectPool.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected BaseObjectPool(IBaseObjectFactory<TBaseObject> entityFactory)
2222
{
2323
this.entityFactory = entityFactory;
2424
}
25-
25+
2626
public void Create(IntPtr entityPointer)
2727
{
2828
Add(entityFactory.Create(entityPointer));
@@ -37,6 +37,7 @@ public void Create(IntPtr entityPointer, out TBaseObject entity)
3737
public void Add(TBaseObject entity)
3838
{
3939
entities[entity.NativePointer] = entity;
40+
OnAdd(entity);
4041
}
4142

4243
public bool Remove(TBaseObject entity)
@@ -48,6 +49,7 @@ public bool Remove(IntPtr entityPointer)
4849
{
4950
if (!entities.Remove(entityPointer, out var entity) || !entity.Exists) return false;
5051
SetEntityNoLongerExists(entity);
52+
OnRemove(entity);
5153
return true;
5254
}
5355

@@ -75,5 +77,13 @@ public ICollection<TBaseObject> GetAllObjects()
7577
{
7678
return entities.Values;
7779
}
80+
81+
public virtual void OnAdd(TBaseObject entity)
82+
{
83+
}
84+
85+
public virtual void OnRemove(TBaseObject entity)
86+
{
87+
}
7888
}
7989
}

0 commit comments

Comments
 (0)