Skip to content

Commit

Permalink
Feat/aot (#14)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
MIRIMIRIM and tautcony authored Aug 13, 2024
1 parent 4da9c30 commit 80c5b7b
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 172 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ jobs:
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.x
dotnet-version: 8.x

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

- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: OKP.Core-${{ matrix.runtime-identifier }}-${{ matrix.configuration }}
path: |
OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*
!OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*.pdb
!OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*.dbg
!OKP.Core/bin/${{ matrix.configuration }}/net6.0/${{ matrix.runtime-identifier }}/publish/*.dwarf
OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*
!OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*.pdb
!OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*.dbg
!OKP.Core/bin/${{ matrix.configuration }}/net8.0/${{ matrix.runtime-identifier }}/publish/*.dwarf
glue:
name: 'Build (Release, osx-universal)'
Expand All @@ -43,8 +43,8 @@ jobs:
steps:
- name: Setup working directory
run: |
mkdir -p OKP.Core/bin/Release/net6.0/osx-x64/publish
mkdir -p OKP.Core/bin/Release/net6.0/osx-arm64/publish
mkdir -p OKP.Core/bin/Release/net8.0/osx-x64/publish
mkdir -p OKP.Core/bin/Release/net8.0/osx-arm64/publish
- name: Setup lipo
run: |
Expand All @@ -56,26 +56,26 @@ jobs:
uses: actions/download-artifact@v3
with:
name: OKP.Core-osx-x64-Release
path: OKP.Core/bin/Release/net6.0/osx-x64/publish
path: OKP.Core/bin/Release/net8.0/osx-x64/publish

- name: Download arm64 build
uses: actions/download-artifact@v3
with:
name: OKP.Core-osx-arm64-Release
path: OKP.Core/bin/Release/net6.0/osx-arm64/publish
path: OKP.Core/bin/Release/net8.0/osx-arm64/publish

- name: Glue the binary
run: |
mkdir -p OKP.Core/bin/Release/net6.0/osx-universal/publish
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
mv OKP.Core/bin/Release/net6.0/osx-x64/publish/config OKP.Core/bin/Release/net6.0/osx-universal/publish/config
mkdir -p OKP.Core/bin/Release/net8.0/osx-universal/publish
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
mv OKP.Core/bin/Release/net8.0/osx-x64/publish/config OKP.Core/bin/Release/net8.0/osx-universal/publish/config
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: OKP.Core-osx-universal-Release
path: |
OKP.Core/bin/Release/net6.0/osx-universal/publish/*
!OKP.Core/bin/Release/net6.0/osx-universal/publish/*.pdb
!OKP.Core/bin/Release/net6.0/osx-universal/publish/*.dbg
!OKP.Core/bin/Release/net6.0/osx-universal/publish/*.dwarf
OKP.Core/bin/Release/net8.0/osx-universal/publish/*
!OKP.Core/bin/Release/net8.0/osx-universal/publish/*.pdb
!OKP.Core/bin/Release/net8.0/osx-universal/publish/*.dbg
!OKP.Core/bin/Release/net8.0/osx-universal/publish/*.dwarf
20 changes: 9 additions & 11 deletions OKP.Core/Interface/Acgnx/AcgnxAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Serilog;
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using static OKP.Core.Interface.TorrentContent;

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

if (result.IsSuccessStatusCode && !raw.Contains("<html"))
{
var apiContent = await result.Content.ReadFromJsonAsync<AcgnxApiStatus>(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString
});
var apiContent = await result.Content.ReadFromJsonAsync(AcgnxModelsSourceGenerationContext.Default.AcgnxApiStatus);
if (apiContent == null)
{
Log.Error("{Site} api server down", site);
Expand Down Expand Up @@ -160,11 +155,7 @@ public override async Task<HttpResult> PostAsync()
Log.Verbose("{Site} formdata content: {@MultipartFormDataContent}", site, form);
var result = await httpClient.PostAsyncWithRetry(apiUrl, form);
var raw = await result.Content.ReadAsStringAsync();
var apiContent = await result.Content.ReadFromJsonAsync<AcgnxApiStatus>(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString
});
var apiContent = await result.Content.ReadFromJsonAsync(AcgnxModelsSourceGenerationContext.Default.AcgnxApiStatus);

if (result.StatusCode == HttpStatusCode.OK && apiContent != null && !raw.Contains("<html"))
{
Expand Down Expand Up @@ -223,4 +214,11 @@ internal class AcgnxApiStatus
public string? Infohash { get; set; }
public string? Title { get; set; }
}

[JsonSerializable(typeof(AcgnxApiStatus))]
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString)
]
internal partial class AcgnxModelsSourceGenerationContext : JsonSerializerContext;
}
10 changes: 5 additions & 5 deletions OKP.Core/Interface/Bangumi/BangumiAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override async Task<HttpResult> PingAsync()
{
var pingReq = await httpClient.GetAsync(pingUrl);
var raw = await pingReq.Content.ReadAsStringAsync();
var teamList = await pingReq.Content.ReadFromJsonAsync<TeamInfo[]>();
var teamList = await pingReq.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.TeamInfoArray);
if (!pingReq.IsSuccessStatusCode || teamList == null)
{
Log.Error("Cannot connect to {Site}.{NewLine}" +
Expand Down Expand Up @@ -107,9 +107,9 @@ public override async Task<HttpResult> PostAsync()
teamsync = false,
file_id = fileId,
};
var response = await httpClient.PostAsJsonAsyncWithRetry(postUrl, addRequest);
var response = await httpClient.PostAsJsonAsyncWithRetry(postUrl, addRequest, BangumiModelsSourceGenerationContext.Default.AddRequest);
var raw = await response.Content.ReadAsStringAsync();
var result = await response.Content.ReadFromJsonAsync<AddResponse>();
var result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.AddResponse);
if (!response.IsSuccessStatusCode || result == null)
{
Log.Error("{Site} upload failed. Unknown reson. {NewLine} {Raw}", site, Environment.NewLine, raw);
Expand All @@ -134,13 +134,13 @@ private async Task<string> UploadTorrent(TorrentContent torrent)
{ torrent.Data.ByteArrayContent, "file", torrent.Data.FileInfo.Name }
};
var response = await httpClient.PostAsyncWithRetry(uploadUrl, form);
var result = await response.Content.ReadFromJsonAsync<UploadResponse>();
var result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.UploadResponse);
if (result == null || !result.success || result.file_id == null)
{
Log.Debug("可能是 {Site} 发布频率限制,先等 1 分钟", site);
Thread.Sleep(60000);
response = await httpClient.PostAsyncWithRetry(uploadUrl, form);
result = await response.Content.ReadFromJsonAsync<UploadResponse>();
result = await response.Content.ReadFromJsonAsync(BangumiModelsSourceGenerationContext.Default.UploadResponse);
if (result == null || !result.success || result.file_id == null)
{
throw new HttpRequestException("Failed to upload torrent file");
Expand Down
17 changes: 16 additions & 1 deletion OKP.Core/Interface/Bangumi/BangumiModels.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// ReSharper disable InconsistentNaming, IdentifierTypo

using System.Text.Json.Serialization;

namespace OKP.Core.Interface.Bangumi
{
#pragma warning disable CS8618
Expand Down Expand Up @@ -75,6 +78,18 @@ public class TeamInfo
public DateTime regDate { get; set; }
public bool approved { get; set; }
}

}

[JsonSerializable(typeof(BangumiModels.UploadResponse))]
[JsonSerializable(typeof(BangumiModels.AddRequest))]
[JsonSerializable(typeof(BangumiModels.AddResponse))]
[JsonSerializable(typeof(BangumiModels.Torrent))]
[JsonSerializable(typeof(BangumiModels.TeamList))]
[JsonSerializable(typeof(BangumiModels.TeamInfo))]
[JsonSerializable(typeof(BangumiModels.TeamInfo[]))]
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString)
]
internal partial class BangumiModelsSourceGenerationContext : JsonSerializerContext;
}
9 changes: 4 additions & 5 deletions OKP.Core/Interface/TorrentContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using Tomlyn;

namespace OKP.Core.Interface
{
Expand Down Expand Up @@ -46,8 +45,8 @@ public TorrentData(string filename)
public string? FilenameRegex { get; set; }
public string? ResolutionRegex { get; set; }
public string? SettingPath { get; set; }
public bool HasSubtitle { get; set; }
public bool IsFinished { get; set; }
// public bool HasSubtitle { get; set; }
// public bool IsFinished { get; set; }
public string? CookiePath { get; set; }
public List<ContentTypes>? Tags { get; set; }
public List<NyaaTorrentFlags>? TorrentFlags { get; set; }
Expand Down Expand Up @@ -116,7 +115,7 @@ public static TorrentContent Build(string filename, string settingFile, string a
throw new IOException();
}

var torrentC = Toml.ToModel<TorrentContent>(File.ReadAllText(settingFilePath));
var torrentC = TomlParseHelper.DeserializeTorrentContent(settingFilePath);
torrentC.SettingPath = Path.GetDirectoryName(settingFilePath);
//if (!File.Exists(torrentC.CookiePath))
//{
Expand Down Expand Up @@ -172,7 +171,7 @@ public static TorrentContent Build(string filename, string settingFile, string a
var userPropPath = IOHelper.BasePath(Constants.DefaultUserPropsPath, Constants.UserPropertiesFileName);
if (File.Exists(userPropPath))
{
var userProp = Toml.ToModel<UserProperties>(File.ReadAllText(userPropPath));
var userProp = TomlParseHelper.DeserializeUserProperties(userPropPath);

if (userProp.UserProp == null)
return torrentC;
Expand Down
18 changes: 11 additions & 7 deletions OKP.Core/OKP.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<PublishAot>true</PublishAot>
<Version>1.0.2-beta</Version>
<Copyright>Copyright (C) 2023 AmusementClub
Released under the GNU GPLv3+.
</Copyright>
<InvariantGlobalization>true</InvariantGlobalization>
<SatelliteResourceLanguages>en-US</SatelliteResourceLanguages>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BencodeNET" Version="4.0.0" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="BencodeNET" Version="5.0.0" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Tomlyn" Version="0.16.2" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.24324.3" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.24324.3" />
<PackageReference Include="Tommy" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 80c5b7b

Please sign in to comment.