From 14fe6b3ca11b1d4972b14e963a67a04f9e290343 Mon Sep 17 00:00:00 2001 From: Kasperki Date: Tue, 17 Oct 2017 20:31:13 +0300 Subject: [PATCH 1/5] start of version check --- .../Reporting/AnalyzerExporter.cs | 28 +++++++++++++--- .../StandardOutputAnalyzerReporter.cs | 8 ++--- .../DiagnosticDescriptors.cs | 22 +++++++++++-- .../UnityEngineAnalyzer.csproj | 1 + .../UnityEngineAnalyzer/UnityVersions.cs | 32 +++++++++++++++++++ 5 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs diff --git a/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs index 9be9ea2..e715ab9 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs @@ -1,16 +1,36 @@ -using System; +using Microsoft.CodeAnalysis; +using System; using System.IO; +using static UnityEngineAnalyzer.CLI.Reporting.DiagnosticInfo; namespace UnityEngineAnalyzer.CLI.Reporting { public abstract class AnalyzerExporter : IAnalyzerExporter { - //NOTE: Can we use using.static.DiagnosticsInfo; C#6 feature? - protected DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity; + protected DiagnosticInfoSeverity MinimalSeverity; + private readonly UnityVersion unityVersion; - public AnalyzerExporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) + public AnalyzerExporter(DiagnosticInfoSeverity MinimalSeverity, UnityVersion unityVersion) { this.MinimalSeverity = MinimalSeverity; + this.unityVersion = unityVersion; + } + + public bool AbleToAnalyzer(DiagnosticInfoSeverity currentSeverity, DiagnosticDescriptor diagnosticInfo, UnityVersion unityVersion = UnityVersion.ALL) + { + if (MinimalSeverity < currentSeverity) + { + return false; + } + + var off = DiagnosticDescriptors.GetVersion(diagnosticInfo); + + if (unityVersion < off.Item1 || unityVersion > off.Item2) + { + return false; + } + + return true; } public abstract void AppendDiagnostic(DiagnosticInfo diagnosticInfo); diff --git a/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs b/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs index be177d1..738ca68 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using static UnityEngineAnalyzer.CLI.Reporting.DiagnosticInfo; namespace UnityEngineAnalyzer.CLI.Reporting { @@ -8,13 +9,13 @@ public class StandardOutputAnalyzerReporter : AnalyzerExporter protected const string ConsoleSeparator = "\t"; protected const string FailurePrefix = "# "; - public StandardOutputAnalyzerReporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) : base(MinimalSeverity) + public StandardOutputAnalyzerReporter(DiagnosticInfoSeverity MinimalSeverity, UnityVersion unityVersion) : base(MinimalSeverity, unityVersion) { } public override void AppendDiagnostic(DiagnosticInfo diagnosticInfo) { - if (diagnosticInfo.Severity < MinimalSeverity) + if (AbleToAnalyzer(MinimalSeverity, diagnosticInfo) == false) { return; } @@ -30,10 +31,9 @@ public override void AppendDiagnostic(DiagnosticInfo diagnosticInfo) Console.Write(diagnosticInfo.Message); Console.ResetColor(); Console.WriteLine(@"{0}{1}{0}{2},{3}", ConsoleSeparator,diagnosticInfo.FileName, diagnosticInfo.LineNumber, diagnosticInfo.CharacterPosition); - } - private ConsoleColor ConsoleColorFromSeverity(DiagnosticInfo.DiagnosticInfoSeverity severity) + private ConsoleColor ConsoleColorFromSeverity(DiagnosticInfoSeverity severity) { switch (severity) { diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs index d4fa7b5..a5a34db 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs @@ -1,4 +1,6 @@ using Microsoft.CodeAnalysis; +using System; +using System.Linq; using System.Resources; using UnityEngineAnalyzer.Animator; using UnityEngineAnalyzer.AOT; @@ -16,7 +18,7 @@ namespace UnityEngineAnalyzer { - static class DiagnosticDescriptors + public static class DiagnosticDescriptors { public static readonly DiagnosticDescriptor DoNotUseOnGUI; public static readonly DiagnosticDescriptor DoNotUseStringMethods; @@ -57,7 +59,7 @@ static DiagnosticDescriptors() CameraMainIsSlow = CreateDiagnosticDescriptor(DiagnosticIDs.CameraMainIsSlow, DiagnosticCategories.GC, DiagnosticSeverity.Warning); } - private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, string category, DiagnosticSeverity severity, bool isEnabledByDefault = true) + private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, string category, DiagnosticSeverity severity, UnityVersion latest = UnityVersion.ALL, bool isEnabledByDefault = true) { var resourceManager = new ResourceManager(typeof(T)); @@ -68,7 +70,23 @@ private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, str category: category, defaultSeverity: severity, isEnabledByDefault: isEnabledByDefault, + customTags: CreateUnityVersionInfo(latest, latest), description: new LocalizableResourceString("Description", resourceManager, typeof(T))); } + + private static string[] CreateUnityVersionInfo(UnityVersion start, UnityVersion end) + { + return new string[] { Enum.GetName(typeof(UnityVersion), start), Enum.GetName(typeof(UnityVersion), end) }; + } + + //TODO USE: (UnityVersion start, UnityVersion end) Tuple when updated to Net Standard 2.0 + public static Tuple GetVersion(DiagnosticDescriptor dc) + { + var list = dc.CustomTags.ToList(); + var start = (UnityVersion)Enum.Parse(typeof(UnityVersion), list[0]); + var end = (UnityVersion)Enum.Parse(typeof(UnityVersion), list[1]); + + return Tuple.Create(start, end); + } } } diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityEngineAnalyzer.csproj b/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityEngineAnalyzer.csproj index 79b219b..4295573 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityEngineAnalyzer.csproj +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityEngineAnalyzer.csproj @@ -134,6 +134,7 @@ True InvokeFunctionMissingResources.resx + diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs new file mode 100644 index 0000000..9c9e9d1 --- /dev/null +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs @@ -0,0 +1,32 @@ +using System; + +namespace UnityEngineAnalyzer +{ + public enum UnityVersion + { + ALL, + UNITY_1_0, + UNITY_2_0, + UNITY_3_0, + UNITY_3_5, + UNITY_4_0, + UNITY_4_1, + UNITY_4_2, + UNITY_4_3, + UNITY_4_4, + UNITY_4_5, + UNITY_4_6, + UNITY_4_7, + UNITY_5_0, + UNITY_5_1, + UNITY_5_2, + UNITY_5_3, + UNITY_5_4, + UNITY_5_5, + UNITY_5_6, + UNITY_2017_0, + UNITY_2017_1, + UNITY_2017_2, + UNITY_2017_3, + } +} \ No newline at end of file From 86b4e1f59bac93259ecc7ad0d3b34cc71da5c591 Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Wed, 18 Oct 2017 11:58:20 +0300 Subject: [PATCH 2/5] Unity Version check working. --- UnityEngineAnalyzer.CLI/AnalyzerReport.cs | 9 ++-- UnityEngineAnalyzer.CLI/Options.cs | 11 ++-- UnityEngineAnalyzer.CLI/Program.cs | 51 +++++++++++++++++-- .../Reporting/AnalyzerExporter.cs | 23 +++------ .../Reporting/ConsoleAnalyzerExporter.cs | 8 +-- .../Reporting/DiagnosticInfo.cs | 1 + .../Reporting/IAnalyzerExporter.cs | 2 +- .../Reporting/JsonAnalyzerExporter.cs | 7 +-- .../StandardOutputAnalyzerReporter.cs | 15 +++--- .../DiagnosticDescriptors.cs | 17 ++++--- .../UnityEngineAnalyzer/DiagnosticIDs.cs | 2 - .../UnityEngineAnalyzer/UnityVersions.cs | 19 +++++-- 12 files changed, 110 insertions(+), 55 deletions(-) diff --git a/UnityEngineAnalyzer.CLI/AnalyzerReport.cs b/UnityEngineAnalyzer.CLI/AnalyzerReport.cs index 815472c..cbebc27 100644 --- a/UnityEngineAnalyzer.CLI/AnalyzerReport.cs +++ b/UnityEngineAnalyzer.CLI/AnalyzerReport.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using Microsoft.CodeAnalysis; using UnityEngineAnalyzer.CLI.Reporting; @@ -52,10 +51,10 @@ public void AppendDiagnostics(IEnumerable diagnosticResults) FileName = fileName, LineNumber = lineNumber, CharacterPosition = characterPosition, - Severity = (DiagnosticInfo.DiagnosticInfoSeverity)diagnostic.Severity + Severity = (DiagnosticInfo.DiagnosticInfoSeverity)diagnostic.Severity, + VersionSpan = DiagnosticDescriptors.GetVersion(diagnostic.Descriptor) }; - foreach (var exporter in _exporters) { exporter.AppendDiagnostic(diagnosticInfo); @@ -71,11 +70,11 @@ public void FinalizeReport(TimeSpan duration) } } - public void InitializeReport(FileInfo projectFile) + public void InitializeReport(Options options) { foreach (var exporter in _exporters) { - exporter.InitializeExporter(projectFile); + exporter.InitializeExporter(options); } } diff --git a/UnityEngineAnalyzer.CLI/Options.cs b/UnityEngineAnalyzer.CLI/Options.cs index e45d863..5892559 100644 --- a/UnityEngineAnalyzer.CLI/Options.cs +++ b/UnityEngineAnalyzer.CLI/Options.cs @@ -1,10 +1,10 @@ using CommandLine; using System.Collections.Generic; -using UnityEngineAnalyzer.CLI.Reporting; +using static UnityEngineAnalyzer.CLI.Reporting.DiagnosticInfo; namespace UnityEngineAnalyzer.CLI { - internal class Options + public class Options { [ValueOption(0)] public string ProjectFile { get; set; } @@ -15,7 +15,10 @@ internal class Options [Option('c', "configuration", HelpText = "Custom json configuration to be used.")] public string ConfigurationFile { get; set; } - [Option('s', "severity", DefaultValue = DiagnosticInfo.DiagnosticInfoSeverity.Warning, HelpText = "Minimal severity to be reported.")] - public DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity { get; set; } + [Option('s', "severity", DefaultValue = DiagnosticInfoSeverity.Warning, HelpText = "Minimal severity to be reported.")] + public DiagnosticInfoSeverity MinimalSeverity { get; set; } + + [Option('v', "version", DefaultValue = UnityVersion.NONE, HelpText = "Check against spesific Unity version.")] + public UnityVersion Version { get; set; } } } \ No newline at end of file diff --git a/UnityEngineAnalyzer.CLI/Program.cs b/UnityEngineAnalyzer.CLI/Program.cs index 800fcf8..2e836b3 100644 --- a/UnityEngineAnalyzer.CLI/Program.cs +++ b/UnityEngineAnalyzer.CLI/Program.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; using UnityEngineAnalyzer.CLI.Reporting; @@ -9,6 +11,7 @@ namespace UnityEngineAnalyzer.CLI public class Program { private static readonly Dictionary AvailableExporters = new Dictionary(); + private const UnityVersion DEFAULT_UNITY_VERSION = UnityVersion.LATEST; static Program() { @@ -29,6 +32,8 @@ public static void Main(string[] args) return; } + options.Version = DefineUnityVersion(options); + var startTime = DateTime.Now; var fileName = options.ProjectFile; @@ -52,11 +57,11 @@ public static void Main(string[] args) if (report.GetExporterCount() == 0) { //It's generally a good idea to make sure that the Console Exporter is last since it is interactive - report.AddExporter(new JsonAnalyzerExporter(options.MinimalSeverity)); - report.AddExporter(new ConsoleAnalyzerExporter(options.MinimalSeverity)); + report.AddExporter(new JsonAnalyzerExporter(options)); + report.AddExporter(new ConsoleAnalyzerExporter(options)); } - report.InitializeReport(fileInfo); + report.InitializeReport(options); var tasks = new List(); if (fileInfo.Exists) @@ -89,6 +94,46 @@ public static void Main(string[] args) } } + //TODO SET TO OWN CLASS + private static UnityVersion DefineUnityVersion(Options options) + { + if (options.Version != UnityVersion.NONE) + { + return options.Version; + } + + //THIS ONLY WORKS ON UNITY >= 5, before that ProjectVersion.txt did not exists + var projectPath = new FileInfo(options.ProjectFile).Directory; + var projectVersionFile = new FileInfo(projectPath + "/ProjectSettings/ProjectVersion.txt"); + if (projectVersionFile.Exists) + { + var projectVersionString = File.ReadAllText(projectVersionFile.FullName); + return TryParseUnityVersion(projectVersionString); + } + + return DEFAULT_UNITY_VERSION; + } + + //TODO UNIT TESTS + private static UnityVersion TryParseUnityVersion(string version) + { + string editorText = "m_EditorVersion: "; + var match = Regex.Match(version, editorText + "[0-9.a-z]*"); + + string src = match.Value.Substring(editorText.Length); + src = src.Replace('.', '_'); + src = src.Substring(0, src.IndexOf('_') + 2); + var unityVersions = Enum.GetValues(typeof(UnityVersion)).Cast(); + foreach (var unityVersion in unityVersions) + { + if (Enum.GetName(typeof(UnityVersion), unityVersion).Contains(src)) + { + return unityVersion; + } + } + + return DEFAULT_UNITY_VERSION; + } } } diff --git a/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs index e715ab9..9056dfc 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs @@ -1,31 +1,24 @@ -using Microsoft.CodeAnalysis; -using System; -using System.IO; -using static UnityEngineAnalyzer.CLI.Reporting.DiagnosticInfo; +using System; namespace UnityEngineAnalyzer.CLI.Reporting { public abstract class AnalyzerExporter : IAnalyzerExporter { - protected DiagnosticInfoSeverity MinimalSeverity; - private readonly UnityVersion unityVersion; + private readonly Options options; - public AnalyzerExporter(DiagnosticInfoSeverity MinimalSeverity, UnityVersion unityVersion) + public AnalyzerExporter(Options options) { - this.MinimalSeverity = MinimalSeverity; - this.unityVersion = unityVersion; + this.options = options; } - public bool AbleToAnalyzer(DiagnosticInfoSeverity currentSeverity, DiagnosticDescriptor diagnosticInfo, UnityVersion unityVersion = UnityVersion.ALL) + public bool IsAnalyzerRelevant(DiagnosticInfo diagnosticInfo) { - if (MinimalSeverity < currentSeverity) + if (options.MinimalSeverity > diagnosticInfo.Severity) { return false; } - var off = DiagnosticDescriptors.GetVersion(diagnosticInfo); - - if (unityVersion < off.Item1 || unityVersion > off.Item2) + if (options.Version < diagnosticInfo.VersionSpan.First || options.Version > diagnosticInfo.VersionSpan.Last) { return false; } @@ -35,7 +28,7 @@ public bool AbleToAnalyzer(DiagnosticInfoSeverity currentSeverity, DiagnosticDes public abstract void AppendDiagnostic(DiagnosticInfo diagnosticInfo); public abstract void FinalizeExporter(TimeSpan duration); - public abstract void InitializeExporter(FileInfo projectFile); + public abstract void InitializeExporter(Options options); public abstract void NotifyException(Exception exception); } } diff --git a/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs index 7705934..7af1c76 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs @@ -1,11 +1,10 @@ using System; -using System.IO; namespace UnityEngineAnalyzer.CLI.Reporting { public class ConsoleAnalyzerExporter : StandardOutputAnalyzerReporter { - public ConsoleAnalyzerExporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) : base(MinimalSeverity) + public ConsoleAnalyzerExporter(Options options) : base(options) { } @@ -19,12 +18,13 @@ public override void FinalizeExporter(TimeSpan duration) Console.ReadKey(); } - public override void InitializeExporter(FileInfo projectFile) + public override void InitializeExporter(Options options) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Unity Syntax Analyzer"); Console.WriteLine(); - Console.WriteLine("Analyzing: {0}", projectFile.FullName); + Console.WriteLine("Analyzing: {0}", options.ProjectFile); + Console.WriteLine("With Unity version: " + Enum.GetName((typeof(UnityVersion)), options.Version)); Console.WriteLine(); Console.ResetColor(); } diff --git a/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs b/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs index 88cedd0..c13d113 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs @@ -10,6 +10,7 @@ public class DiagnosticInfo public int LineNumber { get; set; } public int CharacterPosition { get; set; } public DiagnosticInfoSeverity Severity { get; set; } + public UnityVersionSpan VersionSpan { get; set; } public enum DiagnosticInfoSeverity { diff --git a/UnityEngineAnalyzer.CLI/Reporting/IAnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/IAnalyzerExporter.cs index 3aee835..cdb9bc4 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/IAnalyzerExporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/IAnalyzerExporter.cs @@ -7,7 +7,7 @@ public interface IAnalyzerExporter { void AppendDiagnostic(DiagnosticInfo diagnosticInfo); void FinalizeExporter(TimeSpan duration); - void InitializeExporter(FileInfo projectFile); + void InitializeExporter(Options options); void NotifyException(Exception exception); } } \ No newline at end of file diff --git a/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs index 0e2e4b9..520ff15 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs @@ -15,13 +15,13 @@ public class JsonAnalyzerExporter : AnalyzerExporter private readonly List _exceptions = new List(); private string _destinationReportFile; - public JsonAnalyzerExporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) : base(MinimalSeverity) + public JsonAnalyzerExporter(Options options) : base(options) { } public override void AppendDiagnostic(DiagnosticInfo diagnosticInfo) { - if (diagnosticInfo.Severity >= MinimalSeverity) + if (IsAnalyzerRelevant(diagnosticInfo)) { _jsonSerializer.Serialize(_jsonWriter, diagnosticInfo); } @@ -50,8 +50,9 @@ public override void FinalizeExporter(TimeSpan duration) //Process.Start(_destinationReportFile); } - public override void InitializeExporter(FileInfo projectFile) + public override void InitializeExporter(Options options) { + var projectFile = new FileInfo(options.ProjectFile); if (!projectFile.Exists) { throw new ArgumentException("Project file does not exist"); diff --git a/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs b/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs index 738ca68..ee1dfb5 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using static UnityEngineAnalyzer.CLI.Reporting.DiagnosticInfo; namespace UnityEngineAnalyzer.CLI.Reporting @@ -9,13 +8,13 @@ public class StandardOutputAnalyzerReporter : AnalyzerExporter protected const string ConsoleSeparator = "\t"; protected const string FailurePrefix = "# "; - public StandardOutputAnalyzerReporter(DiagnosticInfoSeverity MinimalSeverity, UnityVersion unityVersion) : base(MinimalSeverity, unityVersion) + public StandardOutputAnalyzerReporter(Options options) : base(options) { } public override void AppendDiagnostic(DiagnosticInfo diagnosticInfo) { - if (AbleToAnalyzer(MinimalSeverity, diagnosticInfo) == false) + if (IsAnalyzerRelevant(diagnosticInfo) == false) { return; } @@ -37,13 +36,13 @@ private ConsoleColor ConsoleColorFromSeverity(DiagnosticInfoSeverity severity) { switch (severity) { - case DiagnosticInfo.DiagnosticInfoSeverity.Hidden: + case DiagnosticInfoSeverity.Hidden: return ConsoleColor.Gray; - case DiagnosticInfo.DiagnosticInfoSeverity.Info: + case DiagnosticInfoSeverity.Info: return ConsoleColor.Green; - case DiagnosticInfo.DiagnosticInfoSeverity.Warning: + case DiagnosticInfoSeverity.Warning: return ConsoleColor.Yellow; - case DiagnosticInfo.DiagnosticInfoSeverity.Error: + case DiagnosticInfoSeverity.Error: return ConsoleColor.Red; default: return ConsoleColor.White; @@ -71,7 +70,7 @@ public override void FinalizeExporter(TimeSpan duration) { } - public override void InitializeExporter(FileInfo projectFile) + public override void InitializeExporter(Options options) { } } diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs index a5a34db..34b4358 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs @@ -55,11 +55,11 @@ static DiagnosticDescriptors() InvokeFunctionMissing = CreateDiagnosticDescriptor(DiagnosticIDs.InvokeFunctionMissing, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); DoNotUseStateName = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStateNameInAnimator, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); DoNotUseStringPropertyNames = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStringPropertyNamesInMaterial, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); - UseNonAllocMethods = CreateDiagnosticDescriptor(DiagnosticIDs.PhysicsUseNonAllocMethods, DiagnosticCategories.GC, DiagnosticSeverity.Warning); + UseNonAllocMethods = CreateDiagnosticDescriptor(DiagnosticIDs.PhysicsUseNonAllocMethods, DiagnosticCategories.GC, DiagnosticSeverity.Warning, UnityVersion.UNITY_5_3); CameraMainIsSlow = CreateDiagnosticDescriptor(DiagnosticIDs.CameraMainIsSlow, DiagnosticCategories.GC, DiagnosticSeverity.Warning); } - private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, string category, DiagnosticSeverity severity, UnityVersion latest = UnityVersion.ALL, bool isEnabledByDefault = true) + private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, string category, DiagnosticSeverity severity, UnityVersion first = UnityVersion.UNITY_1_0, UnityVersion latest = UnityVersion.LATEST, bool isEnabledByDefault = true) { var resourceManager = new ResourceManager(typeof(T)); @@ -70,7 +70,7 @@ private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, str category: category, defaultSeverity: severity, isEnabledByDefault: isEnabledByDefault, - customTags: CreateUnityVersionInfo(latest, latest), + customTags: CreateUnityVersionInfo(first, latest), description: new LocalizableResourceString("Description", resourceManager, typeof(T))); } @@ -79,14 +79,19 @@ private static string[] CreateUnityVersionInfo(UnityVersion start, UnityVersion return new string[] { Enum.GetName(typeof(UnityVersion), start), Enum.GetName(typeof(UnityVersion), end) }; } - //TODO USE: (UnityVersion start, UnityVersion end) Tuple when updated to Net Standard 2.0 - public static Tuple GetVersion(DiagnosticDescriptor dc) + public static UnityVersionSpan GetVersion(DiagnosticDescriptor dc) { var list = dc.CustomTags.ToList(); + + if (list.Count < 2) + { + return new UnityVersionSpan(UnityVersion.NONE, UnityVersion.LATEST); + } + var start = (UnityVersion)Enum.Parse(typeof(UnityVersion), list[0]); var end = (UnityVersion)Enum.Parse(typeof(UnityVersion), list[1]); - return Tuple.Create(start, end); + return new UnityVersionSpan(start, end); } } } diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticIDs.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticIDs.cs index 3b07f22..8bb4f66 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticIDs.cs +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticIDs.cs @@ -21,7 +21,5 @@ public static class DiagnosticIDs public const string DoNotUseRemoting = "AOT0001"; public const string DoNotUseReflectionEmit = "AOT0002"; public const string TypeGetType = "AOT0003"; - - } } diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs index 9c9e9d1..b547fa4 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/UnityVersions.cs @@ -1,10 +1,20 @@ -using System; - -namespace UnityEngineAnalyzer +namespace UnityEngineAnalyzer { + public class UnityVersionSpan + { + public UnityVersion First { get; set; } + public UnityVersion Last { get; set; } + + public UnityVersionSpan (UnityVersion first, UnityVersion last) + { + First = first; + Last = last; + } + } + public enum UnityVersion { - ALL, + NONE, UNITY_1_0, UNITY_2_0, UNITY_3_0, @@ -28,5 +38,6 @@ public enum UnityVersion UNITY_2017_1, UNITY_2017_2, UNITY_2017_3, + LATEST, } } \ No newline at end of file From d406e7713da8b353bae5e76ace51acb3936e0db7 Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Thu, 19 Oct 2017 16:44:58 +0300 Subject: [PATCH 3/5] Refactoring UnityVersionResolver.cs --- UnityEngineAnalyzer.CLI/Program.cs | 45 +------------ .../UnityEngineAnalyzer.CLI.csproj | 1 + .../UnityVersionResolver.cs | 65 +++++++++++++++++++ 3 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 UnityEngineAnalyzer.CLI/UnityVersionResolver.cs diff --git a/UnityEngineAnalyzer.CLI/Program.cs b/UnityEngineAnalyzer.CLI/Program.cs index 2e836b3..9cee5a2 100644 --- a/UnityEngineAnalyzer.CLI/Program.cs +++ b/UnityEngineAnalyzer.CLI/Program.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text.RegularExpressions; using System.Threading.Tasks; using UnityEngineAnalyzer.CLI.Reporting; @@ -11,7 +9,6 @@ namespace UnityEngineAnalyzer.CLI public class Program { private static readonly Dictionary AvailableExporters = new Dictionary(); - private const UnityVersion DEFAULT_UNITY_VERSION = UnityVersion.LATEST; static Program() { @@ -32,7 +29,8 @@ public static void Main(string[] args) return; } - options.Version = DefineUnityVersion(options); + var unityVersionResolver = new UnityVersionResolver(); + options.Version = unityVersionResolver.ResolveVersion(options); var startTime = DateTime.Now; @@ -95,45 +93,6 @@ public static void Main(string[] args) } //TODO SET TO OWN CLASS - private static UnityVersion DefineUnityVersion(Options options) - { - if (options.Version != UnityVersion.NONE) - { - return options.Version; - } - //THIS ONLY WORKS ON UNITY >= 5, before that ProjectVersion.txt did not exists - var projectPath = new FileInfo(options.ProjectFile).Directory; - var projectVersionFile = new FileInfo(projectPath + "/ProjectSettings/ProjectVersion.txt"); - if (projectVersionFile.Exists) - { - var projectVersionString = File.ReadAllText(projectVersionFile.FullName); - return TryParseUnityVersion(projectVersionString); - } - - return DEFAULT_UNITY_VERSION; - } - - //TODO UNIT TESTS - private static UnityVersion TryParseUnityVersion(string version) - { - string editorText = "m_EditorVersion: "; - var match = Regex.Match(version, editorText + "[0-9.a-z]*"); - - string src = match.Value.Substring(editorText.Length); - src = src.Replace('.', '_'); - src = src.Substring(0, src.IndexOf('_') + 2); - - var unityVersions = Enum.GetValues(typeof(UnityVersion)).Cast(); - foreach (var unityVersion in unityVersions) - { - if (Enum.GetName(typeof(UnityVersion), unityVersion).Contains(src)) - { - return unityVersion; - } - } - - return DEFAULT_UNITY_VERSION; - } } } diff --git a/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj b/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj index 48e3142..76e8c8d 100644 --- a/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj +++ b/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj @@ -118,6 +118,7 @@ + diff --git a/UnityEngineAnalyzer.CLI/UnityVersionResolver.cs b/UnityEngineAnalyzer.CLI/UnityVersionResolver.cs new file mode 100644 index 0000000..cb53e73 --- /dev/null +++ b/UnityEngineAnalyzer.CLI/UnityVersionResolver.cs @@ -0,0 +1,65 @@ +using System; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; + +namespace UnityEngineAnalyzer.CLI +{ + //NOTE: This class would benefit from UnitTests specially: TryParseUnityVersion + internal class UnityVersionResolver + { + private const UnityVersion DEFAULT_UNITY_VERSION = UnityVersion.LATEST; + + public UnityVersion ResolveVersion(Options options) + { + if (options.Version != UnityVersion.NONE) + { + return options.Version; + } + + //THIS ONLY WORKS ON UNITY >= 5, before that ProjectVersion.txt did not exists + if (ResolveProjectVersionFilePath(options) != null) + { + var projectVersionString = File.ReadAllText(ResolveProjectVersionFilePath(options)); + return TryParseUnityVersion(projectVersionString); + } + + return DEFAULT_UNITY_VERSION; + } + + private string ResolveProjectVersionFilePath(Options options) + { + var projectPath = new FileInfo(options.ProjectFile).Directory; + var path = Path.Combine(projectPath.FullName, "ProjectSettings", "ProjectVersion.txt"); + var projectVersionFile = new FileInfo(path); + + if (projectVersionFile.Exists) + { + return projectVersionFile.FullName; + } + + return null; + } + + private UnityVersion TryParseUnityVersion(string version) + { + string editorText = "m_EditorVersion: "; + var match = Regex.Match(version, editorText + "[0-9.a-z]*"); + + string src = match.Value.Substring(editorText.Length); + src = src.Replace('.', '_'); + src = src.Substring(0, src.IndexOf('_') + 2); + + var unityVersions = Enum.GetValues(typeof(UnityVersion)).Cast(); + foreach (var unityVersion in unityVersions) + { + if (Enum.GetName(typeof(UnityVersion), unityVersion).Contains(src)) + { + return unityVersion; + } + } + + return DEFAULT_UNITY_VERSION; + } + } +} From 5249fa3a9b51e6207b97bf5d14a72f535a3fa787 Mon Sep 17 00:00:00 2001 From: Kasperki Date: Thu, 19 Oct 2017 18:52:28 +0300 Subject: [PATCH 4/5] Updated DiagnosticDescriptors with new UnityVersion check. --- .../DiagnosticDescriptors.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs index 34b4358..3e68c71 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs @@ -40,23 +40,32 @@ public static class DiagnosticDescriptors static DiagnosticDescriptors() { + //** UNITY ** + + //GC DoNotUseOnGUI = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseOnGUI, DiagnosticCategories.GC, DiagnosticSeverity.Info); DoNotUseStringMethods = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStringMethods, DiagnosticCategories.GC, DiagnosticSeverity.Info); DoNotUseCoroutines = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseCoroutines, DiagnosticCategories.GC, DiagnosticSeverity.Info); - EmptyMonoBehaviourMethod = CreateDiagnosticDescriptor(DiagnosticIDs.EmptyMonoBehaviourMethod, DiagnosticCategories.Miscellaneous, DiagnosticSeverity.Warning); UseCompareTag = CreateDiagnosticDescriptor(DiagnosticIDs.UseCompareTag, DiagnosticCategories.GC, DiagnosticSeverity.Warning); + UseNonAllocMethods = CreateDiagnosticDescriptor(DiagnosticIDs.PhysicsUseNonAllocMethods, DiagnosticCategories.GC, DiagnosticSeverity.Warning, UnityVersion.UNITY_5_3); + CameraMainIsSlow = CreateDiagnosticDescriptor(DiagnosticIDs.CameraMainIsSlow, DiagnosticCategories.GC, DiagnosticSeverity.Warning); + + //Performance DoNotUseFindMethodsInUpdate = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseFindMethodsInUpdate, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); DoNotUseFindMethodsInUpdateRecursive = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseFindMethodsInUpdate, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); - DoNotUseRemoting = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseRemoting, DiagnosticCategories.AOT, DiagnosticSeverity.Info); - DoNotUseReflectionEmit = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseReflectionEmit, DiagnosticCategories.AOT, DiagnosticSeverity.Info); - TypeGetType = CreateDiagnosticDescriptor(DiagnosticIDs.TypeGetType, DiagnosticCategories.AOT, DiagnosticSeverity.Info); - DoNotUseForEachInUpdate = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseForEachInUpdate, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + DoNotUseForEachInUpdate = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseForEachInUpdate, DiagnosticCategories.Performance, DiagnosticSeverity.Warning, UnityVersion.UNITY_1_0, UnityVersion.UNITY_5_5); UnsealedDerivedClass = CreateDiagnosticDescriptor(DiagnosticIDs.UnsealedDerivedClass, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); InvokeFunctionMissing = CreateDiagnosticDescriptor(DiagnosticIDs.InvokeFunctionMissing, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); DoNotUseStateName = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStateNameInAnimator, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); DoNotUseStringPropertyNames = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStringPropertyNamesInMaterial, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); - UseNonAllocMethods = CreateDiagnosticDescriptor(DiagnosticIDs.PhysicsUseNonAllocMethods, DiagnosticCategories.GC, DiagnosticSeverity.Warning, UnityVersion.UNITY_5_3); - CameraMainIsSlow = CreateDiagnosticDescriptor(DiagnosticIDs.CameraMainIsSlow, DiagnosticCategories.GC, DiagnosticSeverity.Warning); + + //Miscellaneous + EmptyMonoBehaviourMethod = CreateDiagnosticDescriptor(DiagnosticIDs.EmptyMonoBehaviourMethod, DiagnosticCategories.Miscellaneous, DiagnosticSeverity.Warning); + + //** AOT ** + DoNotUseRemoting = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseRemoting, DiagnosticCategories.AOT, DiagnosticSeverity.Info); + DoNotUseReflectionEmit = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseReflectionEmit, DiagnosticCategories.AOT, DiagnosticSeverity.Info); + TypeGetType = CreateDiagnosticDescriptor(DiagnosticIDs.TypeGetType, DiagnosticCategories.AOT, DiagnosticSeverity.Info); } private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, string category, DiagnosticSeverity severity, UnityVersion first = UnityVersion.UNITY_1_0, UnityVersion latest = UnityVersion.LATEST, bool isEnabledByDefault = true) From a933c1769ed1f64c26b3dce2c6d26b698a11f0a8 Mon Sep 17 00:00:00 2001 From: Kasperki Date: Thu, 19 Oct 2017 18:57:29 +0300 Subject: [PATCH 5/5] Updated readme.md --- readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.md b/readme.md index 48609ee..175809c 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,9 @@ In order to use the Command Line Interface (CLI), download the latest release of 1. (Optional) minimal severity for reports * Use command `-s Info/Warning/Error` to defined used minimal severity for reporting * Default is Warning +1. (Optional) Unity version for check + * Use command `-v UNITY_2017_1/UNITY_5_5/UNITY_4_0/...` to Unity version + * For default analyzer will try to find ProjectVersion.txt file and parse version automatically. Example: