BITKit/Packages/Runtime~/Unity/Common/Scripts/Mirror/MirrorHelper.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using UnityEngine.Events;
namespace Mirror
{
public partial struct NetworkConstant
{
public const string Connect = "Connect";
public const string StartServer = "StartServer";
public const string StartClient = "StartClient";
public const string StartHost = "StartHost";
public const string StopServer = "StopServer";
public const string StopClient = "StopClient";
public const string StopHost = "StopHost";
public const string TryStartServer = "TryStartServer";
public const string TryStartClient = "TryStartClient";
public const string TryStartHost = "TryStartHost";
public const string TryDisconnect = "TryDisconnect";
public const string OnStartServer = "OnStartServer";
public const string OnStartClient = "OnStartClient";
public const string OnStartHost = "OnStartHost";
public const string OnStopServer = "OnStopServer";
public const string OnStopClient = "OnStopClient";
public const string OnStopHost = "OnStopHost";
public const string Disconnect = "Disconnect";
}
public enum SyncDictionaryOperation : byte
{
OP_ADD,
OP_CLEAR,
OP_REMOVE,
OP_SET
}
public interface INetworkSupport
{
Indexer GetIndexer();
string GetID();
}
public static class MirrorHelper
{
public static void Write(this NetworkWriter writer, Root root)
{
var identity = root.root.GetComponent<NetworkIdentity>();
var id = root.Get(root.id);
writer.Write(identity);
writer.Write(id);
}
public static Root GetRoot(this NetworkReader reader)
{
var identity = reader.Read<NetworkIdentity>();
var id = reader.Read<string>();
if (identity.TryGetComponent<Root>(out var root))
{
return root.Get(id);
}
return null;
}
public static void Write(this NetworkWriter writer, INetworkSupport self)
{
writer.Write(self.GetIndexer());
writer.Write(self.GetID());
}
public static INetworkSupport ReadNetworkSupport(this NetworkReader reader)
{
var indexer = reader.Read<Indexer>();
var id = reader.Read<string>();
return indexer.dictionary.Get(id) as INetworkSupport;
}
public static void Write(this NetworkWriter writer, Rigidbody rigidbody)
{
}
}
public class SyncCallback<TKey, TValue>
{
public SyncDictionaryOperation op;
public TKey key;
public TValue value;
}
public class NetworkDictionary<TKey, TValue> : SyncDictionary<TKey, TValue>
{
public class SyncCallback : Mirror.SyncCallback<TKey, TValue> { }
UnityAction<Mirror.SyncCallback<TKey, TValue>> onCallback;
public NetworkDictionary()
{
Callback += OnCallback;
}
public void AddListener(UnityAction<Mirror.SyncCallback<TKey, TValue>> callback)
{
onCallback += callback;
}
public void RemoveListener(UnityAction<Mirror.SyncCallback<TKey, TValue>> callback)
{
onCallback -= callback;
}
void OnCallback(Operation op, TKey key, TValue value)
{
onCallback?.Invoke(new SyncCallback<TKey, TValue>()
{
op = (SyncDictionaryOperation)((byte)op),
key = key,
value = value,
});
}
}
}