Skip to content

Commit 80c5b7b

Browse files
MIRIMIRIMtautcony
andauthored
Feat/aot (#14)
* chore: upgrade to dotnet 8 * feat: adapt source generation * feat: replace CommandLineParser with System.CommandLine Signed-off-by: MIRIMIRIM <[email protected]> * feat: upgrade System.CommandLine Signed-off-by: MIRIMIRIM <[email protected]> * Update OKP.Core.csproj * fix warning CS8604 * use Tommy instead of Tomlyn * not use gha build nativeaot * feat: remove unused satellite resource languages --------- Signed-off-by: MIRIMIRIM <[email protected]> Co-authored-by: TautCony <[email protected]>
1 parent 4da9c30 commit 80c5b7b

11 files changed

+343
-172
lines changed

.github/workflows/dotnet-ci.yml

+17-17
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ jobs:
2020
- name: Setup .NET Core SDK
2121
uses: actions/setup-dotnet@v3
2222
with:
23-
dotnet-version: 6.x
23+
dotnet-version: 8.x
2424

2525
- name: Publish the application
26-
run: dotnet publish OKP.Core --configuration ${{ matrix.configuration }} --runtime ${{ matrix.runtime-identifier }} --self-contained false
26+
run: dotnet publish OKP.Core --configuration ${{ matrix.configuration }} --runtime ${{ matrix.runtime-identifier }} /p:PublishAot=false
2727

2828
- name: Upload build artifacts
2929
uses: actions/upload-artifact@v3
3030
with:
3131
name: OKP.Core-${{ matrix.runtime-identifier }}-${{ matrix.configuration }}
3232
path: |
33-
OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*
34-
!OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*.pdb
35-
!OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*.dbg
36-
!OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*.dwarf
33+
OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*
34+
!OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*.pdb
35+
!OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*.dbg
36+
!OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*.dwarf
3737
3838
glue:
3939
name: 'Build (Release, osx-universal)'
@@ -43,8 +43,8 @@ jobs:
4343
steps:
4444
- name: Setup working directory
4545
run: |
46-
mkdir -p OKP.Core/bin/Release/net6.0/osx-x64/publish
47-
mkdir -p OKP.Core/bin/Release/net6.0/osx-arm64/publish
46+
mkdir -p OKP.Core/bin/Release/net8.0/osx-x64/publish
47+
mkdir -p OKP.Core/bin/Release/net8.0/osx-arm64/publish
4848
4949
- name: Setup lipo
5050
run: |
@@ -56,26 +56,26 @@ jobs:
5656
uses: actions/download-artifact@v3
5757
with:
5858
name: OKP.Core-osx-x64-Release
59-
path: OKP.Core/bin/Release/net6.0/osx-x64/publish
59+
path: OKP.Core/bin/Release/net8.0/osx-x64/publish
6060

6161
- name: Download arm64 build
6262
uses: actions/download-artifact@v3
6363
with:
6464
name: OKP.Core-osx-arm64-Release
65-
path: OKP.Core/bin/Release/net6.0/osx-arm64/publish
65+
path: OKP.Core/bin/Release/net8.0/osx-arm64/publish
6666

6767
- name: Glue the binary
6868
run: |
69-
mkdir -p OKP.Core/bin/Release/net6.0/osx-universal/publish
70-
lipo -create OKP.Core/bin/Release/net6.0/osx-x64/publish/OKP.Core OKP.Core/bin/Release/net6.0/osx-arm64/publish/OKP.Core -output OKP.Core/bin/Release/net6.0/osx-universal/publish/OKP.Core
71-
mv OKP.Core/bin/Release/net6.0/osx-x64/publish/config OKP.Core/bin/Release/net6.0/osx-universal/publish/config
69+
mkdir -p OKP.Core/bin/Release/net8.0/osx-universal/publish
70+
lipo -create OKP.Core/bin/Release/net8.0/osx-x64/publish/OKP.Core OKP.Core/bin/Release/net8.0/osx-arm64/publish/OKP.Core -output OKP.Core/bin/Release/net8.0/osx-universal/publish/OKP.Core
71+
mv OKP.Core/bin/Release/net8.0/osx-x64/publish/config OKP.Core/bin/Release/net8.0/osx-universal/publish/config
7272
7373
- name: Upload build artifacts
7474
uses: actions/upload-artifact@v3
7575
with:
7676
name: OKP.Core-osx-universal-Release
7777
path: |
78-
OKP.Core/bin/Release/net6.0/osx-universal/publish/*
79-
!OKP.Core/bin/Release/net6.0/osx-universal/publish/*.pdb
80-
!OKP.Core/bin/Release/net6.0/osx-universal/publish/*.dbg
81-
!OKP.Core/bin/Release/net6.0/osx-universal/publish/*.dwarf
78+
OKP.Core/bin/Release/net8.0/osx-universal/publish/*
79+
!OKP.Core/bin/Release/net8.0/osx-universal/publish/*.pdb
80+
!OKP.Core/bin/Release/net8.0/osx-universal/publish/*.dbg
81+
!OKP.Core/bin/Release/net8.0/osx-universal/publish/*.dwarf

OKP.Core/Interface/Acgnx/AcgnxAdapter.cs

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Serilog;
33
using System.Net;
44
using System.Net.Http.Json;
5-
using System.Text.Json;
65
using System.Text.Json.Serialization;
76
using static OKP.Core.Interface.TorrentContent;
87

@@ -101,11 +100,7 @@ public override async Task<HttpResult> PingAsync()
101100

102101
if (result.IsSuccessStatusCode && !raw.Contains("<html"))
103102
{
104-
var apiContent = await result.Content.ReadFromJsonAsync<AcgnxApiStatus>(new JsonSerializerOptions
105-
{
106-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
107-
NumberHandling = JsonNumberHandling.AllowReadingFromString
108-
});
103+
var apiContent = await result.Content.ReadFromJsonAsync(AcgnxModelsSourceGenerationContext.Default.AcgnxApiStatus);
109104
if (apiContent == null)
110105
{
111106
Log.Error("{Site} api server down", site);
@@ -160,11 +155,7 @@ public override async Task<HttpResult> PostAsync()
160155
Log.Verbose("{Site} formdata content: {@MultipartFormDataContent}", site, form);
161156
var result = await httpClient.PostAsyncWithRetry(apiUrl, form);
162157
var raw = await result.Content.ReadAsStringAsync();
163-
var apiContent = await result.Content.ReadFromJsonAsync<AcgnxApiStatus>(new JsonSerializerOptions
164-
{
165-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
166-
NumberHandling = JsonNumberHandling.AllowReadingFromString
167-
});
158+
var apiContent = await result.Content.ReadFromJsonAsync(AcgnxModelsSourceGenerationContext.Default.AcgnxApiStatus);
168159

169160
if (result.StatusCode == HttpStatusCode.OK && apiContent != null && !raw.Contains("<html"))
170161
{
@@ -223,4 +214,11 @@ internal class AcgnxApiStatus
223214
public string? Infohash { get; set; }
224215
public string? Title { get; set; }
225216
}
217+
218+
[JsonSerializable(typeof(AcgnxApiStatus))]
219+
[JsonSourceGenerationOptions(
220+
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
221+
NumberHandling = JsonNumberHandling.AllowReadingFromString)
222+
]
223+
internal partial class AcgnxModelsSourceGenerationContext : JsonSerializerContext;
226224
}

OKP.Core/Interface/Bangumi/BangumiAdapter.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override async Task<HttpResult> PingAsync()
5353
{
5454
var pingReq = await httpClient.GetAsync(pingUrl);
5555
var raw = await pingReq.Content.ReadAsStringAsync();
56-
var teamList = await pingReq.Content.ReadFromJsonAsync<TeamInfo[]>();
56+
var teamList = await pingReq.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.TeamInfoArray);
5757
if (!pingReq.IsSuccessStatusCode || teamList == null)
5858
{
5959
Log.Error("Cannot connect to {Site}.{NewLine}" +
@@ -107,9 +107,9 @@ public override async Task<HttpResult> PostAsync()
107107
teamsync = false,
108108
file_id = fileId,
109109
};
110-
var response = await httpClient.PostAsJsonAsyncWithRetry(postUrl, addRequest);
110+
var response = await httpClient.PostAsJsonAsyncWithRetry(postUrl, addRequest, BangumiModelsSourceGenerationContext.Default.AddRequest);
111111
var raw = await response.Content.ReadAsStringAsync();
112-
var result = await response.Content.ReadFromJsonAsync<AddResponse>();
112+
var result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.AddResponse);
113113
if (!response.IsSuccessStatusCode || result == null)
114114
{
115115
Log.Error("{Site} upload failed. Unknown reson. {NewLine} {Raw}", site, Environment.NewLine, raw);
@@ -134,13 +134,13 @@ private async Task<string> UploadTorrent(TorrentContent torrent)
134134
{ torrent.Data.ByteArrayContent, "file", torrent.Data.FileInfo.Name }
135135
};
136136
var response = await httpClient.PostAsyncWithRetry(uploadUrl, form);
137-
var result = await response.Content.ReadFromJsonAsync<UploadResponse>();
137+
var result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.UploadResponse);
138138
if (result == null || !result.success || result.file_id == null)
139139
{
140140
Log.Debug("可能是 {Site} 发布频率限制,先等 1 分钟", site);
141141
Thread.Sleep(60000);
142142
response = await httpClient.PostAsyncWithRetry(uploadUrl, form);
143-
result = await response.Content.ReadFromJsonAsync<UploadResponse>();
143+
result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.UploadResponse);
144144
if (result == null || !result.success || result.file_id == null)
145145
{
146146
throw new HttpRequestException("Failed to upload torrent file");

OKP.Core/Interface/Bangumi/BangumiModels.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// ReSharper disable InconsistentNaming, IdentifierTypo
2+
3+
using System.Text.Json.Serialization;
4+
25
namespace OKP.Core.Interface.Bangumi
36
{
47
#pragma warning disable CS8618
@@ -75,6 +78,18 @@ public class TeamInfo
7578
public DateTime regDate { get; set; }
7679
public bool approved { get; set; }
7780
}
78-
7981
}
82+
83+
[JsonSerializable(typeof(BangumiModels.UploadResponse))]
84+
[JsonSerializable(typeof(BangumiModels.AddRequest))]
85+
[JsonSerializable(typeof(BangumiModels.AddResponse))]
86+
[JsonSerializable(typeof(BangumiModels.Torrent))]
87+
[JsonSerializable(typeof(BangumiModels.TeamList))]
88+
[JsonSerializable(typeof(BangumiModels.TeamInfo))]
89+
[JsonSerializable(typeof(BangumiModels.TeamInfo[]))]
90+
[JsonSourceGenerationOptions(
91+
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
92+
NumberHandling = JsonNumberHandling.AllowReadingFromString)
93+
]
94+
internal partial class BangumiModelsSourceGenerationContext : JsonSerializerContext;
8095
}

OKP.Core/Interface/TorrentContent.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Net.Http.Headers;
77
using System.Text;
88
using System.Text.RegularExpressions;
9-
using Tomlyn;
109

1110
namespace OKP.Core.Interface
1211
{
@@ -46,8 +45,8 @@ public TorrentData(string filename)
4645
public string? FilenameRegex { get; set; }
4746
public string? ResolutionRegex { get; set; }
4847
public string? SettingPath { get; set; }
49-
public bool HasSubtitle { get; set; }
50-
public bool IsFinished { get; set; }
48+
// public bool HasSubtitle { get; set; }
49+
// public bool IsFinished { get; set; }
5150
public string? CookiePath { get; set; }
5251
public List<ContentTypes>? Tags { get; set; }
5352
public List<NyaaTorrentFlags>? TorrentFlags { get; set; }
@@ -116,7 +115,7 @@ public static TorrentContent Build(string filename, string settingFile, string a
116115
throw new IOException();
117116
}
118117

119-
var torrentC = Toml.ToModel<TorrentContent>(File.ReadAllText(settingFilePath));
118+
var torrentC = TomlParseHelper.DeserializeTorrentContent(settingFilePath);
120119
torrentC.SettingPath = Path.GetDirectoryName(settingFilePath);
121120
//if (!File.Exists(torrentC.CookiePath))
122121
//{
@@ -172,7 +171,7 @@ public static TorrentContent Build(string filename, string settingFile, string a
172171
var userPropPath = IOHelper.BasePath(Constants.DefaultUserPropsPath, Constants.UserPropertiesFileName);
173172
if (File.Exists(userPropPath))
174173
{
175-
var userProp = Toml.ToModel<UserProperties>(File.ReadAllText(userPropPath));
174+
var userProp = TomlParseHelper.DeserializeUserProperties(userPropPath);
176175

177176
if (userProp.UserProp == null)
178177
return torrentC;

OKP.Core/OKP.Core.csproj

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8-
<PublishSingleFile>true</PublishSingleFile>
8+
<PublishAot>true</PublishAot>
99
<Version>1.0.2-beta</Version>
1010
<Copyright>Copyright (C) 2023 AmusementClub
1111
Released under the GNU GPLv3+.
1212
</Copyright>
13+
<InvariantGlobalization>true</InvariantGlobalization>
14+
<SatelliteResourceLanguages>en-US</SatelliteResourceLanguages>
1315
</PropertyGroup>
1416

1517
<ItemGroup>
16-
<PackageReference Include="BencodeNET" Version="4.0.0" />
17-
<PackageReference Include="CommandLineParser" Version="2.9.1" />
18+
<PackageReference Include="BencodeNET" Version="5.0.0" />
1819
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
19-
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
20-
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
21-
<PackageReference Include="Tomlyn" Version="0.16.2" />
20+
<PackageReference Include="Serilog" Version="4.0.1" />
21+
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
22+
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
23+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.24324.3" />
24+
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.24324.3" />
25+
<PackageReference Include="Tommy" Version="3.1.2" />
2226
</ItemGroup>
2327

2428
<ItemGroup>

0 commit comments

Comments
 (0)