Beispiel #1
0
        public override string ToString()
        {
            var sb = new StringBuilder("Tree inspection results:").AppendLine();

            if (IsOk)
            {
                sb.Indent().AppendLine("Everything is OK.");
                return(sb.ToString());
            }

            sb.Indent().Append("Problem: ").Append(ProblemType).AppendLine();
            sb.Indent().Append("With node: ").AppendLine(ProblematicNode?.ToString() ?? "MISSING");
            sb.Indent().Append("An automatic fix is: ").Append(AutofixStatus).AppendLine();

            if (AutofixStatus == TreeInspectionAutofixStatus.Impossible)
            {
                return(sb.ToString());
            }

            sb.Indent().AppendLine("Possible fixes:");
            foreach (var possibleFix in PossibleFixes)
            {
                sb.Append(possibleFix.ToString(2));
            }

            return(sb.ToString());
        }
        private static void Render([NotNull] HerculesVector vector, [NotNull] StringBuilder builder, int depth)
        {
            if (vector.Count == 0)
            {
                builder
                .Indent(depth)
                .Append(OpeningSquareBracket)
                .Append(ClosingSquareBracket);

                return;
            }

            builder
            .Indent(depth)
            .AppendLine(OpeningSquareBracket);

            var counter = 0;

            foreach (var element in vector.Elements)
            {
                Render(element, builder, depth + 1);

                if (++counter < vector.Count)
                {
                    builder.Append(Comma);
                }

                builder.AppendLine();
            }

            builder
            .Indent(depth)
            .Append(ClosingSquareBracket);
        }
Beispiel #3
0
        private static string DisplayContainer(Container container, int level)
        {
            StringBuilder sb = new StringBuilder();

            sb.Indent(level);

            if (container.IsOpenable)
            {
                sb.AppendFormat("{0} {1} ({2})\n", container.Article, container.Name, container.State);
            }
            else
            {
                sb.AppendFormat("{0} {1}\n", container.Article, container.Name);
            }

            if (container.IsOpen || container.IsTransparent)
            {
                foreach (var child in container.Contents)
                {
                    Container c = child as Container;
                    if (c != null)
                    {
                        sb.AppendFormat("{0}", DisplayContainer(c, level + 1));
                    }
                    else
                    {
                        sb.Indent(level + 1);
                        sb.AppendFormat("{0} {1}\n", child.Article, child.Name);
                    }
                }
            }

            return(sb.ToString());
        }
Beispiel #4
0
        public string Inspect()
        {
            var response = new StringBuilder();

            if (Configuration.Any())
            {
                response.Line("[Configuration]");
            }
            foreach (var config in Configuration)
            {
                response.Indent("{0}: {1}", config.Key, config.Value.Trim().Replace("\n", " "));
            }

            if (GlobalDependencies.Any())
            {
                response.Line("[Global Dependencies]");
            }
            foreach (var dep in GlobalDependencies)
            {
                response.Indent(dep.Text.Trim());
            }

            foreach (var group in Groups)
            {
                response.Line("[{0}]", group.Name);
                foreach (var dep in group.Dependencies)
                {
                    response.Indent(dep.Text.Trim());
                }
            }

            return(response.ToString());
        }
Beispiel #5
0
        private static void Traverse(ITreeNode node, StringBuilder sb, int depth, CompletionTargetMarker marker)
        {
            if (node.IsWhitespaceToken())
            {
                return;
            }

            sb.Indent(depth);
            sb.ObjectId(node, marker);
            sb.AppendLine();

            foreach (var child in node.Children())
            {
                if (Visited.Contains(child))
                {
                    sb.Indent(depth);
                    sb.Append("--> ");
                    sb.ObjectId(node, marker);
                    sb.AppendLine();
                }
                else
                {
                    Visited.Add(child);
                    Traverse(child, sb, depth + 1, marker);
                }
            }
        }
Beispiel #6
0
        public string ToString(int indent)
        {
            var sb = new StringBuilder();

            sb.Indent(indent).AppendLine("Node fix:");
            if (SpanStart != null)
            {
                sb.Indent(indent + 1).Append("SpanStart = ").AppendLine(SpanStart.ToString());
            }
            if (SpanLength != null)
            {
                sb.Indent(indent + 1).Append("SpanLength = ").AppendLine(SpanLength.ToString());
            }
            if (Text != null)
            {
                sb.Indent(indent + 1).Append("Text = ").AppendLine(Text);
            }

            if (SpanStart == null && SpanLength == null && Text == null)
            {
                sb.Indent(indent + 1).AppendLine("(empty)");
            }

            return(sb.ToString());
        }
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("Type:");
            builder.Indent(indent + 1).Append(ParsedType.FullName).AppendLine();
            return(builder.ToString());
        }
Beispiel #8
0
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("ValueCreation:");
            builder.Indent(indent + 1).AppendFormat("Type: {0}", type).AppendLine();
            return(builder.ToString());
        }
Beispiel #9
0
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("Literal:");
            builder.Indent(indent + 1).AppendFormat("Type: {0}", type.FullName).AppendLine();
            builder.Indent(indent + 1).AppendFormat("Value: {0}", Value.ToString()).AppendLine();
            return(builder.ToString());
        }
            public override string ToString(int indent)
            {
                StringBuilder builder = new StringBuilder();

                builder.Indent(indent).AppendLine("VariableDeclaration:");
                builder.Indent(indent + 1).AppendLine("Symbol:");
                builder.Indent(indent + 2).AppendFormat("{0} {1}", Variable.VariableType, Variable.Name).AppendLine();
                return(builder.ToString());
            }
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("Cast:");
            builder.Indent(indent + 1).AppendFormat("Type: {0}", type).AppendLine();
            builder.Indent(indent + 1).AppendLine("Expression:");
            builder.AppendLine(target.ToString(indent + 2));
            return(builder.ToString());
        }
Beispiel #12
0
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("Assignment:");
            builder.Indent(indent + 1).AppendLine("Left:");
            builder.AppendLine(left.ToString(indent + 2));
            builder.Indent(indent + 1).AppendLine("Right:");
            builder.AppendLine(right.ToString(indent + 2));
            return(builder.ToString());
        }
Beispiel #13
0
            public override string ToString(int indent)
            {
                StringBuilder builder = new StringBuilder();

                builder.Indent(indent).AppendLine("UnaryOperator:");
                builder.Indent(indent + 1).AppendLine("Operator:");
                builder.Indent(indent + 2).AppendLine(UnaryOperatorType.ToString());
                builder.Indent(indent + 1).AppendLine("Operand:");
                builder.AppendLine(operand.ToString(indent + 2));
                return(builder.ToString());
            }
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("While:");
            builder.Indent(indent + 1).AppendLine("Condition:");
            builder.AppendLine(condition.ToString(indent + 2));
            builder.Indent(indent + 1).AppendLine("Block:");
            builder.AppendLine(block.ToString(indent + 2));
            return(builder.ToString());
        }
Beispiel #15
0
        /// <summary>
        /// Formats a <see cref="FragmentToken"/>.
        /// </summary>
        /// <param name="obj">
        /// The object to format.
        /// </param>
        /// <param name="builder">
        /// The string builder to which the formatter outputs.
        /// </param>
        public void Format(object obj, StringBuilder builder)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var token = obj as FragmentToken;

            if (token == null)
            {
                throw new ArgumentException(@"Parameter must be of type FragmentToken", nameof(obj));
            }

            this.context.Element.HasContent = true;

            builder.Indent(this.context).Append("<!--");

            var lines = token.ContentString.GetLines().ToArray();

            if (lines.Count() == 1)
            {
                if (string.IsNullOrWhiteSpace(lines[0]))
                {
                    builder.Append(" ");
                }
                else
                {
                    builder.AppendFormat(" {0} ", lines[0]);
                }
            }
            else
            {
                ++this.context.LocalDepth;
                builder.AppendLine();
                foreach (var line in lines)
                {
                    builder
                    .Append(string.Empty.Equals(line) ? string.Empty : this.context.GetIndent(false))
                    .Append(line)
                    .AppendLine();
                }

                --this.context.LocalDepth;
                builder.Indent(this.context, false);
            }

            builder.AppendFormat("-->").AppendLine(this.context);
        }
Beispiel #16
0
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("Local:");
            builder.Indent(indent + 1).AppendLine("Name:");
            builder.Indent(indent + 2).AppendLine(Name);
            builder.Indent(indent + 1).AppendLine("Type:");
            builder.Indent(indent + 2).AppendLine(ExpressionReturnType.FullName);
            return(builder.ToString());
        }
        public override string GetCode(Random random)
        {
            var code = new StringBuilder("count++;\n");

            code.Indent(4);
            code.Insert(0, CodeGenerator.GetLoopCode(InnerLoop, InnerLoopType, "j", random));
            code.Append("}\n");
            code.Indent(4);
            code.Insert(0, CodeGenerator.GetLoopCode(OuterLoop, OuterLoopType, "i", random));
            code.Append("}\n");
            code.Insert(0, "var count=0;\n");
            return(code.ToString());
        }
Beispiel #18
0
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("BinaryOperator:");
            builder.Indent(indent + 1).AppendLine("Left:");
            builder.AppendLine(left.ToString(indent + 2));
            builder.Indent(indent + 1).AppendLine("Operator:");
            builder.Indent(indent + 2).AppendLine(BinaryOperatorType.ToString());
            builder.Indent(indent + 1).AppendLine("Right:");
            builder.AppendLine(right.ToString(indent + 2));
            return(builder.ToString());
        }
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("ForEach:");
            builder.Indent(indent + 1).AppendLine("Variable:");
            builder.AppendLine(variable.ToString(indent + 2));
            builder.Indent(indent + 1).AppendLine("Collection:");
            builder.AppendLine(collection.ToString(indent + 2));
            builder.Indent(indent + 1).AppendLine("Body:");
            builder.AppendLine(body.ToString(indent + 2));
            return(builder.ToString());
        }
Beispiel #20
0
        static void AddDefaults()
        {
            defaultTypeEncoder = ((obj, builder) =>
            {
                Type type = obj.GetType();
                builder.BeginObject();

                builder.WriteTypeInfoIfAttributePresent(type);

                obj.EnumerateFields(DEFAULT_JSON_BINDING_FLAGS, (targetObj, field, jsonName) =>
                {
                    var value = field.GetValue(targetObj);
                    builder.EncodePair(jsonName, value);

                    //Format for another name value pair
                    builder.Indent();
                    builder.Comma();
                    builder.LineBreak();
                });

                builder.EndObject();
            });

            SetTypeEncoder(typeof(IEnumerable), (obj, builder) =>
            {
                builder.BeginArray();
                builder.LineBreak();
                foreach (var item in (IEnumerable)obj)
                {
                    builder.Indent();
                    builder.EncodeValue(item);
                    builder.Comma();
                    builder.LineBreak();
                }
                builder.EndArray();
            });

            SetTypeEncoder(typeof(IDictionary), (obj, builder) =>
            {
                builder.BeginObject();
                var table = obj as IDictionary;
                foreach (var key in table.Keys)
                {
                    builder.Indent();
                    builder.EncodePair(key.ToString(), table[key]);
                    builder.Comma();
                    builder.LineBreak();
                }
                builder.EndObject();
            });
        }
Beispiel #21
0
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("VariableDeclaration:");
            builder.Indent(indent + 1).AppendLine("Symbol:");
            builder.Indent(indent + 2).Append(GetSignature()).AppendLine();
            if (initializer != null)
            {
                builder.Indent(indent + 1).AppendLine("Initializer:");
                builder.AppendLine(initializer.ToString(indent + 2));
            }
            return(builder.ToString());
        }
        private static void Render([NotNull] HerculesTags tags, [NotNull] StringBuilder builder, int depth)
        {
            if (tags.Count == 0)
            {
                builder
                .Indent(depth)
                .Append(OpeningCurlyBracket)
                .Append(ClosingCurlyBracket);

                return;
            }

            builder
            .Indent(depth)
            .AppendLine(OpeningCurlyBracket);

            var counter = 0;

            foreach (var pair in tags)
            {
                builder
                .Indent(depth + 1)
                .Append(Quote)
                .Append(pair.Key)
                .Append(Quote)
                .Append(Colon)
                .Append(Space);

                if (HasChildren(pair.Value))
                {
                    builder.AppendLine();
                    Render(pair.Value, builder, depth + 1);
                }
                else
                {
                    Render(pair.Value, builder, 0);
                }

                if (++counter < tags.Count)
                {
                    builder.Append(Comma);
                }

                builder.AppendLine();
            }

            builder
            .Indent(depth)
            .Append(ClosingCurlyBracket);
        }
Beispiel #23
0
        private string BuildStructPutter()
        {
            var sb = new StringBuilder();

            sb.Indent(level).AppendLine("public virtual void Put(int fieldPos, object fieldValue)");
            sb.Indent(level).AppendLine("{");
            IndentUp();

            sb.Indent(level).AppendLine("switch (fieldPos)");
            sb.Indent(level).AppendLine("{");
            IndentUp();
            for (int i = 0; i < Fields.Count; ++i)
            {
                sb.Indent(level)
                .AppendFormat("case {0}: return this.{1} = ({2})fieldValue; break;", i, Fields[i].CSharpPropertyName,
                              Fields[i].GenType.CSharpTypeName)
                .AppendLine();
            }
            sb.Indent(level)
            .AppendLine("default: throw new BaijiRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");");
            IndentDown();
            sb.Indent(level).AppendLine("}");

            IndentDown();
            sb.Indent(level).AppendLine("}");
            return(sb.ToString());
        }
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("Condition:");
            builder.Indent(indent + 1).AppendLine("True:");
            builder.AppendLine(trueBlock.ToString(indent + 2));
            if (falseBlock != null)
            {
                builder.Indent(indent + 1).AppendLine("False:");
                builder.AppendLine(falseBlock.ToString(indent + 2));
            }
            return(builder.ToString());
        }
Beispiel #25
0
        private string BuildStructPutter()
        {
            var sb = new StringBuilder();

            sb.Indent(level).AppendLine("// Used by DatumReader. Applications should not call.");
            sb.Indent(level).AppendLine("@SuppressWarnings(value=\"unchecked\")");
            sb.Indent(level).AppendLine("public void put(int field, java.lang.Object value) {");
            IndentUp();

            sb.Indent(level).AppendLine("switch (field) {");
            IndentUp();
            for (int i = 0; i < Fields.Count; ++i)
            {
                sb.Indent(level)
                .AppendFormat("case {0}: this.{1} = ({2})value; break;", i, Fields[i].JavaName,
                              Fields[i].GenType.JavaTypeName)
                .AppendLine();
            }
            sb.Indent(level)
            .AppendLine("default: throw new BaijiRuntimeException(\"Bad index \" + field + \" in put()\");");
            IndentDown();
            sb.Indent(level).AppendLine("}");

            IndentDown();
            sb.Indent(level).AppendLine("}");
            return(sb.ToString());
        }
Beispiel #26
0
 public override void GetReaderParams(StringBuilder sb, ref int level, IEnumerable <IEnumerable <ParamDataBase> > resultColumns)
 {
     sb.Indent(level).AppendLine("while (dr.Read())");
     sb.Indent(level).AppendLine("{");
     ++level;
     foreach (ParamData paramData in resultColumns.First <IEnumerable <ParamDataBase> >())
     {
         sb.Indent(level);
         paramData.WriteGetValueFromReader(sb);
         sb.AppendLine();
     }
     --level;
     sb.Indent(level).AppendLine("}");
 }
Beispiel #27
0
        private string BuildStructGetter()
        {
            var sb = new StringBuilder();

            sb.Indent(level).AppendLine("// Used by DatumWriter. Applications should not call.");
            sb.Indent(level).AppendLine("public java.lang.Object get(int field) {");
            IndentUp();

            sb.Indent(level).AppendLine("switch (field)");
            sb.Indent(level).AppendLine("{");
            IndentUp();
            for (int i = 0; i < Fields.Count; ++i)
            {
                sb.Indent(level)
                .AppendFormat("case {0}: return this.{1};", i, Fields[i].JavaName)
                .AppendLine();
            }
            sb.Indent(level)
            .AppendLine(
                "default: throw new BaijiRuntimeException(\"Bad index \" + field + \" in get()\");");
            IndentDown();
            sb.Indent(level).AppendLine("}");

            IndentDown();
            sb.Indent(level).AppendLine("}");
            return(sb.ToString());
        }
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("MethodCall:");
            builder.Indent(indent + 1).AppendFormat("ReturnType: {0}", type).AppendLine();
            builder.Indent(indent + 1).Append("Args:").AppendLine();
            foreach (var arg in args)
            {
                builder.AppendLine(arg.ToString(indent + 2));
            }
            builder.Indent(indent + 1).AppendLine("Function:");
            builder.AppendLine(function.ToString(indent + 2));
            return(builder.ToString());
        }
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("ObjectCreation:");
            builder.Indent(indent + 1).AppendFormat("Type: {0}", type).AppendLine();
            builder.Indent(indent + 1).AppendLine("Args:");
            foreach (var arg in args)
            {
                builder.AppendLine(arg.ToString(indent + 2));
            }
            builder.Indent(indent + 1).AppendLine("Constructor:");
            builder.Indent(indent + 2).AppendLine(Constructor.ToString());
            return(builder.ToString());
        }
Beispiel #30
0
        public override string ToString(int indent)
        {
            StringBuilder builder = new StringBuilder();

            builder.Indent(indent).AppendLine("ArrayAccess:");
            builder.Indent(indent + 1).AppendLine("Array:");
            builder.Append(array.ToString(indent + 2)).AppendLine();
            builder.Indent(indent + 1).AppendFormat("ElementType: {0}", type.FullName).AppendLine();
            builder.Indent(indent + 1).AppendLine("Indices:");
            foreach (var ind in indices)
            {
                builder.AppendLine(ind.ToString(indent + 2));
            }
            return(builder.ToString());
        }
Beispiel #31
0
        public static string DumpArray(Array array)
        {
            var builder = new StringBuilder();

            if (array.Length == 0)
            {
                builder.Indent("[]");
                return builder.ToString();
            }

            return builder.ToString();
        }
        private void BuildSerializeField(StringBuilder sb, FieldContext field, string prefix = "", bool isElement = false, bool isFieldLess = false)
        {
            var genType = field.GenType;
            var name = prefix + (isFieldLess ? "" : FieldNameFromField(field));
            if (genType.IsStruct)
            {
                BuildSerializeStruct(sb, name);
            }
            else if (genType.IsContainer)
            {
                BuildSerializeContainer(sb, name, genType);
            }
            else if (genType.IsBaseType || genType.IsEnum)
            {
                sb.Indent(level).Append("oprot.");

                if (genType.IsBaseType)
                {
                    switch (genType.GType)
                    {
                        case GType.Binary:
                            sb.Append("writeBinary(").Append(name).Append(");");
                            break;
                        case GType.String:
                            sb.Append("writeString(").Append(name).Append(");");
                            break;
                        case GType.Bool:
                            sb.Append("writeBool(").Append(name).Append(".booleanValue());");
                            break;
                        case GType.Byte:
                            sb.Append("writeByte(").Append(name).Append(".byteValue());");
                            break;
                        case GType.I16:
                            sb.Append("writeI16(").Append(name).Append(".shortValue());");
                            break;
                        case GType.I32:
                            sb.Append("writeI32(").Append(name).Append(".intValue());"); ;
                            break;
                        case GType.I64:
                            sb.Append("writeI64(").Append(name).Append(".longValue());");
                            break;
                        case GType.Double:
                            sb.Append("writeDouble(").Append(name).Append(".doubleValue());"); ;
                            break;
                        default:
                            throw new ArgumentException(string.Format("DO NOT KNOW HOW TO SERIALIZE BASE TYPE {0}", genType.GType));
                    }
                }
                else if (genType.IsEnum)
                {
                    sb.Append("writeI32(").Append(name).Append(".getValue());");
                }
                sb.AppendLine();
            }
            else
            {
                throw new ArgumentException(string.Format("DO NOT KNOW HOW TO SERIALIZE {0} TYPE {1}", field.Name, genType.JavaTypeName));
            }
        }
        private void BuildDeserializeField(StringBuilder sb, FieldContext field, string prefix = "", bool isPropertyless = false)
        {
            var genType = field.GenType;

            string name = prefix + (isPropertyless ? "" : PropertyNameFromField(field));

            if (genType.IsStruct)
            {
                BuildDeserializeStruct(sb, genType.CSharpTypeName, name);
            }
            else if (genType.IsContainer)
            {
                BuildDeserializeContainer(sb, genType, name);
            }
            else if (genType.IsBaseType || genType.IsEnum)
            {
                sb.Indent(level).Append(name).Append(" = ");

                if (genType.IsEnum)
                {
                    sb.Append("(").Append(genType.CSharpTypeName).Append(")");
                }

                sb.Append("iprot.");

                if (genType.IsBaseType)
                {
                    switch (genType.GType)
                    {
                        case GType.Binary:
                            sb.Append("ReadBinary();");
                            break;
                        case GType.String:
                            sb.Append("ReadString();");
                            break;
                        case GType.Bool:
                            sb.Append("ReadBool();");
                            break;
                        case GType.Byte:
                            sb.Append("ReadByte();");
                            break;
                        case GType.I16:
                            sb.Append("ReadI16();");
                            break;
                        case GType.I32:
                            sb.Append("ReadI32();");
                            break;
                        case GType.I64:
                            sb.Append("ReadI64();");
                            break;
                        case GType.Double:
                            sb.Append("ReadDouble();");
                            break;
                        default:
                            throw new ArgumentException(string.Format("DO NOT KNOW HOW TO DESERIALIZE BASE TYPE {0}", genType.GType));
                    }
                }
                else if (genType.IsEnum)
                {
                    sb.Append("ReadI32();");
                }
                sb.AppendLine();
            }
            else
            {
                throw new ArgumentException(string.Format("DO NOT KNOW HOW TO DESERIALIZE FIELD {0} TYPE {1}", field.Name, genType.CSharpTypeName));
            }
        }
        private void BuildSerializeField(StringBuilder sb, FieldContext field, string prefix = "", bool isElement = false, bool isPropertyLess = false)
        {
            var genType = field.GenType;
            var name = prefix + (isPropertyLess ? "" : PropertyNameFromField(field));
            if (genType.IsStruct)
            {
                BuildSerializeStruct(sb, name);
            }
            else if (genType.IsContainer)
            {
                BuildSerializeContainer(sb, name, genType);
            }
            else if (genType.IsBaseType || genType.IsEnum)
            {
                sb.Indent(level).Append("oprot.");

                //string nullableName = !isElement ? name + ".Value" : name;
                string nullableName = name + ".Value";

                if (genType.IsBaseType)
                {
                    switch (genType.GType)
                    {
                        case GType.Binary:
                            sb.Append("WriteBinary(").Append(name).Append(");");
                            break;
                        case GType.String:
                            sb.Append("WriteString(").Append(name).Append(");");
                            break;
                        case GType.Bool:
                            sb.Append("WriteBool(").Append(nullableName).Append(");");
                            break;
                        case GType.Byte:
                            sb.Append("WriteByte(").Append(nullableName).Append(");");
                            break;
                        case GType.I16:
                            sb.Append("WriteI16(").Append(nullableName).Append(");");
                            break;
                        case GType.I32:
                            sb.Append("WriteI32(").Append(nullableName).Append(");"); ;
                            break;
                        case GType.I64:
                            sb.Append("WriteI64(").Append(nullableName).Append(");");
                            break;
                        case GType.Double:
                            sb.Append("WriteDouble(").Append(nullableName).Append(");"); ;
                            break;
                        default:
                            throw new ArgumentException(string.Format("DO NOT KNOW HOW TO SERIALIZE BASE TYPE {0}", genType.GType));
                    }
                }
                else if (genType.IsEnum)
                {
                    sb.Append("WriteI32((int)").Append(nullableName).Append(");");
                }
                sb.AppendLine();
            }
            else
            {
                throw new ArgumentException(string.Format("DO NOT KNOW HOW TO SERIALIZE {0} TYPE {1}", field.Name, genType.CSharpTypeName));
            }
        }
Beispiel #35
0
        public void LogPacket(IPacket packet, PacketDirection direction)
        {
            var builder = new StringBuilder();
            var prefix = direction == PacketDirection.Server ? "[CLIENT > SERVER] " :
                                                               "[CLIENT < SERVER] ";
            var packetType = packet.GetType();
            var packetName = packetType.Name;
            var packetFields = packetType.GetFields(BindingFlags);

            builder.Append(DateTime.Now.ToString("[~HH:mm:ss.fff] "));
            builder.Append(prefix);
            builder.Append(FormatPacketName(packetName));
            builder.AppendFormat(" 0x{0:X2}", packet.ID);

            if (packet is UnknownPacket)
            {
                var unknownPacket = packet as UnknownPacket;
                builder.AppendFormat(" Length {0} Version {1}", unknownPacket.Length, unknownPacket.Version);
            }

            if (packetFields.Length > 0)
            {
                builder.AppendLine();
                builder.AppendLine("{");

                for (int i = 0; i < packetFields.Length; i++)
                {
                    var field = packetFields[i];
                    var fieldName = field.Name;
                    var fieldValue = field.GetValue(packet);

                    if (field.IsPrivate && !LogPrivateFields)
                        continue;

                    builder.Indent();
                    builder.AppendFormat("{0}: ", fieldName);

                    if (fieldValue == null) builder.Append("null");
                    else if (fieldValue is string) builder.AppendFormat("{0}{1}{2}", "\"", fieldValue, "\"");
                    else if (fieldValue is ICommand[])
                    {
                        var cmd = fieldValue as ICommand[];
                        builder.Append(DumpCommandArray(cmd));
                    }
                    else if (fieldValue is byte[])
                    {
                        var byteArray = fieldValue as byte[];
                        builder.AppendLine();
                        builder.Append(DumpByteArray(byteArray));
                    }
                    else builder.Append(fieldValue);
                    builder.AppendLine();
                }

                builder.AppendLine("}");
            }
            else builder.AppendLine(" { }"); // no fields

            var builderString = builder.ToString();

            LogWriter.WriteLine(builderString);
            if (LogConsole) Console.WriteLine(builderString);
        }
Beispiel #36
0
        private static string DumpCommandArray(ICommand[] cmds)
        {
            var builder = new StringBuilder();

            if (cmds.Length == 0)
                builder.Append("Empty");

            for (int i = 0; i < cmds.Length; i++)
            {
                var cmdType = cmds[i].GetType();
                var cmdName = cmdType.Name;
                var cmdFields = cmdType.GetFields();

                builder.Append(cmdName);
                builder.Indent("[");
                builder.AppendLine();

                for (int k = 0; k < cmdFields.Length; k++)
                {
                    var field = cmdFields[i];
                    var fieldName = field.Name;
                    var fieldValue = field.GetValue(cmds[i]);

                    if (field.IsPrivate)
                        continue;

                    builder.Indent();
                    builder.AppendFormat("{0}: ", fieldName);

                    if (fieldValue == null) builder.Append("null");
                    else if (fieldValue is string) builder.AppendFormat("{0}{1}{2}", "\"", fieldValue, "\"");
                    else if (fieldValue is ICommand[])
                    {
                        var cmd = fieldValue as ICommand[];
                        builder.Append(DumpCommandArray(cmd));
                    }
                    else if (fieldValue is byte[])
                    {
                        var byteArray = fieldValue as byte[];
                        builder.AppendLine();
                        builder.Append(DumpByteArray(byteArray));
                    }
                    else builder.Append(fieldValue);
                    builder.AppendLine();
                }

                builder.AppendLine();
                builder.Indent("]");
            }
            return builder.ToString();
        }
Beispiel #37
0
        private static string DumpByteArray(byte[] bytes)
        {
            var builder = new StringBuilder();

            if (bytes.Length == 0)
            {
                builder.Indent("[]");
                return builder.ToString();
            }

            builder.Indent("[");
            for (int i = 0; i < bytes.Length; i++)
            {
                if (i % 32 == 0) // add new lines every 32 bytes
                {
                    builder.AppendLine();
                    builder.Indent(2);
                }
                builder.Append(bytes[i].ToString("X2") + " ");
            }
            builder.AppendLine();
            builder.Indent("]");
            return builder.ToString();
        }
        private void BuildDeserializeListElement(StringBuilder sb, GenType listType, string prefix = "")
        {
            string elem = Tmp("_elem");

            var felem = new FieldContext(elem, listType.ElementType);

            sb.Indent(level).Append(DeclareField(felem, true)).AppendLine();

            BuildDeserializeField(sb, felem);

            sb.Indent(level).Append(prefix).Append(".Add(").Append(elem).Append(");").AppendLine();
        }
 private void ScopeDown(StringBuilder sb)
 {
     level--;
     sb.Indent(level).AppendLine("}");
 }
        private void BuildDeserializeContainer(StringBuilder sb, GenType genType, string prefix = "")
        {
            ScopeUp(sb);

            string obj;

            if (genType.IsMap)
            {
                obj = Tmp("_map");
            }
            else if (genType.IsSet)
            {
                obj = Tmp("_set");
            }
            else
            {
                obj = Tmp("_list");
            }

            sb.Indent(level).Append(prefix).Append(" = new ").Append(genType.CSharpTypeName).Append("();").AppendLine();
            if (genType.IsMap)
            {
                sb.Indent(level).Append("TMap ").Append(obj).Append(" = iprot.ReadMapBegin();").AppendLine();
            }
            else if (genType.IsSet)
            {
                sb.Indent(level).Append("TSet ").Append(obj).Append(" = iprot.ReadSetBegin();").AppendLine();
            }
            else
            {
                sb.Indent(level).Append("TList ").Append(obj).Append(" = iprot.ReadListBegin();").AppendLine();
            }

            string i = Tmp("_i");
            sb.Indent(level).Append("for(int ").Append(i).Append(" = 0; ").Append(i).Append(" < ").Append(obj).Append(".Count").Append("; ").Append("++").Append(i).Append(")").AppendLine();
            ScopeUp(sb);

            if (genType.IsMap)
            {
                BuildDeserializeMapElement(sb, genType, prefix);
            }
            else if (genType.IsSet)
            {
                BuildDeserializeSetElement(sb, genType, prefix);
            }
            else if (genType.IsList)
            {
                BuildDeserializeListElement(sb, genType, prefix);
            }

            ScopeDown(sb);

            if (genType.IsMap)
            {
                sb.Indent(level).Append("iprot.ReadMapEnd();").AppendLine();
            }
            else if (genType.IsSet)
            {
                sb.Indent(level).Append("iprot.ReadSetEnd();").AppendLine();
            }
            else if (genType.IsList)
            {
                sb.Indent(level).Append("iprot.ReadListEnd();").AppendLine();
            }

            ScopeDown(sb);
        }
        private string BuildStructToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Indent(level).Append("public override string ToString() {").AppendLine();
            IndentUp();

            sb.Indent(level).Append("StringBuilder __sb = new StringBuilder(\"").Append(Name).Append("(\");").AppendLine();

            Fields.Sort();

            sb.Indent(level).Append("bool __first = true;").AppendLine();

            foreach(var field in Fields)
            {
                sb.Indent(level).Append("if (").Append(field.CSharpPropertyName).Append(" != null) {").AppendLine();
                IndentUp();
                sb.Indent(level).Append("if(!__first) { __sb.Append(\", \"); }").AppendLine();
                sb.Indent(level).Append("__first = false;").AppendLine();
                sb.Indent(level).Append("__sb.Append(\"").Append(field.CSharpPropertyName).Append(": \");").AppendLine();

                if (field.GenType.IsStruct)
                {
                    sb.Indent(level).Append("__sb.Append(").Append(field.CSharpPropertyName).Append("== null ? \"<null>\" : ")
                        .Append(field.CSharpPropertyName).Append(".ToString());").AppendLine();
                }
                else
                {
                    sb.Indent(level).Append("__sb.Append(").Append(field.CSharpPropertyName).Append(");").AppendLine();
                }

                IndentDown();
                sb.Indent(level).Append("}").AppendLine();
            }

            sb.Indent(level).Append("__sb.Append(\")\");").AppendLine();
            sb.Indent(level).Append("return __sb.ToString();").AppendLine();

            IndentDown();
            sb.Indent(level).Append("}").AppendLine().AppendLine();

            return sb.ToString();
        }
        private void BuildSerializeContainer(StringBuilder sb, string prefix, GenType genType)
        {
            ScopeUp(sb);

            if (genType.IsMap)
            {
                sb.Indent(level).Append("oprot.WriteMapBegin(new TMap(").Append(genType.KeyType.TTypeName).Append(", ");
                sb.Append(genType.ValueType.TTypeName).Append(", ").Append(prefix).Append(".Count));").AppendLine();
            }
            else if (genType.IsSet)
            {
                sb.Indent(level).Append("oprot.WriteSetBegin(new TSet(").Append(genType.ElementType.TTypeName).Append(", ");
                sb.Append(prefix).Append(".Count));").AppendLine();
            }
            else if (genType.IsList)
            {
                sb.Indent(level).Append("oprot.WriteListBegin(new TList(").Append(genType.ElementType.TTypeName).Append(", ");
                sb.Append(prefix).Append(".Count));").AppendLine();
            }

            string iter = Tmp("_iter");
            if (genType.IsMap)
            {
                sb.Indent(level).Append("foreach (").Append(genType.KeyType.CSharpTypeName).Append(" ").Append(iter).Append(" in ").Append(prefix).Append(".Keys)").AppendLine();
            }
            else if (genType.IsSet || genType.IsList)
            {
                sb.Indent(level).Append("foreach (").Append(genType.ElementType.CSharpTypeName).Append(" ").Append(iter).Append(" in ").Append(prefix).Append(")").AppendLine();
            }

            ScopeUp(sb);

            if (genType.IsMap)
            {
                BuildSerializeMapElement(sb, genType, iter, prefix);
            }
            else if (genType.IsSet)
            {
                BuildSerializeSetElement(sb, genType, iter);
            }
            else if (genType.IsList)
            {
                BuildSerializeListElement(sb, genType, iter);
            }

            ScopeDown(sb);

            if (genType.IsMap)
            {
                sb.Indent(level).AppendLine("oprot.WriteMapEnd();");
            }
            else if (genType.IsSet)
            {
                sb.Indent(level).AppendLine("oprot.WriteSetEnd();");
            }
            else if (genType.IsList)
            {
                sb.Indent(level).AppendLine("oprot.WriteListEnd();");
            }

            ScopeDown(sb);
        }
 private void BuildSerializeStruct(StringBuilder sb, string prefix)
 {
     sb.Indent(level).Append(prefix).Append(".Write(oprot);").AppendLine();
 }
        private string BuildStructReader()
        {
            StringBuilder sb = new StringBuilder();
            tempNum = 0;
            sb.Indent(level).AppendLine("public void Read(TProtocol iprot)");
            ScopeUp(sb);

            Fields.Sort();

            sb.Indent(level).Append("TField field;").AppendLine();
            sb.Indent(level).Append("iprot.ReadStructBegin();").AppendLine();
            sb.Indent(level).Append("while (true)").AppendLine();
            ScopeUp(sb);

            sb.Indent(level).Append("field = iprot.ReadFieldBegin();").AppendLine();

            sb.Indent(level).Append("if (field.Type == TType.Stop) { ").AppendLine();
            IndentUp();
            sb.Indent(level).Append("break;").AppendLine();
            IndentDown();
            sb.Indent(level).Append("}").AppendLine();

            sb.Indent(level).Append("switch (field.ID)").AppendLine();

            ScopeUp(sb);

            foreach (var field in Fields)
            {
                sb.Indent(level).Append("case ").Append(field.Id).Append(":").AppendLine();
                IndentUp();
                sb.Indent(level).Append("if (field.Type == ").Append(field.GenType.TTypeName).Append(") {").AppendLine();
                IndentUp();

                BuildDeserializeField(sb, field);

                IndentDown();
                sb.Indent(level).Append("} else {").AppendLine();
                sb.Indent(level).Append("    TProtocolUtil.Skip(iprot, field.Type);").AppendLine();
                sb.Indent(level).Append("}").AppendLine();
                sb.Indent(level).Append("break;").AppendLine();
                IndentDown();
            }

            sb.Indent(level).Append("default:").AppendLine();
            IndentUp();
            sb.Indent(level).Append("TProtocolUtil.Skip(iprot, field.Type);").AppendLine();
            sb.Indent(level).Append("break;").AppendLine();
            IndentDown();

            ScopeDown(sb);

            sb.Indent(level).Append("iprot.ReadFieldEnd();").AppendLine();

            ScopeDown(sb);

            sb.Indent(level).Append("iprot.ReadStructEnd();").AppendLine();

            foreach (var field in Fields)
            {
                // validation
                if (field.Required)
                {
                    sb.Indent(level).Append("if (").Append(field.CSharpPropertyName).Append(" == null)").AppendLine();
                    IndentUp();
                    sb.Indent(level).Append("throw new TProtocolException(TProtocolException.INVALID_DATA, \"missing required property '").Append(field.CSharpPropertyName).Append("'\");").AppendLine();
                    IndentDown();
                }
            }

            IndentDown();

            sb.Indent(level).Append("}").AppendLine().AppendLine();

            return sb.ToString();
        }
        private string BuildStructWriter()
        {
            StringBuilder sb = new StringBuilder();
            tempNum = 0;
            sb.Indent(level).AppendLine("public void Write(TProtocol oprot) {");
            IndentUp();

            Fields.Sort();

            sb.Indent(level).Append("TStruct struc = new TStruct(\"").Append(Name).AppendLine("\");");
            sb.Indent(level).AppendLine("oprot.WriteStructBegin(struc);");

            if (Fields.Count > 0)
            {
                // validation
                foreach (var field in Fields)
                {
                    if (field.Required)
                    {
                        sb.Indent(level).Append("if (").Append(field.CSharpPropertyName).Append(" == null)").AppendLine();
                        IndentUp();
                        sb.Indent(level).Append("throw new TProtocolException(TProtocolException.INVALID_DATA, \"missing required property '").Append(field.CSharpPropertyName).Append("'\");").AppendLine();
                        IndentDown();
                    }
                }

                sb.Indent(level).AppendLine("TField field = new TField();");

                foreach (var field in Fields)
                {
                    sb.Indent(level).Append("if (").Append(field.CSharpPropertyName).AppendLine(" != null) {");
                    IndentUp();
                    sb.Indent(level).Append("field.Name = ").Append("\"").Append(field.Name).AppendLine("\";");
                    sb.Indent(level).Append("field.Type = ").Append(field.GenType.TTypeName).AppendLine(";");
                    sb.Indent(level).Append("field.ID = ").Append(field.Id).AppendLine(";");
                    sb.Indent(level).Append("oprot.WriteFieldBegin(field);").AppendLine();

                    BuildSerializeField(sb, field);

                    sb.Indent(level).Append("oprot.WriteFieldEnd();").AppendLine();
                    IndentDown();
                    sb.Indent(level).AppendLine("}");
                }
            }

            sb.Indent(level).AppendLine("oprot.WriteFieldStop();");
            sb.Indent(level).AppendLine("oprot.WriteStructEnd();");
            IndentDown();
            sb.Indent(level).AppendLine("}").AppendLine().AppendLine();

            return sb.ToString();
        }
 private string objectToLua(object obj, int indentLevel)
 {
     Type t = obj.GetType();
     if (t == typeof(Int32) ||
         t == typeof(float) ||
         t == typeof(Double) )
     {
           return obj.ToString();
     }
     else if (t == typeof(Boolean))
     {
         return (bool)obj ? "true" : "false";
     }
     else if (t.IsEnum || t == typeof(String))
     {
         return "[[" + obj.ToString() + "]]";
     }
     else if (obj is IEnumerable)
     {
         IEnumerable ie = obj as IEnumerable;
         StringBuilder sb = new StringBuilder();
         sb.Append("{");
         foreach (object o in ie)
         {
             sb.AppendLine();
             sb.Indent(indentLevel).Append(objectToLua(o, indentLevel + 1)).AppendLine(",");
         }
         sb.Indent(indentLevel - 1).Append("}");
         return sb.ToString();
     }
     else if (t.IsSerializable)
     {
         if (seen.Contains(obj))
         {
             throw new Exception("Circular reference detected");
         }
         else
         {
             seen.Add(obj);
         }
         var props = t.GetProperties();
         StringBuilder sb = new StringBuilder();
         sb.AppendLine("{");
         sb.Indent(indentLevel - 1).Append("objectType = [[").Append(t.Name).AppendLine("]],");
         foreach (var p in props)
         {
             if (!p.IsDefined(typeof(XmlIgnoreAttribute), true))
             {
                 String name = p.Name;
                 if (caseType == CaseType.CamelCaseStartLower)
                 {
                     name = name.Substring(0, 1).ToLower() + name.Substring(1);
                 }
                 sb.Indent(indentLevel).Append(name).Append(" = ").Append(objectToLua(p.GetValue(obj, null), indentLevel + 1)).AppendLine(",");
             }
         }
         sb.Indent(indentLevel - 1).Append("}");
         return sb.ToString();
     }
     else
     {
         throw new SerializationException("Can't serialize this type");
     }
 }
 private void BuildDeserializeStruct(StringBuilder sb, string name, string prefix = "")
 {
     sb.Indent(level).Append(prefix).Append(" = new ").Append(name).Append("();").AppendLine();
     sb.Indent(level).Append(prefix).Append(".Read(iprot);").AppendLine();
 }
        private string BuildStructEquals()
        {
            StringBuilder sb = new StringBuilder();

            sb.Indent(level).Append("public override bool Equals(object that) {").AppendLine();
            IndentUp();

            sb.Indent(level).Append("var other = that as ").Append(CSharpName).Append(";").AppendLine();
            sb.Indent(level).Append("if (other == null) return false;").AppendLine();
            sb.Indent(level).Append("if (ReferenceEquals(this, other)) return true;").AppendLine();

            Fields.Sort();

            bool first = true;

            foreach (var field in Fields)
            {
                if (first)
                {
                    first = false;
                    sb.Indent(level).Append("return ");
                    IndentUp();
                }
                else
                {
                    sb.AppendLine();
                    sb.Indent(level).Append("&& ");
                }

                if (field.GenType.IsContainer)
                {
                    sb.Append("TCollections.Equals(");
                    sb.Append(field.CSharpPropertyName).Append(", other.").Append(field.CSharpPropertyName).Append(")");
                }
                else if (field.GenType.IsBinary)
                {
                    sb.Append("(").Append(field.CSharpPropertyName).Append(" == null ? other.").Append(field.CSharpPropertyName).
                       Append(" == null : ").Append(field.CSharpPropertyName).Append(".SequenceEqual(other.").
                       Append(field.CSharpPropertyName).Append("))");
                }
                else
                {
                    sb.Append("System.Object.Equals(");
                    sb.Append(field.CSharpPropertyName).Append(", other.").Append(field.CSharpPropertyName).Append(")");
                }
            }
            if (first)
            {
                sb.Indent(level).Append("return true;").AppendLine();
            }
            else
            {
                sb.AppendLine(";");
                IndentDown();
            }

            IndentDown();
            sb.Indent(level).Append("}").AppendLine().AppendLine();

            return sb.ToString();
        }
        private void BuildDeserializeMapElement(StringBuilder sb, GenType mapType, string prefix = "")
        {
            string key = Tmp("_key");
            string val = Tmp("_val");

            var fkey = new FieldContext(key, mapType.KeyType);
            var fval = new FieldContext(val, mapType.ValueType);

            sb.Indent(level).Append(DeclareField(fkey)).AppendLine();
            sb.Indent(level).Append(DeclareField(fval)).AppendLine();

            BuildDeserializeField(sb, fkey);
            BuildDeserializeField(sb, fval);

            sb.Indent(level).Append(prefix).Append("[").Append(key).Append("] = ").Append(val).Append(";").AppendLine();
        }
 private void ScopeUp(StringBuilder sb)
 {
     sb.Indent(level).AppendLine("{");
     level++;
 }
        private string BuildStructHashcode()
        {
            StringBuilder sb = new StringBuilder();
            sb.Indent(level).Append("public override int GetHashCode() {").AppendLine();
            IndentUp();

            sb.Indent(level).Append("int hashcode = 0;").AppendLine();
            sb.Indent(level).Append("unchecked {").AppendLine();
            IndentUp();

            Fields.Sort();

            foreach (var field in Fields)
            {
                if (field.GenType.IsContainer)
                {
                    sb.Indent(level).Append("hashcode = (hashcode * 397) ^ ");
                    sb.Append("(").Append(field.CSharpPropertyName).Append(" == null ? 0 : ");
                    sb.Append("(TCollections.GetHashCode(").Append(field.CSharpPropertyName).Append("))");
                    sb.Append(");").AppendLine();
                }
                else if (field.GenType.IsBinary)
                {
                    sb.Indent(level).Append("if (").Append(field.CSharpPropertyName).AppendLine(" != null)");
                    ScopeUp(sb);
                    string elem = Tmp("_byte");
                    sb.Indent(level).Append("foreach (byte ").Append(elem).Append(" in ").Append(field.CSharpPropertyName).AppendLine(")");
                    IndentUp();
                    sb.Indent(level).Append("hashcode = (hashcode * 397) ^").Append(elem).AppendLine(";"); 
                    IndentDown();
                    ScopeDown(sb);
                }
                else
                {
                    sb.Indent(level).Append("hashcode = (hashcode * 397) ^ ");
                    sb.Append("(").Append(field.CSharpPropertyName).Append(" == null ? 0 : ");
                    sb.Append("(").Append(field.CSharpPropertyName).Append(".GetHashCode())");
                    sb.Append(");").AppendLine();
                }

            }

            IndentDown();
            sb.Indent(level).Append("}").AppendLine();
            sb.Indent(level).Append("return hashcode;").AppendLine();

            IndentDown();
            sb.Indent(level).Append("}").AppendLine().AppendLine();

            return sb.ToString();
        }