private static void WriteSerializeFieldStatementRecursive(ICodeWriter o, bool writeToBitstream, Type variableType, string variableName)
 {
     if (variableType.IsArray)
     {
         Type elemType = variableType.GetElementType();
         if (writeToBitstream)
         {
             WriteStreamWriteStatement(o, variableName + ".Length");
             o.BeginBlock("for (int i = 0; i < {0}.Length; i++) {{", variableName);
             WriteSerializeFieldStatementRecursive(o, writeToBitstream, elemType, variableName + "[i]");
             o.EndBlock("}");
         }
         else
         {
             string lengthVariableName = "_" + variableName + "Length";
             o.WriteLine("int {0};", lengthVariableName);
             WriteStreamReadStatement(o, "out", lengthVariableName);
             o.WriteLine("{0} = new {1}[{2}];", variableName, elemType.ToString(), lengthVariableName);
             o.BeginBlock("for (int i = 0; i < {0}; i++) {{", lengthVariableName);
             WriteSerializeFieldStatementRecursive(o, writeToBitstream, elemType, variableName + "[i]");
             o.EndBlock("}");
         }
     }
     else
     {
         WriteSerializeNonCollectionStatement(o, writeToBitstream, variableType, variableName);
     }
 }
 public void Write(ICodeWriter o)
 {
     o.WriteLine("[Transient]");
     o.BeginBlock("internal sealed class {0} : IEventFactory {{", factoryName);
     WriteRecreateEvent(o);
     o.EndBlock("}");
 }
 public static void WriteAccessor(ICodeWriter o, string typeName, string variableNameInLowerCamelCase)
 {
     o.BeginBlock("public {0} {1} {{", typeName, GetVariableNameInUpperCamelCase(variableNameInLowerCamelCase));
     o.WriteLine("get {{ return {0}; }}", variableNameInLowerCamelCase);
     o.WriteLine("set {{ {0} = value; }}", variableNameInLowerCamelCase);
     o.EndBlock("}");
 }
 public static void WriteSingleton(ICodeWriter o, string className)
 {
     o.WriteLine("private static {0} instance = new {0}();", className);
     o.BeginBlock("public static {0} Instance {{", className);
     o.WriteLine("get { return instance; }");
     o.EndBlock("}");
     o.WriteLine("private {0}() {{ }}", className);
 }
 public void Write(ICodeWriter o)
 {
     o.WriteLine("[Transient]");
     o.BeginBlock("internal sealed class {0} : IEventHandlers {{", handlersName);
     WriteCallHandler(o);
     WriteEvents(o);
     o.EndBlock("}");
 }
 private void WriteCallHandler(ICodeWriter o)
 {
     o.BeginBlock("public void CallHandler(IEvent e) {");
     o.BeginBlock("switch (e.Id) {");
     foreach (EventInfo ei in eventInfos)
     {
         o.BeginBlock("case {0}:", ei.EventId);
         string handlerName = GetHandlerName(ei.Type.Name);
         o.BeginBlock("if ({0} != null)", handlerName);
         o.WriteLine("{0}(({1})e);", handlerName, ei.Type.Name);
         o.EndBlock("break;");
         o.EndBlock("");
     }
     o.BeginBlock("default:");
     o.WriteLine("throw new UnknownEventIdException(e.Id, \"{0}\");", handlersName + ".CallHandler()");
     o.EndBlock("");
     o.EndBlock("}");
     o.EndBlock("}");
 }
 public void Write(ICodeWriter o)
 {
     string className = t.Name;
     string protocolName = attr.ProtocolName;
     o.BeginBlock("internal partial class {0} : IProtocolInfo {{", className);
     ClassGeneratorHelper.WriteSingleton(o, className);
     WriteName(o, protocolName);
     WriteVersionProperty(o, "MajorVersion", attr.MajorVersion);
     WriteVersionProperty(o, "MinorVersion", minorVersion);
     o.EndBlock("}");
 }
 private void WriteRecreateEvent(ICodeWriter o)
 {
     o.BeginBlock("public IEvent RecreateSimpleEvent(BitStream source) {");
     o.WriteLine("Debug.Assert(source != null);");
     o.WriteLine("IEvent _event;");
     o.WriteLine("int id;");
     o.WriteLine("if(!source.Read(out id)) throw new DeserializationException();");
     o.WriteLine("source.ResetReadPointer();");
     o.BeginBlock("switch (id) {");
     foreach (EventInfo ei in eventInfos)
     {
         o.BeginBlock("case {0}:", ei.EventId);
         o.WriteLine("_event = new {0}(source);", ei.Type.Name);
         o.WriteLine("break;");
         o.EndBlock("");
     }
     o.BeginBlock("default:");
     o.WriteLine("throw new UnknownEventIdException(id, \"{0}\");", factoryName + ".RecreateEvent()");
     o.EndBlock("");
     o.EndBlock("}");
     o.WriteLine("return _event;");
     o.EndBlock("}");
 }
 public void Write(ICodeWriter o)
 {
     o.BeginBlock("internal partial class {0} : IEvent {{", type.Name);
     WriteCtorWithId(o);
     WriteCtorWithStream(o);
     WriteAccessors(o);
     WriteGetStream(o);
     WriteId(o);
     WriteSourceOid(o);
     WriteTargetOid(o);
     WriteSender(o);
     WriteProtocolInfo(o);
     o.EndBlock("}");
 }
 private void WriteSetData(ICodeWriter o)
 {
     StringBuilder arg = new StringBuilder();
     FieldInfo[] fields = GetSerializableFields();
     for (int i = 0; i < fields.Length; i++)
     {
         if (0 < i) arg.Append(", ");
         arg.Append(fields[i].FieldType.ToString());
         arg.Append(" ");
         arg.Append(fields[i].Name);
     }
     o.BeginBlock("public void SetData({0}) {{", arg.ToString());
     foreach (FieldInfo field in fields)
     {
         o.WriteLine("this.{0} = {0};", field.Name);
     }
     o.EndBlock("}");
 }
        // TODO: ProtocolGenerator can't handle null reference.
        private void WriteGetStream(ICodeWriter o)
        {
            o.BeginBlock("public BitStream Stream {");
            o.BeginBlock("get {");
            o.WriteLine("BitStream eventStream = new BitStream();");
            WriteStreamWriteStatement(o, "id");
            WriteStreamWriteStatement(o, "sourceOId");
            WriteStreamWriteStatement(o, "targetOId");

            foreach (FieldInfo field in GetSerializableFields())
            {
                WriteSerializeFieldStatementRecursive(o, true, field.FieldType, field.Name);
            }
            o.WriteLine("return eventStream;");
            o.EndBlock("}");
            o.EndBlock("}");
        }
        private void WriteCtorWithStream(ICodeWriter o)
        {
            o.BeginBlock("public {0}(BitStream source) {{", type.Name);
            WriteStreamReadStatement(o, "out", "id");
            WriteStreamReadStatement(o, "out", "sourceOId");
            WriteStreamReadStatement(o, "out", "targetOId");

            foreach (FieldInfo field in GetSerializableFields())
            {
                WriteSerializeFieldStatementRecursive(o, false, field.FieldType, field.Name);
            }
            o.EndBlock("}");
        }
 private void WriteCtorWithId(ICodeWriter o)
 {
     o.BeginBlock("public {0}() {{", type.Name);
     o.WriteLine("id = {0};", eventId);
     o.EndBlock("}");
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="o"></param>
        /// <param name="typeName"></param>
        /// <param name="variableNameInLowerCamelCase"></param>
        /// <param name="defaultValue">Can be null</param>
        /// <param name="doesWriteGetter"></param>
        /// <param name="getterAccessibility">Can be null</param>
        /// <param name="doesWriteSetter"></param>
        /// <param name="setterAccessibility">Can be null</param>
        private static void WritePropertyImpl(ICodeWriter o, string typeName, string variableNameInLowerCamelCase, string defaultValue, bool doesWriteGetter, string getterAccessibility, bool doesWriteSetter, string setterAccessibility)
        {
            string getterAccessibilityWithSpace = "";
            string setterAccessibilityWithSpace = "";
            string defaultValueWithAssignMark = "";

            if (getterAccessibility != null)
            {
                getterAccessibilityWithSpace = getterAccessibility + " ";
            }
            if (setterAccessibility != null)
            {
                setterAccessibilityWithSpace = setterAccessibility + " ";
            }
            if (defaultValue != null)
            {
                defaultValueWithAssignMark = " = " + defaultValue;
            }

            string variableNameInUpperCamelCase = GetVariableNameInUpperCamelCase(variableNameInLowerCamelCase);

            o.BeginBlock("public {0} {1} {{", typeName, variableNameInUpperCamelCase);
            if (doesWriteGetter)
            {
                o.WriteLine("{0}get {{ return {1}; }}", getterAccessibilityWithSpace, variableNameInLowerCamelCase);
            }
            if (doesWriteSetter)
            {
                o.WriteLine("{0}set {{ {1} = value; }}", setterAccessibilityWithSpace, variableNameInLowerCamelCase);
            }
            o.EndBlock("}");
            o.WriteLine("{0} {1}{2};", typeName, variableNameInLowerCamelCase, defaultValueWithAssignMark);
        }
 public static void WriteGetAccessor(ICodeWriter o, string typeName, string propertyName, string value)
 {
     o.BeginBlock("public {0} {1} {{", typeName, propertyName);
     o.WriteLine("get {{ return {0}; }}", value);
     o.EndBlock("}");
 }