public static CustomScriptAssembly FromCustomScriptAssemblyData(string path, string guid, CustomScriptAssemblyData customScriptAssemblyData)
        {
            if (customScriptAssemblyData == null)
                return null;

            var pathPrefix = path.Substring(0, path.Length - AssetPath.GetFileName(path).Length);

            var customScriptAssembly = new CustomScriptAssembly();

            customScriptAssembly.Name = customScriptAssemblyData.name;
            customScriptAssembly.GUID = guid;
            customScriptAssembly.References = customScriptAssemblyData.references;
            customScriptAssembly.FilePath = path;
            customScriptAssembly.PathPrefix = pathPrefix;
            customScriptAssembly.AutoReferenced = customScriptAssemblyData.autoReferenced;
            customScriptAssembly.OverrideReferences = customScriptAssemblyData.overrideReferences;
            customScriptAssembly.NoEngineReferences = customScriptAssemblyData.noEngineReferences;
            customScriptAssembly.PrecompiledReferences = customScriptAssemblyData.precompiledReferences ?? new string[0];
            customScriptAssembly.DefineConstraints = customScriptAssemblyData.defineConstraints ?? new string[0];
            customScriptAssembly.VersionDefines = (customScriptAssemblyData.versionDefines ?? new VersionDefine[0]);

            if (customScriptAssemblyData.includePlatforms != null && customScriptAssemblyData.includePlatforms.Length > 0)
                customScriptAssembly.IncludePlatforms = GetPlatformsFromNames(customScriptAssemblyData.includePlatforms);

            if (customScriptAssemblyData.excludePlatforms != null && customScriptAssemblyData.excludePlatforms.Length > 0)
                customScriptAssembly.ExcludePlatforms = GetPlatformsFromNames(customScriptAssemblyData.excludePlatforms);

            var compilerOptions = new ScriptCompilerOptions();

            compilerOptions.AllowUnsafeCode = customScriptAssemblyData.allowUnsafeCode;

            customScriptAssembly.CompilerOptions = compilerOptions;

            return customScriptAssembly;
        }
        private static Dictionary <string, PrecompiledAssembly> FilenameToPrecompiledAssembly(PrecompiledAssembly[] precompiledAssemblies)
        {
            if (precompiledAssemblies == null)
            {
                return(new Dictionary <string, PrecompiledAssembly>(0));
            }

            var dictionary = new Dictionary <string, PrecompiledAssembly>();

            foreach (var assembly in precompiledAssemblies)
            {
                var filename = AssetPath.GetFileName(assembly.Path);
                if (!dictionary.TryGetValue(filename, out var existingAssembly))
                {
                    dictionary[filename] = assembly;
                    continue;
                }

                if (existingAssembly.Redirected)
                {
                    dictionary[filename] = assembly;
                }
            }
            return(dictionary);
        }
        public bool IsAnyRunningPostProcessorUsingAssembly(ScriptAssembly assembly)
        {
            if (RunningPostProcessors == null ||
                RunningPostProcessors.Count == 0)
            {
                return(false);
            }

            var runningPostProcessorsArray = RunningPostProcessors.ToArray();

            foreach (var postProcessor in runningPostProcessorsArray)
            {
                var postProcessorAssembly = postProcessor.scriptAssembly;

                if (postProcessorAssembly.ScriptAssemblyReferences.Contains(assembly))
                {
                    return(true);
                }

                if (postProcessorAssembly.References.Any(r => AssetPath.GetFileName(r) == assembly.Filename))
                {
                    return(true);
                }
            }

            return(false);
        }
        public static CustomScriptAssemblyReference FromPathAndReference(string path, string reference)
        {
            var pathPrefix = path.Substring(0, path.Length - AssetPath.GetFileName(path).Length);

            var customScriptAssemblyReference = new CustomScriptAssemblyReference();

            customScriptAssemblyReference.FilePath   = path;
            customScriptAssemblyReference.PathPrefix = pathPrefix;
            customScriptAssemblyReference.Reference  = reference;

            return(customScriptAssemblyReference);
        }
        public static CustomScriptAssembly FromCustomScriptAssemblyData(string path, string guid, CustomScriptAssemblyData customScriptAssemblyData)
        {
            if (customScriptAssemblyData == null)
            {
                return(null);
            }

            var pathPrefix = path.Substring(0, path.Length - AssetPath.GetFileName(path).Length);

            var customScriptAssembly = new CustomScriptAssembly();

            customScriptAssembly.Name                  = customScriptAssemblyData.name;
            customScriptAssembly.GUID                  = guid;
            customScriptAssembly.References            = customScriptAssemblyData.references;
            customScriptAssembly.FilePath              = path;
            customScriptAssembly.PathPrefix            = pathPrefix;
            customScriptAssembly.AutoReferenced        = customScriptAssemblyData.autoReferenced;
            customScriptAssembly.OverrideReferences    = customScriptAssemblyData.overrideReferences;
            customScriptAssembly.PrecompiledReferences = customScriptAssemblyData.precompiledReferences ?? new string[0];
            customScriptAssembly.DefineConstraints     = customScriptAssemblyData.defineConstraints ?? new string[0];
            customScriptAssembly.VersionDefines        = (customScriptAssemblyData.versionDefines ?? new VersionDefine[0]);

            customScriptAssemblyData.optionalUnityReferences = customScriptAssemblyData.optionalUnityReferences ?? new string[0];
            foreach (var optionalUnityReferenceString in customScriptAssemblyData.optionalUnityReferences)
            {
                var optionalUnityReference = (OptionalUnityReferences)Enum.Parse(typeof(OptionalUnityReferences), optionalUnityReferenceString);
                customScriptAssembly.OptionalUnityReferences |= optionalUnityReference;
            }

            if (customScriptAssemblyData.includePlatforms != null && customScriptAssemblyData.includePlatforms.Length > 0)
            {
                customScriptAssembly.IncludePlatforms = GetPlatformsFromNames(customScriptAssemblyData.includePlatforms);
            }

            if (customScriptAssemblyData.excludePlatforms != null && customScriptAssemblyData.excludePlatforms.Length > 0)
            {
                customScriptAssembly.ExcludePlatforms = GetPlatformsFromNames(customScriptAssemblyData.excludePlatforms);
            }

            var compilerOptions = new ScriptCompilerOptions();

            compilerOptions.AllowUnsafeCode = customScriptAssemblyData.allowUnsafeCode;

            customScriptAssembly.CompilerOptions = compilerOptions;

            return(customScriptAssembly);
        }
        private static Dictionary <string, PrecompiledAssembly> ValidateAndGetNameToPrecompiledAssembly(PrecompiledAssembly[] precompiledAssemblies)
        {
            if (precompiledAssemblies == null)
            {
                return(new Dictionary <string, PrecompiledAssembly>(0));
            }

            Dictionary <string, PrecompiledAssembly> fileNameToUserPrecompiledAssemblies = new Dictionary <string, PrecompiledAssembly>(precompiledAssemblies.Length);

            var sameNamedPrecompiledAssemblies = new Dictionary <string, List <string> >(precompiledAssemblies.Length);

            for (int i = 0; i < precompiledAssemblies.Length; i++)
            {
                var precompiledAssembly = precompiledAssemblies[i];

                var fileName = AssetPath.GetFileName(precompiledAssembly.Path);
                if (!fileNameToUserPrecompiledAssemblies.ContainsKey(fileName))
                {
                    fileNameToUserPrecompiledAssemblies.Add(fileName, precompiledAssembly);
                }
                else
                {
                    if (!sameNamedPrecompiledAssemblies.ContainsKey(fileName))
                    {
                        sameNamedPrecompiledAssemblies.Add(fileName, new List <string>
                        {
                            fileNameToUserPrecompiledAssemblies[fileName].Path
                        });
                    }
                    sameNamedPrecompiledAssemblies[fileName].Add(precompiledAssembly.Path);
                }
            }

            foreach (var precompiledAssemblyNameToIndexes in sameNamedPrecompiledAssemblies)
            {
                string paths = string.Empty;
                foreach (var precompiledPath in precompiledAssemblyNameToIndexes.Value)
                {
                    paths += $"{Environment.NewLine}{precompiledPath}";
                }

                throw new PrecompiledAssemblyException(
                          $"Multiple precompiled assemblies with the same name {precompiledAssemblyNameToIndexes.Key} included or the current platform. Only one assembly with the same name is allowed per platform. Assembly paths: {paths}", paths);
            }

            return(fileNameToUserPrecompiledAssemblies);
        }
        }                                     // Name or GUID

        public static CustomScriptAssemblyReference FromCustomScriptAssemblyReferenceData(string path, CustomScriptAssemblyReferenceData customScriptAssemblyReferenceData)
        {
            if (customScriptAssemblyReferenceData == null)
            {
                return(null);
            }

            var pathPrefix = path.Substring(0, path.Length - AssetPath.GetFileName(path).Length);

            var customScriptAssemblyReference = new CustomScriptAssemblyReference();

            customScriptAssemblyReference.FilePath   = path;
            customScriptAssemblyReference.PathPrefix = pathPrefix;
            customScriptAssemblyReference.Reference  = customScriptAssemblyReferenceData.reference;

            return(customScriptAssemblyReference);
        }
        public static CustomScriptAssembly FromCustomScriptAssemblyData(string path, CustomScriptAssemblyData customScriptAssemblyData)
        {
            CustomScriptAssembly result;

            if (customScriptAssemblyData == null)
            {
                result = null;
            }
            else
            {
                string pathPrefix = path.Substring(0, path.Length - AssetPath.GetFileName(path).Length);
                CustomScriptAssembly customScriptAssembly = new CustomScriptAssembly();
                customScriptAssembly.Name       = customScriptAssemblyData.name;
                customScriptAssembly.References = customScriptAssemblyData.references;
                customScriptAssembly.FilePath   = path;
                customScriptAssembly.PathPrefix = pathPrefix;
                customScriptAssemblyData.optionalUnityReferences = (customScriptAssemblyData.optionalUnityReferences ?? new string[0]);
                string[] optionalUnityReferences = customScriptAssemblyData.optionalUnityReferences;
                for (int i = 0; i < optionalUnityReferences.Length; i++)
                {
                    string value = optionalUnityReferences[i];
                    OptionalUnityReferences optionalUnityReferences2 = (OptionalUnityReferences)Enum.Parse(typeof(OptionalUnityReferences), value);
                    customScriptAssembly.OptionalUnityReferences |= optionalUnityReferences2;
                }
                if (customScriptAssemblyData.includePlatforms != null && customScriptAssemblyData.includePlatforms.Length > 0)
                {
                    customScriptAssembly.IncludePlatforms = (from name in customScriptAssemblyData.includePlatforms
                                                             select CustomScriptAssembly.GetPlatformFromName(name)).ToArray <CustomScriptAssemblyPlatform>();
                }
                if (customScriptAssemblyData.excludePlatforms != null && customScriptAssemblyData.excludePlatforms.Length > 0)
                {
                    customScriptAssembly.ExcludePlatforms = (from name in customScriptAssemblyData.excludePlatforms
                                                             select CustomScriptAssembly.GetPlatformFromName(name)).ToArray <CustomScriptAssemblyPlatform>();
                }
                result = customScriptAssembly;
            }
            return(result);
        }
        public static void UpdateCodeGenScriptAssembly(ref ScriptAssembly scriptAssembly)
        {
            scriptAssembly.ScriptAssemblyReferences = new ScriptAssembly[0];

            int newReferenceCount = 0;
            var references        = new string[scriptAssembly.References.Length];

            foreach (var reference in scriptAssembly.References)
            {
                var name = AssetPath.GetFileName(reference);
                if (!Utility.FastStartsWith(name, k_UnityEngineModules, k_UnityEngineModulesLower) &&
                    !Utility.FastStartsWith(name, k_UnityEditorModules, k_UnityEditorModulesLower))
                {
                    references[newReferenceCount] = reference;
                    newReferenceCount++;
                }
            }
            var result = new string[newReferenceCount + 1];

            Array.Copy(references, result, newReferenceCount);
            result[newReferenceCount] = AssetPath.Combine(EditorApplication.applicationContentsPath, "Managed", "Unity.CompilationPipeline.Common.dll");
            scriptAssembly.References = result;
        }
示例#10
0
        public static Dictionary <string, string> GetUnityReferences(ScriptAssembly scriptAssembly, TargetAssembly targetAssembly, PrecompiledAssembly[] unityAssemblies, HashSet <string> predefinedCustomTargetReferences, EditorScriptCompilationOptions options, UnityReferencesOptions unityReferencesOptions)
        {
            var references = new Dictionary <string, string>();

            bool assemblyEditorOnly        = (scriptAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly;
            bool buildingForEditor         = (options & EditorScriptCompilationOptions.BuildingForEditor) == EditorScriptCompilationOptions.BuildingForEditor;
            bool excludeUnityModules       = unityReferencesOptions == UnityReferencesOptions.ExcludeModules;
            bool isOverridingUnityAssembly = false;

            // Add Unity assemblies (UnityEngine.dll, UnityEditor.dll) referencees.
            if (unityAssemblies == null)
            {
                return(references);
            }

            foreach (var unityAssembly in unityAssemblies)
            {
                if ((unityAssembly.Flags & (AssemblyFlags.UserOverride | AssemblyFlags.UserOverrideCandidate)) != AssemblyFlags.None)
                {
                    var unityAssemblyFileName = AssetPath.GetFileName(unityAssembly.Path);

                    // This scriptAssembly is overriding this unityAssembly so it should probably not depend on itself.
                    if (unityAssemblyFileName == scriptAssembly.Filename)
                    {
                        isOverridingUnityAssembly = true;
                        continue;
                    }

                    // Custom targets may override Unity references, do not add them to avoid duplicated references.
                    if (predefinedCustomTargetReferences != null && predefinedCustomTargetReferences.Contains(unityAssemblyFileName))
                    {
                        continue;
                    }

                    // If this scriptAssembly/targetAssembly explicitly references another
                    // scriptAssembly that has actually overridden this unityAssembly, we should
                    // not add the unityAssembly to the references as well. It's possible
                    // that this scriptAssembly is using new APIs that don't exist in the shipped
                    // copy of the unityAssembly.
                    if (targetAssembly != null && targetAssembly.References.Any(ta => ta.Filename == unityAssemblyFileName))
                    {
                        continue;
                    }
                }

                var isUnityModule = (unityAssembly.Flags & AssemblyFlags.UnityModule) == AssemblyFlags.UnityModule;

                if (isUnityModule && excludeUnityModules)
                {
                    continue;
                }

                var moduleExcludedForRuntimeCode = (unityAssembly.Flags & AssemblyFlags.ExcludedForRuntimeCode) == AssemblyFlags.ExcludedForRuntimeCode;

                // Add Unity editor assemblies (UnityEditor.dll) to all assemblies when building inside the editor
                if ((buildingForEditor && !moduleExcludedForRuntimeCode) || assemblyEditorOnly)
                {
                    if ((unityAssembly.Flags & AssemblyFlags.UseForMono) != 0)
                    {
                        references[Path.GetFileName(unityAssembly.Path)] = unityAssembly.Path;
                    }
                }
                else
                {
                    bool unityAssemblyEditorOnly = (unityAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly;

                    // Add Unity runtime assemblies (UnityEngine.dll) to all assemblies
                    if (!unityAssemblyEditorOnly && !moduleExcludedForRuntimeCode)
                    {
                        if (IsPrecompiledAssemblyCompatibleWithBuildTarget(unityAssembly, scriptAssembly.BuildTarget))
                        {
                            references[Path.GetFileName(unityAssembly.Path)] = unityAssembly.Path;
                        }
                    }
                }
            }

            // UserOverride assemblies should not have a dependency on Editor assemblies.
            if (isOverridingUnityAssembly && !assemblyEditorOnly)
            {
                references = references.Where(kvp => !kvp.Key.Contains("UnityEditor")).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            }

            return(references);
        }
示例#11
0
 public static bool IsPrecompiledAssemblyNunit(ref PrecompiledAssembly precompiledAssembly)
 {
     return(AssetPath.GetFileName(precompiledAssembly.Path) == k_NunitAssemblyName);
 }
示例#12
0
 public static bool ShouldAddNunitReferences(EditorBuildRules.TargetAssembly targetAssembly)
 {
     return(!targetAssembly.PrecompiledReferences.Any(x => AssetPath.GetFileName(x.Path) == k_NunitAssemblyName) &&
            (PlayerSettings.playModeTestRunnerEnabled || (targetAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly));
 }