Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [2.0.1] - 2020-03-19

When Visual Studio installation is compatible with C# 8.0, setup the language version to not prompt the user with unsupported constructs. (So far Unity only supports C# 7.3).
Use Unity's TypeCache to improve project generation speed.
Properly check for a managed assembly before displaying a warning regarding legacy PDB usage.
Add support for selective project generation (embedded, local, registry, git, builtin, player).
  • Loading branch information
Unity Technologies committed Mar 19, 2020
1 parent fb11542 commit ad189f9
Show file tree
Hide file tree
Showing 19 changed files with 639 additions and 322 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Code Editor Package for Visual Studio

## [2.0.1] - 2020-03-19

When Visual Studio installation is compatible with C# 8.0, setup the language version to not prompt the user with unsupported constructs. (So far Unity only supports C# 7.3).
Use Unity's TypeCache to improve project generation speed.
Properly check for a managed assembly before displaying a warning regarding legacy PDB usage.
Add support for selective project generation (embedded, local, registry, git, builtin, player).


## [2.0.0] - 2019-11-06

- Improved Visual Studio and Visual Studio for Mac automatic discovery
Expand Down
33 changes: 2 additions & 31 deletions Editor/FileUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static string Normalize(string path)

if (Path.DirectorySeparatorChar == WinSeparator)
path = path.Replace(UnixSeparator, WinSeparator);
if (Path.DirectorySeparatorChar == UnixSeparator)
path = path.Replace(WinSeparator, UnixSeparator);

return path.Replace(string.Concat(WinSeparator, WinSeparator), WinSeparator.ToString());
}
Expand All @@ -57,36 +59,5 @@ internal static bool IsFileInProjectDirectory(string fileName)

return string.Equals(Path.GetDirectoryName(fileName), basePath, StringComparison.OrdinalIgnoreCase);
}

public static string FileNameWithoutExtension(string path)
{
if (string.IsNullOrEmpty(path))
{
return "";
}

var indexOfDot = -1;
var indexOfSlash = 0;
for (var i = path.Length - 1; i >= 0; i--)
{
if (indexOfDot == -1 && path[i] == '.')
{
indexOfDot = i;
}

if (indexOfSlash == 0 && path[i] == '/' || path[i] == '\\')
{
indexOfSlash = i + 1;
break;
}
}

if (indexOfDot == -1)
{
indexOfDot = path.Length - 1;
}

return path.Substring(indexOfSlash, indexOfDot - indexOfSlash);
}
}
}
101 changes: 101 additions & 0 deletions Editor/Image.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
using System;
using System.IO;

namespace Microsoft.Unity.VisualStudio.Editor
{
public sealed class Image : IDisposable {

long position;
Stream stream;

Image (Stream stream)
{
this.stream = stream;
this.position = stream.Position;
this.stream.Position = 0;
}

bool Advance (int length)
{
if (stream.Position + length >= stream.Length)
return false;

stream.Seek (length, SeekOrigin.Current);
return true;
}

bool MoveTo (uint position)
{
if (position >= stream.Length)
return false;

stream.Position = position;
return true;
}

void IDisposable.Dispose ()
{
stream.Position = position;
}

ushort ReadUInt16 ()
{
return (ushort) (stream.ReadByte ()
| (stream.ReadByte () << 8));
}

uint ReadUInt32 ()
{
return (uint) (stream.ReadByte ()
| (stream.ReadByte () << 8)
| (stream.ReadByte () << 16)
| (stream.ReadByte () << 24));
}

bool IsManagedAssembly ()
{
if (stream.Length < 318)
return false;
if (ReadUInt16 () != 0x5a4d)
return false;
if (!Advance (58))
return false;
if (!MoveTo (ReadUInt32 ()))
return false;
if (ReadUInt32 () != 0x00004550)
return false;
if (!Advance (20))
return false;
if (!Advance (ReadUInt16 () == 0x20b ? 222 : 206))
return false;

return ReadUInt32 () != 0;
}

public static bool IsAssembly (string file)
{
if (file == null)
throw new ArgumentNullException ("file");

using (var stream = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.Read))
return IsAssembly (stream);
}

public static bool IsAssembly (Stream stream)
{
if (stream == null)
throw new ArgumentNullException (nameof(stream));
if (!stream.CanRead)
throw new ArgumentException (nameof(stream));
if (!stream.CanSeek)
throw new ArgumentException (nameof(stream));

using (var image = new Image (stream))
return image.IsManagedAssembly ();
}
}
}
11 changes: 11 additions & 0 deletions Editor/Image.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/ProjectGeneration.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 170 additions & 0 deletions Editor/ProjectGeneration/AssemblyNameProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEditor.PackageManager;

namespace Microsoft.Unity.VisualStudio.Editor
{
public interface IAssemblyNameProvider
{
string[] ProjectSupportedExtensions { get; }
string ProjectGenerationRootNamespace { get; }
ProjectGenerationFlag ProjectGenerationFlag { get; }

string GetAssemblyNameFromScriptPath(string path);
string GetAssemblyName(string assemblyOutputPath, string assemblyName);
bool IsInternalizedPackagePath(string path);
IEnumerable<Assembly> GetAssemblies(Func<string, bool> shouldFileBePartOfSolution);
IEnumerable<string> GetAllAssetPaths();
UnityEditor.PackageManager.PackageInfo FindForAssetPath(string assetPath);
ResponseFileData ParseResponseFile(string responseFilePath, string projectDirectory, string[] systemReferenceDirectories);
void ToggleProjectGeneration(ProjectGenerationFlag preference);
}

public class AssemblyNameProvider : IAssemblyNameProvider
{
ProjectGenerationFlag m_ProjectGenerationFlag = (ProjectGenerationFlag)EditorPrefs.GetInt("unity_project_generation_flag", 0);

public string[] ProjectSupportedExtensions => EditorSettings.projectGenerationUserExtensions;

public string ProjectGenerationRootNamespace => EditorSettings.projectGenerationRootNamespace;

public ProjectGenerationFlag ProjectGenerationFlag
{
get => m_ProjectGenerationFlag;
private set
{
EditorPrefs.SetInt("unity_project_generation_flag", (int)value);
m_ProjectGenerationFlag = value;
}
}

public string GetAssemblyNameFromScriptPath(string path)
{
return CompilationPipeline.GetAssemblyNameFromScriptPath(path);
}

public IEnumerable<Assembly> GetAssemblies(Func<string, bool> shouldFileBePartOfSolution)
{
foreach (var assembly in CompilationPipeline.GetAssemblies())
{
if (assembly.sourceFiles.Any(shouldFileBePartOfSolution))
{
yield return new Assembly(assembly.name, @"Temp\Bin\Debug\", assembly.sourceFiles, new[] { "DEBUG", "TRACE" }.Concat(assembly.defines).Concat(EditorUserBuildSettings.activeScriptCompilationDefines).ToArray(), assembly.assemblyReferences, assembly.compiledAssemblyReferences, assembly.flags)
{
compilerOptions =
{
ResponseFiles = assembly.compilerOptions.ResponseFiles,
AllowUnsafeCode = assembly.compilerOptions.AllowUnsafeCode,
ApiCompatibilityLevel = assembly.compilerOptions.ApiCompatibilityLevel
}
};
}
}

if (ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.PlayerAssemblies))
{
foreach (var assembly in CompilationPipeline.GetAssemblies(AssembliesType.Player).Where(assembly => assembly.sourceFiles.Any(shouldFileBePartOfSolution)))
{
yield return new Assembly(assembly.name, @"Temp\Bin\Debug\Player\", assembly.sourceFiles, new[] { "DEBUG", "TRACE" }.Concat(assembly.defines).ToArray(), assembly.assemblyReferences, assembly.compiledAssemblyReferences, assembly.flags)
{
compilerOptions =
{
ResponseFiles = assembly.compilerOptions.ResponseFiles,
AllowUnsafeCode = assembly.compilerOptions.AllowUnsafeCode,
ApiCompatibilityLevel = assembly.compilerOptions.ApiCompatibilityLevel
}
};
}
}
}

public string GetCompileOutputPath(string assemblyName)
{
if (assemblyName.EndsWith(".Player", StringComparison.Ordinal))
{
return @"Temp\Bin\Debug\Player\";
}
else
{
return @"Temp\Bin\Debug\";
}
}

public IEnumerable<string> GetAllAssetPaths()
{
return AssetDatabase.GetAllAssetPaths();
}

public UnityEditor.PackageManager.PackageInfo FindForAssetPath(string assetPath)
{
return UnityEditor.PackageManager.PackageInfo.FindForAssetPath(assetPath);
}

public bool IsInternalizedPackagePath(string path)
{
if (string.IsNullOrEmpty(path.Trim()))
{
return false;
}
var packageInfo = FindForAssetPath(path);
if (packageInfo == null)
{
return false;
}
var packageSource = packageInfo.source;
switch (packageSource)
{
case PackageSource.Embedded:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Embedded);
case PackageSource.Registry:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Registry);
case PackageSource.BuiltIn:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.BuiltIn);
case PackageSource.Unknown:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Unknown);
case PackageSource.Local:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Local);
case PackageSource.Git:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Git);
case PackageSource.LocalTarball:
return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.LocalTarBall);
}

return false;
}

public ResponseFileData ParseResponseFile(string responseFilePath, string projectDirectory, string[] systemReferenceDirectories)
{
return CompilationPipeline.ParseResponseFile(
responseFilePath,
projectDirectory,
systemReferenceDirectories
);
}

public void ToggleProjectGeneration(ProjectGenerationFlag preference)
{
if (ProjectGenerationFlag.HasFlag(preference))
{
ProjectGenerationFlag ^= preference;
}
else
{
ProjectGenerationFlag |= preference;
}
}

public void ResetProjectGenerationFlag()
{
ProjectGenerationFlag = ProjectGenerationFlag.None;
}

public string GetAssemblyName(string assemblyOutputPath, string assemblyName)
{
return assemblyOutputPath.EndsWith(@"\Player\", StringComparison.Ordinal) ? assemblyName + ".Player" : assemblyName;
}
}
}
11 changes: 11 additions & 0 deletions Editor/ProjectGeneration/AssemblyNameProvider.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Editor/ProjectGeneration/FileIOProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.IO;
using System.Text;

namespace Microsoft.Unity.VisualStudio.Editor
{
public interface IFileIO
{
bool Exists(string fileName);

string ReadAllText(string fileName);
void WriteAllText(string fileName, string content);
}

class FileIOProvider : IFileIO
{
public bool Exists(string fileName)
{
return File.Exists(fileName);
}

public string ReadAllText(string fileName)
{
return File.ReadAllText(fileName);
}

public void WriteAllText(string fileName, string content)
{
File.WriteAllText(fileName, content, Encoding.UTF8);
}
}
}
Loading

0 comments on commit ad189f9

Please sign in to comment.