using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace UnityEngine.AdaptivePerformance
{
///
/// 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.
///
public abstract class AdaptivePerformanceLoaderHelper : AdaptivePerformanceLoader
{
///
/// 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.
///
protected Dictionary m_SubsystemInstanceMap = new Dictionary();
///
/// 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.
///
///
/// Type of the subsystem to get.
///
/// The loaded subsystem, or null if no subsystem found.
public override T GetLoadedSubsystem()
{
Type subsystemType = typeof(T);
ISubsystem subsystem;
m_SubsystemInstanceMap.TryGetValue(subsystemType, out subsystem);
return subsystem as T;
}
///
/// Start a subsystem instance of a given type. Subsystem is assumed to already be loaded from
/// a previous call to CreateSubsystem.
///
/// A subclass of
protected void StartSubsystem() where T : class, ISubsystem
{
T subsystem = GetLoadedSubsystem();
if (subsystem != null)
subsystem.Start();
}
///
/// Stop a subsystem instance of a given type. Subsystem is assumed to already be loaded from
/// a previous call to CreateSubsystem.
///
/// A subclass of
protected void StopSubsystem() where T : class, ISubsystem
{
T subsystem = GetLoadedSubsystem();
if (subsystem != null)
subsystem.Stop();
}
///
/// Destroy a subsystem instance of a given type. Subsystem is assumed to already be loaded from
/// a previous call to CreateSubsystem.
///
/// A subclass of
protected void DestroySubsystem() where T : class, ISubsystem
{
T subsystem = GetLoadedSubsystem();
if (subsystem != null)
subsystem.Destroy();
}
///
/// Creates a subsystem with a given list of descriptors and a specific subsystem id.
///
/// The descriptor type being passed in.
/// The subsystem type being requested.
/// List of TDescriptor instances to use for subsystem matching.
/// The identifier key of the particualr subsystem implementation being requested.
protected void CreateSubsystem(List descriptors, string id)
where TDescriptor : ISubsystemDescriptor
where TSubsystem : ISubsystem
{
if (descriptors == null)
throw new ArgumentNullException("descriptors");
SubsystemManager.GetSubsystemDescriptors(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;
}
}
}
}
///
/// Override of to provide for clearing the instance map.true
///
/// If you override in your subclass, you must call the base
/// implementation to allow the instance map tp be cleaned up correctly.
///
///
/// True if de-initialization was successful.
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
}
}