-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotification.cs
More file actions
115 lines (104 loc) · 3.75 KB
/
Notification.cs
File metadata and controls
115 lines (104 loc) · 3.75 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
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
using System;
using System.Diagnostics;
namespace GCMod
{
public static class Notification
{
public static Process powershell;
public static readonly object _lock = new();
public static void Initialize()
{
try
{
string script = @"
$script:toastMgr = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$script:toastText02 = [Windows.UI.Notifications.ToastTemplateType]::ToastText02
$script:template = $script:toastMgr::GetTemplateContent($script:toastText02)
$script:notifier = $script:toastMgr::CreateToastNotifier('GCMod')
$ErrorActionPreference = 'Stop'
while ($true) {
$inputJson = [Console]::In.ReadLine()
if ([string]::IsNullOrEmpty($inputJson)) { break }
try {
$data = ConvertFrom-Json $inputJson
$toastXml = $script:template.CloneNode($true)
$toastXml.SelectSingleNode('//text[@id=""1""]').InnerText = $data.Title;
$toastXml.SelectSingleNode('//text[@id=""2""]').InnerText = $data.Message;
$toast = [Windows.UI.Notifications.ToastNotification]::new($toastXml)
$toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds($data.ExpirationTime)
$script:notifier.Show($toast)
} catch {
[Console]::Error.WriteLine($_.Exception.Message)
}
}
";
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{script}\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
powershell = new Process { StartInfo = psi };
powershell.Start();
powershell.BeginErrorReadLine();
powershell.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Plugin.Log.LogError($"PowerShell Error: {e.Data}");
}
};
AppDomain.CurrentDomain.ProcessExit += (sender, e) => Cleanup();
}
catch (Exception e)
{
Plugin.Log.LogError($"PowerShell initialize failed: {e.Message}");
powershell = null;
}
}
private static string Escape(string s)
{
return s.Replace("\"", "\"\"").Replace("&", "&").Replace("<", "<").Replace(">", ">");
}
public static void Show(string title, string message, int expirationTime = 3)
{
if (powershell == null || powershell.HasExited)
{
return;
}
try
{
string json = $"{{\"Title\":\"{Escape(title)}\",\"Message\":\"{Escape(message)}\",\"ExpirationTime\":{expirationTime}}}";
lock (_lock)
{
powershell.StandardInput.WriteLine(json);
powershell.StandardInput.Flush();
}
}
catch (Exception e)
{
Plugin.Log.LogError(e.Message);
}
}
public static void Cleanup()
{
try
{
if (powershell != null && !powershell.HasExited)
{
powershell.StandardInput.Close();
powershell.Kill(true);
powershell.Close();
}
powershell = null;
}
catch
{
}
}
}
}