-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIProcess.cs
More file actions
166 lines (148 loc) · 5.43 KB
/
Copy pathIProcess.cs
File metadata and controls
166 lines (148 loc) · 5.43 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
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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace AoShinhoServ_Monitor
{
public class IProcess
{
public static bool KillAll(int ProcessId, ROServers.Type type)
{
try
{
switch (type)
{
case ROServers.Type.WSproxy:
case ROServers.Type.DevConsole:
case ROServers.Type.ROBrowser:
Process.Start(new ProcessStartInfo
{
FileName = "taskkill",
Arguments = $"/PID {ProcessId} /F /T",
CreateNoWindow = true,
UseShellExecute = false
});
break;
default:
Process p = Process.GetProcessById(ProcessId);
p.Kill();
break;
}
return true;
}
catch (Exception)
{
return false;
}
}
public static bool Do_Kill_All(bool is_serv = false)
{
Parallel.ForEach(ILogging.processesInfos.ToArray().ToList(), it => {
if (!is_serv)
{
ILogging.processesInfos.Remove(it);
KillAll(it.pID, it.type);
}
else
{
switch (it.type)
{
case ROServers.Type.WSproxy:
case ROServers.Type.DevConsole:
case ROServers.Type.ROBrowser:
break;
default:
ILogging.processesInfos.Remove(it);
KillAll(it.pID, it.type);
break;
}
}
});
return true;
}
private static readonly object _trackLock = new object();
public static void SaveTrackedPIDs()
{
lock (_trackLock)
{
var pids = string.Join(",", ILogging.processesInfos.ToArray().Select(p => $"{p.pID}:{p.type}"));
Properties.Settings.Default.TrackedPIDs = pids;
Properties.Settings.Default.Save();
}
}
public static void KillOrphanProcesses()
{
string tracked = Properties.Settings.Default.TrackedPIDs;
if (string.IsNullOrEmpty(tracked)) return;
foreach (var entry in tracked.Split(','))
{
var parts = entry.Split(':');
if (parts.Length == 2
&& int.TryParse(parts[0], out int pid)
&& Enum.TryParse(parts[1], out ROServers.Type type))
{
try
{
var p = Process.GetProcessById(pid);
if (!p.HasExited)
KillAll(pid, type);
}
catch { }
}
}
Properties.Settings.Default.TrackedPIDs = "";
Properties.Settings.Default.Save();
}
public static string GetFileName(string FilePath) => System.IO.Path.GetFileNameWithoutExtension(FilePath);
#region ValidatePathConfig
public static bool CheckServerPath()
{
if (CheckMissingFile(Configuration.LoginPath, "login-server.exe") ||
CheckMissingFile(Configuration.CharPath, "char-server.exe") ||
CheckMissingFile(Configuration.WebPath, "web-server.exe") ||
CheckMissingFile(Configuration.MapPath, "map-server.exe"))
{
ErrorHandler.ShowError($"Failed to find Servers", "Missing File");
return false;
}
return true;
}
public static bool CheckMissingFile(string file, string mes)
{
if (!File.Exists(file) || file == string.Empty)
{
return true;
}
return false;
}
#endregion ValidatePathConfig
public static ROServers.Type GetProcessType(Process ROProcess)
{
ROServers.Type type = ROServers.Type.DevConsole;
Parallel.ForEach(ILogging.processesInfos, it =>
{
if (it.pID == ROProcess.Id)
type = it.type;
});
if (type != ROServers.Type.DevConsole)
return type;
switch (ROProcess.ProcessName.ToLowerInvariant())
{
case var n when n == GetFileName(Configuration.LoginPath).ToLowerInvariant():
return ROServers.Type.Login;
case var n when n == GetFileName(Configuration.CharPath).ToLowerInvariant():
return ROServers.Type.Char;
case var n when n == GetFileName(Configuration.WebPath).ToLowerInvariant():
return ROServers.Type.Web;
case var n when n == GetFileName(Configuration.MapPath).ToLowerInvariant():
return ROServers.Type.Map;
default:
return ROServers.Type.DevConsole;
}
}
}
}