Skip to content

Commit 2f80bfd

Browse files
committed
*
1 parent d30e9ce commit 2f80bfd

File tree

7 files changed

+85
-83
lines changed

7 files changed

+85
-83
lines changed
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Json;
2+
using Flurl.Http;
23
using Kantan.Net;
34
using Newtonsoft.Json.Linq;
45

@@ -10,22 +11,22 @@ namespace SmartImage.Lib.Clients;
1011

1112
public sealed class AnilistClient : IDisposable
1213
{
13-
private readonly GraphQLClient m_client;
14+
private readonly GraphQLClient m_client;
1415

15-
public AnilistClient()
16-
{
17-
m_client = new GraphQLClient("https://graphql.anilist.co");
18-
}
16+
public AnilistClient()
17+
{
18+
m_client = new GraphQLClient("https://graphql.anilist.co");
19+
}
1920

20-
public async Task<string> GetTitleAsync(int anilistId)
21-
{
22-
/*
21+
public async Task<string> GetTitleAsync(int anilistId)
22+
{
23+
/*
2324
* https://anilist.gitbook.io/anilist-apiv2-docs/overview/graphql
2425
* https://anilist.gitbook.io/anilist-apiv2-docs/overview/graphql/getting-started
2526
* https://graphql.org/learn/queries/
2627
*/
2728

28-
const string GRAPH_QUERY = @"query ($id: Int) { # Define which variables will be used in the query (id)
29+
const string GRAPH_QUERY = @"query ($id: Int) { # Define which variables will be used in the query (id)
2930
Media(id: $id, type: ANIME) { # Insert our variables into the query arguments (id) (type: ANIME is hard-coded in the query)
3031
id
3132
title {
@@ -36,21 +37,21 @@ public async Task<string> GetTitleAsync(int anilistId)
3637
}
3738
}";
3839

39-
var response = await m_client.ExecuteAsync(GRAPH_QUERY, new
40-
{
41-
query = GRAPH_QUERY,
42-
id = anilistId
43-
});
40+
var response = await m_client.ExecuteAsync(GRAPH_QUERY, new
41+
{
42+
query = GRAPH_QUERY,
43+
id = anilistId
44+
});
4445

45-
return response["data"]["Media"]["title"]["english"];
46-
}
46+
return response["data"]["Media"]["title"]["english"];
47+
}
4748

48-
#region IDisposable
49+
#region IDisposable
4950

50-
public void Dispose()
51-
{
52-
m_client.Dispose();
53-
}
51+
public void Dispose()
52+
{
53+
m_client.Dispose();
54+
}
5455

55-
#endregion
56+
#endregion
5657
}

SmartImage.Lib 3/Engines/Impl/Search/Ascii2DEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override bool VerifyQuery(SearchQuery q)
4444

4545
}
4646
if (ok) {
47-
b2 = q.Image.Width < MAX_WIDTH;
47+
b2 = q.ImageInfo.Width < MAX_WIDTH;
4848
}
4949
else {
5050
b2 = true;

SmartImage.Lib 3/Engines/Impl/Upload/BaseUploadEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ static BaseUploadEngine()
7171
};
7272
}
7373

74-
protected virtual async Task<UploadResult> ProcessResultAsync(
75-
IFlurlResponse response, CancellationToken ct = default)
74+
protected virtual async Task<UploadResult> ProcessResultAsync(IFlurlResponse response,
75+
CancellationToken ct = default)
7676
{
7777
string url = null;
7878
bool ok;

SmartImage.Lib 3/SearchClient.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public static void Init()
8888
/// <param name="reload"></param>
8989
/// <param name="token">Cancellation token passed to <see cref="BaseSearchEngine.GetResultAsync"/></param>
9090
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload = true,
91-
CancellationToken token = default)
91+
CancellationToken token = default, TaskScheduler scheduler = default)
9292
{
93+
scheduler??= TaskScheduler.Default;
9394
if (!query.IsUploaded) {
9495
throw new ArgumentException($"Query was not uploaded", nameof(query));
9596
}
@@ -105,7 +106,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload
105106

106107
Debug.WriteLine($"Config: {Config} | {Engines.QuickJoin()}");
107108

108-
var tasks = GetSearchTasks(query, token);
109+
var tasks = GetSearchTasks(query, token, scheduler);
109110

110111
var results = new SearchResult[tasks.Count];
111112
int i = 0;
@@ -216,7 +217,7 @@ private void OpenResult(SearchResult result)
216217

217218
}
218219

219-
public List<Task<SearchResult>> GetSearchTasks(SearchQuery query, CancellationToken token)
220+
public List<Task<SearchResult>> GetSearchTasks(SearchQuery query, CancellationToken token, TaskScheduler scheduler)
220221
{
221222

222223
var tasks = Engines.Select(e =>
@@ -226,9 +227,10 @@ public List<Task<SearchResult>> GetSearchTasks(SearchQuery query, CancellationTo
226227
var res = e.GetResultAsync(query, token)
227228
.ContinueWith( (r) =>
228229
{
230+
Debug.Assert(r.IsCompleted);
229231
ProcessResult(r.Result);
230232
return r.Result;
231-
}, token);
233+
}, token, continuationOptions: TaskContinuationOptions.None, scheduler);
232234

233235
return res;
234236
}).ToList();

SmartImage.Lib 3/SearchQuery.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
global using CBN = JetBrains.Annotations.CanBeNullAttribute;
33
global using NN = System.Diagnostics.CodeAnalysis.NotNullAttribute;
44
global using MNNW = System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute;
5+
global using ISImage=SixLabors.ImageSharp.Image;
56
using System.Diagnostics;
67
using System.Diagnostics.CodeAnalysis;
78
using System.Drawing;
@@ -10,6 +11,8 @@
1011
using Flurl.Http;
1112
using JetBrains.Annotations;
1213
using Novus.FileTypes;
14+
using Novus.Streams;
15+
using SixLabors.ImageSharp;
1316
using SmartImage.Lib.Engines;
1417
using SmartImage.Lib.Engines.Impl.Upload;
1518
using SmartImage.Lib.Utilities;
@@ -55,10 +58,10 @@ private SearchQuery() : this(null) { }
5558
static SearchQuery() { }
5659

5760
[MN]
58-
public Image Image { get; private set; }
61+
public ImageInfo ImageInfo { get; private set; }
5962

60-
[MNNW(true, nameof(Image))]
61-
public bool HasImage => Image != null;
63+
[MNNW(true, nameof(ImageInfo))]
64+
public bool HasImage => ImageInfo != null;
6265

6366
[MN]
6467
public string FilePath { get; private set; }
@@ -68,16 +71,17 @@ static SearchQuery() { }
6871

6972
public bool LoadImage()
7073
{
71-
if (HasUni && Image == null) {
74+
if (HasUni && ImageInfo == null) {
7275
if (HasFile) {
73-
Image = Image.FromFile(FilePath);
76+
ImageInfo = ISImage.Identify(FilePath);
7477
}
7578
else if (OperatingSystem.IsWindows()) {
76-
Image = Image.FromStream(Uni.Stream);
79+
Uni.Stream.TrySeek();
80+
ImageInfo = ISImage.Identify(Uni.Stream);
7781
}
7882

7983
}
80-
84+
8185
return HasImage;
8286
}
8387

@@ -168,7 +172,7 @@ public static bool IsValidSourceType(object str)
168172
public void Dispose()
169173
{
170174
Uni?.Dispose();
171-
Image?.Dispose();
175+
// ImageInfo?.Dispose();
172176
Debug.WriteLine($"Disposing {ValueString} w/ {Size}");
173177
}
174178

SmartImage.Lib 3/Utilities/NonPublicMembersConverter.cs

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,42 @@ namespace SmartImage.Lib.Utilities;
1010
#pragma warning disable CS0649
1111
public class NonPublicMembersConverter<T> : JsonConverter<T> where T : class
1212
{
13-
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14-
{
15-
T instance = (T)Activator.CreateInstance(typeToConvert, nonPublic: true);
16-
17-
while (reader.Read())
18-
{
19-
if (reader.TokenType == JsonTokenType.EndObject)
20-
{
21-
break;
22-
}
23-
24-
if (reader.TokenType != JsonTokenType.PropertyName)
25-
{
26-
throw new JsonException();
27-
}
28-
29-
string propertyName = reader.GetString();
30-
31-
PropertyInfo propertyInfo =
32-
typeToConvert.GetProperty(propertyName,
33-
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
34-
35-
if (propertyInfo != null && propertyInfo.CanWrite)
36-
{
37-
reader.Read(); // Move to the property value
38-
object value = JsonSerializer.Deserialize(ref reader, propertyInfo.PropertyType, options);
39-
propertyInfo.SetValue(instance, value);
40-
}
41-
else
42-
{
43-
reader.Skip();
44-
}
45-
}
46-
47-
return instance;
48-
}
49-
50-
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
51-
{
52-
JsonSerializer.Serialize(writer, value, options);
53-
}
13+
14+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
15+
{
16+
T instance = (T) Activator.CreateInstance(typeToConvert, nonPublic: true);
17+
18+
while (reader.Read()) {
19+
if (reader.TokenType == JsonTokenType.EndObject) {
20+
break;
21+
}
22+
23+
if (reader.TokenType != JsonTokenType.PropertyName) {
24+
throw new JsonException();
25+
}
26+
27+
string propertyName = reader.GetString();
28+
29+
PropertyInfo propertyInfo =
30+
typeToConvert.GetProperty(propertyName,
31+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
32+
33+
if (propertyInfo != null && propertyInfo.CanWrite) {
34+
reader.Read(); // Move to the property value
35+
object value = JsonSerializer.Deserialize(ref reader, propertyInfo.PropertyType, options);
36+
propertyInfo.SetValue(instance, value);
37+
}
38+
else {
39+
reader.Skip();
40+
}
41+
}
42+
43+
return instance;
44+
}
45+
46+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
47+
{
48+
JsonSerializer.Serialize(writer, value, options);
49+
}
50+
5451
}

SmartImage.UI/MainWindow.xaml.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ public MainWindow()
177177

178178
#region
179179

180-
private void OnValidationRaised(object sender, RoutedEventArgs e)
181-
{
182-
183-
}
180+
private void OnValidationRaised(object sender, RoutedEventArgs e) { }
184181

185182
private static readonly ILogger Logger = LoggerFactory
186183
.Create(builder => builder.AddDebug().AddProvider(new DebugLoggerProvider()))
@@ -891,7 +888,8 @@ private async Task RunAsync()
891888

892889
// HandleQueryAsync();
893890
try {
894-
var r = await Client.RunSearchAsync(Query, reload: false, token: m_cts.Token);
891+
var r = await Client.RunSearchAsync(Query, reload: false, token: m_cts.Token,
892+
scheduler: TaskScheduler.FromCurrentSynchronizationContext());
895893

896894
}
897895
catch (Exception e) {

0 commit comments

Comments
 (0)