forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResSecrets.cs
More file actions
73 lines (69 loc) · 3.07 KB
/
Copy pathResSecrets.cs
File metadata and controls
73 lines (69 loc) · 3.07 KB
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
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using _ThisAssembly = System.Properties.ThisAssembly;
namespace System.Application.Security
{
internal static class ResSecrets
{
public const string Prefix_UIRes = "System.Application.UI.Resources.";
public const string Prefix_Res = "System.Application.Resources.";
internal static string? GetResValue(string name, bool isSingle, ResValueFormat format, string namespacePrefix = Prefix_UIRes, Assembly? assembly = null)
{
assembly ??= Assembly.GetExecutingAssembly();
Stream? func(string x) => assembly.GetManifestResourceStream(x);
var r = GetResValueCore(func, name, isSingle, namespacePrefix, format);
return r;
}
internal enum ResValueFormat : byte
{
String,
StringGuidD,
StringGuidN,
}
static string? GetResValueCore(Func<string, Stream?> func, string name, bool isSingle, string namespacePrefix, ResValueFormat format)
{
static string GetResFileName(string name, bool isSingle) => isSingle ? $"{name}.pfx" : $"{name}-{(_ThisAssembly.Debuggable ? "debug" : "release")}.pfx";
static string GetResFileNameH(string name) => Hashs.String.Crc32(name, isLower: false);
static string GetResFullName(string name, string namespacePrefix) => string.IsNullOrEmpty(namespacePrefix) ? name : namespacePrefix + name;
static MemoryStream GetMemoryStream(Stream stream)
{
if (stream is not MemoryStream ms)
{
ms = new MemoryStream();
stream.CopyTo(ms);
}
return ms;
}
static string ReadStream(Stream stream, ResValueFormat format)
{
if (format == ResValueFormat.String)
{
using var reader = new StreamReader(stream, EncodingCache.UTF8NoBOM);
return reader.ReadToEnd();
}
if (format == ResValueFormat.StringGuidD || format == ResValueFormat.StringGuidN)
{
using var ms = GetMemoryStream(stream);
var guid = new Guid(ms.ToArray());
return format switch
{
ResValueFormat.StringGuidD => guid.ToString("D"),
ResValueFormat.StringGuidN => guid.ToString("N"),
_ => throw new ArgumentOutOfRangeException(nameof(format), format, null),
};
}
else
{
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}
name = GetResFileName(name, isSingle);
name = GetResFileNameH(name);
name = GetResFullName(name, namespacePrefix);
using var stream = func(name);
if (stream != null) return ReadStream(stream, format);
return null;
}
}
}