From 31f0dc777f90fbce6f2ca28bc6c0d5bbde7f10ad Mon Sep 17 00:00:00 2001 From: Connor Ivy Date: Fri, 26 Apr 2024 10:51:16 -0500 Subject: [PATCH 1/2] use speckle newtonsoft for dll conflict detection --- .../DllConflictManagement.csproj | 2 +- .../DllConflictManagement/DllConflictManager.cs | 16 ++++++++++++++-- .../Serialization/SpeckleNewtonsoftSerializer.cs | 10 ++++++++++ .../Serialization/SystemTextJsonSerializer.cs | 16 ---------------- ConnectorRevit/ConnectorRevit/Entry/App.cs | 14 ++++++++++++-- 5 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs delete mode 100644 ConnectorCore/DllConflictManagement/Serialization/SystemTextJsonSerializer.cs diff --git a/ConnectorCore/DllConflictManagement/DllConflictManagement.csproj b/ConnectorCore/DllConflictManagement/DllConflictManagement.csproj index 444ad75619..658cf470c9 100644 --- a/ConnectorCore/DllConflictManagement/DllConflictManagement.csproj +++ b/ConnectorCore/DllConflictManagement/DllConflictManagement.csproj @@ -13,7 +13,7 @@ - + diff --git a/ConnectorCore/DllConflictManagement/DllConflictManager.cs b/ConnectorCore/DllConflictManagement/DllConflictManager.cs index 28c21a8cb0..26f34f13c2 100644 --- a/ConnectorCore/DllConflictManagement/DllConflictManager.cs +++ b/ConnectorCore/DllConflictManagement/DllConflictManager.cs @@ -11,18 +11,22 @@ public sealed class DllConflictManager private readonly DllConflictManagmentOptionsLoader _optionsLoader; private readonly DllConflictEventEmitter _eventEmitter; private readonly string[] _assemblyPathFragmentsToIgnore; + private readonly string[] _exactAssemblyPathsToIgnore; + public ICollection AllConflictInfo => _assemblyConflicts.Values; public ICollection AllConflictInfoAsDtos => _assemblyConflicts.Values.ToDtos().ToList(); public DllConflictManager( DllConflictManagmentOptionsLoader optionsLoader, DllConflictEventEmitter eventEmitter, - params string[] assemblyPathFragmentsToIgnore + string[]? assemblyPathFragmentsToIgnore = null, + string[]? exactAssemblyPathsToIgnore = null ) { _optionsLoader = optionsLoader; _eventEmitter = eventEmitter; - _assemblyPathFragmentsToIgnore = assemblyPathFragmentsToIgnore; + _assemblyPathFragmentsToIgnore = assemblyPathFragmentsToIgnore ?? Array.Empty(); + _exactAssemblyPathsToIgnore = exactAssemblyPathsToIgnore ?? Array.Empty(); } /// @@ -99,6 +103,14 @@ HashSet visitedAssemblies private bool ShouldSkipCheckingConflictBecauseOfAssemblyLocation(Assembly loadedAssembly) { + foreach (var exactPath in _exactAssemblyPathsToIgnore) + { + if (Path.GetDirectoryName(loadedAssembly.Location) == exactPath) + { + return true; + } + } + foreach (var pathFragment in _assemblyPathFragmentsToIgnore) { if (loadedAssembly.Location.Contains(pathFragment)) diff --git a/ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs b/ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs new file mode 100644 index 0000000000..753d81e54a --- /dev/null +++ b/ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs @@ -0,0 +1,10 @@ +using Speckle.Newtonsoft.Json; + +namespace Speckle.DllConflictManagement.Serialization; + +public class SpeckleNewtonsoftSerializer : ISerializer +{ + public string Serialize(T obj) => JsonConvert.SerializeObject(obj); + + public T Deserialize(string serialized) => JsonConvert.DeserializeObject(serialized); +} diff --git a/ConnectorCore/DllConflictManagement/Serialization/SystemTextJsonSerializer.cs b/ConnectorCore/DllConflictManagement/Serialization/SystemTextJsonSerializer.cs deleted file mode 100644 index df8f9ebd0f..0000000000 --- a/ConnectorCore/DllConflictManagement/Serialization/SystemTextJsonSerializer.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Text.Json; - -namespace Speckle.DllConflictManagement.Serialization; - -/// -/// Just a wrapper around the System.Text.Json serializer. If we decide that we want to switch to newtonsoft -/// or even something that is not json-based, then we only need to make a new implementation of the ISerializer -/// -public class SystemTextJsonSerializer : ISerializer -{ - private readonly JsonSerializerOptions _jsonSerializerOptions = new() { WriteIndented = true }; - - public string Serialize(T obj) => JsonSerializer.Serialize(obj, _jsonSerializerOptions); - - public T Deserialize(string serialized) => JsonSerializer.Deserialize(serialized); -} diff --git a/ConnectorRevit/ConnectorRevit/Entry/App.cs b/ConnectorRevit/ConnectorRevit/Entry/App.cs index 2b7de76678..ce01ed30c7 100644 --- a/ConnectorRevit/ConnectorRevit/Entry/App.cs +++ b/ConnectorRevit/ConnectorRevit/Entry/App.cs @@ -31,6 +31,10 @@ public class App : IExternalApplication public static UIControlledApplication UICtrlApp { get; set; } private bool _initialized; + private static readonly string[] s_assemblyPathFragmentsToIgnore = new string[] + { + "Microsoft.Net\\assembly\\GAC_MSIL\\" + }; public Result OnStartup(UIControlledApplication application) { @@ -44,13 +48,19 @@ public Result OnStartup(UIControlledApplication application) UICtrlApp.ControlledApplication.DocumentOpening += ControlledApplication_DocumentOpening; DllConflictEventEmitter eventEmitter = new(); - ISerializer serializer = new SystemTextJsonSerializer(); + ISerializer serializer = new SpeckleNewtonsoftSerializer(); AnalyticsWithoutDependencies analytics = new(eventEmitter, serializer, "Revit", GetRevitVersion()); eventEmitter.OnAction += analytics.TrackEvent; DllConflictManagmentOptionsLoader optionsLoader = new(serializer, "Revit", GetRevitVersion()); // ignore dll conflicts when dll lives in GAC because they are noisy and not an issue (at least in revit) - DllConflictManager conflictManager = new(optionsLoader, eventEmitter, "Microsoft.Net\\assembly\\GAC_MSIL\\"); + DllConflictManager conflictManager = + new( + optionsLoader, + eventEmitter, + s_assemblyPathFragmentsToIgnore, + new string[] { $"C:\\Program Files\\Autodesk\\Revit {GetRevitVersion()}" } + ); RevitDllConflictUserNotifier conflictNotifier = new(conflictManager, eventEmitter); try From 9736d5679735fce6ed78478536dbdc6165dd03f6 Mon Sep 17 00:00:00 2001 From: Connor Ivy Date: Fri, 26 Apr 2024 15:12:17 -0500 Subject: [PATCH 2/2] fix build --- .../Serialization/SpeckleNewtonsoftSerializer.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs b/ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs index 753d81e54a..2fffc24bf3 100644 --- a/ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs +++ b/ConnectorCore/DllConflictManagement/Serialization/SpeckleNewtonsoftSerializer.cs @@ -6,5 +6,9 @@ public class SpeckleNewtonsoftSerializer : ISerializer { public string Serialize(T obj) => JsonConvert.SerializeObject(obj); - public T Deserialize(string serialized) => JsonConvert.DeserializeObject(serialized); + public T Deserialize(string serialized) => + JsonConvert.DeserializeObject(serialized) + ?? throw new InvalidOperationException( + $"Unable to deserialize the provided JSON into an object of type {typeof(T)}" + ); }