mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] 接入ET8服务端
[+] 接入ET8服务端
This commit is contained in:
103
Assets/GameScripts/DotNet/Core/Entity/EntityRef.cs
Normal file
103
Assets/GameScripts/DotNet/Core/Entity/EntityRef.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public readonly struct EntityRef<T> where T: Entity
|
||||
{
|
||||
private readonly long instanceId;
|
||||
private readonly T entity;
|
||||
|
||||
private EntityRef(T t)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
this.instanceId = 0;
|
||||
this.entity = null;
|
||||
return;
|
||||
}
|
||||
this.instanceId = t.InstanceId;
|
||||
this.entity = t;
|
||||
}
|
||||
|
||||
private T UnWrap
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.entity == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (this.entity.InstanceId != this.instanceId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return this.entity;
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator EntityRef<T>(T t)
|
||||
{
|
||||
return new EntityRef<T>(t);
|
||||
}
|
||||
|
||||
public static implicit operator T(EntityRef<T> v)
|
||||
{
|
||||
return v.UnWrap;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public struct EntityWeakRef<T> where T: Entity
|
||||
{
|
||||
private long instanceId;
|
||||
// 使用WeakReference,这样不会导致entity dispose了却无法gc的问题
|
||||
// 不过暂时没有测试WeakReference的性能
|
||||
private readonly WeakReference<T> weakRef;
|
||||
|
||||
private EntityWeakRef(T t)
|
||||
{
|
||||
if (t != null)
|
||||
{
|
||||
this.instanceId = 0;
|
||||
this.weakRef = null;
|
||||
return;
|
||||
}
|
||||
this.instanceId = t.InstanceId;
|
||||
this.weakRef = new WeakReference<T>(t);
|
||||
}
|
||||
|
||||
private T UnWrap
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.instanceId == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.weakRef.TryGetTarget(out T entity))
|
||||
{
|
||||
this.instanceId = 0;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (entity.InstanceId != this.instanceId)
|
||||
{
|
||||
this.instanceId = 0;
|
||||
return null;
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator EntityWeakRef<T>(T t)
|
||||
{
|
||||
return new EntityWeakRef<T>(t);
|
||||
}
|
||||
|
||||
public static implicit operator T(EntityWeakRef<T> v)
|
||||
{
|
||||
return v.UnWrap;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user