Skip to content

Commit

Permalink
Adding a copy of GetMissingDependencies to om.runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rube200 committed Nov 10, 2023
1 parent d47df6b commit fa35809
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions framework/OpenMod.Runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
Expand All @@ -18,7 +19,6 @@
using OpenMod.API.Ioc;
using OpenMod.API.Permissions;
using OpenMod.API.Persistence;
using OpenMod.Common.Helpers;
using OpenMod.Common.Hotloading;
using OpenMod.Core.Helpers;
using OpenMod.Core.Ioc;
Expand Down Expand Up @@ -325,14 +325,53 @@ public void Init(List<Assembly> openModAssemblies, RuntimeInitParameters paramet
m_Logger.LogDebug(ex, "Missing dependencies");
m_Logger.LogWarning("Some types from assembly {Assembly} couldn't be loaded.", openModHostAssembly.FullName);

var missingDependencies = ex.GetMissingDependencies();
var missingDependencies = GetMissingDependencies(ex);
m_Logger.LogWarning("Missing dependencies: {MissingAssemblies}", string.Join(", ", missingDependencies));
}
}

return assembliesTypes.FirstOrDefault(t => t.IsAssignableTo<IHostInformation>());
}

private static readonly Regex s_MissingFileAssemblyVersionRegex =
new("'(?<assembly>\\S+?), Version=(?<version>.+?), ",
RegexOptions.Compiled);

private static readonly Regex s_TypeLoadAssemblyVersionRegex =
new("assembly:(?<assembly>\\S+?), Version=(?<version>.+?), ",
RegexOptions.Compiled);
//Copy of AssemblyExtensions.GetMissingDependencies
private static IEnumerable<AssemblyName> GetMissingDependencies(ReflectionTypeLoadException reflectionTypeLoadException)
{
if (reflectionTypeLoadException == null)
{
throw new ArgumentNullException(nameof(reflectionTypeLoadException));
}

var missingAssemblies = new Dictionary<string, Version>();
var loaderExceptions = reflectionTypeLoadException.LoaderExceptions;
foreach (var loaderException in loaderExceptions)
{
//TypeLoadException is just matching with MissingFileAssemblyVersionRegex
var match = s_MissingFileAssemblyVersionRegex.Match(loaderException.Message);
if (!match.Success)
match = s_TypeLoadAssemblyVersionRegex.Match(loaderException.Message);

if (!match.Success)
continue;

var assemblyName = match.Groups["assembly"].Value;
var version = System.Version.Parse(match.Groups["version"].Value);

if (missingAssemblies.TryGetValue(assemblyName, out var currentVersion) && currentVersion >= version)
continue;

missingAssemblies[assemblyName] = version;
}

return missingAssemblies.Select(s => new AssemblyName(s.Key) { Version = s.Value });
}

public async Task PerformSoftReloadAsync()
{
await ShutdownAsync();
Expand Down

0 comments on commit fa35809

Please sign in to comment.