static void AssignGraphAttr(BaseGraph graph, BaseGraphCommand command, string inputCommand) { var field = graph.GetType().GetField(command.graphFieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (field == null) { throw new InvalidOperationException("Can't assign graph field '" + command.graphFieldName + "': not found"); } field.SetValue(graph, command.graphFieldValue); }
public static void Export(BaseGraph graph, string filePath) { List <string> commands = new List <string>(); List <string> nodeNames = new List <string>(); var nodeToNameMap = new Dictionary <BaseNode, string>(); //GraphAttr commands var fields = graph.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var field in fields) { var attrs = field.GetCustomAttributes(true); if (attrs.Any(a => a is TextSerializeField)) { string s = GenerateGraphAttributeCommand(field.Name, field.GetValue(graph)); if (s != null) { commands.Add(s); } } } //CreateNode commands foreach (var node in graph.allNodes) { var attrs = new BaseGraphCLIAttributes(); //load node attributes FieldInfo[] attrFields = node.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (var field in attrFields) { var attributes = field.GetCustomAttributes(false); bool isInput = attributes.Any(a => { return(a.GetType() == typeof(InputAttribute)); }); if (isInput) { continue; } //if the field can't be jsonified, we skip it if (Jsonizer.allowedJsonTypes.FindIndex(j => j.type == field.FieldType) == -1) { continue; } attrs.Add(field.Name, field.GetValue(node)); } string nodeName = node.name; int i = 0; //unique name generation while (nodeNames.Contains(nodeName)) { nodeName = node.name + i++; } nodeNames.Add(nodeName); nodeToNameMap[node] = nodeName; commands.Add(GenerateNewNodeCommand(node.GetType(), nodeName, node.rect.position, attrs)); } //Link commands foreach (var link in graph.nodeLinkTable.GetLinks()) { if (link.fromNode == null || link.toNode == null) { continue; } var fromName = nodeToNameMap[link.fromNode]; var toName = nodeToNameMap[link.toNode]; var fromAnchorName = link.fromAnchor.fieldName; var toAnchorName = link.toAnchor.fieldName; commands.Add(GenerateLinkAnchorNameCommand(fromName, fromAnchorName, toName, toAnchorName)); } File.WriteAllLines(filePath, commands.ToArray()); }