fix: Adds better error message for missing assemblies (#1150)
Adds better error messaging for when the server cannot find a dependency. The most common error is MimeKit is missing because the assemblyDirectories field in modernuo.json does not have any valid paths.
This commit is contained in:
parent
b8146f26f1
commit
1a44d824d9
2 changed files with 35 additions and 11 deletions
|
|
@ -29,33 +29,50 @@ namespace Server
|
|||
private static TypeCache m_NullCache;
|
||||
public static Assembly[] Assemblies { get; set; }
|
||||
|
||||
internal static Assembly AssemblyResolver(object sender, ResolveEventArgs args) =>
|
||||
LoadAssemblyByAssemblyName(args.Name);
|
||||
internal static Assembly AssemblyResolver(object sender, ResolveEventArgs args)
|
||||
{
|
||||
var assemblyName = new AssemblyName(args.Name);
|
||||
var assembly = LoadAssemblyByAssemblyName(assemblyName);
|
||||
if (assembly == null)
|
||||
{
|
||||
throw new FileNotFoundException(
|
||||
$"Could not load file or assembly {assemblyName}. The system cannot find the file specified. Review the assemblyDirectories field in {ServerConfiguration.ConfigurationFilePath}",
|
||||
$"{assemblyName.Name}.dll"
|
||||
);
|
||||
}
|
||||
|
||||
return assembly;
|
||||
}
|
||||
|
||||
private static void EnsureAssemblyDirectories()
|
||||
{
|
||||
if (ServerConfiguration.AssemblyDirectories.Count == 0)
|
||||
{
|
||||
ServerConfiguration.AssemblyDirectories.Add(Path.Combine(Core.BaseDirectory, "Assemblies"));
|
||||
ServerConfiguration.AssemblyDirectories.Add("./Assemblies");
|
||||
ServerConfiguration.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public static Assembly LoadAssemblyByAssemblyName(string fullAssemblyName)
|
||||
public static Assembly LoadAssemblyByAssemblyName(AssemblyName assemblyName)
|
||||
{
|
||||
var assemblyName = new AssemblyName(fullAssemblyName);
|
||||
var assemblyFile = $"{assemblyName.Name}.dll";
|
||||
if (assemblyName?.Name == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var fullName = assemblyName.FullName;
|
||||
var fileName = $"{assemblyName.Name}.dll";
|
||||
|
||||
EnsureAssemblyDirectories();
|
||||
var assemblyDirectories = ServerConfiguration.AssemblyDirectories;
|
||||
|
||||
foreach (var assemblyDir in assemblyDirectories)
|
||||
{
|
||||
var assemblyPath = Path.Combine(assemblyDir, assemblyFile);
|
||||
var assemblyPath = PathUtility.GetFullPath(Path.Combine(assemblyDir, fileName), Core.BaseDirectory);
|
||||
if (File.Exists(assemblyPath))
|
||||
{
|
||||
var assemblyNameCheck = AssemblyName.GetAssemblyName(assemblyPath);
|
||||
if (assemblyNameCheck.FullName == assemblyName.FullName)
|
||||
if (assemblyNameCheck.FullName == fullName)
|
||||
{
|
||||
return AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
|
||||
}
|
||||
|
|
@ -88,12 +105,17 @@ namespace Server
|
|||
|
||||
for (var i = 0; i < files.Length; i++)
|
||||
{
|
||||
var assembly = LoadAssemblyByFileName(files[i]);
|
||||
var assemblyFile = files[i];
|
||||
var assembly = LoadAssemblyByFileName(assemblyFile);
|
||||
if (assembly == null)
|
||||
{
|
||||
throw new FileNotFoundException($"Could not load {files[i]}");
|
||||
throw new FileNotFoundException(
|
||||
$"Could not load file or assembly {assemblyFile}. The system cannot find the file specified. Review the assemblyDirectories field in {ServerConfiguration.ConfigurationFilePath}",
|
||||
assemblyFile
|
||||
);
|
||||
}
|
||||
assemblies[i] = LoadAssemblyByFileName(files[i]);
|
||||
|
||||
assemblies[i] = assembly;
|
||||
}
|
||||
|
||||
Assemblies = assemblies;
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ public static class ServerConfiguration
|
|||
|
||||
public static List<IPEndPoint> Listeners => m_Settings.Listeners;
|
||||
|
||||
public static string ConfigurationFilePath => _relPath;
|
||||
|
||||
public static ClientVersion GetSetting(string key, ClientVersion defaultValue) =>
|
||||
m_Settings.Settings.TryGetValue(key, out var value) ? new ClientVersion(value) : defaultValue;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue