예제 #1
0
        private void SetPluginSetting(string key, string value)
        {
            PropertyInfo property = settings.GetType().GetProperty(key);

            if (property != null)
            {
                foreach (PluginSettingAttribute attribute in property.GetCustomAttributes(typeof(PluginSettingAttribute), false))
                {
                    object propertyValue = GetSettingValueFromConfigValue(attribute, property.PropertyType, value);
                    property.SetValue(settings, propertyValue, null);
                }
            }
        }
		public static Dictionary<string, object> GetPluginSettings(this IPluginSettings plugin)
		{
			// TODO: deal witn the game depedent settings
			var dictionary = new Dictionary<string, object>();
			var members = plugin.GetType().GetMembers();
			foreach (var member in members)
			{
				if (member.MemberType == MemberTypes.Property)
				{
					var field = plugin.GetType().GetProperty(member.Name).GetValue(plugin, null);
					dictionary.Add(member.Name, field);
				}
			}

			return dictionary;
		}
예제 #3
0
        public static IEnumerable<AddInsParameter> GetParameters(this IPluginSettings settings)
        {
            var properties = settings.GetType().GetProperties();
            var result = new List<AddInsParameter>();
            foreach (var prop in properties)
            {
                var attribute = prop.GetCustomAttribute<AddInsParameter>();
                if (attribute == null) continue;
                attribute.PropertyName = prop.Name;
                if (prop.PropertyType.Name == "Boolean") attribute.ControlType = ControlType.CheckBox;
                else if (attribute.AvailableValue != null) attribute.ControlType = ControlType.ComboBox;
                else attribute.ControlType = ControlType.TextBox;

                //attribute.ControlType = IPluginSettingExtension.GetInputType(prop.PropertyType);
                result.Add(attribute);
            }
            return result;
        }
예제 #4
0
        public void SaveSettings(IPluginSettings settings)
        {
            string name = settings.GetType().GetAssemblyName() + ".json";

            if (string.IsNullOrEmpty(name) || name == ".json")
            {
                return;
            }

            try
            {
                string settingsPath = Path.Combine(ServerSettings.ApplicationPath, "Plugins", name);
                Directory.CreateDirectory(Path.Combine(ServerSettings.ApplicationPath, "Plugins"));
                string json = ServerSettings.Serialize(settings);
                File.WriteAllText(settingsPath, json);
            }
            catch (Exception e)
            {
                Logger.Error(e, $"Unable to Save Settings for {name}");
            }
        }
예제 #5
0
        //Можно ли устранить дублирование кода
        public static bool SetParameters(this IPluginSettings settings, IEnumerable<AddInsParameter> parameters)
        {

            var result = true;
            var type = settings.GetType();
            //Нужно проверять, что все свойства получили значение, а не количество свойств.
            if (type.GetProperties().Length != parameters.Count()) return false;
            foreach (var param in parameters)
            {
                if (param.Value == null || param.Value ==string.Empty)
                {
                    param.ErrorMessage = "Значение не задано";
                    result = false;
                    continue;
                }
                if (param.AvailableValue != null && !param.AvailableValue.Contains(param.Value))
                {
                    param.ErrorMessage = "Задано недопустимое значение";
                    result = false;
                    continue;
                }

                var prop = type.GetProperty(param.PropertyName);
                if (prop != null && !(param.Value == null))
                {
                    switch (prop.PropertyType.Name)
                    {
                        case "Int32":
                            int parseInt = 0;
                            if (int.TryParse(param.Value, out parseInt))
                                prop.SetValue(settings, parseInt);
                            else
                                param.ErrorMessage = "Не является целым числом";
                            break;
                        case "Double":
                            double parseDouble = 0;
                            if (double.TryParse(param.Value, out parseDouble))
                                prop.SetValue(settings, double.Parse(param.Value));
                            else
                            {
                                param.ErrorMessage = "Не является числом с плавающей точкой";
                                result = false;
                            }
                            break;
                        case "Boolean":
                            bool parseBool = false;
                            if (bool.TryParse(param.Value, out parseBool))
                                prop.SetValue(settings, parseBool);
                            else
                            {
                                param.ErrorMessage = "Задано некорректное значение";
                                result = false;
                            }
                            break;
                        default:
                            prop.SetValue(settings, param.Value);
                            break;
                    }
                }
                else
                {
                    param.ErrorMessage += "Не удалось найти свойство";
                    result = false;
                }
                var propValue = prop.GetValue(settings);
                if (propValue != null && propValue.ToString() != param.Value)
                {
                    param.ErrorMessage = "Не удалось установить значение для свойтсва";
                    result = false;
                }
            }
            return result;
        }
예제 #6
0
 public PluginSettingsPropertyDescriptor(IPluginSettings pluginSettings)
     : base(pluginSettings.GetType().Name, BuildAttributeArray(pluginSettings))
 {
     this.pluginSettings = pluginSettings;
 }
예제 #7
0
 public PluginSettingsPropertyDescriptor(IPluginSettings pluginSettings)
     : base(pluginSettings.GetType().Name, BuildAttributeArray(pluginSettings))
 {
     this.pluginSettings = pluginSettings;
 }