-
Notifications
You must be signed in to change notification settings - Fork 9
/
Program.cs
109 lines (92 loc) · 3.76 KB
/
Program.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
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
namespace BTD6_Mod_Manager_Updater
{
class Program
{
static string projName = "BTD6 Mod Manager";
static void Main(string[] args)
{
Output("Welcome to the updater for " + projName);
if (args.Length == 0)
return;
Output("Updater for " + projName + " has started!");
CloseWindow(projName);
CloseWindow(projName); //Try one more time just to be safe and make sure it's closed
ExtractFiles();
string projExe = Environment.CurrentDirectory + "\\BTD6 Mod Manager.exe";
if (!File.Exists(projExe))
return;
Output("Restarting " + projName + "...");
Process.Start(projExe);
Output("Update complete. You may now close this window");
}
public static void ExtractFiles()
{
Output("Extracting files from Update");
var files = Directory.GetFiles(Environment.CurrentDirectory);
foreach (var file in files)
{
if (!file.EndsWith(".zip") && !file.EndsWith(".rar") && !file.EndsWith(".7z"))
continue;
var fileInfo = new FileInfo(file);
string cleanedName = fileInfo.Name.Replace(".", "").Replace(" ","").Replace("_","").ToLower();
if (cleanedName != "btd6modmanagerzip")
continue;
using (ZipArchive archive = ZipFile.OpenRead(file))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string destinationPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, entry.FullName));
if (destinationPath.StartsWith(Environment.CurrentDirectory, StringComparison.Ordinal))
{
if (new FileInfo(destinationPath).Name == "Updater.exe")
continue;
if (File.Exists(destinationPath))
File.Delete(destinationPath);
entry.ExtractToFile(destinationPath);
Output($"Extracting file: {destinationPath}");
}
}
}
}
}
public static void CloseWindow(string windowMainTitle)
{
while (IsProgramRunning(windowMainTitle))
{
var openWindowProcesses = Process.GetProcesses()
.Where(p => p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer");
foreach (var a in openWindowProcesses)
{
//if (a.MainWindowTitle == windowMainTitle)
if (a.MainWindowTitle == windowMainTitle)
{
Output(projName + " is currently open. Close it to continue update");
a.CloseMainWindow();
}
}
Thread.Sleep(350);
}
}
public static bool IsProgramRunning(string windowMainTitle)
{
var openWindowProcesses = System.Diagnostics.Process.GetProcesses()
.Where(p => p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer");
foreach (var a in openWindowProcesses)
{
if (a.MainWindowTitle == windowMainTitle)
return true;
}
return false;
}
public static void Output(string text)
{
Console.WriteLine(">> " + text);
}
}
}