public NativeFunctionInstance(string name, MelonObject self, MelonEngine engine, NativeFunctionDelegate del) : base(name, engine) { Self = self; ReturnTypeAttribute returnTypeAttribute = del.Method.GetCustomAttribute <ReturnTypeAttribute>(); if (returnTypeAttribute != null) { ReturnType = new TypeReference(engine, engine.GetType(returnTypeAttribute.Type)); } ParameterAttribute[] parameterAttributes = del.Method.GetCustomAttributes <ParameterAttribute>().ToArray(); if (parameterAttributes?.Length > 0) { ParameterTypes = new FunctionParameter[parameterAttributes.Length]; bool hasVarargs = false; for (int i = 0; i < parameterAttributes.Length; i++) { if (parameterAttributes[i].IsGeneric) { ParameterTypes[i] = new FunctionParameter(parameterAttributes[i].Name, parameterAttributes[i].GenericIndex); } else { ParameterTypes[i] = new FunctionParameter(parameterAttributes[i].Name, new TypeReference(engine, engine.GetType(parameterAttributes[i].Type))); } if (parameterAttributes[i].IsVarargs) { if (hasVarargs || i < parameterAttributes.Length - 1) { throw new MelonException("Varargs parameter can only appear once and has to be the last parameter"); } ParameterTypes[i].IsVarargs = true; hasVarargs = true; } } } else { ParameterTypes = new[] { new FunctionParameter("", new TypeReference(engine, engine.anyType)) { IsVarargs = true } }; } Delegate = del; }
List <DndFunction> GetDndFunctionsStartingWith(EditorProviderDetails expectedProviderDetails, string tokenLeftOfCaret) { List <DndFunction> dndFunctions = Expressions.GetFunctionsStartingWith(tokenLeftOfCaret); if (expectedProviderDetails.Type == null) { return(dndFunctions); } List <DndFunction> filteredFunctions = new List <DndFunction>(); foreach (DndFunction dndFunction in dndFunctions) { ReturnTypeAttribute returnType = dndFunction.GetType().GetCustomAttribute <ReturnTypeAttribute>(); if (returnType != null && returnType.Matches(expectedProviderDetails.Type)) { filteredFunctions.Add(dndFunction); } } return(filteredFunctions); }
/// <summary> /// Gets the return type of method. /// </summary> /// <param name="method">The method.</param> /// <returns>TypeDescriptor.</returns> private TypeDescriptor getReturnType(MethodInfo method) { var attributes = method.GetCustomAttributes(typeof(ReturnTypeAttribute), false); ReturnTypeAttribute attribute = null; if (attributes.Length > 0) { attribute = attributes[0] as ReturnTypeAttribute; } if (attribute == null) { //default return type return(Translator.GetTypeDescriptor(method, (m) => m.ReturnType)); } else { //forced return type return(attribute.ReturnInfo); } }