using System;
using System.Collections.Generic;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
namespace UnityEditor.AddressableAssets.HostingServices
{
///
/// Base class for hosting services.
///
public abstract class BaseHostingService : IHostingService
{
const string k_HostingServiceContentRootKey = "ContentRoot";
const string k_IsHostingServiceRunningKey = "IsEnabled";
const string k_DescriptiveNameKey = "DescriptiveName";
internal const string k_InstanceIdKey = "InstanceId";
internal bool WasEnabled { get; set; }
void OnPlayModeStateChanged(PlayModeStateChange obj) => SaveEnabledState();
void OnQuitting() => SaveEnabledState();
void SaveEnabledState() => WasEnabled = IsHostingServiceRunning;
internal void OnEnable()
{
EditorApplication.quitting += OnQuitting;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
if (!IsHostingServiceRunning && WasEnabled)
StartHostingService();
}
internal void OnDisable()
{
SaveEnabledState();
EditorApplication.quitting -= OnQuitting;
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
StopHostingService();
}
///
/// List of content roots for hosting service.
///
public abstract List HostingServiceContentRoots { get; }
///
/// Dictionary of profile variables defined by the hosting service.
///
public abstract Dictionary ProfileVariables { get; }
///
/// Gets the current running status of the hosting service.
///
public abstract bool IsHostingServiceRunning { get; }
///
/// Starts the hosting service.
///
public abstract void StartHostingService();
///
/// Stops the hosting service.
///
public abstract void StopHostingService();
///
/// Render the hosting service GUI.
///
public abstract void OnGUI();
ILogger m_Logger = Debug.unityLogger;
///
/// Get and set the logger for the hosting service.
///
public ILogger Logger
{
get { return m_Logger; }
set { m_Logger = value ?? Debug.unityLogger; }
}
///
/// Decodes a profile variable lookup ID based on string key
///
/// the key to look up
/// The variable lookup ID.
protected virtual string DisambiguateProfileVar(string key)
{
return string.Format("{0}.ID_{1}", key, InstanceId);
}
///
public virtual string DescriptiveName { get; set; }
///
public virtual int InstanceId { get; set; }
///
public virtual string EvaluateProfileString(string key)
{
string retVal;
ProfileVariables.TryGetValue(key, out retVal);
return retVal;
}
///
public virtual void OnBeforeSerialize(KeyDataStore dataStore)
{
dataStore.SetData(k_HostingServiceContentRootKey, string.Join(";", HostingServiceContentRoots.ToArray()));
dataStore.SetData(k_IsHostingServiceRunningKey, WasEnabled);
dataStore.SetData(k_DescriptiveNameKey, DescriptiveName);
dataStore.SetData(k_InstanceIdKey, InstanceId);
}
///
public virtual void OnAfterDeserialize(KeyDataStore dataStore)
{
var contentRoots = dataStore.GetData(k_HostingServiceContentRootKey, string.Empty);
HostingServiceContentRoots.AddRange(contentRoots.Split(';'));
WasEnabled = dataStore.GetData(k_IsHostingServiceRunningKey, false);
DescriptiveName = dataStore.GetDataString(k_DescriptiveNameKey, string.Empty);
InstanceId = dataStore.GetData(k_InstanceIdKey, -1);
}
static T[] ArrayPush(T[] arr, T val)
{
var newArr = new T[arr.Length + 1];
Array.Copy(arr, newArr, arr.Length);
newArr[newArr.Length - 1] = val;
return newArr;
}
///
/// Logs a formatted message to the Logger specifically on this service.
///
/// Severity of the log
/// The base string
/// The parameters to be formatted into the base string
protected void LogFormat(LogType logType, string format, object[] args)
{
Logger.LogFormat(logType, format, ArrayPush(args, this));
}
///
/// Logs an info severity formatted message to the Logger specifically on this service.
///
/// The base string
/// The parameters to be formatted into the base string
protected void Log(string format, params object[] args)
{
LogFormat(LogType.Log, format, args);
}
///
/// Logs an warning severity formatted message to the Logger specifically on this service.
///
/// The base string
/// The parameters to be formatted into the base string
protected void LogWarning(string format, params object[] args)
{
LogFormat(LogType.Warning, format, args);
}
///
/// Logs an error severity formatted message to the Logger specifically on this service.
///
/// The base string
/// The parameters to be formatted into the base string
protected void LogError(string format, params object[] args)
{
LogFormat(LogType.Error, format, args);
}
}
}