-
Notifications
You must be signed in to change notification settings - Fork 16
/
PythonInstaller.cs
190 lines (164 loc) · 3.92 KB
/
PythonInstaller.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using UnityEditor;
using System;
using UnityEngine;
using System.Diagnostics;
using System.IO;
#pragma warning disable 612, 618
namespace WakaTime
{
/// <summary>
/// Python installer.
/// </summary>
public class PythonInstaller
{
/// <summary>
/// HTTP client
///
/// TODO: Use the new client solution.
/// </summary>
static WWW www = null;
/// <summary>
/// Process that is installing python.
/// </summary>
static Process installProcess = null;
/// <summary>
/// Gets the application data folder.
/// </summary>
/// <returns>Folder</returns>
static string GetFileFolder()
{
return System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
}
/// <summary>
/// Gets python path
/// </summary>
/// <returns>Python path</returns>
static string GetFilePath()
{
return GetFileFolder() + PythonManager.GetPythonFileName();
}
/// <summary>
/// Returns whether python is downloaded.
/// </summary>
/// <returns>Whether python is downloaded</returns>
static bool IsDownloaded()
{
return File.Exists(GetFilePath());
}
/// <summary>
/// Downloads and installs pyhton.
/// </summary>
static public void DownloadAndInstall()
{
if (!PythonManager.IsPythonInstalled())
{
if (!IsDownloaded())
{
Download();
}
else
{
Install();
}
}
}
/// <summary>
/// Returns whether python is being installed.
/// </summary>
/// <returns>Whether python is being installed</returns>
static public bool IsInstalling()
{
return IsDownloading() || installProcess != null;
}
/// <summary>
/// Returns whether python is being downloaded.
/// </summary>
/// <returns>Whether python is being downloaded</returns>
public static bool IsDownloading()
{
return www != null;
}
/// <summary>
/// Downloads python.
/// </summary>
static public void Download()
{
string url = PythonManager.GetPythonDownloadUrl();
www = new WWW(url);
EditorApplication.update = WhileDownloading;
}
/// <summary>
/// Function to call while python is being downloaded.
/// </summary>
static void WhileDownloading()
{
EditorUtility.DisplayProgressBar("Downloading Python", "Python is being downloaded", www.progress);
if (www.isDone)
{
EditorApplication.update = null;
DownloadCompleted();
}
}
/// <summary>
/// Handles download completion.
/// Saves python to the filesystem and installs it.
/// </summary>
static void DownloadCompleted()
{
EditorUtility.ClearProgressBar();
if (Main.IsDebug)
{
UnityEngine.Debug.Log("Python downloaded: " + www.bytesDownloaded.ToString());
}
string dir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
string localFile = dir + PythonManager.GetPythonFileName();
try
{
System.IO.FileStream stream = new System.IO.FileStream(localFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
stream.Write(www.bytes, 0, www.bytes.Length);
// close file stream
stream.Close();
www = null;
}
catch (Exception ex)
{
if (Main.IsDebug)
{
UnityEngine.Debug.LogError("Python download failed: " + ex.Message);
}
}
Install();
}
/// <summary>
/// Installs pyhton.
/// </summary>
static void Install()
{
string arguments = "/i \"" + GetFilePath() + "\"";
arguments = arguments + " /norestart /qb!";
try
{
var procInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardError = true,
FileName = "msiexec",
CreateNoWindow = true,
Arguments = arguments
};
installProcess = Process.Start(procInfo);
installProcess.WaitForExit();
installProcess.Close();
installProcess = null;
}
catch (Exception ex)
{
if (Main.IsDebug)
{
UnityEngine.Debug.LogError("Python installation failed: " + ex.Message);
}
}
}
}
}
#pragma warning restore 612, 618