internal AsyncLoadOperation(ScriptDomain domain, byte[] assemblyBytes, ScriptSecurityMode securityMode)
 {
     this.loadDomain   = domain;
     this.asmBytes     = assemblyBytes;
     this.loadType     = AssemblyLoadType.LoadFromBytes;
     this.securityMode = securityMode;
 }
 // Constructor
 internal AsyncLoadOperation(ScriptDomain domain, AssemblyName assemblyName, ScriptSecurityMode securityMode)
 {
     this.loadDomain   = domain;
     this.asmName      = assemblyName;
     this.loadType     = AssemblyLoadType.LoadByName;
     this.securityMode = securityMode;
 }
 internal AsyncLoadOperation(ScriptDomain domain, string assemblyPath, ScriptSecurityMode securityMode)
 {
     this.loadDomain   = domain;
     this.asmPath      = assemblyPath;
     this.loadType     = AssemblyLoadType.LoadByPath;
     this.securityMode = securityMode;
 }
        // Constructor
        internal ScriptAssembly(ScriptDomain domain, Assembly rawAssembly, CompilationResult compileResult = null)
        {
            this.domain        = domain;
            this.rawAssembly   = rawAssembly;
            this.compileResult = compileResult;

            // Get all raw types
            Type[] rawTypes = rawAssembly.GetTypes();

            // Create cached types
            foreach (Type type in rawTypes)
            {
                if (type.IsNested == false)
                {
                    // Create a root type
                    ScriptType asmType = new ScriptType(this, null, type);

                    scriptTypes.Add(type.FullName, asmType);
                }
            }

            matchedTypes.AddRange(scriptTypes.Values);

            // Link up nested types
            foreach (ScriptType type in matchedTypes)
            {
                // Build nested type tree reccursivley
                CreateNestedTypes(type);
            }

            matchedTypes.Clear();
        }
        public static T CreateAssembly <T>(ScriptDomain domain, CompilationResult result, Assembly rawAssembly = null) where T : ScriptAssembly, new()
        {
            // Check for success
            if (result.Success == false)
            {
                return(null);
            }

            if (rawAssembly == null)
            {
                rawAssembly = result.OutputAssembly;
            }

            T asm = new T()
            {
                domain            = domain,
                rawAssembly       = rawAssembly,
                assemblyPath      = result.OutputFile,
                rawAssemblyImage  = result.OutputAssemblyImage,
                compileResult     = result,
                isRuntimeCompiled = true,
            };

            // Build the assembly
            asm.BuildAssembly();

            return(asm);
        }
 // Constructor
 internal AsyncCompileOperation(ScriptDomain domain, bool isCSharpSource, ScriptSecurityMode securityMode, string[] sourceOrFiles, IMetadataReferenceProvider[] additionalReferenceAssemblies = null)
 {
     this.compileDomain        = domain;
     this.sourceCompileType    = (isCSharpSource == true) ? CompileType.CompileSource : CompileType.CompileFile;
     this.securityMode         = securityMode;
     this.sourceOrFiles        = sourceOrFiles;
     this.additionalReferences = additionalReferenceAssemblies;
 }
        /// <summary>
        /// Set the specified domain as the active domain.
        /// The active domain is used when resolving script types from an unspecified source.
        /// </summary>
        /// <param name="domain">The domain to make active</param>
        public static void MakeDomainActive(ScriptDomain domain)
        {
            // Check for null domain
            if (domain == null)
            {
                throw new ArgumentNullException(nameof(domain));
            }

            // Make active
            active = domain;
        }
        /// <summary>
        /// Set the domain with the specified name as the active domain.
        /// The active domain is used when resolving script types from an unspecified source.
        /// </summary>
        /// <param name="domainName">The name of the domain to make active</param>
        public static void MakeDomainActive(string domainName)
        {
            // Find domain with name
            ScriptDomain domain = FindDomain(domainName);

            // Make active
            if (domain != null)
            {
                MakeDomainActive(domain);
            }
        }
        // Constructor
        internal ScriptAssembly(ScriptDomain domain, Assembly rawAssembly, CodeSecurityEngine securityEngine, CompilationResult compileResult = null)
        {
            this.domain         = domain;
            this.rawAssembly    = rawAssembly;
            this.securityEngine = securityEngine;
            this.compileResult  = compileResult;

            // Create cached types
            foreach (Type type in rawAssembly.GetTypes())
            {
                scriptTypes.Add(type.FullName, new ScriptType(this, type));
            }
        }
        private static bool ResolveSearchDomain(ref ScriptDomain searchDomain)
        {
            // Check for specified domain
            if (searchDomain == null)
            {
                // Get the active domain
                searchDomain = ScriptDomain.Active;

                // No domain found to search
                if (searchDomain == null)
                {
                    return(false);
                }
            }
            return(true);
        }
        public static T CreateAssembly <T>(ScriptDomain domain, Assembly rawAssembly, byte[] assemblyImage) where T : ScriptAssembly, new()
        {
            if (rawAssembly == null)
            {
                return(null);
            }

            T asm = new T()
            {
                domain           = domain,
                rawAssembly      = rawAssembly,
                rawAssemblyImage = assemblyImage,
            };

            // Build the assembly
            asm.BuildAssembly();

            return(asm);
        }
        public static T CreateAssembly <T>(ScriptDomain domain, Assembly rawAssembly, string assemblyPath) where T : ScriptAssembly, new()
        {
            if (rawAssembly == null)
            {
                return(null);
            }

            T asm = new T()
            {
                domain       = domain,
                rawAssembly  = rawAssembly,
                assemblyPath = assemblyPath,
            };

            // Build the assembly
            asm.BuildAssembly();

            return(asm);
        }
        public static ScriptAssembly CreateAssembly(ScriptDomain domain, Assembly rawAssembly, byte[] assemblyImage)
        {
            if (rawAssembly == null)
            {
                return(null);
            }

            ScriptAssembly asm = new ScriptAssembly()
            {
                domain           = domain,
                rawAssembly      = rawAssembly,
                rawAssemblyImage = assemblyImage,
            };

            // Build the assembly
            asm.BuildAssembly();

            return(asm);
        }
        public static ScriptAssembly CreateAssembly(ScriptDomain domain, Assembly rawAssembly, string assemblyPath)
        {
            if (rawAssembly == null)
            {
                return(null);
            }

            ScriptAssembly asm = new ScriptAssembly()
            {
                domain       = domain,
                rawAssembly  = rawAssembly,
                assemblyPath = assemblyPath,
            };

            // Build the assembly
            asm.BuildAssembly();

            return(asm);
        }
        /// <summary>
        /// Creates a new <see cref="ScriptDomain"/> into which assemblies and scripts may be loaded.
        /// </summary>
        /// <returns>A new instance of <see cref="ScriptDomain"/></returns>
        public static ScriptDomain CreateDomain(string domainName, bool initCompiler = true, bool makeActiveDomain = true, AppDomain sandboxDomain = null)
        {
            // Create a new named domain
            ScriptDomain domain = new ScriptDomain(domainName, sandboxDomain);

            // Load the roslyn settings - do this now because the next load request could be from a worker thread
            RoslynCSharp.LoadResources();

            // Check for compiler
            if (initCompiler == true)
            {
                domain.InitializeCompilerService();
            }

            // Make domain active
            if (makeActiveDomain == true)
            {
                MakeDomainActive(domain);
            }

            return(domain);
        }
        /// <summary>
        /// Attempt to find all types in the specified domain.
        /// </summary>
        /// <param name="includeNonPublic">Should non-public types be included in the search</param>
        /// <param name="searchDomain">The domai to search or null if the active domain should be used</param>
        /// <returns>An array of <see cref="ScriptType"/> that exist in the specified domain or an empty array if no types were found</returns>
        public static ScriptType[] FindAllTypes(bool includeNonPublic = true, ScriptDomain searchDomain = null)
        {
            // Try to resolve domain
            if (ResolveSearchDomain(ref searchDomain) == false)
            {
                return(new ScriptType[0]);
            }

            // Use shared types list
            matchedTypes.Clear();

            // Search all assemblies
            foreach (ScriptAssembly assembly in searchDomain.Assemblies)
            {
                // Find types
                ScriptType[] types = assembly.FindAllTypes(includeNonPublic);

                // Add to result
                matchedTypes.AddRange(types);
            }

            return(matchedTypes.ToArray());
        }
        /// <summary>
        /// Attempt to find the first type that inherits from the specified generic sub type.
        /// </summary>
        /// <typeparam name="T">The generic type that the type must inherit from</typeparam>
        /// <param name="searchDomain">The domain to search or null if the active domain should be used</param>
        /// <returns>A <see cref="ScriptType"/> matching the specified inheritance constraints or null if the type was not found</returns>
        public static ScriptType FindSubTypeOf <T>(ScriptDomain searchDomain = null)
        {
            // Try to resolve domain
            if (ResolveSearchDomain(ref searchDomain) == false)
            {
                return(null);
            }

            // Search all assemblies
            foreach (ScriptAssembly assembly in searchDomain.Assemblies)
            {
                // Try to find type
                ScriptType type = assembly.FindSubTypeOf <T>();

                // Check for success
                if (type != null)
                {
                    return(type);
                }
            }

            // Type not found
            return(null);
        }
        /// <summary>
        /// Enumerate all types in the specified domain that inherit from <see cref="UnityEngine.ScriptableObject"/>.
        /// </summary>
        /// <param name="includeNonPublic">Should non-public types be include in the search</param>
        /// <param name="searchDomain">The domain to search or null if the active domain should be used</param>
        /// <returns>Enumerable of matching results</returns>
        public static IEnumerable <ScriptType> EnumerateAllScriptableObjectTypes(bool includeNonPublic = true, ScriptDomain searchDomain = null)
        {
            // Try to resolve domain
            if (ResolveSearchDomain(ref searchDomain) == false)
            {
                yield break;
            }

            // Search all assemblies
            foreach (ScriptAssembly assembly in searchDomain.Assemblies)
            {
                // Try to find type
                foreach (ScriptType type in assembly.EnumerateAllScriptableObjectTypes(includeNonPublic))
                {
                    // Return the type
                    yield return(type);
                }
            }
        }