forked from hansschmucker/NVStreamer1080
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoAction.cs
128 lines (118 loc) · 5.08 KB
/
AutoAction.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
namespace NVStreamer1080 {
public class AutoAction {
public AutoAction() { }
public AutoAction(string json) {
FromJSON(json);
}
public AutoAction(Dictionary<string, object> data) {
FromDictionary(data);
}
public void FromJSON(string json) {
var data=JSON.Deserialize<Dictionary<string, object>>(json);
FromDictionary(data);
}
public void FromDictionary(Dictionary<string, object> data) {
TargetAction = !data.ContainsKey("Action") ? ActionType.Open : (ActionType)((int)data["Action"]);
Process = !data.ContainsKey("Process") ? "" : (string)data["Process"];
WorkingDirectory = !data.ContainsKey("Cwd") ? "" : (string)data["Cwd"];
Args = !data.ContainsKey("Arg") ? "" : (string)data["Arg"];
Delay = !data.ContainsKey("Delay") ? 0 : (int)data["Delay"];
AbortOnInput = data.ContainsKey("Interrupt") && (bool)data["Interrupt"];
}
internal JavaScriptSerializer JSON = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };
public enum ActionType {
Open,
Close,
Kill,
Exit,
Standby,
Hibernate
}
public ActionType TargetAction=ActionType.Exit;
public string Process = "";
public string WorkingDirectory = "";
public string Args = "";
public int Delay = 0;
public bool AbortOnInput = false;
public override string ToString() {
return (
TargetAction == ActionType.Open ? "Launch " + Process :
TargetAction == ActionType.Close ? "Close " + Process :
TargetAction == ActionType.Kill ? "Kill " + Process :
TargetAction == ActionType.Exit ? "Kill Stream" :
TargetAction == ActionType.Standby ? "Sleep" :
TargetAction == ActionType.Hibernate ? "Hibernate" :
"Unknown Action"
)+", "+Delay+"s delay"+(AbortOnInput?" Abort on Input":"");
}
public string ToJSON() {
return JSON.Serialize(ToDictionary());
}
public Dictionary<string, object> ToDictionary() {
return (new Dictionary<string, object>() {
{ "Action",(int)TargetAction },
{ "Process",Process },
{ "Cwd",WorkingDirectory },
{ "Arg",Args },
{ "Delay",Delay },
{ "Interrupt",AbortOnInput}
});
}
public string Execute() {
switch (TargetAction) {
case ActionType.Open:
var p = new ProcessStartInfo() {
FileName = Process,
Arguments = Args,
WorkingDirectory = WorkingDirectory
};
try {
System.Diagnostics.Process.Start(p);
} catch (Exception) { }
return $"Launched {Path.GetFileNameWithoutExtension(Process)}";
case ActionType.Close: {
var procs = System.Diagnostics.Process.GetProcesses().Where(a => a.ProcessName.ToLower() == Process.ToLower());
foreach (var proc in procs)
try {
proc.CloseMainWindow();
} catch (Exception) { }
return $"Closed {procs.Count()} {Process}";
}
case ActionType.Kill: {
var procs = System.Diagnostics.Process.GetProcesses().Where(a => a.ProcessName.ToLower() == Process.ToLower());
foreach (var proc in procs)
try {
proc.Kill();
} catch (Exception) { }
return $"Killed {procs.Count()} {Process}";
}
case ActionType.Exit: {
var procs = System.Diagnostics.Process.GetProcesses().Where(a => a.ProcessName.ToLower() == "nvstreamer");
foreach (var proc in procs)
try {
proc.Close();
} catch (Exception) { }
return "Exited NvStreamer";
}
case ActionType.Standby: {
Application.SetSuspendState(PowerState.Suspend, true, true);
}
return "Entering standby";
case ActionType.Hibernate: {
Application.SetSuspendState(PowerState.Hibernate, true, true);
}
return "Entering hibernation";
}
return "No action taken";
}
}
}