123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
-
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
-
- using UnityEngine;
-
- namespace UnityEngine.AdaptivePerformance
- {
- /// <summary>
- /// Adaptive Performance Loader abstract subclass used as a base class for specific provider implementations. Class provides some
- /// helper logic that can be used to handle subsystem handling in a typesafe manner, reducing potential boilerplate
- /// code.
- /// </summary>
- public abstract class AdaptivePerformanceLoaderHelper : AdaptivePerformanceLoader
- {
- /// <summary>
- /// Map of loaded susbsystems. Used so Unity doesn't always have to call AdaptivePerformanceManger and do a manual
- /// search to find the instance it loaded.
- /// </summary>
- protected Dictionary<Type, ISubsystem> m_SubsystemInstanceMap = new Dictionary<Type, ISubsystem>();
-
- /// <summary>
- /// Gets the loaded subsystem of the specified type. This is implementation-specific, because implementations contain data on
- /// what they have loaded and how best to get it.
- /// </summary>
- ///
- /// <typeparam name="T">Type of the subsystem to get.</typeparam>
- ///
- /// <returns>The loaded subsystem, or null if no subsystem found.</returns>
- public override T GetLoadedSubsystem<T>()
- {
- Type subsystemType = typeof(T);
- ISubsystem subsystem;
- m_SubsystemInstanceMap.TryGetValue(subsystemType, out subsystem);
- return subsystem as T;
- }
-
- /// <summary>
- /// Start a subsystem instance of a given type. Subsystem is assumed to already be loaded from
- /// a previous call to CreateSubsystem.
- /// </summary>
- /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
- protected void StartSubsystem<T>() where T : class, ISubsystem
- {
- T subsystem = GetLoadedSubsystem<T>();
- if (subsystem != null)
- subsystem.Start();
- }
-
- /// <summary>
- /// Stop a subsystem instance of a given type. Subsystem is assumed to already be loaded from
- /// a previous call to CreateSubsystem.
- /// </summary>
- /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
- protected void StopSubsystem<T>() where T : class, ISubsystem
- {
- T subsystem = GetLoadedSubsystem<T>();
- if (subsystem != null)
- subsystem.Stop();
- }
-
- /// <summary>
- /// Destroy a subsystem instance of a given type. Subsystem is assumed to already be loaded from
- /// a previous call to CreateSubsystem.
- /// </summary>
- /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
- protected void DestroySubsystem<T>() where T : class, ISubsystem
- {
- T subsystem = GetLoadedSubsystem<T>();
- if (subsystem != null)
- subsystem.Destroy();
- }
-
- /// <summary>
- /// Creates a subsystem with a given list of descriptors and a specific subsystem id.
- /// </summary>
- /// <typeparam name="TDescriptor">The descriptor type being passed in.</typeparam>
- /// <typeparam name="TSubsystem">The subsystem type being requested.</typeparam>
- /// <param name="descriptors">List of TDescriptor instances to use for subsystem matching.</param>
- /// <param name="id">The identifier key of the particualr subsystem implementation being requested.</param>
- protected void CreateSubsystem<TDescriptor, TSubsystem>(List<TDescriptor> descriptors, string id)
- where TDescriptor : ISubsystemDescriptor
- where TSubsystem : ISubsystem
- {
- if (descriptors == null)
- throw new ArgumentNullException("descriptors");
-
- SubsystemManager.GetSubsystemDescriptors<TDescriptor>(descriptors);
-
- if (descriptors.Count > 0)
- {
- foreach (var descriptor in descriptors)
- {
- ISubsystem subsys = null;
- if (String.Compare(descriptor.id, id, true) == 0)
- {
- subsys = descriptor.Create();
- }
- if (subsys != null)
- {
- m_SubsystemInstanceMap[typeof(TSubsystem)] = subsys;
- break;
- }
- }
- }
- }
-
- /// <summary>
- /// Override of <see cref="Deinitialize"/> to provide for clearing the instance map.true
- ///
- /// If you override <see cref="Deinitialize"/> in your subclass, you must call the base
- /// implementation to allow the instance map tp be cleaned up correctly.
- /// </summary>
- ///
- /// <returns>True if de-initialization was successful.</returns>
- public override bool Deinitialize()
- {
- m_SubsystemInstanceMap.Clear();
- return base.Deinitialize();
- }
-
- #if UNITY_EDITOR
- virtual public void WasAssignedToBuildTarget(BuildTargetGroup buildTargetGroup)
- {
- }
-
- virtual public void WasUnassignedFromBuildTarget(BuildTargetGroup buildTargetGroup)
- {
- }
-
- #endif
- }
- }
|