diff --git a/Game.Server/SceneEntity.cs b/Game.Server/SceneEntity.cs index ec13dc6cc..359a3a9df 100644 --- a/Game.Server/SceneEntity.cs +++ b/Game.Server/SceneEntity.cs @@ -1242,7 +1242,7 @@ public void UnloadScene() { actor = FindNamedActor(choreoActor.GetName()); if (actor != null) { - ActorList[index] = new EHANDLE().Set(actor); + ActorList[index] = actor.GetRefEHandle(); NetworkProp().NetworkStateForceUpdate(); } } diff --git a/Game.Server/Soundscape.cs b/Game.Server/Soundscape.cs index 170a9806b..9c7ec7e6a 100644 --- a/Game.Server/Soundscape.cs +++ b/Game.Server/Soundscape.cs @@ -342,10 +342,10 @@ public void DelegateStartTouch(BaseEntity ent) { // Just in case.. we shouldn't already be in the player's list because it should have // called DelegateEndTouch, but this seems to happen when they're noclipping. - player.TriggerSoundscapeList.Remove(new EHANDLE().Set(this)); + player.TriggerSoundscapeList.Remove(GetRefEHandle()); // Add us to the player's list of soundscapes and - player.TriggerSoundscapeList.Insert(0, new EHANDLE().Set(this)); + player.TriggerSoundscapeList.Insert(0, GetRefEHandle()); WriteAudioParamsTo(ref player.GetAudioParams()); } @@ -354,7 +354,7 @@ public void DelegateEndTouch(BaseEntity ent) { return; // Remove us from the ent's list of soundscapes. - player.TriggerSoundscapeList.Remove(new EHANDLE().Set(this)); + player.TriggerSoundscapeList.Remove(GetRefEHandle()); while (player.TriggerSoundscapeList.Count > 0) { if (player.TriggerSoundscapeList[0].Get() is EnvSoundscapeTriggerable ss) { // Make this one current. diff --git a/Game.Shared/BaseCombatWeaponShared.cs b/Game.Shared/BaseCombatWeaponShared.cs index c5e8a14d0..188fc34d6 100644 --- a/Game.Shared/BaseCombatWeaponShared.cs +++ b/Game.Shared/BaseCombatWeaponShared.cs @@ -255,7 +255,7 @@ private static void RecvProxy_WeaponState(ref readonly RecvProxyData data, objec public virtual bool CanBePickedUpByNPCs() => true; TimeUnit_t UnlockTime; - readonly EHANDLE Locker = new(); + EHANDLE Locker = new(); public void Lock(TimeUnit_t lockTime, BaseEntity? locker) { UnlockTime = gpGlobals.CurTime + lockTime; diff --git a/Source.Common/BaseHandle.cs b/Source.Common/BaseHandle.cs index e27d5456e..d63707337 100644 --- a/Source.Common/BaseHandle.cs +++ b/Source.Common/BaseHandle.cs @@ -5,46 +5,32 @@ /// public struct BaseHandle : IEquatable, IBaseHandle { - public bool Equals(BaseHandle otherHandle) => Index == otherHandle.Index; + public readonly bool Equals(BaseHandle otherHandle) => Index == otherHandle.Index; public uint Index; - public uint Unpack() => Index; + public readonly uint Unpack() => Index; - public void Invalidate() => Index = (uint)Constants.INVALID_EHANDLE_INDEX; - public BaseHandle() => Invalidate(); + public BaseHandle() => Index = (uint)Constants.INVALID_EHANDLE_INDEX; public BaseHandle(uint value) => Index = value; public BaseHandle(in BaseHandle handle) => Index = handle.Index; - public BaseHandle(int entry, int serial) => Init(entry, serial); + public BaseHandle(int entry, int serial) => Index = (uint)(entry | (serial << Constants.NUM_ENT_ENTRY_BITS)); - public void Init(in BaseHandle otherHandle) => Index = otherHandle.Index; - public void Init(uint entindex) => Index = entindex; - public void Init(ulong entindex) => Index = (uint)entindex; - public void Init(int entry, int serial) => Index = (uint)(entry | (serial << Constants.NUM_ENT_ENTRY_BITS)); - - public int GetEntryIndex() => (int)(Index & Constants.ENT_ENTRY_MASK); - public int GetSerialNumber() => (int)(Index >> Constants.NUM_ENT_ENTRY_BITS); + public readonly int GetEntryIndex() => (int)(Index & Constants.ENT_ENTRY_MASK); + public readonly int GetSerialNumber() => (int)(Index >> Constants.NUM_ENT_ENTRY_BITS); public static bool operator ==(BaseHandle a, BaseHandle b) => a.Index == b.Index; public static bool operator !=(BaseHandle a, BaseHandle b) => a.Index != b.Index; public static bool operator <(BaseHandle a, BaseHandle b) => a.Index < b.Index; public static bool operator >(BaseHandle a, BaseHandle b) => a.Index > b.Index; - public override bool Equals(object? obj) { + public override readonly bool Equals(object? obj) { return obj switch { BaseHandle b => Index == b.Index, _ => false }; } - public override int GetHashCode() => (int)Index; - public bool IsValid() => Index != Constants.INVALID_EHANDLE_INDEX; - - public BaseHandle Set(IHandleEntity? entity) { - if (entity != null) - this.Index = entity.GetRefEHandle().Index; - else - Index = Constants.INVALID_EHANDLE_INDEX; - return this; - } + public override readonly int GetHashCode() => (int)Index; + public readonly bool IsValid() => Index != Constants.INVALID_EHANDLE_INDEX; } @@ -54,25 +40,19 @@ public BaseHandle Set(IHandleEntity? entity) { /// public struct Handle : IEquatable>, IEquatable, IBaseHandle { - public uint Unpack() => Index; + public readonly uint Unpack() => Index; - public bool Equals(Handle otherHandle) => Index == otherHandle.Index; - public bool Equals(BaseHandle otherHandle) => Index == otherHandle.Index; + public readonly bool Equals(Handle otherHandle) => Index == otherHandle.Index; + public readonly bool Equals(BaseHandle otherHandle) => Index == otherHandle.Index; public uint Index; - public void Invalidate() => Index = (uint)Constants.INVALID_EHANDLE_INDEX; - public Handle() => Invalidate(); + public Handle() => Index = (uint)Constants.INVALID_EHANDLE_INDEX; public Handle(uint value) => Index = value; public Handle(in BaseHandle handle) => Index = handle.Index; - public Handle(int entry, int serial) => Init(entry, serial); - - public void Init(in BaseHandle otherHandle) => Index = otherHandle.Index; - public void Init(uint entindex) => Index = entindex; - public void Init(ulong entindex) => Index = (uint)entindex; - public void Init(int entry, int serial) => Index = (uint)(entry | (serial << Constants.NUM_ENT_ENTRY_BITS)); + public Handle(int entry, int serial) => Index = (uint)(entry | (serial << Constants.NUM_ENT_ENTRY_BITS)); - public int GetEntryIndex() => (int)(Index & Constants.ENT_ENTRY_MASK); - public int GetSerialNumber() => (int)(Index >> Constants.NUM_ENT_ENTRY_BITS); + public readonly int GetEntryIndex() => (int)(Index & Constants.ENT_ENTRY_MASK); + public readonly int GetSerialNumber() => (int)(Index >> Constants.NUM_ENT_ENTRY_BITS); public static bool operator ==(BaseHandle a, Handle b) => a.Index == b.Index; public static bool operator !=(BaseHandle a, Handle b) => a.Index != b.Index; @@ -89,7 +69,7 @@ public struct Handle : IEquatable>, IEquatable, IBaseHa public static bool operator <(Handle a, Handle b) => a.Index < b.Index; public static bool operator >(Handle a, Handle b) => a.Index > b.Index; - public override bool Equals(object? obj) { + public override readonly bool Equals(object? obj) { return obj switch { BaseHandle b => Index == b.Index, Handle b => Index == b.Index, @@ -101,14 +81,39 @@ public override bool Equals(object? obj) { public static implicit operator Handle(BaseHandle handle) => new(handle.Index); public static implicit operator BaseHandle(Handle handle) => new(handle.Index); - public override int GetHashCode() => (int)Index; - public bool IsValid() => Index != Constants.INVALID_EHANDLE_INDEX; + public override readonly int GetHashCode() => (int)Index; + public readonly bool IsValid() => Index != Constants.INVALID_EHANDLE_INDEX; +} + +public static class BaseHandleExts +{ + public static void Invalidate(ref this BaseHandle handle) => handle.Index = (uint)Constants.INVALID_EHANDLE_INDEX; + + public static void Init(ref this BaseHandle handle, in BaseHandle otherHandle) => handle.Index = otherHandle.Index; + public static void Init(ref this BaseHandle handle, uint entindex) => handle.Index = entindex; + public static void Init(ref this BaseHandle handle, ulong entindex) => handle.Index = (uint)entindex; + public static void Init(ref this BaseHandle handle, int entry, int serial) => handle.Index = (uint)(entry | (serial << Constants.NUM_ENT_ENTRY_BITS)); + + public static BaseHandle Set(ref this BaseHandle handle, IHandleEntity? entity) { + if (entity != null) + handle.Index = entity.GetRefEHandle().Index; + else + handle.Index = Constants.INVALID_EHANDLE_INDEX; + return handle; + } + + public static void Invalidate(ref this Handle handle) => handle.Index = (uint)Constants.INVALID_EHANDLE_INDEX; + + public static void Init(ref this Handle handle, in BaseHandle otherHandle) => handle.Index = otherHandle.Index; + public static void Init(ref this Handle handle, uint entindex) => handle.Index = entindex; + public static void Init(ref this Handle handle, ulong entindex) => handle.Index = (uint)entindex; + public static void Init(ref this Handle handle, int entry, int serial) => handle.Index = (uint)(entry | (serial << Constants.NUM_ENT_ENTRY_BITS)); - public Handle Set(IHandleEntity? entity) { + public static Handle Set(ref this Handle handle, IHandleEntity? entity) { if (entity != null) - this.Index = entity.GetRefEHandle().Index; + handle.Index = entity.GetRefEHandle().Index; else - Index = Constants.INVALID_EHANDLE_INDEX; - return this; + handle.Index = Constants.INVALID_EHANDLE_INDEX; + return handle; } } diff --git a/Source.Common/IBaseHandle.cs b/Source.Common/IBaseHandle.cs index 9adef684a..f1786773f 100644 --- a/Source.Common/IBaseHandle.cs +++ b/Source.Common/IBaseHandle.cs @@ -9,10 +9,5 @@ public interface IBaseHandle int GetEntryIndex(); int GetHashCode(); int GetSerialNumber(); - void Init(in BaseHandle otherHandle); - void Init(int entry, int serial); - void Init(uint entindex); - void Init(ulong entindex); - void Invalidate(); bool IsValid(); } diff --git a/Usings/GlobalUsings.cs b/Usings/GlobalUsings.cs index a390ed81e..6ef7cb3f5 100644 --- a/Usings/GlobalUsings.cs +++ b/Usings/GlobalUsings.cs @@ -25,6 +25,7 @@ global using static Source.Common.DataMapConstants; global using static Source.Common.PartitionConstants; global using static Source.Common.DispCollCommon; +global using static Source.Common.BaseHandleExts; global using CRC32_t = uint; global using HCursor = nint; global using UtlSymId_t = ulong;