Example #1
0
        static void FindAllPresenterAction()
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                var types = assembly.GetTypes();
                foreach (var type in types)
                {
                    if (type.GetCustomAttributes(typeof(PresenterAttribute), true).Length == 0)
                    {
                        continue;
                    }

                    var presenterName = type.Name.EndsWith("Presenter") ?
                                        type.Name.Substring(0, type.Name.LastIndexOf("Presenter")) : type.Name;
                    var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                    foreach (var method in methods)
                    {
                        var actionAttrs = method.GetCustomAttributes(typeof(PresenterActionAttribute), true);
                        if (actionAttrs.Length == 0)
                        {
                            continue;
                        }

                        var url = presenterName + "/" + method.Name;
                        if (_actionMapping.ContainsKey(url))
                        {
                            var existed = _actionMapping[url];
                            Debug.LogErrorFormat("Url duplicate {0}.\n{1}, {2}.{3} \n{4}, {5}.{6}", url,
                                                 existed.PresenterType.Assembly.FullName, existed.PresenterType.FullName, existed.Method.Name,
                                                 assembly.FullName, type.FullName, method.Name);
                        }
                        else
                        {
                            bool isAsync    = false;
                            var  parameters = method.GetParameters();
                            if (parameters.Length != 0)
                            {
                                var last = parameters[parameters.Length - 1];
                                if (last.ParameterType == typeof(AsyncReturn) || last.ParameterType.IsSubclassOf(typeof(AsyncReturn)))
                                {
                                    isAsync = true;
                                }
                            }
                            var actionInfo = new PresenterActionInfo()
                            {
                                IsAsync       = isAsync,
                                Url           = url,
                                DisplayUrl    = ((PresenterActionAttribute)(actionAttrs[0])).DisplayName,
                                PresenterType = type,
                                Method        = method
                            };
                            _actionMapping.Add(url, actionInfo);
                        }
                    }
                }
            }
        }
        void DrawParameters(PresenterActionInfo action)
        {
            EditorGUILayout.LabelField("Parameters");
            EditorGUI.indentLevel++;

            var binderProperties = serializedObject.FindProperty("_toSendDataBinders");
            var parameters       = action.Method.GetParameters();

            var parameterLength = parameters.Length;

            if (action.IsAsync)
            {
                parameterLength--;
            }

            if (parameterLength == 0)
            {
                EditorGUILayout.HelpBox("No parameter to send", MessageType.Info);
            }
            else
            {
                for (var i = 0; i < parameterLength; i++)
                {
                    if (binderProperties.arraySize <= i)
                    {
                        binderProperties.InsertArrayElementAtIndex(i);
                    }

                    var binderProperty = binderProperties.GetArrayElementAtIndex(i);
                    var parameter      = parameters[i];

                    var paraType          = parameter.ParameterType;
                    var requireBinderInfo = BinderUtil.GetRequireBinderInfoByValueType(paraType);
                    binderProperty.objectReferenceValue = EditorKit.DrawBinderField(
                        parameter.Name, requireBinderInfo.ValueTypeName, binderProperty.objectReferenceValue, requireBinderInfo.InterfaceType);
                }
            }
            while (binderProperties.arraySize > parameters.Length)
            {
                binderProperties.DeleteArrayElementAtIndex(parameters.Length);
            }

            EditorGUI.indentLevel--;
        }
        void DrawResponse(PresenterActionInfo action)
        {
            EditorGUILayout.LabelField("Responese");
            EditorGUI.indentLevel++;

            List <Type> retTypeList = new List <Type>();

            if (action.IsAsync)
            {
                var parameters = action.Method.GetParameters();
                var lastParam  = parameters[parameters.Length - 1];
                if (lastParam.ParameterType == typeof(AsyncReturn) || lastParam.ParameterType.IsSubclassOf(typeof(AsyncReturn)))
                {
                    var genericTypes = lastParam.ParameterType.GetGenericArguments();
                    foreach (var gt in genericTypes)
                    {
                        retTypeList.Add(gt);
                    }
                }
                else
                {
                    EditorGUILayout.HelpBox("it is an async action but the last parameter is not AysncReturn", MessageType.Error);
                    return;
                }
            }
            else if (action.Method.ReturnType != typeof(void))
            {
                retTypeList.Add(action.Method.ReturnType);
            }

            var binderListProperty = serializedObject.FindProperty("_responseDataBinder");

            while (binderListProperty.arraySize > retTypeList.Count)
            {
                binderListProperty.DeleteArrayElementAtIndex(retTypeList.Count);
            }

            if (retTypeList.Count == 0)
            {
                EditorGUILayout.HelpBox("No response data", MessageType.Info);
            }
            else
            {
                for (var i = 0; i < retTypeList.Count; i++)
                {
                    if (binderListProperty.arraySize <= i)
                    {
                        binderListProperty.InsertArrayElementAtIndex(i);
                    }

                    var binderProperty = binderListProperty.GetArrayElementAtIndex(i);
                    var retType        = retTypeList[i];

                    var requireBinderInfo = BinderUtil.GetRequireBinderInfoByValueType(retType);

                    binderProperty.objectReferenceValue = EditorKit.DrawBinderField(
                        "return " + i, requireBinderInfo.ValueTypeName, binderProperty.objectReferenceValue, requireBinderInfo.InterfaceType);
                }
            }

            EditorGUI.indentLevel--;
        }