-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
360 lines (314 loc) · 14.3 KB
/
MainForm.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text.Json;
using System.IO.Compression;
using System.Diagnostics;
namespace DazToUnrealOnlineInstaller
{
enum InstallSteps
{
Initial = 0,
Download = 1,
ChooseUnrealVersion = 2,
StartDazStudio = 3,
FindDazStudio = 4,
CloseDazStudio = 5,
InstallDazStudioPlugin = 6,
StartUnrealEditor = 7,
FindUnrealEditor = 8,
CloseUnrealEditor = 9,
BackupUnrealEditor = 10,
InstallUnrealEditorPlugin = 11,
Exit,
Quit
}
public partial class MainForm : Form
{
private InstallSteps CurrentInstallStep = InstallSteps.Initial;
private string TempDirectory;
private string DazStudioPluginFolder;
private string UnrealEditorPluginFolder;
private const string DazStudioPluginName = "dzunrealbridge.dll";
private const string UnrealEditorPluginName = "DazToUnreal";
public MainForm()
{
InitializeComponent();
TempDirectory = CreateTemporaryDirectory();
TempDirectoryLabel.Text = String.Format("Downloads to temp directory: {0}", TempDirectory);
}
private void NextButton_MouseClick(object sender, MouseEventArgs e)
{
NextButton.Enabled = false;
switch (CurrentInstallStep)
{
case InstallSteps.Initial:
MoveToNextStep(true);
break;
case InstallSteps.Download:
InstructionLabel.Text = String.Format("Downloading Plugin. Please wait.");
if (DownloadUnrealPlugin(TempDirectory))
{
ExtractPlugin(Path.Combine(TempDirectory, "DazToUnreal.zip"), TempDirectory);
UnrealVersionComboBox.Visible = true;
UnrealVersionComboBox.Items.AddRange(GetUnrealVersions());
UnrealVersionComboBox.SelectedIndex = UnrealVersionComboBox.Items.Count - 1;
TempDirectoryLabel.Text = String.Format("Downloaded to temp directory: {0}", TempDirectory);
InstructionLabel.Text = String.Format("Choose an Unreal Version and click Next");
MoveToNextStep();
}
break;
case InstallSteps.ChooseUnrealVersion:
UnrealVersionComboBox.Enabled = false;
MoveToNextStep(true);
break;
case InstallSteps.StartDazStudio:
if (IsDazStudioRunning())
{
MoveToNextStep(true);
break;
}
InstructionLabel.Text = String.Format("Start Daz Studio and click Next");
break;
case InstallSteps.FindDazStudio:
if(!IsDazStudioRunning())
{
break;
}
DazStudioPluginFolder = GetRunningDazStudioPluginFolder();
DazStudioFolderLabel.Text = string.Format("Daz Studio Plugin Folder found at {0}", DazStudioPluginFolder);
MoveToNextStep(true);
break;
case InstallSteps.CloseDazStudio:
InstructionLabel.Text = String.Format("Close Daz Studio and click Next (May take a minute to fully close)");
if (IsDazStudioRunning())
{
break;
}
MoveToNextStep(true);
break;
case InstallSteps.InstallDazStudioPlugin:
string sourceDllPath = Path.Combine(TempDirectory, UnrealVersionComboBox.Text, "DazToUnreal", "Resources", DazStudioPluginName);
string targetDllPath = Path.Combine(DazStudioPluginFolder, DazStudioPluginName);
File.Copy(sourceDllPath, targetDllPath, true);
MoveToNextStep();
break;
case InstallSteps.StartUnrealEditor:
if (IsUnrealEditorRunning())
{
MoveToNextStep(true);
break;
}
InstructionLabel.Text = String.Format("Start Unreal Editor and click Next");
break;
case InstallSteps.FindUnrealEditor:
if (!IsUnrealEditorRunning())
{
break;
}
UnrealEditorPluginFolder = GetRunningUnrealEditorPluginFolder();
UnrealEditorFolderLabel.Text = string.Format("Unreal Engine Plugin Folder found at {0}", UnrealEditorPluginFolder);
MoveToNextStep(true);
break;
case InstallSteps.CloseUnrealEditor:
InstructionLabel.Text = String.Format("Close Unreal Editor and click Next");
if (IsUnrealEditorRunning())
{
break;
}
MoveToNextStep(true);
break;
case InstallSteps.BackupUnrealEditor:
if(Directory.Exists(UnrealEditorPluginFolder) && !string.IsNullOrWhiteSpace(BackupFolderPathTextBox.Text))
{
if (Path.GetFullPath(BackupFolderPathTextBox.Text) == BackupFolderPathTextBox.Text)
{
InstructionLabel.Text = String.Format("Backing up plugin. Please Wait.");
Directory.CreateDirectory(BackupFolderPathTextBox.Text);
Directory.Move(UnrealEditorPluginFolder, Path.Combine(BackupFolderPathTextBox.Text, "DazToUnreal"));
MoveToNextStep(true);
break;
}
}
if(Directory.Exists(UnrealEditorPluginFolder))
{
InstructionLabel.Text = String.Format("Plugin Folder Exists in Engine Already. Choose backup location and click Next.");
string drive = Path.GetPathRoot(UnrealEditorPluginFolder);
string recommendedBackupPath = Path.Combine(drive, "DazToUnrealBackup", DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss"));
BackupFolderPathTextBox.Text = recommendedBackupPath;
BackupFolderPathTextBox.Visible = true;
ChooseBackupFolderButton.Visible = true;
break;
}
MoveToNextStep(true);
break;
case InstallSteps.InstallUnrealEditorPlugin:
InstructionLabel.Text = String.Format("Installing Plugin. Please Wait.");
string sourcePluginPath = Path.Combine(TempDirectory, UnrealVersionComboBox.Text, UnrealEditorPluginName);
CopyDirectory(sourcePluginPath, UnrealEditorPluginFolder, true);
MoveToNextStep(true);
break;
case InstallSteps.Exit:
if (Directory.Exists(UnrealEditorPluginFolder))
{
InstructionLabel.Text = String.Format("Plugin Installed. Click Exit.");
NextButton.Text = "Exit";
MoveToNextStep();
}
break;
case InstallSteps.Quit:
Application.Exit();
break;
}
NextButton.Enabled = true;
}
private void MoveToNextStep(bool AutoClick = false)
{
CurrentInstallStep++;
if (AutoClick)
{
NextButton_MouseClick(null, null);
};
}
public string CreateTemporaryDirectory()
{
string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(directory);
return directory;
}
public bool DownloadUnrealPlugin(string directory)
{
var client = new WebClient();
client.Headers.Add(HttpRequestHeader.UserAgent, "DazToUnrealOnlineInstaller");
string data = client.DownloadString("https://api.github.com/repos/David-Vodhanel/DazToUnreal/releases/latest");
JsonDocument ReleaseData = JsonDocument.Parse(data);
JsonElement AssetsElement;
if (ReleaseData.RootElement.TryGetProperty("assets", out AssetsElement))
{
foreach (JsonElement DownloadElement in AssetsElement.EnumerateArray())
{
JsonElement FileNameProperty;
if (DownloadElement.TryGetProperty("name", out FileNameProperty))
{
String FileName = FileNameProperty.GetString();
if (FileName == "DazToUnreal.zip")
{
JsonElement DownloadURLProperty;
if (DownloadElement.TryGetProperty("browser_download_url", out DownloadURLProperty))
{
String DownloadURL = DownloadURLProperty.GetString();
client.DownloadFile(DownloadURL, Path.Combine(directory, "DazToUnreal.zip"));
return true;
}
}
}
}
}
return false;
}
public bool ExtractPlugin(String zipFile, String directory)
{
try
{
ZipFile.ExtractToDirectory(zipFile, directory);
}
catch
{
return false;
}
return true; ;
}
public string[] GetUnrealVersions()
{
string[] versionDirectories = Directory.GetDirectories(TempDirectory);
string[] versions = new string[versionDirectories.Length];
for(int i = 0; i < versionDirectories.Length; i++)
{
versions[i] = new DirectoryInfo(versionDirectories[i]).Name;
}
return versions;
}
public bool IsDazStudioRunning()
{
Process[] DazSutdioProcesses = Process.GetProcessesByName("DazStudio");
return DazSutdioProcesses.Length > 0;
}
public string GetRunningDazStudioPluginFolder()
{
Process process = Process.GetProcessesByName("DazStudio").First();
string exePath = process.MainModule.FileName;
string pluginFolderPath = Path.Combine(Path.GetDirectoryName(exePath), "plugins");
return pluginFolderPath;
}
public bool IsUnrealEditorRunning()
{
Process[] Unreal5Processes = Process.GetProcessesByName("UnrealEditor");
if (Unreal5Processes.Length > 0) return true;
Process[] Unreal4Processes = Process.GetProcessesByName("UE4Editor");
if (Unreal4Processes.Length > 0) return true;
return false;
}
public string GetRunningUnrealEditorPluginFolder()
{
Process[] Unreal5Processes = Process.GetProcessesByName("UnrealEditor");
for(int i = 0; i < Unreal5Processes.Count(); i++)
{
string exePath = Unreal5Processes[i].MainModule.FileName;
string binariesFolderPath = Path.GetDirectoryName(exePath);
string pluginFolderPath = Path.GetFullPath(Path.Combine(binariesFolderPath, "..", "..", "Plugins", "Marketplace", UnrealEditorPluginName));
return pluginFolderPath;
}
Process[] Unreal4Processes = Process.GetProcessesByName("UE4Editor");
for (int i = 0; i < Unreal4Processes.Count(); i++)
{
string exePath = Unreal4Processes[i].MainModule.FileName;
string binariesFolderPath = Path.GetDirectoryName(exePath);
string pluginFolderPath = Path.GetFullPath(Path.Combine(binariesFolderPath, "..", "..", "Plugins", "Marketplace", UnrealEditorPluginName));
return pluginFolderPath;
}
return string.Empty;
}
private void ChooseBackupFolderButton_Click(object sender, EventArgs e)
{
BackupFolderBrowserDialog.SelectedPath = BackupFolderPathTextBox.Text;
BackupFolderBrowserDialog.ShowDialog();
BackupFolderPathTextBox.Text = BackupFolderBrowserDialog.SelectedPath;
}
// From: https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
}
}