/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 1) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { if (!att.IsVerifyLogin) { // 如果不需要验证登录数据,则第一个对象必须是NetState对象 Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } var componentType = param[0].ParameterType; var field = componentType.GetField("ComponentId"); if (field == null || !field.IsStatic) { Logs.Error("{0}.{1} 必须包含一个 ComponentId 的常量字符串作为登录验证后和NetState绑定的组件。"); return; } } if (att.MethodType == NetMethodType.PacketReader) { #region PacketReader if (param[1].ParameterType != typeof(PacketReader)) { Logs.Error("{0}.{1} 的第二个参数必须是 PacketReader 对象", classType.Name, methodinfo.Name); return; } //string mehtodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, module.{1});", att.OpCode, methodinfo.Name); initCode.AppendLine(); //callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", mehtodName); //callCode.AppendLine("{"); //callCode.AppendFormat("module.{0}(netstate, reader);", methodinfo.Name); //callCode.AppendLine("}"); #endregion } if (att.MethodType == NetMethodType.ProtocolStruct) { #region ProtocolStruct if (!param[1].ParameterType.IsClass) { Logs.Error("{0}.{1} 的第二个参数必须是class类型。", classType.Name, methodinfo.Name); return; } if (param[1].ParameterType.GetInterface(typeof(IPacketReader).FullName) == null) { Logs.Error("{0}.{1} 的第二个参数必须实现 IPacketReader 接口", classType.Name, methodinfo.Name); // 自己实现一个对对象的协议读取类 AddRdadProxy(param[1].ParameterType); string methodName = Utils.GetFixBeCallProxyName(methodinfo.Name); initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); callCode.AppendFormat(" var obj = {0}ReadProxy.Read(reader);\r\n", param[1].ParameterType.Name); callCode.AppendFormat("module.{0}(obj);", methodName); callCode.AppendLine("}"); } else { // 如果对象实现了 IPacketReader 接口,则直接使用,否则则自己生成协议代码 string methodName = Utils.GetFixBeCallProxyName(methodinfo.Name); initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); callCode.AppendFormat(" var package = DogSE.Library.Common.StaticObjectPool<{0}>.AcquireContent();", param[1].ParameterType.FullName); callCode.AppendLine("package.Read(reader);"); callCode.AppendFormat("module.{0}(netstate, package);", methodName); callCode.AppendFormat("DogSE.Library.Common.StaticObjectPool<{0}>.ReleaseContent(package);", param[1].ParameterType.FullName); callCode.AppendLine("}"); } #endregion } if (att.MethodType == NetMethodType.SimpleMethod) { #region SimpleMethod string methodName = Utils.GetFixBeCallProxyName(methodinfo.Name); initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof(int)) { callCode.AppendFormat("var p{0} = reader.ReadInt32();\r\n", i); } else if (p.ParameterType == typeof(byte)) { callCode.AppendFormat("var p{0} = reader.ReadByte();\r\n", i); } else if (p.ParameterType == typeof(long)) { callCode.AppendFormat("var p{0} = reader.ReadLong64();\r\n", i); } else if (p.ParameterType == typeof(float)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof(double)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof(bool)) { callCode.AppendFormat("var p{0} = reader.ReadBoolean();\r\n", i); } else if (p.ParameterType == typeof(string)) { callCode.AppendFormat("var p{0} = reader.ReadUTF8String();\r\n", i); } else if (p.ParameterType.IsEnum) { callCode.AppendFormat("var p{0} = ({1})reader.ReadByte();\r\n", i, Utils.GetFixFullTypeName(p.ParameterType.FullName)); } else if (p.ParameterType.IsLayoutSequential) { callCode.AppendFormat("var p{0} = reader.ReadStruct <{1}>();\r\n", i, Utils.GetFixFullTypeName(p.ParameterType.FullName)); } else if (p.ParameterType.IsArray) { // 数组 #region 处理数组的读取 var arrayType = p.ParameterType.GetElementType(); callCode.AppendFormat("var len{0} = reader.ReadInt32();\r\n", i); callCode.AppendFormat("var p{0} = new {1}[len{0}];", i, Utils.GetFixFullTypeName(arrayType.FullName)); // 这里只创建值类型 callCode.AppendFormat("for(int i =0;i< len{0};i++){{\r\n", i); if (arrayType == typeof(int)) { callCode.AppendFormat("p{0}[i] = reader.ReadInt32();\r\n", i); } else if (arrayType == typeof(byte)) { callCode.AppendFormat("p{0}[i] = reader.ReadByte();\r\n", i); } else if (arrayType == typeof(long)) { callCode.AppendFormat("p{0}[i] = reader.ReadLong64();\r\n", i); } else if (arrayType == typeof(float)) { callCode.AppendFormat("p{0}[i] = reader.ReadFloat();\r\n", i); } else if (arrayType == typeof(double)) { callCode.AppendFormat("p{0}[i] = reader.ReadFloat();\r\n", i); } else if (arrayType == typeof(bool)) { callCode.AppendFormat("p{0}[i] = reader.ReadBoolean();\r\n", i); } else if (arrayType == typeof(string)) { callCode.AppendFormat("p{0}[i] = reader.ReadUTF8String();\r\n", i); } else if (arrayType.IsEnum) { callCode.AppendFormat("p{0}[i] = ({1})reader.ReadByte();\r\n", i, Utils.GetFixFullTypeName(arrayType.FullName)); } else if (arrayType.IsLayoutSequential) { callCode.AppendFormat("p{0}[i] = reader.ReadStruct <{1}>();\r\n", i, Utils.GetFixFullTypeName(arrayType.FullName)); } else if (arrayType.IsClass) { AddRdadProxy(arrayType); callCode.AppendFormat("p{1}[i] = {0}ReadProxy.Read(reader);\r\n", arrayType.Name, i); } callCode.AppendLine("}"); #endregion } else if (p.ParameterType.IsClass) { AddRdadProxy(p.ParameterType); callCode.AppendFormat(" var p{1} = {0}ReadProxy.Read(reader);\r\n", p.ParameterType.Name, i); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } if (param[0].ParameterType != typeof(NetState) && att.IsVerifyLogin) { // 作为验证数据 //var componentType = param[0].ParameterType; //callCode.AppendFormat("var {0} = netstate.GetComponent<{1}>({1}.ComponentId);", // componentType.Name.ToLower(), componentType.Name); //callCode.AppendFormat("module.{0}({1}", methodinfo.Name, componentType.Name.ToLower()); callCode.AppendFormat("module.{0}(", methodName); } else { // 不需要验证 callCode.AppendFormat("module.{0}(", methodName); } for (int i = 1; i < param.Length; i++) callCode.AppendFormat("p{0},", i); if (param.Length > 1) callCode.Remove(callCode.Length - 1, 1); callCode.AppendLine(");"); callCode.AppendLine("}"); #endregion } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 1) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } if (att.MethodType == NetMethodType.PacketReader) { Logs.Error("客户端代理类不支持这种模式 {0}", att.MethodType.ToString()); return; } if (att.MethodType == NetMethodType.ProtocolStruct) { #region ProtocolStruct Type parameterType = param[1].ParameterType; if (!parameterType.IsClass) { Logs.Error("{0}.{1} 的第二个参数必须是class类型。", classType.Name, methodinfo.Name); return; } if (parameterType.GetInterface(typeof(IPacketWriter).FullName) == null) { // 自己实现一个对对象的协议写入类 AddWriteProxy(parameterType); string methodName = methodinfo.Name; StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); methonNameCode.AppendFormat("public void {0}({1} obj)", methodName, parameterType.FullName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = PacketWriter.AcquireContent({0});", att.OpCode); streamWriterCode.AppendLine(); streamWriterCode.AppendFormat( @" PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( {0} ); if ( packetProfile != null ) packetProfile.RegConstruct(); ", att.OpCode); streamWriterCode.AppendFormat("{0}WriteProxy.Write(obj, pw);", parameterType.Name); streamWriterCode.AppendLine("NetState.Send(pw);"); streamWriterCode.AppendLine(" if ( packetProfile != null ) packetProfile.Record(pw.Length);"); streamWriterCode.AppendLine("PacketWriter.ReleaseContent(pw);"); streamWriterCode.AppendLine("}"); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); } else { // 如果对象实现了 IPacketWriter 接口,则直接使用,否则则自己生成协议代码 string methodName = methodinfo.Name; StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); methonNameCode.AppendFormat("public void {0}({1} obj)", methodName, parameterType.FullName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = PacketWriter.AcquireContent({0});", att.OpCode); streamWriterCode.AppendLine(); streamWriterCode.AppendFormat( @" PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( {0} ); if ( packetProfile != null ) packetProfile.RegConstruct(); ", att.OpCode); streamWriterCode.AppendLine("obj.Write(pw);"); streamWriterCode.AppendLine("NetState.Send(pw);"); streamWriterCode.AppendLine(" if ( packetProfile != null ) packetProfile.Record(pw.Length);"); streamWriterCode.AppendLine("PacketWriter.ReleaseContent(pw);"); streamWriterCode.AppendLine("}"); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); } #endregion } if (att.MethodType == NetMethodType.SimpleMethod) { #region SimpleMethod string methodName = Utils.GetFixCallProxyName(methodinfo.Name); StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); StringBuilder commentCode = new StringBuilder(); Console.WriteLine(classType.FullName + "." + methodinfo.Name); var doc = funDoc.FirstOrDefault(o => o.Name.IndexOf("M:" + classType.FullName + "." + methodinfo.Name) == 0); if (doc == null) doc = new FunItem(); commentCode.AppendFormat(@" /// <summary> /// {0} /// </summary> ", doc.Summary); methonNameCode.AppendFormat("public void {0}(", methodName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = PacketWriter.AcquireContent({0});", att.OpCode); streamWriterCode.AppendLine(); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof(int)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("int {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(byte)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("byte {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(long)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("long {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(float)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("float {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(double)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("double {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(bool)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("bool {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(string)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("string {0},", p.Name); streamWriterCode.AppendFormat("pw.WriteUTF8Null({0});\r\n", p.Name); } else if (p.ParameterType == typeof(DateTime)) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("DateTime {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0}.Ticks);\r\n", p.Name); } else if (p.ParameterType.IsEnum) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); streamWriterCode.AppendFormat("pw.Write((byte){0});\r\n", p.Name); } else if (p.ParameterType.IsLayoutSequential) { commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); streamWriterCode.AppendFormat("pw.WriteStruct({0});\r\n", p.Name); } else if (p.ParameterType.IsArray) { #region 数组的处理 var arrayType = p.ParameterType.GetElementType(); commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); // 先写入长度 streamWriterCode.AppendFormat("pw.Write((int){0}.Length);\r\n", p.Name); streamWriterCode.AppendFormat("for(int i = 0;i < {0}.Length;i++){{\r\n", p.Name); if (arrayType == typeof(int)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(byte)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(long)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(float)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(double)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(bool)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(string)) { streamWriterCode.AppendFormat("pw.WriteUTF8Null({0}[i]);\r\n", p.Name); } else if (p.ParameterType == typeof(DateTime)) { streamWriterCode.AppendFormat("pw.Write({0}.Ticks);\r\n", p.Name); } else if (arrayType.IsEnum) { streamWriterCode.AppendFormat("pw.Write((byte){0}[i]);\r\n", p.Name); } else if (arrayType.IsLayoutSequential) { streamWriterCode.AppendFormat("pw.WriteStruct({0}[i]);\r\n", p.Name); } streamWriterCode.AppendLine("}"); #endregion } else if (p.ParameterType.IsClass) { AddWriteProxy(p.ParameterType, doc.GetParamSummary(p.Name)); commentCode.AppendFormat("/// <param name=`{0}`>{1}</param>\r\n", p.Name, doc.GetParamSummary(p.Name)); methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); streamWriterCode.AppendFormat("{0}WriteProxy.Write({1}, pw);\r\n", p.ParameterType.Name, p.Name); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } streamWriterCode.AppendLine("NetState.Send(pw);PacketWriter.ReleaseContent(pw);"); streamWriterCode.AppendLine("}"); if (param.Length > 1) methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(commentCode.ToString()); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); #endregion } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 1) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } if (att.MethodType == NetMethodType.PacketReader) { Logs.Error("客户端代理类不支持这种模式 {0}", att.MethodType.ToString()); return; } if (att.MethodType == NetMethodType.ProtocolStruct) { // TODO: 稍后补充这种模式 Logs.Error("客户端代理类暂时不支持这种模式 {0}", att.MethodType.ToString()); return; } if (att.MethodType == NetMethodType.SimpleMethod) { string methodName = methodinfo.Name; StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); methonNameCode.AppendFormat("public void {0}(NetState netstate,", methodName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = new PacketWriter({0});", att.OpCode); streamWriterCode.AppendLine(); streamWriterCode.AppendFormat( @" PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( {0} ); if ( packetProfile != null ) packetProfile.RegConstruct(); ", att.OpCode); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof (int)) { methonNameCode.AppendFormat("int {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (long)) { methonNameCode.AppendFormat("long {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (float)) { methonNameCode.AppendFormat("float {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (double)) { methonNameCode.AppendFormat("double {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (bool)) { methonNameCode.AppendFormat("bool {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (string)) { methonNameCode.AppendFormat("string {0},", p.Name); streamWriterCode.AppendFormat("pw.WriteUTF8Null({0});\r\n", p.Name); } else if (p.ParameterType.IsEnum) { methonNameCode.AppendFormat("{0} {1},", p.ParameterType.FullName, p.Name); streamWriterCode.AppendFormat("pw.Write((byte){0});\r\n", p.Name); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } streamWriterCode.AppendLine("netstate.Send(pw);pw.Dispose();"); streamWriterCode.AppendLine("}"); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> private void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 1) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } if (att.MethodType == NetMethodType.PacketReader) { Logs.Error("客户端代理类不支持这种模式 {0}", att.MethodType.ToString()); return; } if (att.MethodType == NetMethodType.ProtocolStruct) { #region ProtocolStruct Type parameterType = param[1].ParameterType; if (!parameterType.IsClass) { Logs.Error("{0}.{1} 的第二个参数必须是class类型。", classType.Name, methodinfo.Name); return; } string methodName = Utils.GetFixBeCallProxyName(methodinfo.Name); StringBuilder methonNameCode = new StringBuilder(); methonNameCode.AppendFormat("internal abstract void {0}(", methodName); methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(parameterType.FullName), parameterType.Name); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(");"); callCode.AppendLine(methonNameCode.ToString()); #endregion } if (att.MethodType == NetMethodType.SimpleMethod) { #region SimpleMethod string methodName = Utils.GetFixBeCallProxyName(methodinfo.Name); StringBuilder methonNameCode = new StringBuilder(); methonNameCode.AppendFormat("internal abstract void {0}(", methodName); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof(int)) { methonNameCode.AppendFormat("int {0},", p.Name); } else if (p.ParameterType == typeof(byte)) { methonNameCode.AppendFormat("byte {0},", p.Name); } else if (p.ParameterType == typeof(long)) { methonNameCode.AppendFormat("long {0},", p.Name); } else if (p.ParameterType == typeof(float)) { methonNameCode.AppendFormat("float {0},", p.Name); } else if (p.ParameterType == typeof(double)) { methonNameCode.AppendFormat("double {0},", p.Name); } else if (p.ParameterType == typeof(bool)) { methonNameCode.AppendFormat("bool {0},", p.Name); } else if (p.ParameterType == typeof(string)) { methonNameCode.AppendFormat("string {0},", p.Name); } else if (p.ParameterType.IsEnum) { methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); } else if (p.ParameterType.IsLayoutSequential) { methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); } else if (p.ParameterType.IsArray) { methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); } else if (p.ParameterType.IsClass) { methonNameCode.AppendFormat("{0} {1},", Utils.GetFixFullTypeName(p.ParameterType.FullName), p.Name); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } if (param.Length > 1) methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(");"); callCode.AppendLine(methonNameCode.ToString()); #endregion } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 2) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } if (att.MethodType == NetMethodType.PacketReader) { if (param[1].ParameterType != typeof(PacketReader)) { Logs.Error("{0}.{1} 的第二个参数必须是 PacketReader 对象", classType.Name, methodinfo.Name); return; } //string mehtodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, module.{1});", att.OpCode, methodinfo.Name); initCode.AppendLine(); //callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", mehtodName); //callCode.AppendLine("{"); //callCode.AppendFormat("module.{0}(netstate, reader);", methodinfo.Name); //callCode.AppendLine("}"); } if (att.MethodType == NetMethodType.ProtocolStruct) { if (param[1].ParameterType.GetInterface(typeof(IPacketReader).FullName) == null) { Logs.Error("{0}.{1} 的第二个参数必须实现 IPacketReader 接口", classType.Name, methodinfo.Name); return; } if (!param[1].ParameterType.IsClass) { Logs.Error("{0}.{1} 的第二个参数必须是class类型。", classType.Name, methodinfo.Name); return; } string methodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); if (att.IsVerifyLogin) callCode.AppendLine("if (!netstate.IsVerifyLogin) return;"); callCode.AppendFormat(" var package = DogSE.Library.Common.StaticObjectPool<{0}>.AcquireContent();", param[1].ParameterType.FullName); callCode.AppendLine("package.Read(reader);"); callCode.AppendFormat("module.{0}(netstate, package);", methodinfo.Name); callCode.AppendFormat("DogSE.Library.Common.StaticObjectPool<{0}>.ReleaseContent(package);", param[1].ParameterType.FullName); callCode.AppendLine("}"); } if (att.MethodType == NetMethodType.SimpleMethod) { string methodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); if (att.IsVerifyLogin) callCode.AppendLine("if (!netstate.IsVerifyLogin) return;"); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof (int)) { callCode.AppendFormat("var p{0} = reader.ReadInt32();\r\n", i); } else if (p.ParameterType == typeof (long)) { callCode.AppendFormat("var p{0} = reader.ReadLong64();\r\n", i); } else if (p.ParameterType == typeof (float)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof (double)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof (bool)) { callCode.AppendFormat("var p{0} = reader.ReadBoolean();\r\n", i); } else if (p.ParameterType == typeof (string)) { callCode.AppendFormat("var p{0} = reader.ReadUTF8String();\r\n", i); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } callCode.AppendFormat("module.{0}(netstate", methodinfo.Name); for (int i = 1; i < param.Length; i++) callCode.AppendFormat(",p{0}", i); callCode.AppendLine(");"); callCode.AppendLine("}"); } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 1) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } if (att.MethodType == NetMethodType.PacketReader) { Logs.Error("客户端代理类不支持这种模式 {0}", att.MethodType.ToString()); return; } if (att.MethodType == NetMethodType.ProtocolStruct) { #region ProtocolStruct Type parameterType = param[1].ParameterType; if (!parameterType.IsClass) { Logs.Error("{0}.{1} 的第二个参数必须是class类型。", classType.Name, methodinfo.Name); return; } if (parameterType.GetInterface(typeof(IPacketWriter).FullName) == null) { // 自己实现一个对对象的协议写入类 AddWriteProxy(parameterType); string methodName = methodinfo.Name; StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); methonNameCode.AppendFormat("public void {0}(NetState netstate, {1} obj)", methodName, parameterType.FullName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = PacketWriter.AcquireContent({0});", att.OpCode); streamWriterCode.AppendLine(); streamWriterCode.AppendFormat( @" PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( {0} ); if ( packetProfile != null ) packetProfile.RegConstruct(); ", att.OpCode); streamWriterCode.AppendFormat("{0}WriteProxy.Write(obj, pw);", parameterType.Name); streamWriterCode.AppendLine("netState.Send(pw);"); streamWriterCode.AppendLine(" if ( packetProfile != null ) packetProfile.Record(pw.Length);"); streamWriterCode.AppendLine("PacketWriter.ReleaseContent(pw);"); streamWriterCode.AppendLine("}"); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); } else { // 如果对象实现了 IPacketWriter 接口,则直接使用,否则则自己生成协议代码 string methodName = methodinfo.Name; StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); methonNameCode.AppendFormat("public void {0}(NetState netstate, {1} obj)", methodName, parameterType.FullName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = PacketWriter.AcquireContent({0});", att.OpCode); streamWriterCode.AppendLine(); streamWriterCode.AppendFormat( @" PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( {0} ); if ( packetProfile != null ) packetProfile.RegConstruct(); ", att.OpCode); streamWriterCode.AppendLine("obj.Write(pw);"); streamWriterCode.AppendLine("netstate.Send(pw);"); streamWriterCode.AppendLine(" if ( packetProfile != null ) packetProfile.Record(pw.Length);"); streamWriterCode.AppendLine("PacketWriter.ReleaseContent(pw);"); streamWriterCode.AppendLine("}"); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); } #endregion } if (att.MethodType == NetMethodType.SimpleMethod) { #region SimpleMethod string methodName = methodinfo.Name; StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); methonNameCode.AppendFormat("public void {0}(NetState netstate,", methodName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = PacketWriter.AcquireContent({0});", att.OpCode); streamWriterCode.AppendLine(); streamWriterCode.AppendFormat( @" PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( {0} ); if ( packetProfile != null ) packetProfile.RegConstruct(); ", att.OpCode); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof (int)) { methonNameCode.AppendFormat("int {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (long)) { methonNameCode.AppendFormat("long {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (float)) { methonNameCode.AppendFormat("float {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (double)) { methonNameCode.AppendFormat("double {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (bool)) { methonNameCode.AppendFormat("bool {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof (string)) { methonNameCode.AppendFormat("string {0},", p.Name); streamWriterCode.AppendFormat("pw.WriteUTF8Null({0});\r\n", p.Name); } else if (p.ParameterType == typeof(DateTime)) { methonNameCode.AppendFormat("DateTime {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0}.Ticks);\r\n", p.Name); } else if (p.ParameterType.IsEnum) { methonNameCode.AppendFormat("{0} {1},", p.ParameterType.FullName, p.Name); streamWriterCode.AppendFormat("pw.Write((byte){0});\r\n", p.Name); } else if (p.ParameterType.IsLayoutSequential) { methonNameCode.AppendFormat("{0} {1},", p.ParameterType.FullName, p.Name); AddWriteProxyByStruct(p.ParameterType); //streamWriterCode.AppendFormat("pw.WriteStruct({0});\r\n", p.Name); streamWriterCode.AppendFormat("{0}WriteProxy.Write({1}, pw);\r\n", p.ParameterType.Name, p.Name); } else if (p.ParameterType.IsArray) { #region 数组的处理 var arrayType = p.ParameterType.GetElementType(); methonNameCode.AppendFormat("{0} {1},", p.ParameterType.FullName, p.Name); streamWriterCode.AppendFormat("int {0}len = {0} == null ? 0:{0}.Length;", p.Name); // 先写入长度 streamWriterCode.AppendFormat("pw.Write({0}len);\r\n", p.Name); streamWriterCode.AppendFormat("for(int i = 0;i < {0}len ;i++){{\r\n", p.Name); if (arrayType == typeof(int)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(byte)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(long)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(float)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(double)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(bool)) { streamWriterCode.AppendFormat("pw.Write({0}[i]);\r\n", p.Name); } else if (arrayType == typeof(string)) { streamWriterCode.AppendFormat("pw.WriteUTF8Null({0}[i]);\r\n", p.Name); } else if (arrayType.IsEnum) { streamWriterCode.AppendFormat("pw.Write((byte){0}[i]);\r\n", p.Name); } else if (arrayType.IsLayoutSequential) { //streamWriterCode.AppendFormat("pw.WriteStruct({0}[i]);\r\n", p.Name); AddWriteProxyByStruct(arrayType); streamWriterCode.AppendFormat("{0}WriteProxy.Write({1}[i], pw);\r\n", arrayType.Name, p.Name); } else if (arrayType.IsClass) { AddWriteProxy(arrayType); streamWriterCode.AppendFormat("{0}WriteProxy.Write({1}[i], pw);\r\n", arrayType.Name, p.Name); } streamWriterCode.AppendLine("}"); #endregion } else if (p.ParameterType.IsClass) { AddWriteProxy(p.ParameterType); methonNameCode.AppendFormat("{0} {1},", p.ParameterType.FullName, p.Name); streamWriterCode.AppendFormat("{0}WriteProxy.Write({1}, pw);\r\n", p.ParameterType.Name, p.Name); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } streamWriterCode.AppendLine("netstate.Send(pw);"); streamWriterCode.AppendLine(" if ( packetProfile != null ) packetProfile.Record(pw.Length);"); streamWriterCode.AppendLine("PacketWriter.ReleaseContent(pw);"); streamWriterCode.AppendLine("}"); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); #endregion } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 2) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } if (att.MethodType == NetMethodType.PacketReader) { if (param[1].ParameterType != typeof(PacketReader)) { Logs.Error("{0}.{1} 的第二个参数必须是 PacketReader 对象", classType.Name, methodinfo.Name); return; } //string mehtodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, module.{1});", att.OpCode, methodinfo.Name); initCode.AppendLine(); //callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", mehtodName); //callCode.AppendLine("{"); //callCode.AppendFormat("module.{0}(netstate, reader);", methodinfo.Name); //callCode.AppendLine("}"); } if (att.MethodType == NetMethodType.ProtocolStruct) { if (param[1].ParameterType.GetInterface(typeof(IPacketReader).FullName) == null) { Logs.Error("{0}.{1} 的第二个参数必须实现 IPacketReader 接口", classType.Name, methodinfo.Name); return; } if (!param[1].ParameterType.IsClass) { Logs.Error("{0}.{1} 的第二个参数必须是class类型。", classType.Name, methodinfo.Name); return; } string methodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); if (att.IsVerifyLogin) { callCode.AppendLine("if (!netstate.IsVerifyLogin) return;"); } callCode.AppendFormat(" var package = DogSE.Library.Common.StaticObjectPool<{0}>.AcquireContent();", param[1].ParameterType.FullName); callCode.AppendLine("package.Read(reader);"); callCode.AppendFormat("module.{0}(netstate, package);", methodinfo.Name); callCode.AppendFormat("DogSE.Library.Common.StaticObjectPool<{0}>.ReleaseContent(package);", param[1].ParameterType.FullName); callCode.AppendLine("}"); } if (att.MethodType == NetMethodType.SimpleMethod) { string methodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); if (att.IsVerifyLogin) { callCode.AppendLine("if (!netstate.IsVerifyLogin) return;"); } for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof(int)) { callCode.AppendFormat("var p{0} = reader.ReadInt32();\r\n", i); } else if (p.ParameterType == typeof(long)) { callCode.AppendFormat("var p{0} = reader.ReadLong64();\r\n", i); } else if (p.ParameterType == typeof(float)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof(double)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof(bool)) { callCode.AppendFormat("var p{0} = reader.ReadBoolean();\r\n", i); } else if (p.ParameterType == typeof(string)) { callCode.AppendFormat("var p{0} = reader.ReadUTF8String();\r\n", i); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } callCode.AppendFormat("module.{0}(netstate", methodinfo.Name); for (int i = 1; i < param.Length; i++) { callCode.AppendFormat(",p{0}", i); } callCode.AppendLine(");"); callCode.AppendLine("}"); } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 2) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { if (!att.IsVerifyLogin) { // 如果不需要验证登录数据,则第一个对象必须是NetState对象 Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } var componentType = param[0].ParameterType; var field = componentType.GetField("ComponentId"); if (field == null || !field.IsStatic) { Logs.Error("{0}.{1} 必须包含一个 ComponentId 的常量字符串作为登录验证后和NetState绑定的组件。"); return; } } if (att.MethodType == NetMethodType.PacketReader) { if (param[1].ParameterType != typeof(PacketReader)) { Logs.Error("{0}.{1} 的第二个参数必须是 PacketReader 对象", classType.Name, methodinfo.Name); return; } //string mehtodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, module.{1});", att.OpCode, methodinfo.Name); initCode.AppendLine(); //callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", mehtodName); //callCode.AppendLine("{"); //callCode.AppendFormat("module.{0}(netstate, reader);", methodinfo.Name); //callCode.AppendLine("}"); } if (att.MethodType == NetMethodType.ProtocolStruct) { if (param[1].ParameterType.GetInterface(typeof(IPacketReader).FullName) == null) { Logs.Error("{0}.{1} 的第二个参数必须实现 IPacketReader 接口", classType.Name, methodinfo.Name); return; } if (!param[1].ParameterType.IsClass) { Logs.Error("{0}.{1} 的第二个参数必须是class类型。", classType.Name, methodinfo.Name); return; } string methodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); callCode.AppendFormat(" var package = DogSE.Library.Common.StaticObjectPool<{0}>.AcquireContent();", param[1].ParameterType.FullName); callCode.AppendLine("package.Read(reader);"); callCode.AppendFormat("module.{0}(netstate, package);", methodinfo.Name); callCode.AppendFormat("DogSE.Library.Common.StaticObjectPool<{0}>.ReleaseContent(package);", param[1].ParameterType.FullName); callCode.AppendLine("}"); } if (att.MethodType == NetMethodType.SimpleMethod) { string methodName = methodinfo.Name; initCode.AppendFormat("PacketHandlerManager.Register({0}, {1});", att.OpCode, methodName); initCode.AppendLine(); callCode.AppendFormat("void {0}(NetState netstate, PacketReader reader)", methodName); callCode.AppendLine("{"); if (att.IsVerifyLogin) callCode.AppendLine("if (!netstate.IsVerifyLogin) return;"); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof (int)) { callCode.AppendFormat("var p{0} = reader.ReadInt32();\r\n", i); } else if (p.ParameterType == typeof (long)) { callCode.AppendFormat("var p{0} = reader.ReadLong64();\r\n", i); } else if (p.ParameterType == typeof (float)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof (double)) { callCode.AppendFormat("var p{0} = reader.ReadFloat();\r\n", i); } else if (p.ParameterType == typeof (bool)) { callCode.AppendFormat("var p{0} = reader.ReadBoolean();\r\n", i); } else if (p.ParameterType == typeof (string)) { callCode.AppendFormat("var p{0} = reader.ReadUTF8String();\r\n", i); } else if (p.ParameterType.IsEnum) { callCode.AppendFormat("var p{0} = ({1})reader.ReadByte();\r\n", i, p.ParameterType.FullName); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } if (param[0].ParameterType != typeof(NetState) && att.IsVerifyLogin) { // 作为验证数据 //var componentType = param[0].ParameterType; //callCode.AppendFormat("var {0} = netstate.GetComponent<{1}>({1}.ComponentId);", // componentType.Name.ToLower(), componentType.Name); //callCode.AppendFormat("module.{0}({1}", methodinfo.Name, componentType.Name.ToLower()); callCode.AppendFormat("module.{0}(netstate", methodinfo.Name); } else { // 不需要验证 callCode.AppendFormat("module.{0}(netstate", methodinfo.Name); } for (int i = 1; i < param.Length; i++) callCode.AppendFormat(",p{0}", i); callCode.AppendLine(");"); callCode.AppendLine("}"); } }
/// <summary> /// 添加一个方法 /// </summary> /// <param name="att"></param> /// <param name="methodinfo"></param> void AddMethod(NetMethodAttribute att, MethodInfo methodinfo) { var param = methodinfo.GetParameters(); if (param.Length < 1) { Logs.Error(string.Format("{0}.{1} 不支持 {2} 个参数", classType.Name, methodinfo.Name, param.Length.ToString())); return; } if (param[0].ParameterType != typeof(NetState)) { Logs.Error("{0}.{1} 的第一个参数必须是 NetState 对象", classType.Name, methodinfo.Name); return; } if (att.MethodType == NetMethodType.PacketReader) { Logs.Error("客户端代理类不支持这种模式 {0}", att.MethodType.ToString()); return; } if (att.MethodType == NetMethodType.ProtocolStruct) { // TODO: 稍后补充这种模式 Logs.Error("客户端代理类暂时不支持这种模式 {0}", att.MethodType.ToString()); return; } if (att.MethodType == NetMethodType.SimpleMethod) { string methodName = methodinfo.Name; StringBuilder methonNameCode = new StringBuilder(); StringBuilder streamWriterCode = new StringBuilder(); methonNameCode.AppendFormat("public void {0}(NetState netstate,", methodName); streamWriterCode.AppendLine("{"); streamWriterCode.AppendFormat("var pw = new PacketWriter({0});", att.OpCode); streamWriterCode.AppendLine(); streamWriterCode.AppendFormat( @" PacketProfile packetProfile = PacketProfile.GetOutgoingProfile( {0} ); if ( packetProfile != null ) packetProfile.RegConstruct(); ", att.OpCode); for (int i = 1; i < param.Length; i++) { var p = param[i]; if (p.ParameterType == typeof(int)) { methonNameCode.AppendFormat("int {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(long)) { methonNameCode.AppendFormat("long {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(float)) { methonNameCode.AppendFormat("float {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(double)) { methonNameCode.AppendFormat("double {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(bool)) { methonNameCode.AppendFormat("bool {0},", p.Name); streamWriterCode.AppendFormat("pw.Write({0});\r\n", p.Name); } else if (p.ParameterType == typeof(string)) { methonNameCode.AppendFormat("string {0},", p.Name); streamWriterCode.AppendFormat("pw.WriteUTF8Null({0});\r\n", p.Name); } else if (p.ParameterType.IsEnum) { methonNameCode.AppendFormat("{0} {1},", p.ParameterType.FullName, p.Name); streamWriterCode.AppendFormat("pw.Write((byte){0});\r\n", p.Name); } else { Logs.Error(string.Format("{0}.{1} 存在不支持的参数 {2},类型未:{3}", classType.Name, methodinfo.Name, p.Name, p.ParameterType.Name)); } } streamWriterCode.AppendLine("netstate.Send(pw);pw.Dispose();"); streamWriterCode.AppendLine("}"); methonNameCode.Remove(methonNameCode.Length - 1, 1); methonNameCode.Append(")"); callCode.AppendLine(methonNameCode.ToString()); callCode.AppendLine(streamWriterCode.ToString()); } }