Improve launcher assembly resolution

This addresses dev instances unable to resolve binaries, and types being requested by the plugin manager before the resolver is attached
This commit is contained in:
Luke 2025-01-11 14:32:24 +10:00
parent d9de3c1fc0
commit 6a3e8c3d5d
2 changed files with 18 additions and 13 deletions

View file

@ -27,22 +27,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.Reflection; using System.Reflection;
using TShockPluginManager;
if (args.Length > 0 && args[0].ToLower() == "plugins")
{
var items = args.ToList();
items.RemoveAt(0);
await NugetCLI.Main(items);
return;
}
Dictionary<string, Assembly> _cache = new Dictionary<string, Assembly>(); Dictionary<string, Assembly> _cache = new Dictionary<string, Assembly>();
System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += Default_Resolving; System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += Default_Resolving;
Start(); await StartAsync();
/// <summary> /// <summary>
/// Resolves a module from the ./bin folder, either with a .dll by preference or .exe /// Resolves a module from the ./bin folder, either with a .dll by preference or .exe
@ -53,6 +43,7 @@ Assembly? Default_Resolving(System.Runtime.Loader.AssemblyLoadContext arg1, Asse
if (_cache.TryGetValue(arg2.Name, out Assembly? asm) && asm is not null) return asm; if (_cache.TryGetValue(arg2.Name, out Assembly? asm) && asm is not null) return asm;
var loc = Path.Combine(AppContext.BaseDirectory, "bin", arg2.Name + ".dll"); var loc = Path.Combine(AppContext.BaseDirectory, "bin", arg2.Name + ".dll");
if (File.Exists(loc)) if (File.Exists(loc))
asm = arg1.LoadFromAssemblyPath(loc); asm = arg1.LoadFromAssemblyPath(loc);
@ -70,7 +61,15 @@ Assembly? Default_Resolving(System.Runtime.Loader.AssemblyLoadContext arg1, Asse
/// Initiates the TSAPI server. /// Initiates the TSAPI server.
/// </summary> /// </summary>
/// <remarks>This method exists so that the resolver can attach before TSAPI needs its dependencies.</remarks> /// <remarks>This method exists so that the resolver can attach before TSAPI needs its dependencies.</remarks>
void Start() async Task StartAsync()
{ {
if (args.Length > 0 && args[0].ToLower() == "plugins")
{
var items = args.ToList();
items.RemoveAt(0);
await TShockPluginManager.NugetCLI.Main(items);
return;
}
TerrariaApi.Server.Program.Main(args); TerrariaApi.Server.Program.Main(args);
} }

View file

@ -91,7 +91,13 @@
</ItemGroup> </ItemGroup>
<Copy SourceFiles="@(MOFiles)" DestinationFolder="$(PublishDir)%(RecursiveDir)" /> <Copy SourceFiles="@(MOFiles)" DestinationFolder="$(PublishDir)%(RecursiveDir)" />
</Target> </Target>
<Target Name="MoveBin" AfterTargets="Publish"> <Target Name="MoveDevBin" AfterTargets="PostBuildEvent">
<ItemGroup>
<MoveBinaries Include="$(OutDir)*" Exclude="$(OutDir)\TShock.Server*;$(OutDir)\GeoIP.dat" />
</ItemGroup>
<Move SourceFiles="@(MoveBinaries)" DestinationFolder="$(OutDir)bin" ContinueOnError="true" />
</Target>
<Target Name="MovePublishBin" AfterTargets="Publish">
<ItemGroup> <ItemGroup>
<MoveBinaries Include="$(PublishDir)*" Exclude="$(PublishDir)\TShock.Server*;$(PublishDir)\GeoIP.dat" /> <MoveBinaries Include="$(PublishDir)*" Exclude="$(PublishDir)\TShock.Server*;$(PublishDir)\GeoIP.dat" />
</ItemGroup> </ItemGroup>