Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions TerrariaServerAPI/TerrariaApi.Server/ServerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// Returns the first value from <see cref="AdditionalPluginsPaths"/> if it exists, otherwise null.
/// </summary>
[Obsolete("Use AdditionalPluginsPaths instead", error: false)]
public static string? AdditionalPluginsPath => AdditionalPluginsPaths.FirstOrDefault();

Check warning on line 27 in TerrariaServerAPI/TerrariaApi.Server/ServerApi.cs

View workflow job for this annotation

GitHub Actions / MacOS

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 27 in TerrariaServerAPI/TerrariaApi.Server/ServerApi.cs

View workflow job for this annotation

GitHub Actions / Ubuntu

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 27 in TerrariaServerAPI/TerrariaApi.Server/ServerApi.cs

View workflow job for this annotation

GitHub Actions / Windows

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary> A list of all plugin paths specified by the -additionalplugins flag. </summary>
public static ImmutableList<string> AdditionalPluginsPaths { get; private set; } = ImmutableList.Create<string>();
Expand Down Expand Up @@ -515,36 +515,52 @@
}
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
private static Assembly ResolveAssembly(string pluginsPath, string fileName)
{
string fileName = args.Name.Split(',')[0];
string path = Path.Combine(ServerPluginsDirectoryPath, fileName + ".dll");
try
{
if (File.Exists(path))
{
Assembly assembly;
if (!loadedAssemblies.TryGetValue(fileName, out assembly))
{
var pdbPath = Path.ChangeExtension(fileName, ".pdb");
assembly = Assembly.Load(File.ReadAllBytes(path), File.Exists(pdbPath) ? File.ReadAllBytes(pdbPath) : null);
// We just do this to return a proper error message incase this is a resolved plugin assembly
// referencing an old TerrariaServer version.
if (!InvalidateAssembly(assembly, fileName))
throw new InvalidOperationException(
"The assembly is referencing a version of TerrariaServer prior 1.14.");
string pluginPath = Path.Combine(pluginsPath, fileName + ".dll");

loadedAssemblies.Add(fileName, assembly);
}
return assembly;
}
if (!File.Exists(pluginPath)) return null;

if (loadedAssemblies.TryGetValue(fileName, out var assembly)) return assembly;

var pdbPath = Path.ChangeExtension(pluginPath, ".pdb");
assembly = Assembly.Load(File.ReadAllBytes(pluginPath),
File.Exists(pdbPath) ? File.ReadAllBytes(pdbPath) : null);

// We just do this to return a proper error message incase this is a resolved plugin assembly
// referencing an old TerrariaServer version.
if (!InvalidateAssembly(assembly, fileName))
throw new InvalidOperationException(
"The assembly is referencing a version of TerrariaServer prior 1.14.");

loadedAssemblies.Add(fileName, assembly);

return assembly;
}
catch (Exception ex)
{
LogWriter.ServerWriteLine(
string.Format("Error on resolving assembly \"{0}.dll\":\n{1}", fileName, ex),
$"Error on resolving assembly \"{fileName}.dll\":\n{ex}",
TraceLevel.Error);
}

return null;
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string fileName = args.Name.Split(',')[0];
List<string> pluginsPaths = [ServerPluginsDirectoryPath, ..AdditionalPluginsPaths];

foreach (string pluginsPath in pluginsPaths)
{
Assembly assembly = ResolveAssembly(pluginsPath, fileName);

if (assembly != null) return assembly;
}

return null;
}

Expand Down
Loading