Beispiel #1
0
        List <object> ActionSet()
        {
            var fieldName     = SearchPath.Target;
            var valueAsString = SearchPath.Arguments != null && SearchPath.Arguments.Length > 0 ? SearchPath.Arguments[0] : null;

            if (string.IsNullOrEmpty(fieldName))
            {
                UniumComponent.Warn("No field name given for Set() call");
                return(null);
            }

            if (valueAsString == null)
            {
                UniumComponent.Warn("No field value given for Set() call");
            }

            var results = new List <object>();

            object cachedValue = null;
            Type   cachedType  = null;
            bool   cacheValid  = false;

            foreach (var obj in Selected)
            {
                try
                {
                    // set value

                    var objType = obj.GetType();

                    // set field

                    var fieldInfo = objType.GetField(fieldName);

                    if (fieldInfo != null)
                    {
                        // convert to type

                        if (fieldInfo.FieldType != cachedType)
                        {
                            cachedType  = fieldInfo.FieldType;
                            cachedValue = ConvertType.FromString(valueAsString, cachedType);
                            cacheValid  = true;
                        }

                        if (cacheValid)
                        {
                            fieldInfo.SetValue(obj, cachedValue);
                            results.Add(fieldInfo.GetValue(obj));
                        }

                        continue;
                    }

                    // set property

                    var propInfo = objType.GetProperty(fieldName);

                    if (propInfo != null && propInfo.CanWrite)
                    {
                        // convert to type

                        if (propInfo.PropertyType != cachedType)
                        {
                            cachedType  = propInfo.PropertyType;
                            cachedValue = ConvertType.FromString(valueAsString, cachedType);
                            cacheValid  = true;
                        }

                        if (cacheValid)
                        {
                            propInfo.SetValue(obj, cachedValue, null);
                            results.Add(propInfo.GetValue(obj, null));
                        }
                    }
                }
                catch (Exception e)
                {
                    cacheValid = false;

                    if (UniumComponent.IsDebug)
                    {
                        UniumComponent.Warn(string.Format("Failed to set value for field '{0}' - {1}", fieldName, e.Message));
                    }
                }
            }

            return(results);
        }
Beispiel #2
0
        List <object> ActionInvoke()
        {
            var actionName = SearchPath.Target;

            if (string.IsNullOrEmpty(actionName))
            {
                UniumComponent.Warn("No function name given for Invoke() call");
                return(null);
            }

            var strArgs = SearchPath.Arguments;

            object[] cachedValues = new object[strArgs.Length];
            Type[]   cachedTypes  = new Type[strArgs.Length];
            object[] args         = new object[strArgs.Length];


            var results = new List <object>();


            foreach (var current in Selected)
            {
                try
                {
                    // find something to invoke - either a method, event or field/property with an Invoke function (e.g. UnityEvent or Button)

                    var target     = current;
                    var targetType = target.GetType();
                    var method     = null as MethodInfo;
                    var multicast  = null as MulticastDelegate;

                    var member     = targetType.GetMember(actionName);
                    var memberType = member.Length > 0 ? member[0].MemberType : 0;

                    if ((memberType & MemberTypes.Method) == MemberTypes.Method)
                    {
                        method = targetType.GetMethods().Where(m => m.Name == actionName && m.GetParameters().Length == args.Length).FirstOrDefault();
                    }
                    else if ((memberType & MemberTypes.Event) == MemberTypes.Event)
                    {
                        var field = targetType.GetField(actionName, BindingFlags.Instance | BindingFlags.NonPublic);
                        multicast = field == null ? null : field.GetValue(target) as MulticastDelegate;

                        if (multicast != null)
                        {
                            var delegateList = multicast.GetInvocationList();
                            method = delegateList.Length > 0 ? delegateList[0].Method : null;
                        }
                    }
                    else if ((memberType & MemberTypes.Field) == MemberTypes.Field)
                    {
                        var field = targetType.GetField(actionName);
                        target = field == null  ? null : field.GetValue(target);
                        method = target == null ? null : target.GetType().GetMethod("Invoke");
                    }
                    else if ((memberType & MemberTypes.Property) == MemberTypes.Property)
                    {
                        var prop = targetType.GetProperty(actionName);
                        target = prop == null   ? null : prop.GetValue(target, null);
                        method = target == null ? null : target.GetType().GetMethod("Invoke");
                    }

                    // if we didn't find anything invokable then skip this object

                    if (method == null)
                    {
                        continue;
                    }

                    // otherwise, convert arguments passed in to the appropriate types

                    if (strArgs != null)
                    {
                        var argInfo = method.GetParameters();

                        if (argInfo.Length != strArgs.Length)
                        {
                            //Unium.Instance.Log.Warn( "Can't invoke function {0} - parameters do not match", func );
                            continue;
                        }

                        for (int i = 0; i < args.Length; i++)
                        {
                            // convert string to value

                            var argType = argInfo[i].ParameterType;

                            if (argType != cachedTypes[i])
                            {
                                cachedTypes[i]  = argType;
                                cachedValues[i] = ConvertType.FromString(strArgs[i], argType);
                            }

                            args[i] = cachedValues[i];
                        }
                    }

                    // invoke method

                    object result = null;

                    if (multicast != null)
                    {
                        result = multicast.DynamicInvoke(args);
                    }
                    else
                    {
                        result = method.Invoke(target, args);
                    }

                    results.Add(result);
                }
                catch (Exception e)
                {
                    if (UniumComponent.IsDebug)
                    {
                        UniumComponent.Warn(string.Format("Failed to invoke '{0}' - {1}", actionName, e.Message));
                    }
                }
            }

            return(results);
        }
        List <object> ActionInvoke()
        {
            var functionName = SearchPath.Target;

            if (string.IsNullOrEmpty(functionName))
            {
                UniumComponent.Warn("No function name given for Invoke() call");
                return(null);
            }

            var strArgs = SearchPath.Arguments;

            object[] cachedValues = new object[strArgs.Length];
            Type[]   cachedTypes  = new Type[strArgs.Length];
            object[] args         = new object[strArgs.Length];


            var results = new List <object>();


            foreach (var obj in Selected)
            {
                try
                {
                    // get function pointer

                    var type   = obj.GetType();
                    var method = type.GetMethod(functionName);

                    if (method == null)
                    {
                        continue;
                    }

                    // convert arguments

                    if (strArgs != null)
                    {
                        var argInfo = method.GetParameters();

                        if (argInfo.Length != strArgs.Length)
                        {
                            //Unium.Instance.Log.Warn( "Can't invoke function {0} - parameters do not match", func );
                            continue;
                        }

                        for (int i = 0; i < args.Length; i++)
                        {
                            // convert string to value

                            var argType = argInfo[i].ParameterType;

                            if (argType != cachedTypes[i])
                            {
                                cachedTypes[i]  = argType;
                                cachedValues[i] = ConvertType.FromString(strArgs[i], argType);
                            }

                            args[i] = cachedValues[i];
                        }
                    }

                    // invoke method

                    var result = method.Invoke(obj, args);
                    results.Add(result);
                }
                catch (Exception e)
                {
                    if (UniumComponent.IsDebug)
                    {
                        UniumComponent.Warn(string.Format("Failed to invoke '{0}' - {1}", functionName, e.Message));
                    }
                }
            }

            return(results);
        }