/********* ** Properties *********/ /// <summary>Parse a raw config schema for a content pack.</summary> /// <param name="rawSchema">The raw config schema.</param> /// <param name="logWarning">The callback to invoke on each validation warning, passed the field name and reason respectively.</param> private InvariantDictionary <ConfigField> LoadConfigSchema(InvariantDictionary <ConfigSchemaFieldConfig> rawSchema, Action <string, string> logWarning) { InvariantDictionary <ConfigField> schema = new InvariantDictionary <ConfigField>(); if (rawSchema == null || !rawSchema.Any()) { return(schema); } foreach (string key in rawSchema.Keys) { ConfigSchemaFieldConfig field = rawSchema[key]; // validate key if (Enum.TryParse(key, true, out ConditionKey conditionKey)) { logWarning(key, $"can't use {conditionKey} as a config field, because it's a reserved condition name."); continue; } // read allowed values InvariantHashSet allowValues = this.ParseCommaDelimitedField(field.AllowValues); if (!allowValues.Any()) { logWarning(key, $"no {nameof(ConfigSchemaFieldConfig.AllowValues)} specified."); continue; } // read default values InvariantHashSet defaultValues = this.ParseCommaDelimitedField(field.Default); { // inject default if (!defaultValues.Any() && !field.AllowBlank) { defaultValues = new InvariantHashSet(allowValues.First()); } // validate values string[] invalidValues = defaultValues.Except(allowValues).ToArray(); if (invalidValues.Any()) { logWarning(key, $"default values '{string.Join(", ", invalidValues)}' are not allowed according to {nameof(ConfigSchemaFieldConfig.AllowBlank)}."); continue; } // validate allow multiple if (!field.AllowMultiple && defaultValues.Count > 1) { logWarning(key, $"can't have multiple default values because {nameof(ConfigSchemaFieldConfig.AllowMultiple)} is false."); continue; } } // add to schema schema[key] = new ConfigField(allowValues, defaultValues, field.AllowBlank, field.AllowMultiple); } return(schema); }
/********* ** Private methods *********/ /// <summary>Parse a raw config schema for a content pack.</summary> /// <param name="rawSchema">The raw config schema.</param> /// <param name="logWarning">The callback to invoke on each validation warning, passed the field name and reason respectively.</param> private InvariantDictionary <ConfigField> LoadConfigSchema(InvariantDictionary <ConfigSchemaFieldConfig> rawSchema, Action <string, string> logWarning) { InvariantDictionary <ConfigField> schema = new InvariantDictionary <ConfigField>(); if (rawSchema == null || !rawSchema.Any()) { return(schema); } foreach (string rawKey in rawSchema.Keys) { ConfigSchemaFieldConfig field = rawSchema[rawKey]; // validate format if (!TokenName.TryParse(rawKey, out TokenName name)) { logWarning(rawKey, $"the name '{rawKey}' is not in a valid format."); continue; } if (name.HasSubkey()) { logWarning(rawKey, $"the name '{rawKey}' can't have a subkey (:)."); continue; } // validate reserved keys if (name.TryGetConditionType(out ConditionType _)) { logWarning(rawKey, $"can't use {name.Key} as a config field, because it's a reserved condition key."); continue; } // read allowed values InvariantHashSet allowValues = this.ParseCommaDelimitedField(field.AllowValues); if (!allowValues.Any()) { logWarning(rawKey, $"no {nameof(ConfigSchemaFieldConfig.AllowValues)} specified."); continue; } // read default values InvariantHashSet defaultValues = this.ParseCommaDelimitedField(field.Default); { // inject default if (!defaultValues.Any() && !field.AllowBlank) { defaultValues = new InvariantHashSet(allowValues.First()); } // validate values string[] invalidValues = defaultValues.ExceptIgnoreCase(allowValues).ToArray(); if (invalidValues.Any()) { logWarning(rawKey, $"default values '{string.Join(", ", invalidValues)}' are not allowed according to {nameof(ConfigSchemaFieldConfig.AllowBlank)}."); continue; } // validate allow multiple if (!field.AllowMultiple && defaultValues.Count > 1) { logWarning(rawKey, $"can't have multiple default values because {nameof(ConfigSchemaFieldConfig.AllowMultiple)} is false."); continue; } } // add to schema schema[rawKey] = new ConfigField(allowValues, defaultValues, field.AllowBlank, field.AllowMultiple); } return(schema); }
/********* ** Private methods *********/ /// <summary>Parse a raw config schema for a content pack.</summary> /// <param name="rawSchema">The raw config schema.</param> /// <param name="logWarning">The callback to invoke on each validation warning, passed the field name and reason respectively.</param> /// <param name="formatVersion">The content format version.</param> private InvariantDictionary <ConfigField> LoadConfigSchema(InvariantDictionary <ConfigSchemaFieldConfig> rawSchema, Action <string, string> logWarning, ISemanticVersion formatVersion) { InvariantDictionary <ConfigField> schema = new InvariantDictionary <ConfigField>(); if (rawSchema == null || !rawSchema.Any()) { return(schema); } foreach (string rawKey in rawSchema.Keys) { ConfigSchemaFieldConfig field = rawSchema[rawKey]; // validate format if (string.IsNullOrWhiteSpace(rawKey)) { logWarning(rawKey, "the config field name can't be empty."); continue; } if (rawKey.Contains(InternalConstants.PositionalInputArgSeparator) || rawKey.Contains(InternalConstants.NamedInputArgSeparator)) { logWarning(rawKey, $"the name '{rawKey}' can't have input arguments ({InternalConstants.PositionalInputArgSeparator} or {InternalConstants.NamedInputArgSeparator} character)."); continue; } // validate reserved keys if (Enum.TryParse <ConditionType>(rawKey, true, out _)) { logWarning(rawKey, $"can't use {rawKey} as a config field, because it's a reserved condition key."); continue; } // read allowed/default values InvariantHashSet allowValues = this.ParseCommaDelimitedField(field.AllowValues); InvariantHashSet defaultValues = this.ParseCommaDelimitedField(field.Default); // pre-1.7 behaviour if (formatVersion.IsOlderThan("1.7")) { // allowed values are required if (!allowValues.Any()) { logWarning(rawKey, $"no {nameof(ConfigSchemaFieldConfig.AllowValues)} specified (and format version is less than 1.7)."); continue; } // inject default if needed if (!defaultValues.Any() && !field.AllowBlank) { defaultValues = new InvariantHashSet(allowValues.First()); } } // validate allowed values if (!field.AllowBlank && !defaultValues.Any()) { logWarning(rawKey, $"if {nameof(field.AllowBlank)} is false, you must specify {nameof(field.Default)}."); continue; } if (allowValues.Any() && defaultValues.Any()) { string[] invalidValues = defaultValues.ExceptIgnoreCase(allowValues).ToArray(); if (invalidValues.Any()) { logWarning(rawKey, $"default values '{string.Join(", ", invalidValues)}' are not allowed according to {nameof(ConfigSchemaFieldConfig.AllowValues)}."); continue; } } // validate allow multiple if (!field.AllowMultiple && defaultValues.Count > 1) { logWarning(rawKey, $"can't have multiple default values because {nameof(ConfigSchemaFieldConfig.AllowMultiple)} is false."); continue; } // add to schema schema[rawKey] = new ConfigField(allowValues, defaultValues, field.AllowBlank, field.AllowMultiple, field.Description); } return(schema); }