예제 #1
0
        public static void GetOrSetProperty(this CommandModule self, object config)
        {
            if (!self.Context.Args.TryGetFirst(out var propertyNameOrIndex))
            {
                self.ShowConfigurableProperties(config);
                return;
            }

            if (propertyNameOrIndex == "all")
            {
                self.ShowConfigurablePropertyValues(config);
                return;
            }

            var promoLevel = self.Context.Player?.PromoteLevel ?? MyPromoteLevel.Admin;
            var properties = GetConfigurableProperties(config, promoLevel).ToArray();

            var propertyName = propertyNameOrIndex;

            if (int.TryParse(propertyNameOrIndex, out var propertyIndex))
            {
                var maxPropertyIndex = properties.Length - 1;
                if (maxPropertyIndex < propertyIndex)
                {
                    self.Context.Respond($"Index out of bounds; max: {maxPropertyIndex}", Color.Red);
                    self.ShowConfigurableProperties(config);
                    return;
                }

                propertyName = properties[propertyIndex].Name;
            }

            if (!properties.TryGetFirst(p => p.Name == propertyName, out var property))
            {
                self.Context.Respond($"Property not found: \"{propertyName}\"", Color.Red);
                self.ShowConfigurableProperties(config);
                return;
            }

            if (property.TryGetAttribute(out ConfigPropertyAttribute prop) &&
                !prop.IsVisibleTo(promoLevel))
            {
                self.Context.Respond($"Property not visible: \"{propertyName}\"", Color.Red);
                self.ShowConfigurableProperties(config);
                return;
            }

            if (promoLevel == MyPromoteLevel.Admin &&
                self.Context.Args.TryGetElementAt(1, out var arg))
            {
                var newValue = ParsePrimitive(property.PropertyType, arg);
                property.SetValue(config, newValue);
            }

            var value = property.GetValue(config);

            self.Context.Respond($"> {propertyName}: {value}");
        }