Repository navigation
Expand file tree
/
Copy pathDynamicEntity.cs
More file actions
118 lines (99 loc) · 4.66 KB
/
Copy pathDynamicEntity.cs
File metadata and controls
118 lines (99 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Concurrent;
using SampSharp.Entities;
using SampSharp.Streamer.Entities.Interop;
namespace SampSharp.Streamer.Entities;
/// <summary>
/// Base class for all streamer-managed entities (objects, pickups, text labels,
/// map icons, checkpoints). Unlike ECS <see cref="SampSharp.Entities.Component"/>,
/// a <see cref="DynamicEntity"/> lives inside streamer.dll's memory rather than
/// the EntityManager: identity is the streamer-id alone, lifetime is controlled by
/// explicit Destroy* calls.
/// </summary>
/// <remarks>
/// <see cref="EventParameterAttribute"/> tells <see cref="EventDispatcher"/> to
/// pass derived instances straight through as event arguments (the same way it
/// treats value types and arrays) instead of trying to resolve them from DI.
/// Without it the dispatcher would think an [Event] handler takes fewer
/// arguments than we actually pass — for example, OnPlayerPickUpDynamicPickup
/// dispatches (EntityId, DynamicPickup) but a DI-classified parameter would be
/// counted as zero, giving the "argument count mismatch" log and silently
/// skipping the handler.
/// </remarks>
[EventParameter]
public abstract class DynamicEntity
{
/// <summary>
/// Per-type registry mapping streamer-id → stable <see cref="EntityId"/>.
/// Required so that the same streamer item — whether obtained from a
/// Create* call or from a streamer event — yields the same EntityId,
/// because legacy VSRP code keys Dictionary<EntityId, …> on this.
/// </summary>
private static readonly ConcurrentDictionary<(Type, int), EntityId> _entityRegistry = new();
private EntityId _entity;
/// <summary>Streamer-side id. Stable for the lifetime of the entity.</summary>
public int Id { get; protected set; }
/// <summary>
/// Stable EntityId, identical for every managed wrapper of the same
/// streamer item (obtained either via Create* or via a streamer event).
/// </summary>
public EntityId Entity
{
get
{
if (_entity == default)
_entity = _entityRegistry.GetOrAdd((GetType(), Id), _ => EntityId.NewEntityId());
return _entity;
}
}
public abstract bool IsAlive { get; }
/// <summary>Kind of streamer item, the <c>type</c> argument of the native per-item calls.</summary>
internal abstract StreamerType ItemType { get; }
/// <summary>Legacy alias mirroring the old SampSharp Component.IsComponentAlive.</summary>
public bool IsComponentAlive => IsAlive;
/// <summary>Legacy alias for pattern matching like <c>is { IsValid: true }</c>.</summary>
public bool IsValid => IsAlive;
public abstract void DestroyEntity();
/// <summary>
/// Makes the item visible in exactly these virtual worlds; a negative value or an empty list means every world.
/// Returns false when streamer.dll predates world sets, and the item keeps the world it was created with.
/// </summary>
public unsafe bool SetVirtualWorlds(ReadOnlySpan<int> worlds)
{
fixed (int* values = worlds)
return StreamerInterop.Streamer_Item_SetWorlds((int)ItemType, Id, values, worlds.Length);
}
/// <summary>Same as <see cref="SetVirtualWorlds"/>, for interiors.</summary>
public unsafe bool SetInteriors(ReadOnlySpan<int> interiors)
{
fixed (int* values = interiors)
return StreamerInterop.Streamer_Item_SetInteriors((int)ItemType, Id, values, interiors.Length);
}
/// <summary>Legacy alias for Component.Destroy().</summary>
public void Destroy() => DestroyEntity();
/// <summary>
/// Drops the cached EntityId from the registry. DestroyEntity implementations
/// should call this so that re-creating a streamer item with the same id
/// produces a fresh Guid instead of resurrecting a dead one.
/// </summary>
protected void ForgetEntity()
{
_entityRegistry.TryRemove((GetType(), Id), out _);
_entity = default;
}
public override string ToString() => $"{GetType().Name}(Id={Id})";
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj)) return true;
if (obj is not DynamicEntity other) return false;
return GetType() == other.GetType() && Id == other.Id;
}
public override int GetHashCode() => HashCode.Combine(GetType(), Id);
public static bool operator ==(DynamicEntity? left, DynamicEntity? right)
{
if (ReferenceEquals(left, right)) return true;
if (left is null || right is null) return false;
return left.Equals(right);
}
public static bool operator !=(DynamicEntity? left, DynamicEntity? right) => !(left == right);
}