public DataNode(string name, DataNode parent, IContractConfiguratorFactory factory) { this.parent = parent; this.factory = factory; this.name = name; if (parent != null && parent.factory.GetType() != typeof(ContractGroup)) { // Check for duplicate name - probably could be more efficient here int i = 1; while (parent.children.Any(dn => dn.name == this.name)) { this.name = name + "_" + i++; } parent.children.Add(this); root = parent.root; } else if (factory != null && factory.GetType() != typeof(ContractGroup)) { root = this; } else { root = null; } }
public DataNode(string name, DataNode parent, IContractConfiguratorFactory factory) { this.parent = parent; this.factory = factory; this.name = name; if (parent != null && parent.factory.GetType() != typeof(ContractGroup)) { // Check for duplicate name - probably could be more efficient here int i = 1; while (parent.children.Any(dn => dn.name == this.name)) { this.name = name + "_" + i++; } parent.children.Add(this); root = parent.root; } else if (factory != null && factory.GetType() != typeof(ContractGroup)) { root = this; } else { root = null; } }
/// <summary> /// Performs validation to check if the given config node has values that were not expected. /// </summary> /// <param name="configNode">The ConfigNode to check.</param> /// <param name="obj">IContractConfiguratorFactory object for error reporting</param> /// <returns>Always true, but logs a warning if unexpected keys were found</returns> public static bool ValidateUnexpectedValues(ConfigNode configNode, IContractConfiguratorFactory obj) { bool valid = true; if (!keysFound.ContainsKey(configNode)) { obj.hasWarnings = true; LoggingUtil.LogWarning(obj.GetType(), obj.ErrorPrefix() + ": did not attempt to load values for ConfigNode!"); return(false); } Dictionary <string, int> found = keysFound[configNode]; foreach (ConfigNode.Value pair in configNode.values) { if (!found.ContainsKey(pair.name)) { obj.hasWarnings = true; LoggingUtil.LogWarning(obj.GetType(), obj.ErrorPrefix() + ": unexpected attribute '" + pair.name + "' found, ignored."); } } foreach (ConfigNode child in configNode.nodes) { // Exceptions if (child.name == "PARAMETER" && (obj is ContractType || obj is ParameterFactory) || child.name == "REQUIREMENT" && (obj is ContractType || obj is ParameterFactory || obj is ContractRequirement) || child.name == "BEHAVIOUR" && (obj is ContractType) || child.name == "ORBIT" && (obj is Behaviour.OrbitGeneratorFactory || obj is Behaviour.SpawnVesselFactory || obj is Behaviour.SpawnKerbalFactory)) { continue; } if (!found.ContainsKey(child.name)) { obj.hasWarnings = true; LoggingUtil.LogWarning(obj.GetType(), obj.ErrorPrefix() + ": unexpected child node '" + child.name + "' found, ignored."); } } return(valid); }
/// <summary> /// Validates that the given config node does NOT contain the given value. /// </summary> /// <param name="configNode">The ConfigNode to check.</param> /// <param name="field">The field to exclude</param> /// <param name="obj">IContractConfiguratorFactory object for error reporting</param> /// <returns>Always true, but logs a warning for an unexpected value.</returns> public static bool ValidateExcludedValue(ConfigNode configNode, string field, IContractConfiguratorFactory obj) { if (configNode.HasNode(field) || configNode.HasValue(field)) { LoggingUtil.LogWarning(obj.GetType(), "{0}: unexpected entry '{1}' found, ignored.", obj.ErrorPrefix(), field); } return(true); }
/// <summary> /// Checks whether the mandatory field exists, and if not logs and error. Returns true /// only if the validation succeeded. /// </summary> /// <param name="configNode">The ConfigNode to check.</param> /// <param name="field">The child that is expected</param> /// <param name="obj">IContractConfiguratorFactory object for error reporting</param> /// <returns>Whether the validation succeeded, additionally logs an error on failure.</returns> public static bool ValidateMandatoryChild(ConfigNode configNode, string field, IContractConfiguratorFactory obj) { if (!configNode.HasNode(field)) { LoggingUtil.LogError(obj.GetType(), "{0}: missing required child node '{1}'.", obj.ErrorPrefix(), field); return(false); } return(true); }
/// <summary> /// Performs validation to check if the given config node has values that were not expected. /// </summary> /// <param name="configNode">The ConfigNode to check.</param> /// <param name="obj">IContractConfiguratorFactory object for error reporting</param> /// <returns>Always true, but logs a warning if unexpected keys were found</returns> public static bool ValidateUnexpectedValues(ConfigNode configNode, IContractConfiguratorFactory obj) { if (!keysFound.ContainsKey(configNode)) { LoggingUtil.LogWarning(obj.GetType(), obj.ErrorPrefix() + ": did not attempt to load values for ConfigNode!"); return(false); } Dictionary <string, int> found = keysFound[configNode]; foreach (ConfigNode.Value pair in configNode.values) { if (!found.ContainsKey(pair.name)) { LoggingUtil.LogWarning(obj.GetType(), obj.ErrorPrefix() + ": unexpected entry '" + pair.name + "' found, ignored."); } } return(true); }