Exemplo n.º 1
0
 protected virtual string EnumName(CmdParser.ArgInfo.Arg arg, Type sigType)
 {
     return(Capitalize(arg.LongName) + ComponentCatalog.SignatureToString(sigType));
 }
Exemplo n.º 2
0
 protected override string EnumName(CmdParser.ArgInfo.Arg arg, Type sigType)
 {
     return(_compName + "." + base.EnumName(arg, sigType));
 }
Exemplo n.º 3
0
 protected static string CastIfNeeded(CmdParser.ArgInfo.Arg arg)
 {
     return(arg.ItemType == typeof(uint) ? "(uint)" : "");
 }
Exemplo n.º 4
0
 protected bool IsStringColumnType(CmdParser.ArgInfo.Arg arg)
 {
     return(arg.LongName == "column");
 }
Exemplo n.º 5
0
        protected void GenerateImplBody(IndentingTextWriter w, CmdParser.ArgInfo.Arg arg, string argSuffix)
        {
            if (Exclude.Contains(arg.LongName))
            {
                return;
            }

            if (arg.IsSubComponentItemType)
            {
                // We need to create a tree with all the subcomponents, unless the subcomponent is a Trainer.
                Contracts.Assert(arg.ItemType.GetGenericTypeDefinition() == typeof(SubComponent <,>));
                var types    = arg.ItemType.GetGenericArguments();
                var baseType = types[0];
                var sigType  = types[1];
                if (IsTrainer(sigType))
                {
                    if (arg.IsCollection)
                    {
                        w.WriteLine("args{0}.{1} = new[] {{ new SubComponent<{2}, {3}>({4}.Item1, {4}.Item2) }};",
                                    argSuffix, arg.LongName, GetCSharpTypeName(baseType), GetCSharpTypeName(sigType),
                                    arg.LongName + argSuffix);
                    }
                    else
                    {
                        w.WriteLine("args{0}.{1} = new SubComponent<{2}, {3}>({4}.Item1, {4}.Item2);", argSuffix,
                                    arg.LongName, GetCSharpTypeName(baseType), GetCSharpTypeName(sigType),
                                    arg.LongName + argSuffix);
                    }
                    return;
                }
                if (sigType == typeof(SignatureDataLoader))
                {
                    return;
                }
                var typeName = EnumName(arg, sigType);
                w.WriteLine("switch ({0})", arg.LongName + argSuffix);
                w.WriteLine("{");
                using (w.Nest())
                {
                    if (arg.NullName != null)
                    {
                        w.WriteLine("case {0}.None:", typeName);
                        using (w.Nest())
                        {
                            w.WriteLine("args{0}.{1} = null;", argSuffix, arg.LongName);
                            w.WriteLine("break;");
                        }
                    }
                    var infos = ComponentCatalog.GetAllDerivedClasses(baseType, sigType);
                    foreach (var info in infos)
                    {
                        w.WriteLine("case {0}.{1}:", typeName, info.LoadNames[0]);
                        using (w.Nest())
                        {
                            if (info.ArgType != null)
                            {
                                var newArgSuffix = argSuffix + info.LoadNames[0];
                                w.WriteLine("var args{0} = new {1}();", newArgSuffix, GetCSharpTypeName(info.ArgType));
                                w.WriteLine("var defs{0} = new {1}();", newArgSuffix, GetCSharpTypeName(info.ArgType));
                                var args = info.CreateArguments();
                                if (args != null)
                                {
                                    var argInfo = CmdParser.GetArgInfo(args.GetType(), args);
                                    foreach (var a in argInfo.Args)
                                    {
                                        GenerateImplBody(w, a, newArgSuffix);
                                    }
                                }
                                w.WriteLine(
                                    "args{0}.{1} = new {2}(\"{3}\", CmdParser.GetSettings(args{4}, defs{4}));",
                                    argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType), info.LoadNames[0],
                                    newArgSuffix);
                            }
                            else
                            {
                                w.WriteLine("args{0}.{1} = new {2}(\"{3}\");", argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType), info.LoadNames[0]);
                            }
                            w.WriteLine("break;");
                        }
                    }
                }
                w.WriteLine("}");
            }
            else if (arg.IsCollection)
            {
                if (IsColumnType(arg))
                {
                    w.WriteLine("args{0}.{1} = {1}.Select({2}.Parse).ToArray();", argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType));
                }
                else if (IsStringColumnType(arg))
                {
                    w.WriteLine("args{0}.{1} = {2};", argSuffix, arg.LongName, arg.LongName + argSuffix);
                }
                else
                {
                    w.WriteLine("args{0}.{1} = new[] {{ {2} }};", argSuffix, arg.LongName, arg.LongName + argSuffix);
                }
            }
            else
            {
                w.WriteLine("args{0}.{1} = {2};", argSuffix, arg.LongName, arg.LongName + argSuffix);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generate enums for subcomponents. Uses ReflectionUtils to filter only the subcomponents that match the base type and the signature.
        /// </summary>
        /// <param name="w"></param>
        /// <param name="arg"></param>
        /// <param name="seen"></param>
        protected void GenerateEnums(IndentingTextWriter w, CmdParser.ArgInfo.Arg arg, HashSet <Tuple <Type, Type> > seen)
        {
            if (Exclude.Contains(arg.LongName))
            {
                return;
            }

            var moreEnums = new List <CmdParser.ArgInfo.Arg>();

            if (arg.IsHidden || !arg.IsSubComponentItemType)
            {
                return;
            }
            Contracts.Assert(arg.ItemType.GetGenericTypeDefinition() == typeof(SubComponent <,>));
            var types    = arg.ItemType.GetGenericArguments();
            var baseType = types[0];
            var sigType  = types[1];
            var key      = new Tuple <Type, Type>(baseType, sigType);

            if (seen.Contains(key) || IsTrainer(sigType) || sigType == typeof(SignatureDataLoader))
            {
                return;
            }
            seen.Add(key);
            var typeName = EnumName(arg, sigType);

            w.WriteLine("/// <summary> Available choices for {0} </summary>", sigType);
            w.WriteLine("public enum {0}", typeName);
            w.Write("{");
            using (w.Nest())
            {
                var pre = "";
                if (arg.NullName != null)
                {
                    w.WriteLine();
                    GenerateEnumValue(w, null);
                    pre = ",";
                }
                var infos = ComponentCatalog.GetAllDerivedClasses(baseType, sigType);
                foreach (var info in infos)
                {
                    w.WriteLine(pre);
                    if (pre != "")
                    {
                        w.WriteLine();
                    }
                    pre = ",";
                    GenerateEnumValue(w, info);
                    var args = info.CreateArguments();
                    if (args == null)
                    {
                        continue;
                    }
                    var argInfo = CmdParser.GetArgInfo(args.GetType(), args);
                    moreEnums.AddRange(argInfo.Args);
                }
                w.WriteLine();
            }
            w.WriteLine("}");
            w.WriteLine();
            foreach (var argument in moreEnums)
            {
                GenerateEnums(w, argument, seen);
            }
        }