public override object Execute(List <string> args) { if (args.Count < 1 || args.Count > 2) { return(false); } var blockName = args[0]; var ownerType = Owner.GetType(); var deferredNames = new List <string>(); var deferredArgs = new List <string>(); if (blockName.Contains(".")) { deferredNames.AddRange(blockName.Split('.')); blockName = deferredNames[0]; deferredNames = deferredNames.Skip(1).ToList(); deferredArgs.AddRange(args.Skip(1)); args = new List <string> { blockName }; } if (blockName.Contains("]")) { var openBracketIndex = blockName.IndexOf('['); var closeBracketIndex = blockName.IndexOf(']'); var name = blockName.Substring(0, openBracketIndex); var index = blockName.Substring(openBracketIndex + 1, (closeBracketIndex - openBracketIndex) - 1); blockName = name; args = new List <string> { name, index }; } var blockNameLow = blockName.ToLower(); var blockNameSnake = blockName.ToSnakeCase(); var field = TagStructure.GetTagFieldEnumerable(Structure) .Find(f => f.Name == blockName || f.Name.ToLower() == blockNameLow || f.Name.ToSnakeCase() == blockNameSnake); if (field == null) { Console.WriteLine("{0} does not contain a block named \"{1}\"", ownerType.Name, blockName); return(false); } var contextName = ""; object blockValue = null; var structureAttribute = field.FieldType.CustomAttributes.ToList().Find(a => a.AttributeType == typeof(TagStructureAttribute)); if (structureAttribute != null) { if (args.Count != 1) { return(false); } blockValue = field.GetValue(Owner); contextName = $"{blockName}"; } else { if (args.Count != 2) { return(false); } IList fieldValue = null; if (field.FieldType.GetInterface("IList") == null || (fieldValue = (IList)field.GetValue(Owner)) == null) { Console.WriteLine("{0} does not contain a block named \"{1}\"", ownerType.Name, blockName); return(false); } int blockIndex = 0; if (args[1] == "*") { blockIndex = fieldValue.Count - 1; } else if (!int.TryParse(args[1], out blockIndex)) { Console.WriteLine("Invalid index requested from block {0}: {1}", blockName, blockIndex); return(false); } if (blockIndex >= fieldValue.Count || blockIndex < 0) { Console.WriteLine("Invalid index requested from block {0}: {1}", blockName, blockIndex); return(false); } blockValue = fieldValue[blockIndex]; contextName = $"{blockName}[{blockIndex}]"; } var blockStructure = TagStructure.GetTagStructureInfo(blockValue.GetType()); var blockContext = new CommandContext(ContextStack.Context, contextName); blockContext.AddCommand(new ListFieldsCommand(BlamCache, blockStructure, blockValue)); blockContext.AddCommand(new EditBlockCommand(ContextStack, BlamCache, Tag, blockValue)); blockContext.AddCommand(new Editing.ExitToCommand(ContextStack)); ContextStack.Push(blockContext); if (deferredNames.Count != 0) { var name = deferredNames[0]; deferredNames = deferredNames.Skip(1).ToList(); foreach (var deferredName in deferredNames) { name += '.' + deferredName; } args = new List <string> { name }; args.AddRange(deferredArgs); var command = new EditBlockCommand(ContextStack, BlamCache, Tag, blockValue); return(command.Execute(args)); } return(true); }
public override object Execute(List <string> args) { if (args.Count > 4) { return(false); } if (args.Count < 1 || args.Count > 3) { return(false); } var fieldName = args[0]; var fieldNameLow = fieldName.ToLower(); var previousContext = ContextStack.Context; var previousOwner = Owner; var previousStructure = Structure; if (fieldName.Contains(".")) { var lastIndex = fieldName.LastIndexOf('.'); var blockName = fieldName.Substring(0, lastIndex); fieldName = fieldName.Substring(lastIndex + 1, (fieldName.Length - lastIndex) - 1); fieldNameLow = fieldName.ToLower(); var command = new EditBlockCommand(ContextStack, BlamCache, Tag, Owner); if (command.Execute(new List <string> { blockName }).Equals(false)) { while (ContextStack.Context != previousContext) { ContextStack.Pop(); } Owner = previousOwner; Structure = previousStructure; return(false); } command = (ContextStack.Context.GetCommand("EditBlock") as EditBlockCommand); Owner = command.Owner; Structure = command.Structure; if (Owner == null) { while (ContextStack.Context != previousContext) { ContextStack.Pop(); } Owner = previousOwner; Structure = previousStructure; return(false); } } var field = TagStructure.GetTagFieldEnumerable(Structure) .Find(f => f.Name == fieldName || f.Name.ToLower() == fieldNameLow || f.Name.ToSnakeCase().ToLower() == fieldNameLow); var fieldType = field.FieldType; if ((field == null) || (!fieldType.IsGenericType) || (fieldType.GetInterface("IList") == null)) { Console.WriteLine("ERROR: {0} does not contain a tag block named \"{1}\".", Structure.Types[0].Name, args[0]); while (ContextStack.Context != previousContext) { ContextStack.Pop(); } Owner = previousOwner; Structure = previousStructure; return(false); } var blockValue = field.GetValue(Owner) as IList; var index = 0; if (args.Count > 1 && args[1] != "*") { if (!int.TryParse(args[1], out index) || index < 0) { Console.WriteLine($"Invalid index specified: {args[1]}"); return(false); } } var count = -1; if (args.Count > 2) { if (args[2] != "*" && (!int.TryParse(args[2], out count) || count < 1)) { Console.WriteLine($"Invalid count specified: {args[2]}"); return(false); } } if (blockValue == null) { Console.WriteLine($"Invalid index specified: {args[0]}"); return(false); } if (count < 0) { count = blockValue.Count; } if ((index + count) < 0 || (index + count) > blockValue.Count) { Console.WriteLine($"Invalid index: {index}, and count: {count}"); return(false); } Editing.CopyBlockElementsCommand.ElementType = field.FieldType.GenericTypeArguments[0]; Editing.CopyBlockElementsCommand.Elements = new List <object>(); for (var i = index; i < (index + count); i++) { Editing.CopyBlockElementsCommand.Elements.Add(blockValue[i]); } var itemString = index < 2 ? "element" : "elements"; Console.WriteLine($"Successfully copied {count} {itemString}."); while (ContextStack.Context != previousContext) { ContextStack.Pop(); } Owner = previousOwner; Structure = previousStructure; return(true); }