-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.Resource.cs
80 lines (74 loc) · 2.92 KB
/
Helper.Resource.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
using System.Diagnostics;
using System.Reflection;
using System.Resources;
namespace Helper
{
internal static class Resource
{
private const string RootNamespace = "Genshin.Downloader";
public static async Task<byte[]> GetBytesAsync(string name)
{
string resourceName = RootNamespace + "." + name;
Stream? stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName) ?? throw new FileLoadException(resourceName);
byte[] buffer = new byte[stream.Length];
await stream.ReadAsync(buffer);
stream.Close();
return buffer;
}
public static async Task<string> GetTempFileAsync(string name)
{
string path = Path.GetTempFileName();
byte[] bytes = await GetBytesAsync(name);
await File.WriteAllBytesAsync(path, bytes);
return path;
}
public static async Task<int> ProcessStartAsync(ProcessStartInfo info)
{
info.FileName = await GetTempFileAsync(info.FileName);
var process = Process.Start(info);
if (process is null)
{
File.Delete(info.FileName);
return -1;
}
else
{
process.Exited += (object? sender, EventArgs e) => File.Delete(info.FileName);
await process.WaitForExitAsync();
return process.ExitCode;
}
}
public static void MemoryManager(Form? form = null, Type? type = null)
{
const int MB = 1024 * 1024;
ResourceManager? resourceManager = type is null ? null : new(type);
long privateMemorySize64 = Process.GetCurrentProcess().PrivateMemorySize64;
//if (privateMemorySize64 >= MB * 128)
//{
// GC.Collect(2,
// privateMemorySize64 >= MB * 256 ?
// privateMemorySize64 >= MB * 1024 ?
// GCCollectionMode.Aggressive : GCCollectionMode.Forced : GCCollectionMode.Optimized,
// privateMemorySize64 >= MB * 512,
// privateMemorySize64 >= MB * 512);
// GC.WaitForFullGCComplete();
//}
if (form is not null && resourceManager is not null)
form.Text = $"{resourceManager.GetString("$this.Text")} ({privateMemorySize64 / MB} MB)";
}
//public static async Task<MethodInfo> GetMethod(string name)
//{
// try
// {
// byte[] buffer = await GetBytes(name);
// Assembly assembly = Assembly.Load(buffer);
// MethodInfo pointInfo = assembly.EntryPoint ?? throw new FileLoadException("Failed to get EntryPoint.", name);
// return pointInfo;
// }
// catch
// {
// throw;
// }
//}
}
}