forked from akira0245/EasyZoom
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.cs
167 lines (151 loc) · 6.45 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#nullable enable
using Dalamud.Interface.Textures.TextureWraps;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using Dalamud.Interface.Textures;
namespace EasyZoomReborn;
public static class Utils
{
internal static ConcurrentDictionary<string, ImageLoadingResult> CachedTextures = new();
internal static ConcurrentDictionary<(uint ID, bool HQ), ImageLoadingResult> CachedIcons = new();
private static readonly List<Func<byte[], byte[]>> _conversionsToBitmap = new() { b => b, };
static volatile bool ThreadRunning = false;
internal static HttpClient httpClient = null;
public static bool TryGetTextureWrap(string url, out IDalamudTextureWrap textureWrap)
{
ImageLoadingResult result;
if (!CachedTextures.TryGetValue(url, out result))
{
result = new();
CachedTextures[url] = result;
BeginThreadIfNotRunning();
}
textureWrap = result.Texture;
return result.Texture != null;
}
internal static void BeginThreadIfNotRunning()
{
httpClient ??= new()
{
Timeout = TimeSpan.FromSeconds(10),
};
if (ThreadRunning) return;
EasyZoomRebornPlugin.PluginLog.Verbose("Starting ThreadLoadImageHandler");
ThreadRunning = true;
new Thread(() =>
{
int idleTicks = 0;
Safe(delegate
{
while (idleTicks < 100)
{
Safe(delegate
{
{
if (CachedTextures.TryGetFirst(x => x.Value.IsCompleted == false, out var keyValuePair))
{
idleTicks = 0;
keyValuePair.Value.IsCompleted = true;
EasyZoomRebornPlugin.PluginLog.Verbose("Loading image " + keyValuePair.Key);
if (keyValuePair.Key.StartsWith("http:", StringComparison.OrdinalIgnoreCase) || keyValuePair.Key.StartsWith("https:", StringComparison.OrdinalIgnoreCase))
{
var result = httpClient.GetAsync(keyValuePair.Key).Result;
result.EnsureSuccessStatusCode();
var content = result.Content.ReadAsByteArrayAsync().Result;
IDalamudTextureWrap texture = null;
foreach (var conversion in _conversionsToBitmap)
{
if (conversion == null) continue;
try
{
texture = EasyZoomRebornPlugin.TextureProvider.CreateFromImageAsync(conversion(content)).Result;
if (texture != null) break;
}
catch (Exception ex)
{
EasyZoomRebornPlugin.PluginLog.Fatal("Exception in utils try get texture");
}
}
keyValuePair.Value.TextureWrap = texture;
}
else
{
if (File.Exists(keyValuePair.Key))
{
keyValuePair.Value.ImmediateTexture = EasyZoomRebornPlugin.TextureProvider.GetFromFile(keyValuePair.Key);
}
else
{
keyValuePair.Value.ImmediateTexture = EasyZoomRebornPlugin.TextureProvider.GetFromGame(keyValuePair.Key);
}
}
}
}
{
if (CachedIcons.TryGetFirst(x => x.Value.IsCompleted == false, out var keyValuePair))
{
idleTicks = 0;
keyValuePair.Value.IsCompleted = true;
EasyZoomRebornPlugin.PluginLog.Verbose($"Loading icon {keyValuePair.Key.ID}, hq={keyValuePair.Key.HQ}");
keyValuePair.Value.ImmediateTexture = EasyZoomRebornPlugin.TextureProvider.GetFromGameIcon(new(keyValuePair.Key.ID, hiRes:keyValuePair.Key.HQ));
}
}
});
idleTicks++;
if(!CachedTextures.Any(x => x.Value.IsCompleted) && !CachedIcons.Any(x => x.Value.IsCompleted)) Thread.Sleep(100);
}
});
EasyZoomRebornPlugin.PluginLog.Verbose($"Stopping ThreadLoadImageHandler, ticks={idleTicks}");
ThreadRunning = false;
}).Start();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Safe(System.Action a, bool suppressErrors = false)
{
try
{
a();
}
catch (Exception e)
{
if (!suppressErrors) EasyZoomRebornPlugin.PluginLog.Error($"{e.Message}\n{e.StackTrace ?? ""}");
}
}
public static bool TryGetFirst<K, V>(this IDictionary<K, V> dictionary, Func<KeyValuePair<K, V>, bool> predicate, out KeyValuePair<K, V> keyValuePair)
{
try
{
keyValuePair = dictionary.First(predicate);
return true;
}
catch(Exception)
{
keyValuePair = default;
return false;
}
}
}
internal class ImageLoadingResult
{
internal ISharedImmediateTexture? ImmediateTexture;
internal IDalamudTextureWrap? TextureWrap;
internal IDalamudTextureWrap? Texture => ImmediateTexture?.GetWrapOrDefault() ?? TextureWrap;
internal bool IsCompleted = false;
public ImageLoadingResult(ISharedImmediateTexture? immediateTexture)
{
ImmediateTexture = immediateTexture;
}
public ImageLoadingResult(IDalamudTextureWrap? textureWrap)
{
TextureWrap = textureWrap;
}
public ImageLoadingResult()
{
}
}