Ejemplo n.º 1
0
 void SyncProject(MonoIsland island,
                  Dictionary <string, string> allAssetsProjectParts,
                  ScriptCompilerBase.ResponseFileData responseFileData,
                  List <MonoIsland> allProjectIslands)
 {
     SyncProjectFileIfNotChanged(ProjectFile(island), ProjectText(island, ModeForCurrentExternalEditor(), allAssetsProjectParts, responseFileData, allProjectIslands));
 }
        private string ProjectHeader(MonoIsland island,
                                     ScriptCompilerBase.ResponseFileData responseFileData)
        {
            string            targetframeworkversion = "v3.5";
            string            targetLanguageVersion  = "4";
            string            toolsversion           = "4.0";
            string            productversion         = "10.0.20506";
            string            baseDirectory          = ".";
            ScriptingLanguage language = ScriptingLanguageFor(island);

            if (PlayerSettingsEditor.IsLatestApiCompatibility(island._api_compatibility_level))
            {
                if (ScriptEditorUtility.GetScriptEditorFromPreferences() == ScriptEditorUtility.ScriptEditor.Rider ||
                    ScriptEditorUtility.GetScriptEditorFromPreferences() == ScriptEditorUtility.ScriptEditor.VisualStudioCode)
                {
                    targetframeworkversion = "v4.5";
                }
                else
                {
                    targetframeworkversion = "v4.7.1";
                }
                targetLanguageVersion = "7.2";
            }
            else if (_settings.VisualStudioVersion == 9)
            {
                toolsversion   = "3.5";
                productversion = "9.0.21022";
            }

            var arguments = new object[]
            {
                toolsversion, productversion, ProjectGuid(island._output),
                _settings.EngineAssemblyPath,
                _settings.EditorAssemblyPath,
                string.Join(";", new[] { "DEBUG", "TRACE" }.Concat(island._defines).Concat(responseFileData.Defines).Distinct().ToArray()),
                MSBuildNamespaceUri,
                Path.GetFileNameWithoutExtension(island._output),
                EditorSettings.projectGenerationRootNamespace,
                targetframeworkversion,
                targetLanguageVersion,
                baseDirectory,
                island._allowUnsafeCode | responseFileData.Unsafe
            };

            try
            {
                return(string.Format(_settings.GetProjectHeaderTemplate(language), arguments));
            }
            catch (Exception)
            {
                throw new System.NotSupportedException("Failed creating c# project because the c# project header did not have the correct amount of arguments, which is " + arguments.Length);
            }
        }
        ScriptCompilerBase.ResponseFileData parseResponseFileData(MonoIsland island, string responseFilePath)
        {
            var systemReferenceDirectories = CSharpLanguage.GetCSharpCompiler(island._target, true, "Assembly-CSharp") == CSharpCompiler.Microsoft &&
                                             PlayerSettings.GetScriptingBackend(BuildPipeline.GetBuildTargetGroup(island._target)) == ScriptingImplementation.WinRTDotNET
                ? MicrosoftCSharpCompiler.GetClassLibraries(island._target)
                : MonoLibraryHelpers.GetSystemReferenceDirectories(island._api_compatibility_level);

            ScriptCompilerBase.ResponseFileData responseFileData = ScriptCompilerBase.ParseResponseFileFromFile(
                Path.Combine(_projectDirectory, responseFilePath),
                _projectDirectory,
                systemReferenceDirectories);

            if (responseFileData.Errors.Length > 0)
            {
                foreach (var error in responseFileData.Errors)
                {
                    UnityEngine.Debug.LogErrorFormat("{0} Parse Error : {1}", responseFilePath, error);
                }
            }

            return(responseFileData);
        }
Ejemplo n.º 4
0
        string ProjectText(MonoIsland island,
                           Mode mode,
                           Dictionary <string, string> allAssetsProjectParts,
                           ScriptCompilerBase.ResponseFileData responseFileData,
                           List <MonoIsland> allProjectIslands)
        {
            var   projectBuilder    = new StringBuilder(ProjectHeader(island, responseFileData));
            var   references        = new List <string>();
            var   projectReferences = new List <Match>();
            Match match;
            bool  isBuildingEditorProject = island._output.EndsWith("-Editor.dll");

            foreach (string file in island._files)
            {
                if (!ShouldFileBePartOfSolution(file))
                {
                    continue;
                }

                var extension = Path.GetExtension(file).ToLower();
                var fullFile  = EscapedRelativePathFor(file);
                if (".dll" != extension)
                {
                    var tagName = "Compile";
                    projectBuilder.AppendFormat("     <{0} Include=\"{1}\" />{2}", tagName, fullFile, WindowsNewline);
                }
                else
                {
                    references.Add(fullFile);
                }
            }

            string additionalAssetsForProject;
            var    assemblyName = Path.GetFileNameWithoutExtension(island._output);

            // Append additional non-script files that should be included in project generation.
            if (allAssetsProjectParts.TryGetValue(assemblyName, out additionalAssetsForProject))
            {
                projectBuilder.Append(additionalAssetsForProject);
            }

            var allAdditionalReferenceFilenames = new List <string>();

            foreach (string reference in references.Union(island._references).Union(responseFileData.References.Select(r => r.Assembly)))
            {
                if (reference.EndsWith("/UnityEditor.dll") || reference.EndsWith("/UnityEngine.dll") || reference.EndsWith("\\UnityEditor.dll") || reference.EndsWith("\\UnityEngine.dll"))
                {
                    continue;
                }

                match = scriptReferenceExpression.Match(reference);
                if (match.Success)
                {
                    var language       = ScriptCompilers.GetLanguageFromExtension(island.GetExtensionOfSourceFiles());
                    var targetLanguage = (ScriptingLanguage)Enum.Parse(typeof(ScriptingLanguage), language.GetLanguageName(), true);
                    if (mode == Mode.UnityScriptAsUnityProj || ScriptingLanguage.CSharp == targetLanguage)
                    {
                        // Add a reference to a project except if it's a reference to a script assembly
                        // that we are not generating a project for. This will be the case for assemblies
                        // coming from .assembly.json files in non-internalized packages.
                        var dllName = match.Groups["dllname"].Value;
                        if (allProjectIslands.Any(i => Path.GetFileName(i._output) == dllName))
                        {
                            projectReferences.Add(match);
                            continue;
                        }
                    }
                }

                string fullReference = Path.IsPathRooted(reference) ? reference : Path.Combine(_projectDirectory, reference);
                if (!AssemblyHelper.IsManagedAssembly(fullReference))
                {
                    continue;
                }
                if (AssemblyHelper.IsInternalAssembly(fullReference))
                {
                    if (!IsAdditionalInternalAssemblyReference(isBuildingEditorProject, fullReference))
                    {
                        continue;
                    }
                    var referenceName = Path.GetFileName(fullReference);
                    if (allAdditionalReferenceFilenames.Contains(referenceName))
                    {
                        continue;
                    }
                    allAdditionalReferenceFilenames.Add(referenceName);
                }

                //replace \ with / and \\ with /
                fullReference = fullReference.Replace("\\", "/");
                fullReference = fullReference.Replace("\\\\", "/");
                projectBuilder.AppendFormat(" <Reference Include=\"{0}\">{1}", Path.GetFileNameWithoutExtension(fullReference), WindowsNewline);
                projectBuilder.AppendFormat(" <HintPath>{0}</HintPath>{1}", fullReference, WindowsNewline);
                projectBuilder.AppendFormat(" </Reference>{0}", WindowsNewline);
            }

            if (0 < projectReferences.Count)
            {
                string referencedProject;
                projectBuilder.AppendLine("  </ItemGroup>");
                projectBuilder.AppendLine("  <ItemGroup>");
                foreach (Match reference in projectReferences)
                {
                    var targetAssembly = EditorCompilationInterface.Instance.GetTargetAssemblyDetails(reference.Groups["dllname"].Value);
                    ScriptingLanguage targetLanguage = ScriptingLanguage.None;
                    if (targetAssembly != null)
                    {
                        targetLanguage = (ScriptingLanguage)Enum.Parse(typeof(ScriptingLanguage), targetAssembly.Language.GetLanguageName(), true);
                    }
                    referencedProject = reference.Groups["project"].Value;
                    projectBuilder.AppendFormat("    <ProjectReference Include=\"{0}{1}\">{2}", referencedProject,
                                                GetProjectExtension(targetLanguage), WindowsNewline);
                    projectBuilder.AppendFormat("      <Project>{{{0}}}</Project>", ProjectGuid(Path.Combine("Temp", reference.Groups["project"].Value + ".dll")), WindowsNewline);
                    projectBuilder.AppendFormat("      <Name>{0}</Name>", referencedProject, WindowsNewline);
                    projectBuilder.AppendLine("    </ProjectReference>");
                }
            }

            projectBuilder.Append(ProjectFooter(island));
            return(projectBuilder.ToString());
        }