Example #1
0
        public static string DumpArray(Array array)
        {
            var builder = new StringBuilder();

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

            return builder.ToString();
        }
        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 void ScopeDown(StringBuilder sb)
 {
     level--;
     sb.Indent(level).AppendLine("}");
 }
Example #6
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();
        }
        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 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 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);
        }
Example #10
0
        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();
        }
Example #11
0
 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();
 }
Example #12
0
        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));
            }
        }
Example #13
0
        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));
            }
        }
Example #14
0
        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();
        }
Example #15
0
        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();
        }
Example #16
0
        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();
        }
Example #17
0
 private void ScopeUp(StringBuilder sb)
 {
     sb.Indent(level).AppendLine("{");
     level++;
 }
Example #18
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();
        }
Example #19
0
        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));
            }
        }
Example #20
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);
        }
Example #21
0
 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");
     }
 }