-
Notifications
You must be signed in to change notification settings - Fork 5
/
CrestronEnvironmentExtensions.cs
428 lines (362 loc) · 11.3 KB
/
CrestronEnvironmentExtensions.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#region License
/*
* CrestronEnvironmentExtensions.cs
*
* The MIT License
*
* Copyright © 2017 Nivloc Enterprises Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#endregion
using System;
using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharp.Reflection;
namespace Crestron.SimplSharp
{
public static class CrestronEnvironmentEx
{
static CrestronEnvironmentEx ()
{
CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler;
CrestronEnvironment.SystemEventHandler += CrestronEnvironment_SystemEventHandler;
s_haveSystemResources = File.Exists (Path.Combine (InitialParametersClass.ProgramDirectory.ToString (), "SSRefMscorlibResources.dll"))
&& File.Exists (Path.Combine (InitialParametersClass.ProgramDirectory.ToString (), "SSRefResourceLibrary.dll"));
}
private static void CrestronEnvironment_SystemEventHandler (eSystemEventType systemEventType)
{
switch (systemEventType)
{
case eSystemEventType.Rebooting:
HasRebootStarted = true;
break;
}
}
private static void CrestronEnvironment_ProgramStatusEventHandler (eProgramStatusEventType programEventType)
{
switch (programEventType)
{
case eProgramStatusEventType.Stopping:
HasShutdownStarted = true;
break;
}
}
public static bool HasShutdownStarted { get; private set; }
public static bool HasRebootStarted { get; private set; }
public enum SpecialFolder
{
Programs = 0x02,
ApplicationData = 0x1a,
CommonApplicationData = 0x23,
}
internal enum SpecialFolderOption
{
None = 0,
DoNotVerify = 0x4000,
Create = 0x8000
}
/// <summary>
/// Gets or sets the current directory. Actually this is supposed to get
/// and/or set the process start directory acording to the documentation
/// but actually test revealed at beta2 it is just Getting/Setting the CurrentDirectory
/// </summary>
public static string CurrentDirectory
{
get
{
if (currentDirectory != null)
return currentDirectory;
return InitialParametersClass.ProgramDirectory.ToString ();
}
set
{
if (value == null)
throw new ArgumentNullException ("path");
if (value.Trim ().Length == 0)
throw new ArgumentException ("path string must not be an empty string or whitespace string");
if (value.Length >= 260)
throw new ArgumentOutOfRangeException ("path", "maximum length of path is 260 characters");
if (value.IndexOfAny (Path.GetInvalidPathChars ()) != -1)
throw new ArgumentException ("path", "path contains invalid characters");
var tempPath = Path.IsPathRooted (value) ? value : Path.Combine (CurrentDirectory, value);
if (!Directory.Exists (tempPath))
throw new DirectoryNotFoundException ();
currentDirectory = tempPath;
}
}
private static string currentDirectory;
/// <summary>
/// Gets the name of the local computer
/// </summary>
public static string MachineName
{
get { return machineName ?? (machineName = CrestronEthernetHelper.GetEthernetParameter (CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME, 0)); }
}
private static string machineName;
/// <summary>
/// Gets the standard new line value
/// </summary>
public static string NewLine
{
get { return CrestronEnvironment.NewLine; }
}
//
// Support methods and fields for OSVersion property
//
private static object os;
private static PlatformID Platform
{
get { return PlatformID.WinCE; }
}
/// <summary>
/// Gets the current OS version information
/// </summary>
public static OperatingSystem OSVersion
{
get
{
if (os == null)
os = new OperatingSystem (PlatformID.WinCE, CrestronEnvironment.OSVersion.Version);
return os as OperatingSystem;
}
}
/// <summary>
/// Get StackTrace
/// </summary>
public static string StackTrace
{
get
{
try
{
throw new Exception ();
}
catch (Exception ex)
{
return ex.StackTrace;
}
}
}
/// <summary>
/// Get the number of milliseconds that have elapsed since the system was booted
/// </summary>
public static int TickCount
{
get { return CrestronEnvironment.TickCount; }
}
/// <summary>
/// Get the version of the common language runtime
/// </summary>
public static Version Version
{
get { return CrestronEnvironment.OSVersion.Version; }
}
/// <summary>
/// Returns the fully qualified path of the
/// folder specified by the "folder" parameter
/// </summary>
public static string GetFolderPath (SpecialFolder folder)
{
return GetFolderPath (folder, SpecialFolderOption.None);
}
private static string GetFolderPath (SpecialFolder folder, SpecialFolderOption option)
{
string dir = null;
switch (folder)
{
case SpecialFolder.ApplicationData:
dir = String.Format ("\\Nvram\\App{0:D2}\\", InitialParametersClass.ApplicationNumber);
if (option == SpecialFolderOption.Create)
Directory.Create (dir);
break;
case SpecialFolder.Programs:
dir = InitialParametersClass.ProgramDirectory.ToString ();
break;
case SpecialFolder.CommonApplicationData:
dir = "\\Nvram\\";
break;
}
return dir;
}
public static void FailFast (string message)
{
ErrorLog.Error (String.Format ("FailFast: {0}\r\n{1}", message, StackTrace));
if (Debugger.IsAttached)
{
Debugger.WriteLine (message);
Debugger.Break ();
}
string response = String.Empty;
CrestronConsole.SendControlSystemCommand ("stopprog -p:" + InitialParametersClass.ApplicationNumber, ref response);
}
public static void FailFast (string message, Exception exception)
{
ErrorLog.Error (String.Format ("FailFast: {0}\r\nException: {1}", message, exception));
if (Debugger.IsAttached)
{
Debugger.WriteLine (message);
Debugger.Break ();
}
string response = String.Empty;
CrestronConsole.SendControlSystemCommand ("stopprog -p:" + InitialParametersClass.ApplicationNumber, ref response);
}
public static bool IsRunningOnWindows
{
get { return true; }
}
public static bool IsUnix
{
get { return false; }
}
public static bool IsMacOS
{
get { return false; }
}
public static int ProcessorCount
{
get { return 1; }
}
public static void Sleep (int timeoutInMsec)
{
CrestronEnvironment.Sleep (timeoutInMsec);
}
public static void AllowOtherAppsToRun ()
{
CrestronEnvironment.AllowOtherAppsToRun ();
}
public static string UserName
{
get;
set;
}
public static string UserDomainName
{
get { return CrestronEthernetHelper.GetEthernetParameter (CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_DOMAIN_NAME, 0); }
}
private static bool s_haveSystemResources;
private static object s_internalSyncObject;
private static GetStringDelegate s_getString;
private delegate string GetStringDelegate (string name);
internal static object InternalSyncObject
{
get
{
if (ReferenceEquals (s_internalSyncObject, null))
{
Interlocked.CompareExchange (ref s_internalSyncObject, new object (), null);
}
return s_internalSyncObject;
}
}
private static GetStringDelegate GetString
{
get
{
if (ReferenceEquals (s_getString, null))
{
CMonitor.Enter (InternalSyncObject);
try
{
if (ReferenceEquals (s_getString, null))
{
var managerAssembly = Assembly.LoadFrom (Path.Combine (InitialParametersClass.ProgramDirectory.ToString (), "SSRefResourceLibrary.dll"));
if (managerAssembly == null)
{
s_haveSystemResources = false;
return null;
}
var resourceManagerType = managerAssembly.GetType ("SSRef.Resources.ResourceManager");
if (resourceManagerType == null)
{
s_haveSystemResources = false;
return null;
}
var getString = resourceManagerType.GetMethod ("GetString", new CType[] { typeof (string) });
if (getString == null)
{
s_haveSystemResources = false;
return null;
}
var sr = Assembly.LoadFrom (Path.Combine (InitialParametersClass.ProgramDirectory.ToString (), "SSRefMscorlibResources.dll"));
if (sr == null)
{
s_haveSystemResources = false;
return null;
}
var ctorResourceManager = resourceManagerType.GetConstructor (new CType[] {typeof (string), typeof (Assembly)});
var resourceManager = ctorResourceManager.Invoke (new object[] {"SSRefMscorlibResources.SR", sr});
if (resourceManager == null)
{
s_haveSystemResources = false;
return null;
}
s_getString = (GetStringDelegate)CDelegate.CreateDelegate (typeof (GetStringDelegate), resourceManager, getString);
}
}
finally
{
CMonitor.Exit (InternalSyncObject);
}
}
return s_getString;
}
}
public static string GetResourceString (string key)
{
if (!s_haveSystemResources || GetString == null)
return key;
return GetString (key);
}
public static string GetResourceString (string fmt, params Object[] args)
{
if (!s_haveSystemResources || GetString == null)
return fmt;
return String.Format (GetString (fmt), args);
}
private delegate object DelGetCurrentThread ();
private static Type s_CTypeThread;
private static DelGetCurrentThread s_GetCurrentThread;
private static MethodInfo s_GetManagedThreadId;
public static int CurrentManagedThreadId
{
get
{
if (CrestronEnvironment.RuntimeEnvironment != eRuntimeEnvironment.SimplSharpPro)
return 0;
if (s_GetManagedThreadId == null)
{
try
{
s_CTypeThread = Type.GetType ("Crestron.SimplSharpPro.CrestronThread.Thread, SimplSharpPro");
}
catch
{
}
if (s_CTypeThread == null)
return 0;
var propCurrentThread = s_CTypeThread.GetCType ().GetProperty ("CurrentThread");
s_GetCurrentThread = (DelGetCurrentThread)CDelegate.CreateDelegate (typeof (DelGetCurrentThread), null, propCurrentThread.GetGetMethod ());
var propManagedThreadId = s_CTypeThread.GetCType().GetProperty ("ManagedThreadId");
s_GetManagedThreadId = propManagedThreadId.GetGetMethod ();
}
return (int)s_GetManagedThreadId.Invoke(s_GetCurrentThread (), null);
}
}
}
}