Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to .NET 9 and OTAPI Static Hooks #3050

Open
wants to merge 17 commits into
base: general-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions .github/workflows/ci-otapi3-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:
environment: release

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.400
dotnet-version: 9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
24 changes: 12 additions & 12 deletions .github/workflows/ci-otapi3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: 'recursive'

- uses: actions/setup-dotnet@v3
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '6.0.100'
dotnet-version: 9.0.x

- name: Run tests
run: dotnet test
Expand All @@ -25,41 +25,41 @@ jobs:
arch: ["win-x64", "osx-x64", "linux-x64", "linux-arm64", "linux-arm"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: 'recursive'

- uses: actions/setup-dotnet@v3
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '6.0.100'
dotnet-version: 9.0.x

- name: Install msgfmt
run: sudo apt-get install -y gettext

- name: Produce installer
run: |
cd TShockInstaller
dotnet publish -r ${{ matrix.arch }} -f net6.0 -c Release -p:PublishSingleFile=true --self-contained true
dotnet publish -r ${{ matrix.arch }} -f net9.0 -c Release -p:PublishSingleFile=true --self-contained true

- name: Produce build
run: |
cd TShockLauncher
dotnet publish -r ${{ matrix.arch }} -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false
dotnet publish -r ${{ matrix.arch }} -f net9.0 -c Release -p:PublishSingleFile=true --self-contained false

- name: Chmod scripts
if: ${{ matrix.arch != 'win-x64' }}
run: |
chmod +x TShockLauncher/bin/Release/net6.0/${{ matrix.arch }}/publish/TShock.Server
chmod +x TShockLauncher/bin/Release/net9.0/${{ matrix.arch }}/publish/TShock.Server

- name: Copy installer
run: |
cp TShockInstaller/bin/Release/net6.0/${{ matrix.arch }}/publish/* TShockLauncher/bin/Release/net6.0/${{ matrix.arch }}/publish/
cp TShockInstaller/bin/Release/net9.0/${{ matrix.arch }}/publish/* TShockLauncher/bin/Release/net9.0/${{ matrix.arch }}/publish/

# preserve file perms: https://github.com/actions/upload-artifact#maintaining-file-permissions-and-case-sensitive-files
- name: Tarball artifact (non-Windows)
if: ${{ matrix.arch != 'win-x64' }}
run: |
cd TShockLauncher/bin/Release/net6.0/${{ matrix.arch }}/publish/
cd TShockLauncher/bin/Release/net9.0/${{ matrix.arch }}/publish/
tar -cvf ../../../../../../TShock-Beta-${{ matrix.arch }}-Release.tar *

- name: Upload artifact (non-Windows)
Expand All @@ -74,4 +74,4 @@ jobs:
if: ${{ matrix.arch == 'win-x64' }}
with:
name: TShock-Beta-${{ matrix.arch }}-Release
path: TShockLauncher/bin/Release/net6.0/${{ matrix.arch }}/publish/
path: TShockLauncher/bin/Release/net9.0/${{ matrix.arch }}/publish/
7 changes: 2 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/TShockLauncher/bin/Debug/net6.0/TShock.Run.dll",
"windows": {
"program": "${workspaceFolder}/TShockLauncher/bin/Debug/net6.0/TShock.dll",
},
"program": "${workspaceFolder}/TShockLauncher/bin/Debug/net9.0/TShock.Server.dll",
"args": [],
"cwd": "${workspaceFolder}/TShockLauncher/bin/Debug/net6.0/",
"cwd": "${workspaceFolder}/TShockLauncher/bin/Debug/net9.0/",
"console": "integratedTerminal",
"stopAtEntry": false
},
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
{
"label": "Remote Publish",
"options": {
"cwd": "TShockLauncher/bin/Debug/net6.0/linux-arm64"
"cwd": "TShockLauncher/bin/Debug/net9.0/linux-arm64"
},
"command": "C:\\Program Files\\PuTTY\\pscp.exe",
"type": "process",
Expand Down
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TARGETPLATFORM and BUILDPLATFORM are automatically filled in by Docker buildx.
# They should not be set in the global scope manually.

FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/dotnet/sdk:6.0 AS builder
FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/dotnet/sdk:9.0 AS builder

# Copy build context
WORKDIR /TShock
Expand All @@ -27,10 +27,14 @@ RUN \
*) echo "Error: Unsupported platform ${TARGETPLATFORM}" && exit 1 \
;; \
esac && \
dotnet publish -o output/ -r "${ARCH}" -v m -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false
dotnet publish -o output/ -r "${ARCH}" -v m -f net9.0 -c Release -p:PublishSingleFile=true --self-contained false

# Runtime image
FROM --platform=${TARGETPLATFORM} mcr.microsoft.com/dotnet/runtime:6.0 AS runner
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS linux_base
FROM mcr.microsoft.com/dotnet/runtime:9.0-nanoserver-ltsc2022 AS windows_base

FROM ${TARGETOS}_base AS final

WORKDIR /server
COPY --from=builder /TShock/TShockLauncher/output ./

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ If you want to run the `TShockLauncher` (which runs a server), run:
To produce a packaged release (suitable for distribution), run:

1. `cd TShockLauncher`
1. `dotnet publish -r win-x64 -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false`
1. `dotnet publish -r win-x64 -f net9.0 -c Release -p:PublishSingleFile=true --self-contained false`

Note that in this example, you'd be building for `win-x64`. You can build for `win-x64`, `osx-x64`, `linux-x64`, `linux-arm64`, `linux-arm`. Your release will be in the `TShockLauncher/bin/Release/net6.0/` folder under the architecture you specified.
Note that in this example, you'd be building for `win-x64`. You can build for `win-x64`, `osx-x64`, `linux-x64`, `linux-arm64`, `linux-arm`. Your release will be in the `TShockLauncher/bin/Release/net9.0/` folder under the architecture you specified.

### Working with Terraria

Expand Down
4 changes: 2 additions & 2 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ TShock是为泰拉瑞亚服务器和社区开发的一个工具箱。这个工
如果要生成打包后的发行版,运行:

1. `cd TShockLauncher`
1. `dotnet publish -r win-x64 -f net6.0 -c Release -p:PublishSingleFile=true --self-contained false`
1. `dotnet publish -r win-x64 -f net9.0 -c Release -p:PublishSingleFile=true --self-contained false`

注意在这个例子中你将会生成`win-x64`架构的版本。你也可以生成`win-x64`、`osx-x64`、`linux-x64`、`linux-arm64`、`linux-arm`的版本。你可以在`TShockLauncher/bin/Release/net6.0/`文件夹下对应架构的文件夹里找到生成后的发行版。
注意在这个例子中你将会生成`win-x64`架构的版本。你也可以生成`win-x64`、`osx-x64`、`linux-x64`、`linux-arm64`、`linux-arm`的版本。你可以在`TShockLauncher/bin/Release/net9.0/`文件夹下对应架构的文件夹里找到生成后的发行版。

### 跟泰拉瑞亚本体代码交互

Expand Down
18 changes: 4 additions & 14 deletions TShockAPI/TShock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,6 @@ public override void Initialize()
Hooks.AccountHooks.AccountDelete += OnAccountDelete;
Hooks.AccountHooks.AccountCreate += OnAccountCreate;

On.Terraria.RemoteClient.Reset += RemoteClient_Reset;

GetDataHandlers.InitGetDataHandler();
Commands.InitCommands();

Expand All @@ -449,7 +447,7 @@ public override void Initialize()
// Initialize the AchievementManager, which is normally only done on clients.
Game._achievements = new AchievementManager();

IL.Terraria.Initializers.AchievementInitializer.Load += OnAchievementInitializerLoad;
OTAPI.Hooks.Initializers.AchievementInitializerLoad += OnAchievementInitializerLoad;

// Actually call AchievementInitializer.Load, which is also normally only done on clients.
AchievementInitializer.Load();
Expand Down Expand Up @@ -498,17 +496,9 @@ void SafeError(string message)
}
}

private static void RemoteClient_Reset(On.Terraria.RemoteClient.orig_Reset orig, RemoteClient client)
{
client.ClientUUID = null;
orig(client);
}
Arthri marked this conversation as resolved.
Show resolved Hide resolved

private static void OnAchievementInitializerLoad(ILContext il)
private static void OnAchievementInitializerLoad(object sender, OTAPI.Hooks.Initializers.AchievementInitializerLoadEventArgs args)
{
// Modify AchievementInitializer.Load to remove the Main.netMode == 2 check (occupies the first 4 IL instructions)
for (var i = 0; i < 4; i++)
il.Body.Instructions.RemoveAt(0);
args.ShouldLoad = true;
}

protected void CrashReporter_HeapshotRequesting(object sender, EventArgs e)
Expand All @@ -532,7 +522,7 @@ protected override void Dispose(bool disposing)
}
SaveManager.Instance.Dispose();

IL.Terraria.Initializers.AchievementInitializer.Load -= OnAchievementInitializerLoad;
OTAPI.Hooks.Initializers.AchievementInitializerLoad -= OnAchievementInitializerLoad;

ModuleManager.Dispose();

Expand Down
8 changes: 4 additions & 4 deletions TShockAPI/TShockAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<!--
Expand Down Expand Up @@ -33,9 +33,9 @@

<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="GetText.NET" Version="1.7.14" />
<PackageReference Include="MySql.Data" Version="8.4.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.11" />
<PackageReference Include="GetText.NET" Version="8.0.5" />
<PackageReference Include="MySql.Data" Version="9.1.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions TShockInstaller/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

string? url = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
url = $"https://dotnetcli.azureedge.net/dotnet/Runtime/6.0.11/dotnet-runtime-6.0.11-osx-{arch}.tar.gz";
url = $"https://dotnetcli.azureedge.net/dotnet/Runtime/9.0.0/dotnet-runtime-9.0.0-osx-{arch}.tar.gz";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
url = $"https://dotnetcli.azureedge.net/dotnet/Runtime/6.0.11/dotnet-runtime-6.0.11-win-{arch}.zip";
url = $"https://dotnetcli.azureedge.net/dotnet/Runtime/9.0.0/dotnet-runtime-9.0.0-win-{arch}.zip";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
url = $"https://dotnetcli.azureedge.net/dotnet/Runtime/6.0.11/dotnet-runtime-6.0.11-linux-{arch}.tar.gz";
url = $"https://dotnetcli.azureedge.net/dotnet/Runtime/9.0.0/dotnet-runtime-9.0.0-linux-{arch}.tar.gz";

if(url is null)
{
Expand Down
2 changes: 1 addition & 1 deletion TShockInstaller/TShockInstaller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>5.0.0</Version>
Expand Down
6 changes: 3 additions & 3 deletions TShockLauncher.Tests/GroupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public void TestPermissions()
groups.AddPermissions("test", new() { "abc" });

var hasperm = groups.GetGroupByName("test").Permissions.Contains("abc");
Assert.IsTrue(hasperm);
Assert.That(hasperm, Is.True);

groups.DeletePermissions("test", new() { "abc" });

hasperm = groups.GetGroupByName("test").Permissions.Contains("abc");
Assert.IsFalse(hasperm);
Assert.That(hasperm, Is.False);

groups.DeleteGroup("test");

var g = groups.GetGroupByName("test");
Assert.IsNull(g);
Assert.That(g, Is.Null);
}
}

11 changes: 6 additions & 5 deletions TShockLauncher.Tests/ServerInitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ public class ServerInitTests
public void EnsureBoots()
{
var are = new AutoResetEvent(false);
On.Terraria.Main.hook_DedServ cb = (On.Terraria.Main.orig_DedServ orig, Terraria.Main instance) =>
HookEvents.HookDelegate<Terraria.Main, HookEvents.Terraria.Main.DedServEventArgs> cb = (instance, args) =>
{
args.ContinueExecution = false;
are.Set();
Debug.WriteLine("Server init process successful");
};
On.Terraria.Main.DedServ += cb;
HookEvents.Terraria.Main.DedServ += cb;

new Thread(() => TerrariaApi.Server.Program.Main(new string[] { })).Start();
new Thread(() => TerrariaApi.Server.Program.Main([])).Start();

var hit = are.WaitOne(TimeSpan.FromSeconds(10));

On.Terraria.Main.DedServ -= cb;
HookEvents.Terraria.Main.DedServ -= cb;

Assert.IsTrue(hit);
Assert.That(hit, Is.True);
}
}

15 changes: 9 additions & 6 deletions TShockLauncher.Tests/TShockLauncher.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.5.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
22 changes: 11 additions & 11 deletions TShockLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,11 @@ You should have received a copy of the GNU General Public License
return 1;
}

if (args.Length > 0 && args[0].ToLower() == "plugins")
{
var items = args.ToList();
items.RemoveAt(0);
await NugetCLI.Main(items);
return 0;
}


Dictionary<string, Assembly> _cache = new Dictionary<string, Assembly>();

System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += Default_Resolving;

return Start();
return await StartAsync();

/// <summary>
/// Resolves a module from the ./bin folder, either with a .dll by preference or .exe
Expand All @@ -71,6 +62,7 @@ You should have received a copy of the GNU General Public License
if (_cache.TryGetValue(arg2.Name, out Assembly? asm) && asm is not null) return asm;

var loc = Path.Combine(AppContext.BaseDirectory, "bin", arg2.Name + ".dll");

if (File.Exists(loc))
asm = arg1.LoadFromAssemblyPath(loc);

Expand All @@ -88,8 +80,16 @@ You should have received a copy of the GNU General Public License
/// Initiates the TSAPI server.
/// </summary>
/// <remarks>This method exists so that the resolver can attach before TSAPI needs its dependencies.</remarks>
int Start()
async Task<int> StartAsync()
{
if (args.Length > 0 && args[0].ToLower() == "plugins")
{
var items = args.ToList();
items.RemoveAt(0);
await TShockPluginManager.NugetCLI.Main(items);
return 0;
}

TerrariaApi.Server.Program.Main(args);
return 0;
}
Loading
Loading