using System; using System.Collections.Generic; namespace Unity.Netcode { /// /// This class is used to support testable code by allowing any supported component used by NetworkManager to be replaced /// with a mock component or a test version that overloads certain methods to change or record their behavior. /// Components currently supported by ComponentFactory: /// - IDeferredMessageManager /// internal static class ComponentFactory { internal delegate object CreateObjectDelegate(NetworkManager networkManager); private static Dictionary s_Delegates = new Dictionary(); /// /// Instantiates an instance of a given interface /// /// The network manager /// The interface to instantiate it with /// public static T Create(NetworkManager networkManager) { return (T)s_Delegates[typeof(T)](networkManager); } /// /// Overrides the default creation logic for a given interface type /// /// The factory delegate to create the instance /// The interface type to override public static void Register(CreateObjectDelegate creator) { s_Delegates[typeof(T)] = creator; } /// /// Reverts the creation logic for a given interface type to the default logic /// /// The interface type to revert public static void Deregister() { s_Delegates.Remove(typeof(T)); SetDefaults(); } /// /// Initializes the default creation logic for all supported component types /// public static void SetDefaults() { SetDefault(networkManager => new DeferredMessageManager(networkManager)); } private static void SetDefault(CreateObjectDelegate creator) { if (!s_Delegates.ContainsKey(typeof(T))) { s_Delegates[typeof(T)] = creator; } } } }