public static string MakeMethodExecuterCode(MethodDesc desc, bool bIsClient) { System.Reflection.MethodInfo mi = desc.mi; System.Type ctype = desc.hostType; int LimitLevel = 0; if (!bIsClient) { object[] propAttrs = mi.GetCustomAttributes(typeof(RPC.RPCMethodAttribute), false); if (propAttrs != null && propAttrs.Length == 1) { RPC.RPCMethodAttribute propAtt = propAttrs[0] as RPC.RPCMethodAttribute; if (propAtt != null) { LimitLevel = propAtt.LimitLevel; } } } string strOut = ""; strOut += "[RPC.RPCMethordExecuterTypeAttribute(" + RPC.RPCEntrance.GetMethodHashCode(mi, ctype.FullName) + ",\"" + mi.Name + "\",typeof(HExe_" + RPC.RPCEntrance.GetMethodHashCode(mi, ctype.FullName) + "))]" + EndLine; strOut += "public class HExe_" + RPC.RPCEntrance.GetMethodHashCode(mi, ctype.FullName) + ": RPC.RPCMethodExecuter" + EndLine; strOut += "{" + EndLine; strOut += TabStr + "public override int LimitLevel" + EndLine; strOut += TabStr + "{" + EndLine; strOut += TabStr + TabStr + "get { return " + LimitLevel + "; }" + EndLine; strOut += TabStr + "}" + EndLine; if (bIsClient) { strOut += TabStr + "public override object Execute(RPC.RPCObject obj,RPC.PackageProxy pkg)" + EndLine; } else { strOut += TabStr + "public override object Execute(Iocp.NetConnection connect,RPC.RPCObject obj,RPC.PackageProxy pkg,RPC.RPCForwardInfo fwd)" + EndLine; } strOut += TabStr + "{" + EndLine; strOut += TabStr + TabStr + ctype.FullName + " host = obj as " + ctype.FullName + ";" + EndLine; strOut += TabStr + TabStr + "if (host == null) return null;" + EndLine; string strCall = ""; System.Reflection.ParameterInfo[] parameters = mi.GetParameters(); for (int i = 0; i < parameters.Length; i++) { string finalName = parameters[i].Name; bool isNew = false; Type baseType = parameters[i].ParameterType; while (!isNew) { baseType = baseType.BaseType; if (baseType == null) { break; } if (baseType.Name == "IAutoSaveAndLoad") { isNew = true; break; } } if (parameters[i].ParameterType.FullName == "Iocp.NetConnection") { finalName = "connect"; } else if (parameters[i].ParameterType.FullName == "RPC.RPCForwardInfo") { finalName = "fwd"; } else if (isNew) { strOut += TabStr + TabStr + parameters[i].ParameterType.FullName + " " + parameters[i].Name + " = new " + parameters[i].ParameterType.FullName + "();" + EndLine; strOut += TabStr + TabStr + "pkg.Read( " + parameters[i].Name + ");" + EndLine; } else { strOut += TabStr + TabStr + parameters[i].ParameterType.FullName + " " + parameters[i].Name + ";" + EndLine; strOut += TabStr + TabStr + "pkg.Read(out " + parameters[i].Name + ");" + EndLine; } if (i == parameters.Length - 1) { strCall += finalName; } else { strCall += finalName + ","; } } if (mi.ReturnType == typeof(void)) { strOut += TabStr + TabStr + "host." + mi.Name + "(" + strCall + ");" + EndLine + "return null;" + EndLine; } else { strOut += TabStr + TabStr + "return host." + mi.Name + "(" + strCall + ");" + EndLine; } strOut += TabStr + "}" + EndLine; strOut += "}" + EndLine; return(strOut); }
private void LoadMoudleForServ(string moduleName) { System.Reflection.Assembly assembly; assembly = System.Reflection.Assembly.LoadFrom(moduleName); Type[] types = assembly.GetTypes(); foreach (Type t in types) { object[] attrs = t.GetCustomAttributes(typeof(RPC.RPCClassAttribute), false); if (attrs != null && attrs.Length == 1) { RPC.RPCClassAttribute att = attrs[0] as RPC.RPCClassAttribute; if (att == null) { continue; } TreeNode cNode = this.treeView1.Nodes.Add(t.FullName); cNode.Tag = att; cNode.Name = t.FullName; System.Reflection.PropertyInfo[] props = t.GetProperties(); foreach (System.Reflection.PropertyInfo p in props) { if (p.Name == "Item") { object[] propAttrs = p.GetCustomAttributes(typeof(RPC.RPCIndexObjectAttribute), false); if (propAttrs != null && propAttrs.Length == 1) { RPC.RPCIndexObjectAttribute propAtt = propAttrs[0] as RPC.RPCIndexObjectAttribute; if (propAtt == null) { continue; } TreeNode coNode = cNode.Nodes.Add("(I:" + propAtt.ChildIndex.ToString() + ")" + p.Name); coNode.Tag = p; coNode.Name = propAtt.ChildIndex.ToString(); } } else { object[] propAttrs = p.GetCustomAttributes(typeof(RPC.RPCChildObjectAttribute), false); if (propAttrs != null && propAttrs.Length == 1) { RPC.RPCChildObjectAttribute propAtt = propAttrs[0] as RPC.RPCChildObjectAttribute; if (propAtt == null) { continue; } TreeNode coNode = cNode.Nodes.Add("(P:" + propAtt.ChildIndex.ToString() + ")" + p.Name); coNode.Tag = p; coNode.Name = propAtt.ChildIndex.ToString(); } } } { System.Reflection.MethodInfo[] methords = t.GetMethods(); Byte indexOfMethod = 0; foreach (System.Reflection.MethodInfo m in methords) { object[] mtdAttrs = m.GetCustomAttributes(typeof(RPC.RPCMethodAttribute), false); if (mtdAttrs != null && mtdAttrs.Length == 1) { RPC.RPCMethodAttribute propAtt = mtdAttrs[0] as RPC.RPCMethodAttribute; MethodDesc desc = new MethodDesc(); desc.mi = m; desc.rpcAttr = propAtt; desc.hostType = t; desc.MethodIndex = indexOfMethod; indexOfMethod++; desc.HashcodeOfMethod = RPC.RPCEntrance.GetMethodHashCode(m, t.FullName); TreeNode cmNode = cNode.Nodes.Add(m.Name); cmNode.Tag = desc; } } } } } }