Ejemplo n.º 1
0
        private static object CallScriptMethod(MethodInfo scriptMethod, object instance, ScriptExecutionContext context, object[] args)
        {
            //get script attribute
            CCScriptAttribute scriptAttribute = null;
            var scriptAttributes = scriptMethod.GetCustomAttributes(typeof(CCScriptAttribute), false);

            if (scriptAttributes != null && scriptAttributes.Length > 0)
            {
                scriptAttribute = (CCScriptAttribute)scriptAttributes[0];
            }

            //attempt to resolve instance
            if (!scriptMethod.IsStatic && instance == null)
            {
                if (scriptAttribute != null && scriptAttribute.AutoResolveInstance)
                {
                    var property = scriptMethod.DeclaringType.GetProperty("Instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    if (property != null)
                    {
                        instance = property.GetValue(null);
                    }
                    else
                    {
                        var field = scriptMethod.DeclaringType.GetField("Instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                        if (field != null)
                        {
                            instance = field.GetValue(null);
                        }
                    }
                }

                if (instance == null)
                {
                    throw new NoInstanceForNonStaticMethodException();
                }
            }

            List <object> allArgs = new List <object>(args.Length + 1);

            allArgs.Add(context);
            allArgs.AddRange(args);

            object[] trimmedArgs = allArgs.GetRange(0, scriptMethod.GetParameters().Length).ToArray();

            return(scriptMethod.Invoke(instance, trimmedArgs));
        }
Ejemplo n.º 2
0
        private static object CallScriptMethod(MethodInfo scriptMethod, object instance, ScriptExecutionContext context, object[] args)
        {
            //get script attribute
            CCScriptAttribute scriptAttribute = null;
            var scriptAttributes = scriptMethod.GetCustomAttributes(typeof(CCScriptAttribute), false);

            if (scriptAttributes != null && scriptAttributes.Length > 0)
            {
                scriptAttribute = (CCScriptAttribute)scriptAttributes[0];
            }

            //attempt to resolve instance
            if (!scriptMethod.IsStatic && instance == null)
            {
                if (scriptAttribute != null && scriptAttribute.AutoResolveInstance)
                {
                    var property = scriptMethod.DeclaringType.GetProperty("Instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    if (property != null)
                    {
                        instance = property.GetValue(null);
                    }
                    else
                    {
                        var field = scriptMethod.DeclaringType.GetField("Instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                        if (field != null)
                        {
                            instance = field.GetValue(null);
                        }
                    }
                }

                if (instance == null)
                {
                    throw new NoInstanceForNonStaticMethodException();
                }
            }

            var scriptParameters = scriptMethod.GetParameters();

            //trim and coerce arguments
            object[] trimmedArgs;
            if (scriptParameters == null || scriptParameters.Length == 0)
            {
                //cheap path: pass empty array
                trimmedArgs = new object[] { };
            }
            else if ((scriptAttribute != null && scriptAttribute.NeverPassExecutionContext) || !(scriptParameters != null && scriptParameters.Length > 0 && scriptParameters[0].ParameterType.IsAssignableFrom(typeof(ScriptExecutionContext))))
            {
                //do not pass ScriptExecutionContext
                trimmedArgs = CoerceAndTrimArguments(args, scriptParameters, null);

                //display warning if we had to trim
                if (scriptParameters.Length != args.Length)
                {
                    Debug.LogWarning($"[{nameof(ScriptingModule)}] Argument coercion warning: {GetCallableName(scriptMethod)} expected {scriptParameters.Length} arguments but was passed {args.Length}");
                }
            }
            else
            {
                //pass all args including ScriptExecutionContext
                if (scriptParameters.Length == 1)
                {
                    trimmedArgs = new object[] { context };
                }
                else
                {
                    trimmedArgs = CoerceAndTrimArguments(args, scriptParameters, context);
                }

                //display warning if we had to trim
                if (scriptParameters.Length != args.Length + 1)
                {
                    Debug.LogWarning($"[{nameof(ScriptingModule)}] Argument coercion warning: {GetCallableName(scriptMethod)} expected 1+{scriptParameters.Length-1} arguments but was passed 1+{args.Length}");
                }
            }

            return(scriptMethod.Invoke(instance, trimmedArgs));
        }