private void Add(DekiExtFunctionAttribute ext, MethodInfo method, XDoc script) { // convert DekiExtParamAttribute into DekiScriptNativeInvocationTarget.Parameter var parameters = from param in method.GetParameters() let attr = (DekiExtParamAttribute[])param.GetCustomAttributes(typeof(DekiExtParamAttribute), false) select((attr != null) && (attr.Length > 0)) ? new DekiScriptNativeInvocationTarget.Parameter(attr[0].Hint, attr[0].Optional) : null; // create native target invocation var target = new DekiScriptNativeInvocationTarget(this, method, parameters.ToArray()); DekiScriptInvocationTargetDescriptor function; // check if implementation is provided by a script instead if (script != null) { var scriptTarget = new DekiScriptExpressionInvocationTarget(target.Access, target.Parameters, DekiScriptParser.Parse(script)); function = new DekiScriptInvocationTargetDescriptor(target.Access, ext.IsProperty, false, ext.Name ?? method.Name, target.Parameters, target.ReturnType, ext.Description, ext.Transform, scriptTarget); } else { function = new DekiScriptInvocationTargetDescriptor(target.Access, ext.IsProperty, false, ext.Name ?? method.Name, target.Parameters, target.ReturnType, ext.Description, ext.Transform, target); } _functions[Self.At(function.SystemName)] = function; }
protected override Yield Start(XDoc config, Result result) { yield return(Coroutine.Invoke(base.Start, config, new Result())); // loop over all resources Type type = GetType(); string assembly = type.Assembly.FullName.Split(new char[] { ',' }, 2)[0]; foreach (DekiExtLibraryFilesAttribute files in Attribute.GetCustomAttributes(type, typeof(DekiExtLibraryFilesAttribute))) { string prefix = files.Prefix ?? type.Namespace; foreach (string filename in files.Filenames) { MimeType mime = MimeType.FromFileExtension(filename); _files[filename] = Plug.New(string.Format("resource://{0}/{1}.{2}", assembly, prefix, filename)).With("dream.out.type", mime.FullType); } } // check if a public digital signature key was provided string dsaKey = config["deki-signature"].AsText ?? config["dekiwiki-signature"].AsText; if (dsaKey != null) { try { DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(); dsa.ImportCspBlob(Convert.FromBase64String(dsaKey)); _publicDigitalSignature = dsa; } catch { throw new ArgumentException("invalid digital signature provided", "deki-signature"); } } // loop over all instance methods foreach (MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { // check if it has the DekiExtFunction attriute DekiExtFunctionAttribute ext = (DekiExtFunctionAttribute)Attribute.GetCustomAttribute(method, typeof(DekiExtFunctionAttribute)); if (ext != null) { // check if function has an associated script XDekiScript script = null; DekiExtFunctionScriptAttribute scriptAttr = (DekiExtFunctionScriptAttribute)Attribute.GetCustomAttribute(method, typeof(DekiExtFunctionScriptAttribute)); if (scriptAttr != null) { DreamMessage scriptresource = Plug.New(string.Format("resource://{0}/{1}.{2}", assembly, scriptAttr.Prefix ?? type.Namespace, scriptAttr.Scriptname)).With("dream.out.type", MimeType.XML.FullType).GetAsync().Wait(); if (scriptresource.IsSuccessful) { script = new XDekiScript(scriptresource.ToDocument()); } if (script == null) { throw new InvalidOperationException(string.Format("method '{0}' is declard as script, but script could not be loaded", method.Name)); } } // add function Add(ext, method, script); } } // add configuration settings var context = DreamContext.Current; _scriptConfig = new DekiScriptMap(); foreach (KeyValuePair <string, string> entry in Config.ToKeyValuePairs()) { XUri local; if (XUri.TryParse(entry.Value, out local)) { local = context.AsPublicUri(local); _scriptConfig.AddAt(entry.Key.Split('/'), DekiScriptExpression.Constant(local.ToString())); } else { _scriptConfig.AddAt(entry.Key.Split('/'), DekiScriptExpression.Constant(entry.Value)); } } result.Return(); }