Пример #1
0
        public TargetAssembly(string name,
                              SupportedLanguage language,
                              AssemblyFlags flags,
                              TargetAssemblyType type,
                              string pathPrefix,
                              string[] additionalPrefixes,
                              Func <string, int> pathFilter,
                              Func <ScriptAssemblySettings, string[], bool> compatFunc,
                              ScriptCompilerOptions compilerOptions) : this()
        {
            Language           = language;
            Filename           = name;
            Flags              = flags;
            PathPrefix         = pathPrefix;
            AdditionalPrefixes = additionalPrefixes;
            PathFilter         = pathFilter;
            IsCompatibleFunc   = compatFunc;
            Type            = type;
            CompilerOptions = compilerOptions;
            ExplicitPrecompiledReferences = new List <string>();
            VersionDefines = new List <VersionDefine>();

            if (PathPrefix != null)
            {
                MaxPathLength = PathPrefix.Length;
            }
            if (AdditionalPrefixes != null)
            {
                MaxPathLength = UnityEngine.Mathf.Max(MaxPathLength, AdditionalPrefixes.Max(am => am.Length));
            }
        }
Пример #2
0
        public static ScriptAssembly[] GetAllScriptAssemblies(
            Dictionary <string, string> allSourceFiles,
            String projectDirectory,
            ScriptAssemblySettings settings,
            CompilationAssemblies assemblies,
            ISafeModeInfo safeModeInfo,
            TargetAssemblyType onlyIncludeType = TargetAssemblyType.Undefined,
            Func <TargetAssembly, bool> targetAssemblyCondition = null,
            ICompilationSetupWarningTracker warningSink         = null)
        {
            if (allSourceFiles == null || allSourceFiles.Count == 0)
            {
                return(new ScriptAssembly[0]);
            }

            var targetAssemblyFiles = new Dictionary <TargetAssembly, DirtyTargetAssembly>();

            foreach (var entry in allSourceFiles)
            {
                var scriptFile     = entry.Key;
                var assemblyName   = entry.Value;
                var targetAssembly = GetTargetAssembly(scriptFile, assemblyName, projectDirectory, assemblies.CustomTargetAssemblies);

                if (targetAssembly == null)
                {
                    continue;
                }

                if (!IsCompatibleWithPlatformAndDefines(targetAssembly, settings))
                {
                    continue;
                }

                if (targetAssemblyCondition != null && !targetAssemblyCondition(targetAssembly))
                {
                    continue;
                }

                // Optionally only include specific TargetAssemblyType assemblies.
                if (onlyIncludeType != TargetAssemblyType.Undefined && targetAssembly.Type != onlyIncludeType)
                {
                    continue;
                }

                DirtyTargetAssembly dirtyTargetAssembly;

                if (!targetAssemblyFiles.TryGetValue(targetAssembly, out dirtyTargetAssembly))
                {
                    dirtyTargetAssembly = new DirtyTargetAssembly();
                    targetAssemblyFiles[targetAssembly] = dirtyTargetAssembly;
                }

                dirtyTargetAssembly.SourceFiles.Add(AssetPath.Combine(projectDirectory, scriptFile));
            }

            return(ToScriptAssemblies(targetAssemblyFiles, settings, assemblies, warningSink, safeModeInfo));
        }
 public TargetAssembly(string name,
                       SupportedLanguage language,
                       AssemblyFlags flags,
                       TargetAssemblyType type,
                       Func <string, int> pathFilter,
                       Func <BuildTarget, EditorScriptCompilationOptions, bool> compatFunc,
                       ScriptCompilerOptions compilerOptions) : this()
 {
     Language         = language;
     Filename         = name;
     Flags            = flags;
     PathFilter       = pathFilter;
     IsCompatibleFunc = compatFunc;
     Type             = type;
     CompilerOptions  = compilerOptions;
 }
Пример #4
0
        /// <summary>
        /// Get an embedded image from the assmebly
        /// </summary>
        /// <param name="fileName">Filename of embedded resource. I.e. favicon.ico</param>
        /// <param name="resourcePath">i.e. the folder as a namename excluding the assembly name. I.e. Dependencies.Helpers</param>
        /// <param name="targetAssemblyType">I.e Calling or executing</param>
        public static System.Drawing.Image GetEmbeddedResourceImage(
            string fileName,
            string resourcePath,
            TargetAssemblyType targetAssemblyType)
        {
            Assembly assembly;

            if (targetAssemblyType == TargetAssemblyType.Calling)
            {
                assembly = Assembly.GetCallingAssembly();
            }
            else
            {
                assembly = Assembly.GetExecutingAssembly();
            }

            return(GetEmbeddedResourceImage(assembly, fileName, resourcePath));
        }
Пример #5
0
        /// <summary>
        /// Extracts an embedded resource to a memory stream
        /// </summary>
        /// <param name="fileName">Filename of embedded resource. I.e. CsvHelper.dll</param>
        /// <param name="resourcePath">i.e. the folder as a namename excluding the assembly name. I.e. Dependencies.Helpers</param>
        /// <param name="targetAssemblyType">I.e Calling or executing</param>
        public static Stream GetEmbeddedResourceStream(
            string fileName,
            string resourcePath,
            TargetAssemblyType targetAssemblyType)
        {
            Assembly assembly;

            if (targetAssemblyType == TargetAssemblyType.Calling)
            {
                assembly = Assembly.GetCallingAssembly();
            }
            else
            {
                assembly = Assembly.GetExecutingAssembly();
            }

            return(GetEmbeddedResourceStream(assembly, fileName, resourcePath));
        }
        /// <summary>
        /// Returns the embedded resource if it's present in the working folder or if it's been extracted. If it's not present it will extract the embedded resource
        /// to the users AppData folder and return the full path to it
        /// </summary>
        /// <param name="fileName">Filename of embedded resource. I.e. CsvHelper.dll</param>
        /// <param name="resourcePath">i.e. the folder as a namename excluding the assembly name. I.e. Dependencies.Helpers</param>
        /// <param name="targetAssemblyType">I.e. Calling or Executing</param>
        /// <param name="skipHashCheck">Skips hash check on extracted resource</param>
        /// <returns>Full path to present or extracted directory</returns>
        public static string GetEmbeddedResourcePath(
            string fileName,
            string resourcePath,
            TargetAssemblyType targetAssemblyType,
            bool skipHashCheck = false)
        {
            Assembly assembly;

            if (targetAssemblyType == TargetAssemblyType.Calling)
            {
                assembly = Assembly.GetCallingAssembly();
            }
            else
            {
                assembly = Assembly.GetExecutingAssembly();
            }

            return(GetEmbeddedResourcePath(assembly, fileName, resourcePath, skipHashCheck));
        }
        /// <summary>
        /// Extracts an embedded resource to a given location
        /// </summary>
        /// <param name="fileName">Filename of embedded resource. I.e. CsvHelper.dll</param>
        /// <param name="resourcePath">i.e. the folder as a namename excluding the assembly name. I.e. Dependencies.Helpers</param>
        /// <param name="outputDirectory">The parent directory to outoput to</param>
        /// <param name="outputFilename">The filename to give the extracted resources. Use null to default to the present filename</param>
        /// <param name="targetAssemblyType">I.e Calling or executing</param>
        public static void ExtractEmbeddedResource(
            string fileName,
            string resourcePath,
            string outputDirectory,
            string outputFilename,
            TargetAssemblyType targetAssemblyType)
        {
            Assembly assembly;

            if (targetAssemblyType == TargetAssemblyType.Calling)
            {
                assembly = Assembly.GetCallingAssembly();
            }
            else
            {
                assembly = Assembly.GetExecutingAssembly();
            }

            ExtractEmbeddedResource(assembly, fileName, resourcePath, outputDirectory, outputFilename);
        }
Пример #8
0
        public static List <string> GetPrecompiledReferences(ScriptAssembly scriptAssembly, TargetAssemblyType targetAssemblyType, EditorScriptCompilationOptions options, EditorCompatibility editorCompatibility, IEnumerable <PrecompiledAssembly> implicitPrecompiledAssemblies, IEnumerable <PrecompiledAssembly> explicitPrecompiledAssemblies, ICompilationSetupWarningTracker warningSink)
        {
            var references = new List <string>();

            bool buildingForEditor  = (options & EditorScriptCompilationOptions.BuildingForEditor) == EditorScriptCompilationOptions.BuildingForEditor;
            bool assemblyEditorOnly = (scriptAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly;
            bool isCustomAssembly   = (targetAssemblyType & TargetAssemblyType.Custom) == TargetAssemblyType.Custom;

            void AddReferenceIfMatchBuildTargetAndEditorFlag(PrecompiledAssembly precompiledAssembly, bool explicitReference)
            {
                bool compiledAssemblyEditorOnly = (precompiledAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly;

                // Add all pre-compiled runtime assemblies as references to all script assemblies. Don't add pre-compiled editor assemblies as dependencies to runtime assemblies.
                if (!compiledAssemblyEditorOnly || assemblyEditorOnly || (isCustomAssembly && buildingForEditor && editorCompatibility == EditorCompatibility.CompatibleWithEditor))
                {
                    if (IsPrecompiledAssemblyCompatibleWithBuildTarget(precompiledAssembly, scriptAssembly.BuildTarget))
                    {
                        references.Add(precompiledAssembly.Path);
                    }
                    // we don't warn on build target mismatch, as this is actually a common pattern (an asmdef with multiple references to different "target-specific" assemblies with the same symbols - e.g. foo.XboxOne.dll, foo.PS5.dll, foo.WebGL.dll)
                }
                else if (explicitReference && !string.IsNullOrEmpty(scriptAssembly.AsmDefPath))
                {
                    warningSink?.AddAssetWarning(scriptAssembly.AsmDefPath, $"{scriptAssembly.Filename}: can't add reference to {precompiledAssembly.Path} as it is an editor-only assembly");
                }
            }

            if (implicitPrecompiledAssemblies != null)
            {
                foreach (var precompiledAssembly in implicitPrecompiledAssemblies)
                {
                    AddReferenceIfMatchBuildTargetAndEditorFlag(precompiledAssembly, false);
                }
            }
            if (explicitPrecompiledAssemblies != null)
            {
                foreach (var precompiledAssembly in explicitPrecompiledAssemblies)
                {
                    AddReferenceIfMatchBuildTargetAndEditorFlag(precompiledAssembly, true);
                }
            }

            return(references);
        }
Пример #9
0
        public static List <string> GetPrecompiledReferences(ScriptAssembly scriptAssembly, TargetAssemblyType targetAssemblyType, EditorScriptCompilationOptions options, EditorCompatibility editorCompatibility, PrecompiledAssembly[] precompiledAssemblies)
        {
            var references = new List <string>();

            bool buildingForEditor  = (options & EditorScriptCompilationOptions.BuildingForEditor) == EditorScriptCompilationOptions.BuildingForEditor;
            bool assemblyEditorOnly = (scriptAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly;
            bool isCustomAssembly   = (targetAssemblyType & TargetAssemblyType.Custom) == TargetAssemblyType.Custom;

            if (precompiledAssemblies != null)
            {
                foreach (var precompiledAssembly in precompiledAssemblies)
                {
                    bool compiledAssemblyEditorOnly = (precompiledAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly;

                    // Add all pre-compiled runtime assemblies as references to all script assemblies. Don't add pre-compiled editor assemblies as dependencies to runtime assemblies.
                    if (!compiledAssemblyEditorOnly || assemblyEditorOnly || (isCustomAssembly && buildingForEditor && editorCompatibility == EditorCompatibility.CompatibleWithEditor))
                    {
                        if (IsPrecompiledAssemblyCompatibleWithScriptAssembly(precompiledAssembly, scriptAssembly))
                        {
                            references.Add(precompiledAssembly.Path);
                        }
                    }
                }
            }

            return(references);
        }
Пример #10
0
        public static ScriptAssembly[] GetAllScriptAssemblies(IEnumerable <string> allSourceFiles, string projectDirectory, ScriptAssemblySettings settings, CompilationAssemblies assemblies, TargetAssemblyType onlyIncludeType = TargetAssemblyType.Undefined)
        {
            if (allSourceFiles == null || allSourceFiles.Count() == 0)
            {
                return(new ScriptAssembly[0]);
            }

            var targetAssemblyFiles = new Dictionary <TargetAssembly, HashSet <string> >();

            foreach (var scriptFile in allSourceFiles)
            {
                var targetAssembly = GetTargetAssembly(scriptFile, projectDirectory, assemblies.CustomTargetAssemblies);

                if (!IsCompatibleWithPlatform(targetAssembly, settings))
                {
                    continue;
                }

                // Optionally only include specific TargetAssemblyType assemblies.
                if (onlyIncludeType != TargetAssemblyType.Undefined && targetAssembly.Type != onlyIncludeType)
                {
                    continue;
                }

                var scriptExtension = ScriptCompilers.GetExtensionOfSourceFile(scriptFile);
                var scriptLanguage  = ScriptCompilers.GetLanguageFromExtension(scriptExtension);

                if (targetAssembly.Language == null && targetAssembly.Type == TargetAssemblyType.Custom)
                {
                    targetAssembly.Language = scriptLanguage;
                }

                HashSet <string> assemblySourceFiles;

                if (!targetAssemblyFiles.TryGetValue(targetAssembly, out assemblySourceFiles))
                {
                    assemblySourceFiles = new HashSet <string>();
                    targetAssemblyFiles[targetAssembly] = assemblySourceFiles;
                }

                assemblySourceFiles.Add(AssetPath.Combine(projectDirectory, scriptFile));
            }

            return(ToScriptAssemblies(targetAssemblyFiles, settings, assemblies, null));
        }