protected override void OnPlayerLogin(LiteNetLibManager.Player player) { foreach (var mData in invokeMothodsInfos) { List <MethodInfo> methods = mData.Value; foreach (MethodInfo m in methods) { RemoteInvokingAttribute attribute = GetCustomAttribute <RemoteInvokingAttribute>(m); MethodData2Client msg = new MethodData2Client(); msg.data.methodType = attribute.methodType; msg.data.showName = attribute.name; msg.data.description = attribute.description; msg.data.classFullName = mData.Key.FullName; msg.data.methodName = m.Name; ParameterInfo[] parameters = m.GetParameters(); IEnumerable <ParamsDescriptionAttribute> paramsDescriptions = GetCustomAttributes <ParamsDescriptionAttribute>(m); for (int i = 0; i < parameters.Length; i++) { ParameterInfo p = parameters[i]; ParamsData paramsData = new ParamsData(); ParamsDescriptionAttribute paramsDescription = ParamsDescriptionAttribute.GetParamsDescription(paramsDescriptions, p.Name);; if (paramsDescription != null) { paramsData.descriptionName = paramsDescription.paramsDescriptionName; } paramsData.paraName = p.Name; paramsData.paraTypeFullName = p.ParameterType.FullName; try { paramsData.defaultValueStr = GetDefaultValueString(mData.Key, p.ParameterType, paramsDescription); } catch (Exception e) { Debug.LogError(mData.Key.FullName + "." + m.Name + "\n" + e); } paramsData.selectItemValues = GetTypeSelectItemValues(p.ParameterType, paramsDescription); msg.data.paramsDatas.Add(paramsData); } Debug.Log("Send MethodData2Client:" + JsonUtility.ToJson(msg)); netManager.Send(player, msg); } } }
private string[] GetSimpleTypeSelectItem(Type type, ParamsDescriptionAttribute paramsDescription) { if (type == typeof(bool)) { return(new string[] { "true", "false" }); } else if (type.IsEnum) { return(Enum.GetNames(type)); } else if (type == typeof(string)) { if (paramsDescription != null && paramsDescription.selectItemValues != null) { return(paramsDescription.selectItemValues); } } return(new string[0]); }
private string[] GetTypeSelectItemValues(Type type, ParamsDescriptionAttribute paramsDescription) { if (type.IsArray || type.Name == typeof(List <>).Name) { Type itemType = null; if (type.IsArray) { itemType = type.GetElementType(); } else { itemType = type.GetGenericArguments()[0]; } return(GetSimpleTypeSelectItem(itemType, paramsDescription)); } else { return(GetSimpleTypeSelectItem(type, paramsDescription)); } }
private string GetDefaultValueString(Type classType, Type parameterType, ParamsDescriptionAttribute paramsDescription) { object value = null; if (paramsDescription != null && !string.IsNullOrEmpty(paramsDescription.getDefaultValueMethodName)) { MethodInfo info = classType.GetMethod(paramsDescription.getDefaultValueMethodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); if (info != null) { Type returnType = info.ReturnType; if (returnType != parameterType) { Debug.LogError("GetDefaultValueString Method:" + classType.FullName + "." + paramsDescription.getDefaultValueMethodName + " ReturnType(" + returnType.FullName + ") is different from parameterType(" + parameterType + ")"); } else { try { value = info.Invoke(null, new object[0]); } catch (Exception e) { Debug.LogError(e); } } } } if (value == null) { Debug.Log("CreateDefultInstance Type:" + parameterType); value = ReflectionTool.CreateDefultInstance(parameterType); } return(SimpleJsonUtils.ToJson(value)); }