-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.cs
131 lines (115 loc) · 4 KB
/
Helper.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
using System.Diagnostics;
using System.IO.Compression;
using System.Net;
namespace XemuLauncher_CLI
{
public class Helper
{
// The location the XemuUpdater application is stored
//string _currentFullPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName.Replace(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName, "");
//Creates the expected folder structure for a build of Xemu which backs up the zip of the previously downloaded build
public void CreateFolderStructure(XemuBuild build)
{
Console.WriteLine($"Creating {build.FolderName}");
Directory.CreateDirectory(build.FolderName); // Xemu build extracts here
}
public void UninstallBuild(XemuBuild build, bool skipMsg = false)
{
string[] filesToDelete = { $"{build.FolderName}\\{build.ExecutableName}.exe", $"{build.FolderName}\\LICENSE", $"{build.FolderName}\\Xemu.pdb" };
List<string> existingFiles = filesToDelete.Where(File.Exists).ToList();
if (existingFiles.Count == 0) return;
var validFiles = string.Join("\n", existingFiles);
if (!skipMsg)
{
Console.WriteLine($"Are you sure you want to delete\n{validFiles}");
string answer = "";
while (answer != "yes")
{
Console.WriteLine($"Type \"yes\" to continue...");
answer = Console.ReadLine();
}
return;
}
if (!Directory.Exists(build.FolderName)) return;
// Kill Xemu if it's running
try
{
StopProcess(build);
foreach (var file in existingFiles)
{
if (File.Exists(file))
File.Delete(file);
}
}
catch
{
UninstallBuild(build, true);
}
}
// Extracts build of Xemu to expected folder
public void ExtractBuild(XemuBuild build)
{
//Deletes LICENSE file because it isn't needed and also causes issues for some reason
try
{
File.Delete($"{build.FolderName}/LICENSE.txt");
ZipFile.ExtractToDirectory($"{build.FolderName}/{build.ZipName}.zip", build.FolderName);
File.Delete($"{build.FolderName}/LICENSE.txt");
}
catch
{
File.Delete($"{build.FolderName}/LICENSE"); // ignored
}
}
// Starts a build of Xemu
public void OpenLocation(XemuBuild build)
{
try
{
Process.Start($"{build.FolderName}");
}
catch
{
Console.WriteLine($"\"{build.ExecutableName}.exe\" could not be found.", "Error");
}
}
// Starts a build of Xemu
public void StartProcess(XemuBuild build)
{
try
{
Process.Start($"{build.FolderName}\\{build.ExecutableName}");
}
catch
{
Console.WriteLine($"\"{build.ExecutableName}.exe\" could not be started.\nThe file must be present and executable.", "Error");
}
}
// Kills the Xemu process
public void StopProcess(XemuBuild build)
{
try
{
var proc = Process.GetProcessesByName(build.ExecutableName);
proc[0].Kill();
}
catch
{
// ignored
}
}
public bool InternetAvailable()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://github.com"))
return true;
}
catch
{
return false;
}
}
}
}