Пример #1
0
        public static void test_callback2(Action <SPBuffer> callback, SPSizedBuffer buffer)
        {
            if (buffer.Get() != "hello world")
            {
                Console.WriteLine("FAILED");
                return;
            }

            tests++;
            buffer.Set("testing 1 3");
            callback(buffer);
        }
Пример #2
0
        private void AddNatives(SourceMod.IExtension myself, SPMethodAttribute attribute, MethodInfo method)
        {
            var @params = method.GetParameters();

            AddNatives(myself, attribute.MethodName ?? method.Name, (IPluginContext pluginContext, int[] arguments) => {
                var methodParams = new object[@params.Length];
                int argumenti    = 0;
                for (int i = 0; i < @params.Length; i++)
                {
                    var param = @params[i];
                    var type  = param.ParameterType;
                    if (type == typeof(IPluginContext))
                    {
                        methodParams[i] = pluginContext;
                    }
                    else if (type == typeof(string))
                    {
                        methodParams[i] = GetString(pluginContext, arguments[argumenti]);
                        argumenti++;
                    }
                    else if (type == typeof(int))
                    {
                        methodParams[i] = arguments[argumenti];
                        argumenti++;
                    }
                    else if (type == typeof(uint))
                    {
                        methodParams[i] = (uint)arguments[argumenti];
                        argumenti++;
                    }
                    else if (type == typeof(SPBuffer))
                    {
                        methodParams[i] = new SPBuffer(pluginContext, arguments[argumenti]);
                        argumenti++;
                    }
                    else if (type == typeof(SPSizedBuffer))
                    {
                        methodParams[i] = new SPSizedBuffer(pluginContext, arguments[argumenti], (uint)arguments[argumenti + 1]);
                        argumenti      += 2;
                    }
                    else if (type == typeof(IPluginFunction))
                    {
                        var attributes = param.GetCustomAttributes(typeof(SPMethodAttribute), false);
                        if (attributes.Length > 0)
                        {
                            var attr        = attributes[0] as SPMethodAttribute;
                            methodParams[i] = pluginContext.GetFunctionByName(attr.MethodName);
                        }
                        else
                        {
                            methodParams[i] = pluginContext.GetFunctionById((uint)arguments[argumenti]);
                            argumenti++;
                        }
                    }
                    else if (type.FullName.StartsWith("System.Action") && type.IsGenericType)
                    {
                        var callback = typeof(IShareSys).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(m => m.Name == "Callback")
                                       .Where(m => m.GetGenericArguments().Length == param.ParameterType.GetGenericArguments().Length)
                                       .First().MakeGenericMethod(param.ParameterType.GetGenericArguments());

                        var func = pluginContext.GetFunctionById((uint)arguments[argumenti]);
                        argumenti++;
                        methodParams[i] = Delegate.CreateDelegate(param.ParameterType, func, callback);
                    }
                }
                var ret = method.Invoke(null, methodParams);
                if (method.ReturnType == typeof(int))
                {
                    return((int)ret);
                }
                else
                {
                    return(0);
                }
            });
        }