-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathProcessHandler.cs
More file actions
84 lines (73 loc) · 2.73 KB
/
Copy pathProcessHandler.cs
File metadata and controls
84 lines (73 loc) · 2.73 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
// keeps reference of launched unity processes, so that can close them even if project list is refreshed
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
namespace UnityLauncherPro.Helpers
{
public static class ProcessHandler
{
static Dictionary<string, (Process, Project)> processes = new Dictionary<string, (Process, Project)>();
public static void Add(Project proj, Process proc)
{
if (proc == null) return;
var key = proj.Path;
if (processes.ContainsKey(key))
{
// already in the list, maybe trying to launch same project twice? only overwrite if previous process has closed
if (processes[key].Item1 == null) processes[key] = (proc, proj);
}
else
{
processes.Add(key, (proc, proj));
}
// subscribe to process exit here, so that can update proj details row (if it was changed in Unity)
proc.Exited += (object o, EventArgs ea) =>
{
// call method in mainwindow, to easy access for sourcedata and grid
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate ()
{
MainWindow wnd = (MainWindow)Application.Current.MainWindow;
wnd.ProcessExitedCallBack(proj);
});
// remove closed process item
Remove(key);
};
}
public static Process Get(string key)
{
if (processes.ContainsKey(key) && (processes[key].Item1 != null))
{
return processes[key].Item1;
}
return null;
}
// return project for given key
//public static Project GetProject(string key)
//{
// if (processes.ContainsKey(key) && (processes[key].Item2 != null))
// {
// return processes[key].Item2;
// }
// return null;
//}
public static bool IsRunning(string key)
{
return processes.ContainsKey(key) && (processes[key].Item1 != null);
}
public static Project GetSingleRunning()
{
if (processes.Count != 1) return null;
var enumerator = processes.Values.GetEnumerator();
enumerator.MoveNext();
var entry = enumerator.Current;
return entry.Item1 != null ? entry.Item2 : null;
}
public static void Remove(string key)
{
if (processes.ContainsKey(key)) processes.Remove(key);
}
}
}