using System; using System.Collections.Generic; using UnityEngine; namespace Ubisoft.Hotel.Package.Editor { [Serializable] internal class PropertyRegistry { [SerializeField] private string m_key; [SerializeField] private int m_value; [SerializeField] private PackageProperty m_packageProperty; private Type m_type; internal PropertyRegistry(Type type, int value, PackageProperty packageProperty) { m_key = PackageSuiteSetup.GetKeyByType(type); Value = value; PackageProperty = packageProperty; } internal Type Type { get { if (m_type == null) { m_type = PackageSuiteSetup.GetTypeByKey(m_key); } return m_type; } //set => m_type = value; } internal int Value { get { return m_value; } set => m_value = value; } internal PackageProperty PackageProperty { get { return m_packageProperty; } set => m_packageProperty = value; } } [Serializable] public class PackageSuiteSetup { public static Type GetTypeByKey(string key) { return string.IsNullOrEmpty(key) ? null : HtTypes.GetType("Ubisoft.Consumer.PackageManager.Editor", key); } public static string GetKeyByType(Type type) { return (type == null) ? null : type.ToString(); } [SerializeField] private List m_keys; [SerializeField] private List m_selectedValues; [SerializeField] private List m_values; [SerializeField] private List m_properties; private Dictionary m_shortKeys; public PackageSuiteSetup(List types, List selectedValues) { if (types == null) { Keys = null; } else { Keys = new List(); foreach (Type t in types) { Keys.Add(t.ToString()); } } m_selectedValues = selectedValues; } public List Keys { get { return m_keys; } set { m_keys = value; } } public List SelectedValues { get { return m_selectedValues; } set { m_selectedValues = value; } } public List Values { get { if (m_values == null) { m_values = new List(); List types = Types; foreach (Type t in types) { m_values.Add(Enum.GetNames(t)); } } return m_values; } } public List Types { get { List returnValue = null; if (Keys != null) { returnValue = new List(); foreach (string key in Keys) { returnValue.Add(GetTypeByKey(key)); } } return returnValue; } } public string GetShortKeyByKey(string value) { string returnValue = null; if (!string.IsNullOrEmpty(value)) { if (m_shortKeys == null) { m_shortKeys = new Dictionary(); } if (!m_shortKeys.TryGetValue(value, out returnValue)) { string[] tokens = value.Split('+'); if (tokens.Length == 2) { returnValue = tokens[1]; m_shortKeys.Add(value, returnValue); } else { Debug.EditorLogError($"ERROR: {Debug.FormatTextInUserContext(value)} is not a valid name for PackageSuiteSetup type."); } } } return returnValue; } private string GetKeyByShortKey(string value) { string returnValue = value; if (!string.IsNullOrEmpty(value)) { string shortKey; foreach (string key in Keys) { shortKey = GetShortKeyByKey(key); if (shortKey == value) { returnValue = key; } } } return returnValue; } public void Override(Dictionary properties) { if (properties != null) { int index; string shortKey, value; string key; foreach (KeyValuePair pair in properties) { shortKey = pair.Key; value = pair.Value; if (string.IsNullOrEmpty(shortKey)) { Debug.EditorLogError($"ERROR: Null is not a valid PackageSuiteSetup type key"); } else if (string.IsNullOrEmpty(value)) { Debug.EditorLogError($"ERROR: Null is not a valid PackageSuiteSetup type value"); } else { key = GetKeyByShortKey(shortKey); index = GetKeyIndex(key); if (index == -1) { string msg = $"ERROR: {Debug.FormatTextInUserContext(key)} is not a valid PackageSuiteSetup type key"; Debug.EditorLogError(msg); } else { SetSelectedValueByKeyAsString(key, value); } } } } } public string[] GetValuesByTypeAsString(Type type) { return GetValuesByKeyAsString(GetKeyByType(type)); } public string[] GetValuesByKeyAsString(string key) { string[] returnValue = null; int index = GetKeyIndex(key); if (index > -1) { returnValue = Values[index]; } return returnValue; } public int GetSelectedValueByType(Type type) { int returnValue = -1; int index = GetTypeIndex(type); if (index > -1) { returnValue = m_selectedValues[index]; } return returnValue; } public int GetSelectedValueByKey(string key) { int returnValue = -1; int index = GetKeyIndex(key); if (index > -1) { returnValue = m_selectedValues[index]; } return returnValue; } public string GetSelectedValueByTypeAsString(Type type) { string key = GetKeyByType(type); return GetSelectedValueByKeyAsString(key); } public string GetSelectedValueByKeyAsString(string key) { string returnValue = null; int index = GetKeyIndex(key); if (index > -1) { string[] values = GetValuesByKeyAsString(key); returnValue = values[m_selectedValues[index]]; } return returnValue; } public void SetSelectedValueByKeyAsString(string key, string value) { int keyIndex = GetKeyIndex(key); if (keyIndex > -1) { int index = -1; string[] values = GetValuesByKeyAsString(key); for (int i = values.Length - 1; i > -1 && index == -1; --i) { if (values[i] == value) { index = i; } } if (index > -1) { m_selectedValues[keyIndex] = index; } } } public string GetSelectedValuesAsString(bool useShortKeys) { string returnValue = ""; string keyToken; foreach (string key in Keys) { if (!string.IsNullOrEmpty(returnValue)) { returnValue += " "; } keyToken = useShortKeys ? GetShortKeyByKey(key) : key; returnValue += $"{keyToken}:{GetSelectedValueByKeyAsString(key)}"; } return returnValue; } public void RegisterProperty(Type type, int value, PackageProperty property) { if (m_properties == null) { m_properties = new List(); } PropertyRegistry propertyRegistry = GetPropertyRegistry(type); if (propertyRegistry == null) { m_properties.Add(new PropertyRegistry(type, value, property)); } else { propertyRegistry.Value = value; propertyRegistry.PackageProperty = property; } } public bool HasChangedProperty(Type type) { bool returnValue = false; int value = GetSelectedValueByType(type); if (value > -1) { PropertyRegistry propertyRegistry = GetPropertyRegistry(type); if (propertyRegistry != null) { returnValue = propertyRegistry.Value != value; } } return returnValue; } public PackageProperty GetProperty(Type type) { PropertyRegistry propertyRegistry = GetPropertyRegistry(type); return propertyRegistry == null ? null : propertyRegistry.PackageProperty; } private PropertyRegistry GetPropertyRegistry(Type type) { PropertyRegistry returnValue = null; if (m_properties != null) { for (int i = m_properties.Count - 1; i > -1 && returnValue == null; --i) { if (m_properties[i].Type == type) { returnValue = m_properties[i]; } } } return returnValue; } private int GetTypeIndex(Type type) { int returnValue = -1; if (type != null) { string key = GetKeyByType(type); if (key != null) { returnValue = GetKeyIndex(key); } } if (returnValue == -1) { Debug.EditorLogError($"ERROR: Type {Debug.FormatTextInUserContext(type.ToString())} is not a valid PackageSuiteSetup type"); } return returnValue; } private int GetKeyIndex(string key) { int returnValue = -1; if (!string.IsNullOrEmpty(key)) { returnValue = Keys.IndexOf(key); } if (returnValue == -1) { Debug.EditorLogError($"ERROR: Key {Debug.FormatTextInUserContext(key)} is not a valid PackageSuiteSetup key"); } return returnValue; } } }