You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
C#
81 lines
2.4 KiB
C#
using System.Runtime.InteropServices;
|
|
|
|
namespace Fusion.Addons.SimpleKCC
|
|
{
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
internal struct KCCNetworkID
|
|
{
|
|
public const int WORD_COUNT = 2;
|
|
|
|
[FieldOffset(0)]
|
|
public uint Value0;
|
|
|
|
[FieldOffset(4)]
|
|
public uint Value1;
|
|
|
|
public bool IsValid => Value1 != 0;
|
|
|
|
public bool Equals(KCCNetworkID other)
|
|
{
|
|
if (Value0 == other.Value0)
|
|
{
|
|
return Value1 == other.Value1;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public unsafe static KCCNetworkID GetNetworkID(NetworkObject networkObject)
|
|
{
|
|
if (networkObject == null)
|
|
{
|
|
return default(KCCNetworkID);
|
|
}
|
|
|
|
KCCNetworkID result = default(KCCNetworkID);
|
|
if (networkObject.Id.IsValid)
|
|
{
|
|
result.Value0 = networkObject.Id.Raw;
|
|
result.Value1 = 1u;
|
|
}
|
|
else
|
|
{
|
|
NetworkObjectTypeId networkTypeId = networkObject.NetworkTypeId;
|
|
KCCNetworkID kCCNetworkID = *(KCCNetworkID*)(&networkTypeId);
|
|
result.Value0 = kCCNetworkID.Value0;
|
|
result.Value1 = 2u | (kCCNetworkID.Value1 << 2);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public unsafe static NetworkObject GetNetworkObject(NetworkRunner runner, KCCNetworkID networkID)
|
|
{
|
|
switch (networkID.Value1 & 3)
|
|
{
|
|
case 1u:
|
|
{
|
|
NetworkId networkId = default(NetworkId);
|
|
networkId.Raw = networkID.Value0;
|
|
return runner.FindObject(networkId);
|
|
}
|
|
case 2u:
|
|
{
|
|
KCCNetworkID kCCNetworkID = default(KCCNetworkID);
|
|
kCCNetworkID.Value0 = networkID.Value0;
|
|
kCCNetworkID.Value1 = networkID.Value1 >> 2;
|
|
NetworkObjectTypeId networkObjectTypeId = *(NetworkObjectTypeId*)(&kCCNetworkID);
|
|
if (networkObjectTypeId.IsPrefab)
|
|
{
|
|
return runner.Config.PrefabTable.Load(networkObjectTypeId.AsPrefabId, isSynchronous: true);
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|