private static Class BuildBaseClass(Record record, Dictionary<string, Record> AllRecords) { string className = record.Name; Class baseClass = new Class(className); baseClass.Modifiers.Add("public"); baseClass.Modifiers.Add("partial"); if (record.Parent != null) { baseClass.BaseClass = record.Parent; baseClass.Constructors.Add(String.Format("public {0}() {{ }}", className)); baseClass.Constructors.Add(String.Format( "public {0}({1} record) : base(record) {{ }}", className, baseClass.BaseClass )); if (record.Fields.Count > 0) { foreach (RecordField member in record.Fields) { Field field = new Field(member.Type, member.Name); field.Modifiers.Add("public"); if (member.Description != null) { field.Summary = member.Description; } baseClass.Members.Add(field); } } } else { Method method = new Method(className, "Read"); method.Modifiers.Add("public"); method.Modifiers.Add("static"); method.Params.Add(new Parameter("Stream", "stream")); method.MethodBody.Add(String.Format("{0} record = {0}.ReadBase(stream);", className)); CodeBlock switchblock = new CodeBlock("switch (record.Type)"); AddChildCase(record, className, switchblock); CodeLines defaultblock = new CodeLines(); defaultblock.Lines.Add("default:"); defaultblock.Lines.Add("\treturn record;"); switchblock.Add(defaultblock); method.MethodBody.Add(switchblock); baseClass.Members.Add(method); } return baseClass; }
private static void AddChildCase(Record record, string className, CodeBlock switchblock) { foreach (Record childRecord in record.ChildRecords) { if (!childRecord.IsAbstract) { string name = childRecord.Name; CodeLines caseblock = new CodeLines(); caseblock.Lines.Add(String.Format("case {0}Type.{1}:", className, name)); caseblock.Lines.Add(String.Format("\treturn new {0}(record);", name)); switchblock.Add(caseblock); } else { AddChildCase(childRecord, className, switchblock); } } }
private static Method EncodeMethod(Record record, string baseName, List<RecordField> members) { Method method = new Method("void", "Encode"); method.Modifiers.Add("public"); if (record.IsCutomized) { method.Name = "encode"; } else { method.Modifiers.Add("override"); } method.MethodBody.Add("MemoryStream stream = new MemoryStream();"); method.MethodBody.Add("BinaryWriter writer = new BinaryWriter(stream);"); foreach (RecordField member in members) { string typeName = member.Type; string format = "writer.Write({0});"; switch (typeName) { case "String": format = "Record.WriteString(writer, {0}, " + member.ExtraInfo + ");"; break; case "Guid": format = "writer.Write({0}.ToByteArray());"; break; case "List<StringOffset>": format = "WriteStringOffset(writer, {0});"; break; } if (typeName.StartsWith("List<") || typeName.StartsWith("FastSearchList<")) { typeName = StringHelper.GetSubStringBetween(typeName, '<', '>'); CodeBlock foreach_block = new CodeBlock(); string loopVar = typeName.ToLower() + "Var"; foreach_block.Leading = String.Format("foreach({0} {1} in {2})", typeName, loopVar, member.Name); foreach_block.Add(String.Format(format, loopVar)); method.MethodBody.Add(foreach_block); } else { method.MethodBody.Add(String.Format(format, member.Name)); } } string sizeType = baseName == "EscherRecord" ? "UInt32" : "UInt16"; method.MethodBody.Add("this.Data = stream.ToArray();"); method.MethodBody.Add("this.Size = (" + sizeType + ")Data.Length;"); method.MethodBody.Add("base.Encode();"); return method; }
private static Method DecodeMethod(Record record, string baseName, List<RecordField> members) { Method method = new Method("void", "Decode"); method.Modifiers.Add("public"); if (record.IsCutomized) { method.Name = "decode"; } else { method.Modifiers.Add("override"); } method.MethodBody.Add("MemoryStream stream = new MemoryStream(Data);"); method.MethodBody.Add("BinaryReader reader = new BinaryReader(stream);"); foreach (RecordField member in members) { string typeName = member.Type; if (typeName.StartsWith("List<") || typeName.StartsWith("FastSearchList<")) { method.MethodBody.Add(String.Format("int count = {0};", member.ExtraInfo)); method.MethodBody.Add(String.Format("this.{0} = new {1}(count);", member.Name, typeName)); typeName = StringHelper.GetSubStringBetween(typeName, '<', '>'); string format = "{0}.Add(" + GetReadingCode(typeName, "16") + ");"; CodeBlock for_loop = new CodeBlock("for (int i = 0; i < count; i++)"); for_loop.Add(String.Format(format, member.Name)); method.MethodBody.Add(for_loop); } else { string format = "this.{0} = " + GetReadingCode(member.Type, member.ExtraInfo) + ";"; method.MethodBody.Add(String.Format(format, member.Name)); } } return method; }