-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnityOprations.cs
115 lines (100 loc) · 4.96 KB
/
UnityOprations.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
using System.Diagnostics;
using System.Security;
using System.Windows.Forms;
namespace ProjectCloner {
internal class UnityOprations {
private static Form1 form1;
internal void Init(Form1 form) {
form1 = form;
}
public void StartUnityOperations(string unityVersion, string projectpath) {
try {
string unityPath = FindUnityExecutable(unityVersion); // Change this to your desired version
if (unityPath != null) {
StartUnityProcess(unityPath, projectpath);
} else {
MessageBox.Show($"Unity version {unityVersion} is not installed on this machine.");
}
} catch (UnauthorizedAccessException) {
MessageBox.Show("You don't have permission to run Unity. Please run the application as an administrator.");
form1.HandleError("You don't have permission to run Unity. Please run the application as an administrator.");
} catch (SecurityException) {
MessageBox.Show("Security error occurred while trying to run Unity. Please check your security settings.");
form1.HandleError("Security error occurred while trying to run Unity. Please check your security settings.");
} catch (FileNotFoundException) {
MessageBox.Show("Unity executable not found.");
form1.HandleError("Unity executable not found.");
} catch (Exception ex) {
MessageBox.Show($"Error: {ex.Message}");
form1.HandleError($"Error: {ex.Message}");
}
}
public static string? FindUnityExecutable(string version) {
version = "2022.3.11f";
string[] unityInstallationPaths = {
$@"C:\Program Files\Unity\Hub\Editor\{version}1\Editor\",
$@"C:\Program Files (x86)\Unity\Hub\Editor\{version}1\Editor\"
};
string[] unityExecutableNames = {
"Unity.exe",
"Unity"
};
foreach (string path in unityInstallationPaths) {
foreach (string executable in unityExecutableNames) {
string fullPath = Path.Combine(path, executable);
if (File.Exists(fullPath)) {
form1.HandleError($"Unity Version Found at {fullPath}", false, true);
return fullPath;
}
}
}
return null;
}
private void StartUnityProcess(string unityPath, string projectPath) {
Debug.Print("unityPath: " + unityPath);
Debug.Print("projectPath: " + projectPath);
string unityArgs = $" -projectPath {projectPath} -executeMethod ViitorCloud.Base.BaseScripts.PluginObjects.Editor.CheckforPlugin.DownloadThePlugin"; // Change this to your method
ProcessStartInfo startInfo = new ProcessStartInfo {
FileName = unityPath,
Arguments = unityArgs,
CreateNoWindow = true,
UseShellExecute = false
};
Process unityProcess = new Process {
StartInfo = startInfo
};
unityProcess.EnableRaisingEvents = true;
unityProcess.Exited += UnityProcess_Exited;
try {
form1.HandleError("Unity Process Starting", false, true);
unityProcess.Start();
} catch (UnauthorizedAccessException e) {
form1.HandleError("Error occurred : " + e);
throw; // Re-throw the exception to be caught in the button click handler
} catch (SecurityException e) {
form1.HandleError("Error occurred : " + e);
throw; // Re-throw the exception to be caught in the button click handler
} catch (Exception ex) {
form1.HandleError("Error occurred : " + ex);
throw new Exception($"Error starting Unity process: {ex.Message}");
}
}
private void UnityProcess_Exited(object? sender, EventArgs e) {
Process unityProcess = (Process)sender;
// Check if Unity process exited successfully
if (unityProcess.ExitCode == 0) {
MessageBox.Show("Unity operation completed successfully!");
form1.HandleError("Unity operation completed successfully!", true, true);
} else {
// Check if Unity process crashed unexpectedly
if (!unityProcess.HasExited) {
MessageBox.Show("Unity process crashed unexpectedly!");
form1.HandleError("Unity process crashed unexpectedly!");
} else {
MessageBox.Show("Unity operation failed! : " + unityProcess.ExitCode);
form1.HandleError("Unity operation failed! : " + unityProcess.ExitCode);
}
}
}
}
}