/// <summary>
        /// Ensures that dispose method node is created for this empty type.
        /// </summary>
        /// <param name="module">Reference module</param>
        public void EnsureDisposeMethod(CLRDLLModule module)
        {
            foreach (var item in ClassNode.funclist)
            {
                if (item.Name == ProtoCore.DSDefinitions.Keyword.Dispose)
                {
                    return; //Dispose method is already present.
                }
            }
            bool resetModule = false;

            if (Module == null)
            {
                Module      = module;
                resetModule = true;
            }
            AssociativeNode        node = ParseMethod(mDisposeMethod);
            FunctionDefinitionNode func = node as FunctionDefinitionNode;

            if (func != null)
            {
                func.Name            = ProtoCore.DSDefinitions.Keyword.Dispose;
                func.IsStatic        = false;
                func.IsAutoGenerated = true;
                ClassNode.funclist.Add(func);
            }
            if (resetModule)
            {
                Module = null;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets CLRModuleType for given Type. If CLRModuleType instance for the
        /// given type is not found, it creates a new one. If CLRDLLModule is
        /// passed as null, it creates empty CLRModuleType.
        /// </summary>
        /// <param name="module">CLRDLLModule which imports this type</param>
        /// <param name="type">System.Type to be imported in DesignScript</param>
        /// <param name="alias">Alias name, if any. For now its not supported.</param>
        public static CLRModuleType GetInstance(Type type, CLRDLLModule module, string alias)
        {
            CLRModuleType mtype;
            if (!mTypes.TryGetValue(type, out mtype))
            {
                lock (mTypes)
                {
                    if (!mTypes.TryGetValue(type, out mtype))
                    {
                        mtype = new CLRModuleType(type);
                        //Now check that a type with same name is not imported.
                        Type otherType;
                        if (mTypeNames.TryGetValue(mtype.FullName, out otherType))
                            throw new InvalidOperationException(string.Format("Can't import {0}, {1} is already imported as {2}, namespace support needed.", type.FullName, type.Name, otherType.FullName));

                        mTypes.Add(type, mtype);
                        mTypeNames.Add(mtype.FullName, type);
                    }
                }
            }

            if (module != null && mtype.Module == null)
            {
                mtype.Module = module;
                if (type.IsEnum)
                    mtype.ClassNode = mtype.ParseEnumType(type, alias);
                else
                    mtype.ClassNode = mtype.ParseSystemType(type, alias);
            }

            return mtype;
        }
        public static ProtoCore.Type GetProtoCoreType(Type type, CLRDLLModule module)
        {
            ProtoCore.Type protoCoreType;
            if (mTypeMaps.TryGetValue(type, out protoCoreType))
            {
                return(protoCoreType);
            }

            if (type == typeof(object) || !CLRObjectMarshler.IsMarshaledAsNativeType(type))
            {
                if (type.IsEnum)
                {
                    protoCoreType = CLRModuleType.GetInstance(type, module, string.Empty).ProtoCoreType;
                }
                else
                {
                    protoCoreType = CLRModuleType.GetInstance(type, null, string.Empty).ProtoCoreType;
                }
            }
            else
            {
                protoCoreType = CLRObjectMarshler.GetProtoCoreType(type);
            }

            lock (mTypeMaps)
            {
                mTypeMaps[type] = protoCoreType;
            }
            return(protoCoreType);
        }
Esempio n. 4
0
        private void InitializeExtensionApp(Assembly assembly)
        {
            Type extensionAppType  = typeof(IExtensionApplication);
            Type assemblyAttribute = typeof(Autodesk.DesignScript.Runtime.ExtensionApplicationAttribute);

            System.Type appType = CLRDLLModule.GetImplemetationType(assembly, extensionAppType, assemblyAttribute, true);

            if (null == appType)
            {
                return;
            }

            IExtensionApplication extesionApp = null;

            lock (mAssemblies)
            {
                if (!mAssemblies.ContainsKey(assembly))
                {
                    extesionApp = (IExtensionApplication)Activator.CreateInstance(appType, true);
                    mExtensionApps.Add(appType, extesionApp);
                }
            }

            if (null != extesionApp)
            {
                extesionApp.StartUp();
            }
        }
        public GetterFunctionPointer(CLRDLLModule module, String functionName, MemberInfo method, ProtoCore.Type retType)
            : base(module, functionName, method, default(List <ProtoCore.Type>), retType)
        {
            string property;

            if (CoreUtils.TryGetPropertyName(functionName, out property))
            {
                PropertyName = property;
            }
        }
Esempio n. 6
0
        public CLRFFIFunctionPointer(CLRDLLModule module, string name, MemberInfo info, List <ProtoCore.Type> argTypes, ProtoCore.Type returnType)
        {
            Module         = module;
            Name           = name;
            ReflectionInfo = FFIMemberInfo.CreateFrom(info);

            mArgTypes = argTypes == null?GetArgumentTypes() : argTypes.ToArray();

            mReturnType = returnType;
        }
        public CLRFFIFunctionPointer(CLRDLLModule module, string name, MemberInfo info, List<ProtoCore.Type> argTypes, ProtoCore.Type returnType)
        {
            Module = module;
            Name = name;
            ReflectionInfo = FFIMemberInfo.CreateFrom(info);

            if (argTypes == null)
                mArgTypes = GetArgumentTypes(ReflectionInfo);
            else
                mArgTypes = argTypes.ToArray();

            mReturnType = returnType;
        }
Esempio n. 8
0
        /// <summary>
        /// Returns a CLRDLLModule after loading the given assembly.
        /// </summary>
        /// <param name="name">Name of assembly.</param>
        /// <returns>CLRDLLModule for given assembly/module name.</returns>
        public override DLLModule getModule(String name)
        {
            CLRDLLModule module = null;

            if (!mModules.TryGetValue(name, out module))
            {
                //see if it is a c# dll or native dll and create correct appropriate module and then query the module for function pointers.
                string extension = System.IO.Path.GetExtension(name);
                string filename  = System.IO.Path.GetFileName(name);

                bool isDLL = string.Compare(extension, ".dll", StringComparison.OrdinalIgnoreCase) == 0;
                try
                {
                    Assembly theAssembly = FFIExecutionManager.Instance.LoadAssembly(name);
                    Module   testDll     = theAssembly.GetModule(filename);
                    if (testDll == null)
                    {
                        module = new CLRDLLModule(filename, theAssembly);
                    }
                    else
                    {
                        module = new CLRDLLModule(filename, testDll);
                    }
                    lock (mModules)
                    {
                        mModules.Add(name, module);
                    }
                }
                catch (BadImageFormatException exception)
                {
                    //This probably wasn't a .NET dll
                    System.Diagnostics.Debug.WriteLine(exception.Message);
                    System.Diagnostics.Debug.WriteLine(exception.StackTrace);
                    throw new System.Exception(string.Format("Dynamo can only import .NET DLLs. Failed to load library: {0}.", name));
                }

                catch (System.Exception exception)
                {
                    // If the exception is having HRESULT of 0x80131515, then perhaps we need to instruct the user to "unblock" the downloaded DLL. Please seee the following link for details:
                    // http://blogs.msdn.com/b/brada/archive/2009/12/11/visual-studio-project-sample-loading-error-assembly-could-not-be-loaded-and-will-be-ignored-could-not-load-file-or-assembly-or-one-of-its-dependencies-operation-is-not-supported-exception-from-hresult-0x80131515.aspx
                    //
                    System.Diagnostics.Debug.WriteLine(exception.Message);
                    System.Diagnostics.Debug.WriteLine(exception.StackTrace);
                    throw new System.Exception(string.Format("Fail to load library: {0}.", name));
                }
            }

            return(module);
        }
        /// <summary>
        /// Gets CLRModuleType for given Type. If CLRModuleType instance for the
        /// given type is not found, it creates a new one. If CLRDLLModule is
        /// passed as null, it creates empty CLRModuleType.
        /// </summary>
        /// <param name="module">CLRDLLModule which imports this type</param>
        /// <param name="type">System.Type to be imported in DesignScript</param>
        /// <param name="alias">Alias name, if any. For now its not supported.</param>
        public static CLRModuleType GetInstance(Type type, CLRDLLModule module, string alias)
        {
            CLRModuleType mtype;

            if (!mTypes.TryGetValue(type, out mtype))
            {
                lock (mTypes)
                {
                    if (!mTypes.TryGetValue(type, out mtype))
                    {
                        mtype = new CLRModuleType(type);
                        //Now check that a type with same name is not imported.
                        Type otherType;
                        if (mTypeNames.TryGetValue(mtype.ClassName, out otherType))
                        {
                            throw new InvalidOperationException(string.Format("Can't import {0}, {1} is already imported as {2}, namespace support needed.", type.FullName, type.Name, otherType.FullName));
                        }

                        mTypes.Add(type, mtype);
                        mTypeNames.Add(mtype.ClassName, type);
                    }
                }
            }

            if (module != null && mtype.Module == null)
            {
                mtype.Module = module;
                if (type.IsEnum)
                {
                    mtype.ClassNode = mtype.ParseEnumType(type, alias);
                }
                else
                {
                    mtype.ClassNode = mtype.ParseSystemType(type, alias);
                }
            }

            return(mtype);
        }
Esempio n. 10
0
 public DisposeFunctionPointer(CLRDLLModule module, MemberInfo method, ProtoCore.Type retType)
     : base(module, ProtoCore.DSDefinitions.Kw.kw_Dispose, method, default(List <ProtoCore.Type>), retType)
 {
 }
Esempio n. 11
0
        public static ProtoCore.Type GetProtoCoreType(Type type, CLRDLLModule module)
        {
            ProtoCore.Type protoCoreType;
            if (mTypeMaps.TryGetValue(type, out protoCoreType))
                return protoCoreType;

            if (type == typeof(object) || !CLRObjectMarshler.IsMarshaledAsNativeType(type))
            {
                if(type.IsEnum)
                    protoCoreType = CLRModuleType.GetInstance(type, module, string.Empty).ProtoCoreType;
                else
                    protoCoreType = CLRModuleType.GetInstance(type, null, string.Empty).ProtoCoreType;
            }
            else
                protoCoreType = CLRObjectMarshler.GetProtoCoreType(type);

            lock (mTypeMaps)
            {
                mTypeMaps[type] = protoCoreType;
            }
            return protoCoreType;
        }
Esempio n. 12
0
 /// <summary>
 /// Ensures that dispose method node is created for this empty type.
 /// </summary>
 /// <param name="module">Reference module</param>
 public void EnsureDisposeMethod(CLRDLLModule module)
 {
     foreach (var item in ClassNode.funclist)
     {
         if (CoreUtils.IsDisposeMethod(item.Name))
             return; //Dispose method is already present.
     }
     bool resetModule = false;
     if (Module == null)
     {
         Module = module;
         resetModule = true;
     }
     AssociativeNode node = ParseMethod(mDisposeMethod);
     FunctionDefinitionNode func = node as FunctionDefinitionNode;
     if (func != null)
     {
         func.Name = ProtoCore.DSDefinitions.Keyword.Dispose;
         func.IsStatic = false;
         func.IsAutoGenerated = true;
         ClassNode.funclist.Add(func);
     }
     if (resetModule)
         Module = null;
 }
Esempio n. 13
0
        /// <summary>
        /// Returns a CLRDLLModule after loading the given assembly.
        /// </summary>
        /// <param name="name">Name of assembly.</param>
        /// <returns>CLRDLLModule for given assembly/module name.</returns>
        public override DLLModule getModule(String name)
        {
            CLRDLLModule module = null;
            if (!mModules.TryGetValue(name, out module))
            {
                //see if it is a c# dll or native dll and create correct appropriate module and then query the module for function pointers.
                string filename = System.IO.Path.GetFileName(name);

                try
                {
                    Assembly theAssembly = FFIExecutionManager.Instance.LoadAssembly(name);
                    Module testDll = theAssembly.GetModule(filename);
                    if (testDll == null)
                        module = new CLRDLLModule(filename, theAssembly);
                    else
                        module = new CLRDLLModule(filename, testDll);
                    lock (mModules)
                    {
                        mModules.Add(name, module);
                    }
                }
                catch (BadImageFormatException exception)
                {
                    //This probably wasn't a .NET dll
                    System.Diagnostics.Debug.WriteLine(exception.Message);
                    System.Diagnostics.Debug.WriteLine(exception.StackTrace);
                    throw new System.Exception(string.Format("Dynamo can only import .NET DLLs. Failed to load library: {0}.", name));   
                }

                catch (System.Exception exception)
                {
                    // If the exception is having HRESULT of 0x80131515, then perhaps we need to instruct the user to "unblock" the downloaded DLL. Please seee the following link for details:
                    // http://blogs.msdn.com/b/brada/archive/2009/12/11/visual-studio-project-sample-loading-error-assembly-could-not-be-loaded-and-will-be-ignored-could-not-load-file-or-assembly-or-one-of-its-dependencies-operation-is-not-supported-exception-from-hresult-0x80131515.aspx
                    // 
                    System.Diagnostics.Debug.WriteLine(exception.Message);
                    System.Diagnostics.Debug.WriteLine(exception.StackTrace);
                    throw new System.Exception(string.Format("Fail to load library: {0}.", name));
                }
            }

            return module;
        }
Esempio n. 14
0
 public GetterFunctionPointer(CLRDLLModule module, String functionName, MemberInfo method, ProtoCore.Type retType)
     : base(module, functionName, method, default(List<ProtoCore.Type>), retType)
 {
     string property;
     if (CoreUtils.TryGetPropertyName(functionName, out property))
     {
         PropertyName = property;
     }
 }
Esempio n. 15
0
 public DisposeFunctionPointer(CLRDLLModule module, MemberInfo method, ProtoCore.Type retType)
     : base(module, ProtoCore.DSDefinitions.Keyword.Dispose, method, default(List<ProtoCore.Type>), retType)
 {
 }