/// <summary> /// Returns feature friendly name. /// </summary> /// <param name="feature">Feature.</param> /// <returns>Feature name.</returns> public string GetFeatureName(KnownFeature feature) { var attribs = typeof(KnownFeature).GetField(feature.ToString())?.GetCustomAttributes(typeof(DescriptionAttribute), false); var name = (attribs == null) || (attribs.Length == 0) ? feature.ToString() : ((DescriptionAttribute)attribs[0]).Description; return(name); }
/// <summary> /// Deletes option. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> public void DeleteOption(KnownFeature feature, string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } var options = GetOptions(feature, false); if (options == null) { return; } if (options.ContainsKey(name)) { options.Remove(name); } if (!_deletedOptions.ContainsKey(feature)) { _deletedOptions.TryAdd(feature, new List <string>(2)); } var deletedOptions = _deletedOptions[feature]; if (!deletedOptions.Contains(name, StringComparer.OrdinalIgnoreCase)) { deletedOptions.Add(name); } }
/// <summary> /// Loads feature default options. /// </summary> /// <param name="feature">Feature.</param> /// <returns>Default options.</returns> public IDictionary <string, object> LoadDefaultOptions(KnownFeature feature) { Type type = null; var featureTypes = this.GetType().Assembly.GetTypes().Where(t => typeof(Feature).IsAssignableFrom(t)); foreach (var featureType in featureTypes) { if (featureType.IsClass && !featureType.IsAbstract) { var attribs = featureType.GetCustomAttributes(typeof(KnownFeatureAttribute), false); if ((attribs != null) && (attribs.Length > 0) && (((KnownFeatureAttribute)attribs[0]).Feature == feature)) { type = featureType; break; } ; } } var defaultAttribs = type?.GetCustomAttributes(typeof(OptionsDefaultsAttribute), false); if ((defaultAttribs == null) || (defaultAttribs.Length == 0)) { return(null); } var options = new Dictionary <string, object>(defaultAttribs.Length, StringComparer.OrdinalIgnoreCase); defaultAttribs.Cast <OptionsDefaultsAttribute>().ForEach(a => options.Add(a.OptionName, a.DefaultValue)); return(options); }
private void RaiseReset(KnownFeature feature) { if (_options.ContainsKey(feature)) { Reset?.Invoke(this, new FeatureEventArgs(feature)); } }
/// <summary> /// Flushes option values to the storage. /// </summary> /// <param name="feature">Feature.</param> public void Flush(KnownFeature feature) { if (Save(feature)) { RaiseChanged(feature); } }
/// <summary> /// Saves all feature properties to the storage. /// </summary> private bool Save(KnownFeature feature) { if (!_options.ContainsKey(feature)) { return(false); } if (_deletedOptions.ContainsKey(feature)) { var options = _options[feature]; foreach (var deleted in _deletedOptions[feature]) { if (options.ContainsKey(deleted)) { options.Remove(deleted); } } } _optionsPersistenceService.SaveOptions(feature, _options[feature]); _log.LogMessage($"Options saved: {feature.GetDescription()}", LOG_CATEGORY); return(true); }
private void RaiseChanged(KnownFeature feature) { if (_options.ContainsKey(feature)) { Changed?.Invoke(this, new FeatureEventArgs(feature)); } }
/// <summary> /// Custom: logs feature message and exception. /// </summary> public static void LogMessage(this ILog log, KnownFeature feature, string message, Exception ex) { var featureMessage = $"{feature}:{Log.INDENT}"; message = string.IsNullOrEmpty(message) ? featureMessage : featureMessage + message; log.LogMessage(message, ex); }
/// <summary> /// Deletes all properties from the storage. /// </summary> private void DeleteAll(KnownFeature feature) { if (_options.ContainsKey(feature)) { _optionsPersistenceService.DeleteOptions(feature); } Clear(feature); ApplyDefaults(feature); }
private void ApplyDefaults(KnownFeature feature) { if (!_options.ContainsKey(feature)) { return; } var defaultOptions = _optionsPersistenceService.LoadDefaultOptions(feature); defaultOptions?.Keys.ForEach(o => SetOption(feature, o, defaultOptions[o])); }
/// <summary> /// Returns options count. /// </summary> /// <param name="feature">Feature.</param> /// <returns>Count.</returns> /// <remarks>Used for testing only.</remarks> public int GetOptionCount(KnownFeature feature) { if (_options.ContainsKey(feature)) { return(_options[feature].Count); } else { return(0); } }
/// <summary> /// Sets boolean option value. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> /// <param name="value">Option value.</param> public void SetBoolOption(KnownFeature feature, string name, bool value) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } var options = GetOptions(feature); options[name] = Convert.ToInt32(value); }
/// <summary> /// Sets integer option value. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> /// <param name="value">Option value.</param> public void SetIntOption(KnownFeature feature, string name, int value) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } var options = GetOptions(feature); options[name] = value; }
private void Clear(KnownFeature feature) { if (_options.ContainsKey(feature)) { _options[feature].Clear(); } if (_deletedOptions.ContainsKey(feature)) { _deletedOptions[feature].Clear(); } }
public KnownFeature GetKnownFeatureFromFeature(Object.Feature searchedFeature) { KnownFeature potentialknownFeature = null; for (int i = 0; i < knownFeatures.Count; i++) { if (searchedFeature == knownFeatures[i].feature) { potentialknownFeature = knownFeatures[i]; } } return(potentialknownFeature); }
public void LoadDefaultOptions(KnownFeature feature, int expectedCount) { var service = GetService(); var defaults = service.LoadDefaultOptions(feature); if (expectedCount == 0) { Assert.That(defaults, Is.Null); } else { Assert.That(defaults, Is.Not.Null); Assert.That(defaults.Count, Is.EqualTo(expectedCount)); } }
public PotentialObject(StallObject _standObject) { stallObject = _standObject; interestLevel = 0; curiosityLevel = 0; knownFeatures = new List <KnownFeature>(); for (int i = 0; i < stallObject.linkedObject.features.Count; i++) { KnownFeature newKnownFeature = new KnownFeature(); newKnownFeature.feature = stallObject.linkedObject.features[i]; newKnownFeature.interest = 0; newKnownFeature.timeRememberedRmn = 0; newKnownFeature.isKnown = false; knownFeatures.Add(newKnownFeature); } }
/// <summary> /// Checks whether options exists. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> /// <returns>Option status.</returns> public bool OptionExists(KnownFeature feature, string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } var options = GetOptions(feature); if (options == null) { return(false); } return(!string.IsNullOrEmpty(name) && options.ContainsKey(name)); }
public void OptionExists(KnownFeature feature, string name, bool expectedResult, bool expectedLoadDefaults) { var service = GetService(); var result = service.OptionExists(feature, name); Assert.That(result, Is.EqualTo(expectedResult)); _optionsPersistenceServiceMock.Verify(o => o.LoadOptions(feature)); if (expectedLoadDefaults) { _optionsPersistenceServiceMock.Verify(o => o.LoadDefaultOptions(feature)); } else { _optionsPersistenceServiceMock.Verify(o => o.LoadDefaultOptions(feature), Times.Never); } }
/// <summary> /// Loads all properties from the storage. /// </summary> private void Load(KnownFeature feature) { Clear(feature); var options = _optionsPersistenceService.LoadOptions(feature); if (_options.ContainsKey(feature)) { _options.TryRemove(feature, out _); } _options.TryAdd(feature, options); if (options.Count == 0) { ApplyDefaults(feature); } }
private IDictionary <string, object> GetOptions(KnownFeature feature, bool createIfMissing = true) { if (!_options.ContainsKey(feature)) { if (createIfMissing) { Load(feature); } else { return(null); } } var options = _options[feature]; return(options); }
/// <summary> /// Returns string option value. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> /// <param name="defaultValue">Default option value.</param> /// <returns>Option value.</returns> public string GetStringOption(KnownFeature feature, string name, string defaultValue = null) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } var options = GetOptions(feature); if ((options != null) && options.ContainsKey(name)) { var option = options[name]; if (option is string) { return((string)option); } } return(defaultValue != null ? defaultValue : string.Empty); }
public void GetOptionCount(KnownFeature feature, int expectedCount) { var service = GetService(); var countBeforeLoad = service.GetOptionCount(feature); service.Reload(feature); var countAfterLoad = service.GetOptionCount(feature); Assert.That(countAfterLoad, Is.EqualTo(expectedCount)); _optionsPersistenceServiceMock.Verify(o => o.LoadOptions(feature), Times.Once); if (expectedCount == 0) { _optionsPersistenceServiceMock.Verify(o => o.LoadDefaultOptions(feature), Times.Once); } else { _optionsPersistenceServiceMock.Verify(o => o.LoadDefaultOptions(feature), Times.Never); } }
/// <summary> /// Returns boolean option value. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> /// <param name="defaultValue">Default option value.</param> /// <returns>Option value.</returns> public bool GetBoolOption(KnownFeature feature, string name, bool defaultValue = false) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } var options = GetOptions(feature); if ((options != null) && options.ContainsKey(name)) { var option = options[name]; if (option is int) { return(((int)option) == 1); } } return(defaultValue); }
/// <summary> /// Saves feature options to the storage. /// </summary> /// <param name="feature">Feature.</param> /// <param name="options">Options.</param> public void SaveOptions(KnownFeature feature, IDictionary <string, object> options) { if (options == null) { return; } RegistryKey featureKey; try { featureKey = _packageService.GetUserRegistryRootKey(feature.ToString()); } catch { featureKey = null; } if (featureKey == null) { _log.LogMessage($"Failed to saved {feature} options", LOG_CATEGORY); return; } using (featureKey) { // Delete existing values first foreach (var value in featureKey.GetValueNames()) { featureKey.DeleteValue(value); } foreach (var option in options.Keys) { var customValue = options[option]; if (customValue != null) { featureKey.SetValue(option, customValue); } } } }
/// <summary> /// Sets option value. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> /// <param name="value">Option value.</param> public void SetOption(KnownFeature feature, string name, object value) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } if (value is string) { SetStringOption(feature, name, (string)value); } else if (value is int) { SetIntOption(feature, name, (int)value); } else if (value is bool) { SetBoolOption(feature, name, (bool)value); } }
/// <summary> /// Loads feature options from the storage. /// </summary> /// <param name="feature">Feature.</param> /// <returns>Options.</returns> public IDictionary <string, object> LoadOptions(KnownFeature feature) { RegistryKey featureKey; try { featureKey = _packageService.GetUserRegistryRootKey(feature.ToString()); } catch { featureKey = null; } var options = new Dictionary <string, object>(4, StringComparer.OrdinalIgnoreCase); if (featureKey != null) { using (featureKey) { var names = featureKey.GetValueNames(); for (int index = 0; index < names.Length; index++) { var name = names[index]; var customValue = featureKey.GetValue(name); if (customValue != null) { options.Add(name, customValue); } } } } else { _log.LogMessage($"No {feature} options to load", LOG_CATEGORY); } return(options); }
/// <summary> /// Returns option value. /// </summary> /// <param name="feature">Feature.</param> /// <param name="name">Option name.</param> /// <returns>Option value.</returns> public object GetOption(KnownFeature feature, string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } var options = GetOptions(feature); if (options == null) { return(null); } if (options.ContainsKey(name)) { return(options[name]); } else { return(null); } }
/// <summary> /// Saves all feature properties to the storage. /// </summary> private bool Save(KnownFeature feature) { if (!_options.ContainsKey(feature)) { return(false); } if (_deletedOptions.ContainsKey(feature)) { var options = _options[feature]; foreach (var deleted in _deletedOptions[feature]) { if (options.ContainsKey(deleted)) { options.Remove(deleted); } } } _optionsPersistenceService.SaveOptions(feature, _options[feature]); return(true); }
/// <summary> /// Deletes all feature options from the storage. /// </summary> /// <param name="feature">Feature.</param> public void DeleteOptions(KnownFeature feature) { RegistryKey featureKey; try { featureKey = _packageService.GetUserRegistryRootKey(feature.ToString(), false); } catch { featureKey = null; } if (featureKey != null) { using (featureKey) { foreach (var value in featureKey.GetValueNames()) { featureKey.DeleteValue(value); } } } }