// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Reflection;
using UltimateXR.Core;
using UnityEditor;
namespace UltimateXR.Editor.Sdks
{
///
/// Base class for SDK locators. SDK locators are classes instantiated when Unity is loaded or changes are made and the
/// projects is recompiled.
/// They are used to automatically add/remove scripting symbols that allow to use different SDKs without the need of
/// manual user setup.
///
public abstract partial class UxrSdkLocator
{
#region Public Types & Data
///
/// Gets the type of SDK it is (device/multiplayer, etc.).
///
public abstract SupportType Support { get; }
///
/// Gets the SDK name.
///
public abstract string Name { get; }
///
/// Gets the minimum required Unity version as a string.
///
public abstract string MinimumUnityVersion { get; }
///
/// Gets list of scripting symbols that will be added to the project when this SDK is available. Usually
/// will set up an internal list that can be accessed through this array.
///
public abstract string[] AvailableSymbols { get; }
///
/// Gets list of all scripting symbols that this SDK can add to the project.
/// It is used to remove all symbols when it is required.
///
public abstract string[] AllSymbols { get; }
///
/// Gets whether the SDK can be updated. If so, is available.
///
public virtual bool CanBeUpdated => false;
///
/// Gets the package name if the SDK is distributed through the package manager.
///
public virtual string PackageName => null;
///
/// Gets the current SDK state as a string.
///
public string CurrentStateString
{
get
{
switch (CurrentState)
{
case State.Unknown: return StringUnknown;
case State.NeedsHigherUnityVersion: return StringNeedsHigherUnityVersion;
case State.CurrentTargetNotSupported: return $"{StringCurrentTargetNotSupported} ({EditorUserBuildSettings.activeBuildTarget})";
case State.NotInstalled: return StringNotInstalled;
case State.SoonSupported: return StringSoonSupported;
case State.Available: return StringAvailable;
}
return string.Empty;
}
}
///
/// Gets whether the SDK is a package. This will simplify dependency handling since Unity's Assembly system can take
/// care of it.
///
public bool IsPackage => !string.IsNullOrEmpty(PackageName);
///
/// Gets the current SDK state.
///
public State CurrentState { get; protected set; } = State.Unknown;
#endregion
#region Public Methods
///
/// Tries to find the given type name in the current assemblies. It is used to check for given types to see if an SDK
/// is installed or not.
///
/// Type name to look for
/// Boolean telling whether the type was found or not
public static bool IsTypeInAssemblies(string typeName)
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.GetType(typeName) != null)
{
return true;
}
}
return false;
}
///
/// Child SDK locators should implement this method. It will try to locate the SDK and update the internal state making
/// it available through or .
///
public abstract void TryLocate();
///
/// Child SDK locators can implement this method. It will try to get the SDK from somewhere (usually opening a specific
/// URL).
///
public virtual void TryGet()
{
}
///
/// Child SDK locators can implement this method if returns true. It will try to update the
/// SDK from somewhere (usually opening a specific URL).
///
public virtual void TryUpdate()
{
}
#endregion
#region Unity
///
/// Child SDK locators can implement Unity Editor functionality here. The inspector for will
/// iterate through all SDKs and for the installed ones, will allow to draw custom inspector UI underneath.
///
public virtual void OnInspectorGUI()
{
}
#endregion
#region Protected Types & Data
///
/// Gets or sets the incremental version number. It enables backwards compatibility across different SDKs.
///
protected int CurrentVersion { get; set; } = 0;
#endregion
#region Private Types & Data
private const string StringUnknown = "Unknown (not processed)";
private const string StringNeedsHigherUnityVersion = "Needs a higher Unity version";
private const string StringCurrentTargetNotSupported = "Current build target not supported by SDK";
private const string StringNotInstalled = "Not installed";
private const string StringSoonSupported = "Soon supported";
private const string StringAvailable = "Available";
#endregion
}
}