Add TShock Launcher project (TShock.exe) with ./bin folder for dependencies

+ submodule update
TODO: retest on osx and see if we can include file permissions
This commit is contained in:
Luke 2021-12-01 08:26:06 +10:00
parent 404520c789
commit 2b4b6353c2
6 changed files with 195 additions and 23 deletions

66
TShockLauncher/Program.cs Normal file
View file

@ -0,0 +1,66 @@
/*
TShock, a server mod for Terraria
Copyright (C) 2011-2019 Pryaxis & TShock Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* The purpose of this project is to be the launcher of the TSAPI server.
* We use this project:
* - to copy/move around TShockAPI.dll (the TShock plugin to TSAPI)
* - to publishing TShock releases.
* - move dependencies to a ./bin folder
*
* The assembly name of this launcher (TShock.exe) was decided on by a community poll.
*/
using System.Reflection;
Dictionary<string, Assembly> _cache = new Dictionary<string, Assembly>();
System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += Default_Resolving;
Start();
/// <summary>
/// Resolves a module from the ./bin folder, either with a .dll by preference or .exe
/// </summary>
Assembly? Default_Resolving(System.Runtime.Loader.AssemblyLoadContext arg1, AssemblyName arg2)
{
if (arg2?.Name is null) return null;
if (_cache.TryGetValue(arg2.Name, out Assembly? asm) && asm is not null) return asm;
var loc = Path.Combine(Environment.CurrentDirectory, "bin", arg2.Name + ".dll");
if (File.Exists(loc))
asm = arg1.LoadFromAssemblyPath(loc);
loc = Path.ChangeExtension(loc, ".exe");
if (File.Exists(loc))
asm = arg1.LoadFromAssemblyPath(loc);
if(asm is not null)
_cache[arg2.Name] = asm;
return asm;
}
/// <summary>
/// Initiates the TSAPI server.
/// </summary>
/// <remarks>This method exists so that the resolver can attach before TSAPI needs its dependencies.</remarks>
void Start()
{
TerrariaApi.Server.Program.Main(args);
}

View file

@ -0,0 +1,105 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>TShock</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TerrariaServerAPI\TerrariaServerAPI\TerrariaServerAPI.csproj" />
<ProjectReference Include="..\TShockAPI\TShockAPI.csproj">
<!-- dont put TShockAPI in the dependencies -->
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<Reference Include="HttpServer">
<HintPath>..\prebuilts\HttpServer.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<!-- match tshocks dependencies so that publishing outputs all files to ./bin -->
<PackageReference Include="BCrypt.Net-Next" Version="4.0.2" />
<PackageReference Include="MySql.Data" Version="8.0.27" />
<PackageReference Include="SQLite" Version="3.13.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.115.5" />
</ItemGroup>
<Target Name="MoveToBinFolderDebug" AfterTargets="FinalCleanup;PostBuildEvent">
<ItemGroup>
<MoveBinariesForDebug Include="$(TargetDir)*" Exclude="$(TargetDir)\TShock*" />
<TShockPluginFiles Include="$(ProjectDir)../TShockAPI/bin/$(Configuration)/$(TargetFramework)/TShockAPI*" />
</ItemGroup>
<Move SourceFiles="@(MoveBinariesForDebug)" DestinationFolder="$(TargetDir)bin" ContinueOnError="true" />
<Copy SourceFiles="@(TShockPluginFiles)" DestinationFolder="$(TargetDir)ServerPlugins" ContinueOnError="true" />
</Target>
<Target Name="MoveToBinFolderPublish" AfterTargets="Publish">
<ItemGroup>
<MoveBinariesForPublish Include="$(PublishDir)*" Exclude="$(PublishDir)\TShock.exe;$(PublishDir)\TShockAPI*" />
<TShockPluginFilesForPublish Include="$(ProjectDir)../TShockAPI/bin/$(Configuration)/$(TargetFramework)/TShockAPI*" />
</ItemGroup>
<Move SourceFiles="@(MoveBinariesForPublish)" DestinationFolder="$(PublishDir)bin" ContinueOnError="true" />
<Copy SourceFiles="@(TShockPluginFilesForPublish)" DestinationFolder="$(PublishDir)ServerPlugins" ContinueOnError="true" />
</Target>
<UsingTask
TaskName="PatchAppHost"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<HostExe ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
/*
This is a patch to allow self contained apps to run from the root while having the modules in a ./bin folder.
https://github.com/dotnet/sdk/issues/10366
The script replaces the ./TShock.dll with ./bin/TShock.dll
alternates: dnSpy(does this method), NetCoreBeauty
*/
var find = Encoding.UTF8.GetBytes("TShock.dll\0");
var replace = Encoding.UTF8.GetBytes("bin" + Path.DirectorySeparatorChar + "TShock.dll\0");
var contents = File.ReadAllBytes(HostExe);
var matches = 0;
var offset = Array.FindIndex(contents, (b) => {
matches = (b == find[matches]) ? matches + 1 : 0;
return matches == find.Length;
});
if (offset > -1) offset -= find.Length - 1;
var x = 0;
Array.ForEach(replace, _ => { contents[offset + x] = replace[x++]; });
File.WriteAllBytes(HostExe, contents);
</Code>
</Task>
</UsingTask>
<Target Name="PatchAppHostAfterPublish" AfterTargets="Publish" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
<PatchAppHost HostExe="$(PublishDir)TShock.exe" />
<Message Text="Patched TShock.exe host for ./bin redirect" Importance="High" />
</Target>
<UsingTask
TaskName="CreateOsxLauncher"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<OutPath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
File.WriteAllText(Path.Combine(OutPath, "TShock.exe"), "#!/bin/bash\n./bin/TShock");
</Code>
</Task>
</UsingTask>
<Target Name="CreateOsxLauncherAfterPublish" AfterTargets="Publish" Condition="'$(RuntimeIdentifier)' == 'osx-x64'">
<CreateOsxLauncher OutPath="$(PublishDir)" />
<Message Text="Run the launcher with chmod u+x TShock.exe &amp;&amp; ./TShock.exe" Importance="High" />
</Target>
</Project>

View file

@ -0,0 +1,5 @@
{
"additionalProbingPaths": [
"../ServerPlugins"
]
}