static CustomScriptAssemblyReference LoadCustomScriptAssemblyReferenceFromJson(string path, string json)
 {
     try
     {
         var customScriptAssemblyRefData = CustomScriptAssemblyReferenceData.FromJson(json);
         return(CustomScriptAssemblyReference.FromCustomScriptAssemblyReferenceData(path, customScriptAssemblyRefData));
     }
     catch (Exception e)
     {
         throw new AssemblyDefinitionException(e.Message, path);
     }
 }
        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);
        }
Example #3
0
        public AssemblyGraphBuilder(string projectPath)
        {
            _projectPath = projectPath;
            _globalAssemblyDefinition = new CustomScriptAssembly
            {
                Name         = "Assembly-CSharp",
                PathPrefix   = projectPath,
                FilePath     = Path.Combine(projectPath, "main.asmdef"),
                GUID         = CreateNewUnityGuid(),
                IsPredefined = true,
            };

            _editorAssebmlyDefinition = new CustomScriptAssembly
            {
                Name         = "Assembly-CSharp-Editor",
                PathPrefix   = Path.Combine(projectPath, "Editor"),
                FilePath     = Path.Combine(projectPath, "Editor/main-editor.asmdef"),
                GUID         = CreateNewUnityGuid(),
                IsPredefined = true,
            };

            _globalFirstpassAssemblyDefinition = new CustomScriptAssembly
            {
                Name         = "Assembly-CSharp-firstpass",
                PathPrefix   = Path.Combine(projectPath, "Plugins"),
                FilePath     = Path.Combine(projectPath, "Plugins/firstpass.asmdef"),
                GUID         = CreateNewUnityGuid(),
                IsPredefined = true,
            };

            _editorFirstpassAssemblyDefinition = new CustomScriptAssembly
            {
                Name         = "Assembly-CSharp-Editor-firstpass",
                PathPrefix   = Path.Combine(projectPath, "Plugins/Editor"),
                FilePath     = Path.Combine(projectPath, "Plugins/Editor/firstpass-editor.asmdef"),
                GUID         = CreateNewUnityGuid(),
                IsPredefined = true,
            };

            _globalFirstpassAssemblyReferences = new[]
            {
                CustomScriptAssemblyReference.FromPathAndReference(
                    Path.Combine(projectPath, "standard assets/standard assets.asmref"),
                    _globalFirstpassAssemblyDefinition.Name),
                CustomScriptAssemblyReference.FromPathAndReference(
                    Path.Combine(projectPath, "pro standard assets/pro standard assets.asmref"),
                    _globalFirstpassAssemblyDefinition.Name),
                CustomScriptAssemblyReference.FromPathAndReference(
                    Path.Combine(projectPath, "iphone standard assets/iphone standard assets.asmref"),
                    _globalFirstpassAssemblyDefinition.Name),
            };
        }
        }                                     // 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 void SetAllCustomScriptAssemblyReferenceJsonsContents(string[] paths, string[] contents)
        {
            var assemblyRefs = new List <CustomScriptAssemblyReference>();
            var exceptions   = new List <Exception>();

            // We only construct this lookup if it is required, which is when we are using guids instead of assembly names.
            Dictionary <string, CustomScriptAssembly> guidsToAssemblies = null;

            // To check if a path prefix is already being used we use a Dictionary where the key is the prefix and the value is the file path.
            var prefixToFilePathLookup = m_SkipCustomScriptAssemblyGraphValidation ?
                                         null :
                                         CustomScriptAssemblies.GroupBy(x => x.PathPrefix).ToDictionary(x => x.First().PathPrefix, x => new List <string>()
            {
                x.First().FilePath
            }, StringComparer.OrdinalIgnoreCase);

            for (var i = 0; i < paths.Length; ++i)
            {
                var path = paths[i];

                CustomScriptAssemblyReference loadedCustomScriptAssemblyReference = null;

                try
                {
                    var fullPath = AssetPath.IsPathRooted(path) ? AssetPath.GetFullPath(path) : AssetPath.Combine(m_ProjectDirectory, path);

                    if (contents != null)
                    {
                        var jsonContents = contents[i];
                        loadedCustomScriptAssemblyReference = LoadCustomScriptAssemblyReferenceFromJson(fullPath, jsonContents);
                    }
                    else
                    {
                        loadedCustomScriptAssemblyReference = LoadCustomScriptAssemblyReferenceFromJsonPath(fullPath);
                    }

                    if (!m_SkipCustomScriptAssemblyGraphValidation)
                    {
                        // Check both asmdef and asmref files.
                        if (prefixToFilePathLookup.TryGetValue(loadedCustomScriptAssemblyReference.PathPrefix, out var duplicateFilePaths))
                        {
                            var filePaths = new List <string> {
                                loadedCustomScriptAssemblyReference.FilePath
                            };
                            filePaths.AddRange(duplicateFilePaths);

                            throw new AssemblyDefinitionException(
                                      $"Folder '{loadedCustomScriptAssemblyReference.PathPrefix}' contains multiple assembly definition files", filePaths.ToArray());
                        }
                    }

                    // Convert GUID references to assembly names
                    if (GUIDReference.IsGUIDReference(loadedCustomScriptAssemblyReference.Reference))
                    {
                        // Generate the guid to assembly lookup?
                        guidsToAssemblies = guidsToAssemblies ?? CustomScriptAssemblies.ToDictionary(x => x.GUID);

                        var guid = Utility.FastToLower(GUIDReference.GUIDReferenceToGUID(loadedCustomScriptAssemblyReference.Reference));
                        if (guidsToAssemblies.TryGetValue(guid, out var foundAssembly))
                        {
                            loadedCustomScriptAssemblyReference.Reference = foundAssembly.Name;
                        }
                    }
                }
                catch (Exception e)
                {
                    m_CompilationSetupErrorsTracker.SetCompilationSetupErrors(CompilationSetupErrors.LoadError);
                    exceptions.Add(e);
                }

                if (loadedCustomScriptAssemblyReference != null)
                {
                    assemblyRefs.Add(loadedCustomScriptAssemblyReference);

                    if (m_SkipCustomScriptAssemblyGraphValidation)
                    {
                        continue;
                    }

                    if (!prefixToFilePathLookup.TryGetValue(loadedCustomScriptAssemblyReference.PathPrefix, out var duplicateFilePaths))
                    {
                        duplicateFilePaths = new List <string>();
                        prefixToFilePathLookup[loadedCustomScriptAssemblyReference.PathPrefix] = duplicateFilePaths;
                    }

                    duplicateFilePaths.Add(loadedCustomScriptAssemblyReference.FilePath);
                }
            }

            CustomScriptAssemblyReferences = assemblyRefs;
            Exceptions = exceptions.ToArray();
        }
 public bool Equals(CustomScriptAssemblyReference other)
 {
     return(string.Equals(FilePath, other.FilePath, StringComparison.Ordinal) &&
            string.Equals(PathPrefix, other.PathPrefix, StringComparison.Ordinal) &&
            string.Equals(Reference, other.Reference, StringComparison.Ordinal));
 }