misu – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.IO; |
||
4 | using System.Reflection; |
||
5 | using System.Text; |
||
6 | using System.Threading.Tasks; |
||
7 | using System.Windows.Forms; |
||
8 | using Microsoft.Win32; |
||
9 | |||
10 | namespace misu |
||
11 | { |
||
12 | public static class Utilities |
||
13 | { |
||
14 | #region Static Fields and Constants |
||
15 | |||
16 | public static readonly string AssemblyName = Assembly.GetEntryAssembly().GetName().Name; |
||
17 | |||
18 | public static readonly string AssemblyVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); |
||
19 | |||
20 | #endregion |
||
21 | |||
22 | #region Public Methods |
||
23 | |||
24 | public static async Task<byte[]> LoadResource(string resource) |
||
25 | { |
||
26 | var assembly = Assembly.GetExecutingAssembly(); |
||
27 | |||
28 | foreach (var assemblyResource in assembly.GetManifestResourceNames()) |
||
29 | { |
||
30 | if (!assemblyResource.Contains(resource)) |
||
31 | { |
||
32 | continue; |
||
33 | } |
||
34 | |||
35 | using (var manifestResourceStream = assembly.GetManifestResourceStream(assemblyResource)) |
||
36 | { |
||
37 | if (manifestResourceStream == null) |
||
38 | { |
||
39 | break; |
||
40 | } |
||
41 | |||
42 | var memoryStream = new MemoryStream(); |
||
43 | |||
44 | await manifestResourceStream.CopyToAsync(memoryStream); |
||
45 | |||
46 | memoryStream.Position = 0L; |
||
47 | |||
48 | return memoryStream.ToArray(); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | throw new ArgumentException("Resource not found."); |
||
53 | } |
||
54 | |||
55 | public static void Execute(this Control control, Action lambda) |
||
56 | { |
||
57 | if (control.InvokeRequired) |
||
58 | { |
||
59 | control.Invoke((MethodInvoker) lambda.Invoke); |
||
60 | } |
||
61 | else |
||
62 | { |
||
63 | lambda.Invoke(); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public static TValue MapValueToRange<TKey, TValue>(this TValue value, TKey xMin, TKey xMax, TValue yMin, |
||
68 | TValue yMax) |
||
69 | where TKey : struct, IComparable<TValue>, IConvertible |
||
70 | where TValue : struct, IComparable<TKey>, IConvertible |
||
71 | { |
||
72 | return (dynamic) yMin + |
||
73 | ((dynamic) yMax - (dynamic) yMin) * ((dynamic) value - (dynamic) xMin) / |
||
74 | ((dynamic) xMax - (dynamic) xMin); |
||
75 | } |
||
76 | |||
77 | public static IEnumerable<TU> SequenceSubtract<TU, TV>(this IEnumerable<TU> a, |
||
78 | IEnumerable<TV> b, |
||
79 | Func<TU, TV, bool> cmp) |
||
80 | { |
||
81 | var eb = new List<TV>(b); |
||
82 | |||
83 | using (var ea = a.GetEnumerator()) |
||
84 | { |
||
85 | while (ea.MoveNext()) |
||
86 | { |
||
87 | if (ea.Current == null) |
||
88 | { |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | foreach (var ib in eb) |
||
93 | { |
||
94 | if (cmp.Invoke(ea.Current, ib)) |
||
95 | { |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | yield return ea.Current; |
||
100 | |||
101 | break; |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /// <summary> |
||
108 | /// Determines if the sequence a is contained within b. |
||
109 | /// </summary> |
||
110 | /// <typeparam name="T">an equatable type</typeparam> |
||
111 | /// <param name="a">the first sequence</param> |
||
112 | /// <param name="b">the second sequence</param> |
||
113 | /// <returns>true if the first sequence is contained within the second sequence</returns> |
||
114 | public static bool SequenceContains<T>(this IEnumerable<T> a, IEnumerable<T> b) |
||
115 | { |
||
116 | using (var ea = a.GetEnumerator()) |
||
117 | { |
||
118 | using (var eb = b.GetEnumerator()) |
||
119 | { |
||
120 | var aea = ea.MoveNext(); |
||
121 | var aeb = eb.MoveNext(); |
||
122 | |||
123 | while (aea && aeb) |
||
124 | { |
||
125 | var eac = ea.Current; |
||
126 | var ebc = eb.Current; |
||
127 | |||
128 | if (eac.Equals(ebc)) |
||
129 | { |
||
130 | aea = ea.MoveNext(); |
||
131 | aeb = eb.MoveNext(); |
||
132 | |||
133 | continue; |
||
134 | } |
||
135 | |||
136 | aeb = eb.MoveNext(); |
||
137 | } |
||
138 | |||
139 | return !aea; |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /// <summary> |
||
145 | /// Convert Windows forms keys to a textual representation. |
||
146 | /// </summary> |
||
147 | /// <param name="key">the keys to convert</param> |
||
148 | /// <returns>a textual representation of the keys</returns> |
||
149 | /// <remarks>https://stackoverflow.com/questions/23170259/convert-keycode-to-char-string</remarks> |
||
150 | public static string KeysToString(this Keys key) |
||
151 | { |
||
152 | var keyboardState = new byte[255]; |
||
153 | var keyboardStateStatus = Natives.GetKeyboardState(keyboardState); |
||
154 | |||
155 | if (!keyboardStateStatus) |
||
156 | { |
||
157 | return ""; |
||
158 | } |
||
159 | |||
160 | var virtualKeyCode = (uint) key; |
||
161 | var scanCode = Natives.MapVirtualKey(virtualKeyCode, 0); |
||
162 | var inputLocaleIdentifier = Natives.GetKeyboardLayout(0); |
||
163 | |||
164 | var result = new StringBuilder(); |
||
165 | Natives.ToUnicodeEx(virtualKeyCode, scanCode, keyboardState, result, 5, 0, inputLocaleIdentifier); |
||
166 | |||
167 | return result.ToString(); |
||
168 | } |
||
169 | |||
170 | public static bool LaunchOnBootSet(bool enable) |
||
171 | { |
||
172 | using (var key = Registry.CurrentUser.OpenSubKey |
||
173 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
||
174 | { |
||
175 | if (key == null) |
||
176 | { |
||
177 | return false; |
||
178 | } |
||
179 | |||
180 | switch (enable) |
||
181 | { |
||
182 | case true: |
||
183 | key.SetValue(AssemblyName, Assembly.GetEntryAssembly().Location); |
||
184 | break; |
||
185 | default: |
||
186 | key.DeleteValue(AssemblyName, false); |
||
187 | break; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | return true; |
||
192 | } |
||
193 | |||
194 | public static bool LaunchOnBootGet() |
||
195 | { |
||
196 | using (var key = Registry.CurrentUser.OpenSubKey |
||
197 | ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) |
||
198 | { |
||
199 | return key?.GetValue(AssemblyName) != null; |
||
200 | } |
||
201 | } |
||
202 | |||
203 | #endregion |
||
204 | } |
||
205 | } |