using System; using System.Collections.Generic; using UnityEngine; using Ubisoft.Hotel.Package; namespace Ubisoft.Hotel.Logger { /// /// Class responsible for storing the list of channels to enable or disable and whether or not the rest of channels need to be enabled. /// how to treat the channels //[Serializable] [CreateAssetMenu(fileName = "channelsPreset", menuName = "Hotel/Core/Logger/ChannelsPreset")] public class ChannelsPreset : PlayerPackageProperty { public static ChannelsPreset CreateInstanceWithParams(string name) { ChannelsPreset returnValue = CreateInstance(); returnValue.name = name; returnValue.Name = name; return returnValue; } /// /// Class responsible for storing the name of a channel and a flag that states whether or not this channel is supposed to be enabled. /// /// It implements IComparable so a list of ChannelSettings object can be sorted based on channel names. /// [Serializable] public class ChannelSettings : IComparable { public const string ATT_NAME = "name"; public const string ATT_ENABLED = "enabled"; #pragma warning disable IDE1006 // Naming Styles public string name; public bool enabled; #pragma warning restore IDE1006 // Naming Styles internal ChannelSettings(string name, bool enabled) { this.name = name; this.enabled = enabled; } public int CompareTo(ChannelSettings other) { // Ascending alphabetical order return name.CompareTo(other.name); } } public const string ATT_ROOT_UNITY_PATH = "m_rootUnityPath"; public const string ATT_NAME = "m_name"; /// /// Root Unity path ("Assets/...") to the folder that contains all ChannelsPreset objects. /// [SerializeField] private string m_rootUnityPath; /// /// Name of this ChannelsPreset object. /// [SerializeField] private string m_name; /// /// List of channels settings. /// [SerializeField] private List m_channelSettings; /// /// When true all other channels with no settings defined in m_channelSettings will be enabled, otherwise /// they will be disabled. /// [SerializeField] private bool m_otherChannels = true; public bool OtherChannels { get { return m_otherChannels; } } public string RootUnityPath { get { return m_rootUnityPath; } set { m_rootUnityPath = value; } } public string Name { get { return m_name; } set { m_name = value; } } public void Clear() { if (m_channelSettings != null) { m_channelSettings.Clear(); } } public void CopyFrom(ChannelsPreset value) { Clear(); if (value != null) { if (value.m_channelSettings != null) { if (m_channelSettings == null) { m_channelSettings = new List(); } int count = value.m_channelSettings.Count; for (int i = 0; i < count; ++i) { if (value.m_channelSettings[i] != null) { m_channelSettings.Add(new ChannelSettings(value.m_channelSettings[i].name, value.m_channelSettings[i].enabled)); } } } m_otherChannels = value.OtherChannels; } } public HashSet GetEnabledChannels() { return GetChannels(true); } public HashSet GetDisabledChannels() { return GetChannels(false); } public void EnableAll() { SetIsEnabled(true); } public void DisableAll() { SetIsEnabled(false); } public void Sort() { if (m_channelSettings != null) { m_channelSettings.Sort(); } } public void AddChannel(string name, bool enabled) { if (m_channelSettings == null) { m_channelSettings = new List(); } // Search for a channel called 'name'. If found then its 'enabled' field is updated, otherwise a new ChannelSettings object is created. ChannelSettings channel = GetChannelSettings(name); if (channel == null) { m_channelSettings.Add(new ChannelSettings(name, enabled)); } else { channel.enabled = enabled; } } public void Validate() { // Make sure all channel names are different if (m_channelSettings != null) { HashSet channelNames = new HashSet(); int count = m_channelSettings.Count; for (int i = 0; i < count; ++i) { if (channelNames.Contains(m_channelSettings[i].name)) { m_channelSettings[i].name += " 1"; } _ = channelNames.Add(m_channelSettings[i].name); } } } private void SetIsEnabled(bool value) { if (m_channelSettings != null) { int count = m_channelSettings.Count; for (int i = 0; i < count; ++i) { m_channelSettings[i].enabled = value; } } m_otherChannels = value; } private ChannelSettings GetChannelSettings(string name) { ChannelSettings returnValue = null; if (m_channelSettings != null) { int count = m_channelSettings.Count; for (int i = 0; i < count && returnValue == null; ++i) { if (m_channelSettings[i].name == name) { returnValue = m_channelSettings[i]; } } } return returnValue; } private HashSet GetChannels(bool enabled) { HashSet returnValue = new HashSet(); if (m_channelSettings != null) { int count = m_channelSettings.Count; for (int i = 0; i < count; ++i) { if (m_channelSettings[i] != null && m_channelSettings[i].enabled == enabled) { _ = returnValue.Add(m_channelSettings[i].name); } } } return returnValue; } } }