NetworkAuthenticator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace Mirror
  5. {
  6. [Serializable] public class UnityEventNetworkConnection : UnityEvent<NetworkConnectionToClient> {}
  7. /// <summary>Base class for implementing component-based authentication during the Connect phase</summary>
  8. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-authenticators")]
  9. public abstract class NetworkAuthenticator : MonoBehaviour
  10. {
  11. /// <summary>Notify subscribers on the server when a client is authenticated</summary>
  12. [Header("Event Listeners (optional)")]
  13. [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
  14. public UnityEventNetworkConnection OnServerAuthenticated = new UnityEventNetworkConnection();
  15. /// <summary>Notify subscribers on the client when the client is authenticated</summary>
  16. [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
  17. public UnityEvent OnClientAuthenticated = new UnityEvent();
  18. /// <summary>Called when server starts, used to register message handlers if needed.</summary>
  19. public virtual void OnStartServer() {}
  20. /// <summary>Called when server stops, used to unregister message handlers if needed.</summary>
  21. public virtual void OnStopServer() {}
  22. /// <summary>Called on server from OnServerAuthenticateInternal when a client needs to authenticate</summary>
  23. public virtual void OnServerAuthenticate(NetworkConnectionToClient conn) {}
  24. protected void ServerAccept(NetworkConnectionToClient conn)
  25. {
  26. OnServerAuthenticated.Invoke(conn);
  27. }
  28. protected void ServerReject(NetworkConnectionToClient conn)
  29. {
  30. conn.Disconnect();
  31. }
  32. /// <summary>Called when client starts, used to register message handlers if needed.</summary>
  33. public virtual void OnStartClient() {}
  34. /// <summary>Called when client stops, used to unregister message handlers if needed.</summary>
  35. public virtual void OnStopClient() {}
  36. /// <summary>Called on client from OnClientAuthenticateInternal when a client needs to authenticate</summary>
  37. public virtual void OnClientAuthenticate() {}
  38. protected void ClientAccept()
  39. {
  40. OnClientAuthenticated.Invoke();
  41. }
  42. protected void ClientReject()
  43. {
  44. // Set this on the client for local reference
  45. NetworkClient.connection.isAuthenticated = false;
  46. // disconnect the client
  47. NetworkClient.connection.Disconnect();
  48. }
  49. // Reset() instead of OnValidate():
  50. // Any NetworkAuthenticator assigns itself to the NetworkManager, this is fine on first adding it,
  51. // but if someone intentionally sets Authenticator to null on the NetworkManager again then the
  52. // Authenticator will reassign itself if a value in the inspector is changed.
  53. // My change switches OnValidate to Reset since Reset is only called when the component is first
  54. // added (or reset is pressed).
  55. void Reset()
  56. {
  57. #if UNITY_EDITOR
  58. // automatically assign authenticator field if we add this to NetworkManager
  59. NetworkManager manager = GetComponent<NetworkManager>();
  60. if (manager != null && manager.authenticator == null)
  61. {
  62. // undo has to be called before the change happens
  63. UnityEditor.Undo.RecordObject(manager, "Assigned NetworkManager authenticator");
  64. manager.authenticator = this;
  65. }
  66. #endif
  67. }
  68. }
  69. }