/// <summary> /// Returns the child node with the given key, or null. /// </summary> /// <param name="parent">Parent mapping node.</param> /// <param name="childKey">Key of the child node.</param> /// <returns></returns> public static YamlNode?GetChildNodeWithKey(this YamlNode?parent, string childKey) { if (parent == null) { return(null); } if (!(parent is YamlMappingNode parentMapping)) { throw new ConfigurationException("Parent is not a mapping node."); } return(parentMapping.Children.TryGetValue(new YamlScalarNode(childKey), out var childNode) ? childNode : null); }
public override bool TryTypeToNode(object obj, [NotNullWhen(true)] out YamlNode?node) { switch (obj) { case GridId gridId: if (!GridIDMap.TryGetValue(gridId, out var gridMapped)) { Logger.WarningS("map", "Cannot write grid ID '{0}', falling back to nullspace.", gridId); break; } else { node = new YamlScalarNode(gridMapped.ToString(CultureInfo.InvariantCulture)); return(true); } case EntityUid entityUid: if (!EntityUidMap.TryGetValue(entityUid, out var entityUidMapped)) { // Terrible hack to mute this warning on the grids themselves when serializing blueprints. if (!IsBlueprintMode || !CurrentWritingEntity !.HasComponent <MapGridComponent>() || CurrentWritingComponent != "Transform") { Logger.WarningS("map", "Cannot write entity UID '{0}'.", entityUid); } node = new YamlScalarNode("null"); return(true); } else { node = new YamlScalarNode(entityUidMapped.ToString(CultureInfo.InvariantCulture)); return(true); } case IEntity entity: if (!EntityUidMap.TryGetValue(entity.Uid, out var entityMapped)) { Logger.WarningS("map", "Cannot write entity UID '{0}'.", entity.Uid); break; } else { node = new YamlScalarNode(entityMapped.ToString(CultureInfo.InvariantCulture)); return(true); } } node = null; return(false); }
/// <summary> /// Reads a YAML file and returns the top-level node. /// Returns a null if the file doesn't exist or is not a YAML file. /// </summary> /// <param name="rootPath">The directory where the file should exist.</param> /// <param name="fileBaseName">The base name of the file, not including the ".yml" extension.</param> internal static YamlNode?ReadYamlFile(string rootPath, string fileBaseName) { var yamlReader = new YamlStream(); YamlNode?fileNode = null; DoIgnoringExceptions(() => { var fileName = Path.Combine(rootPath, fileBaseName + YamlExtension); using (var readStream = File.OpenText(fileName)) { yamlReader.Load(readStream); } fileNode = yamlReader.Documents[0].RootNode; }); return(fileNode); }
/// <summary> /// Gets the integer value of the given scalar node. Error checking is included. If the node is not present (null), the default value is returned. /// </summary> /// <param name="node">Node.</param> /// <param name="defaultValue">Default value, if node is null.</param> /// <returns></returns> public static int GetNodeInteger(this YamlNode?node, int defaultValue) { if (node == null) { return(defaultValue); } if (!(node is YamlScalarNode scalarNode)) { throw new ConfigurationException("Invalid node type."); } if (!int.TryParse(scalarNode.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int nodeValue) && !int.TryParse(scalarNode.Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out nodeValue)) { throw new ConfigurationException("Invalid node value."); } return(nodeValue); }
public virtual bool TryTypeToNode(object obj, [NotNullWhen(true)] out YamlNode?node) { node = null; return(false); }
/// <summary> /// Gets the node with the specified anchor. /// </summary> /// <param name="anchor">The anchor.</param> /// <param name="node">The node that was retrieved.</param> /// <returns>true if the anchor was found; otherwise false.</returns> public bool TryGetNode(AnchorName anchor, [NotNullWhen(true)] out YamlNode?node) { return(anchors.TryGetValue(anchor, out node)); }
/// <summary> /// Attempts to fetch a node like <see cref="GetNode" />, /// but does not throw a <c>KeyNotFoundException</c> if the node doesn't exist. /// Instead it returns whether the node was successfully found. /// </summary> /// <param name="mapping">The mapping to retrieve the node from.</param> /// <param name="key">The value of the scalar node that will be looked up.</param> /// <param name="returnNode">The node found, <c>null</c> if it could not be found.</param> /// <returns>True if the value could be found, false otherwise.</returns> public static bool TryGetNode(this YamlMappingNode mapping, string key, [NotNullWhen(true)] out YamlNode?returnNode) { return(mapping.Children.TryGetValue(_getFetchNode(key), out returnNode)); }
private static YamlNode?GetChildNode(IDictionary <YamlNode, YamlNode> children, string key, YamlNode?defaultValue = default) { if (children.TryGetValue(new YamlScalarNode(key), out YamlNode? value)) { return(value); } return(defaultValue); }