-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
196 lines (171 loc) · 7.44 KB
/
Copy pathUtils.cs
File metadata and controls
196 lines (171 loc) · 7.44 KB
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
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using System.Reflection;
namespace Custom_Installer
{
internal class Logger
{
public static void Info(string msg, bool fast = false)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Utils.Write("[", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkCyan;
Utils.Write("INFO", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.Cyan;
Utils.Write("] ", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkCyan;
Utils.Write(msg + "\n", fast ? 6 : 18);
}
public static void Error(string msg, bool fast = false)
{
Console.ForegroundColor = ConsoleColor.Red;
Utils.Write("[", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkRed;
Utils.Write("ERROR", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.Red;
Utils.Write("] ", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkRed;
Utils.Write(msg + "\n", fast ? 6 : 18);
}
public static void List(string msg, int index, bool fast = false)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Utils.Write("[", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkCyan;
Utils.WriteNum(index, fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.Cyan;
Utils.Write("] ", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkCyan;
Utils.Write(msg + "\n", fast ? 6 : 18);
}
public static string Ask(string msg, bool fast = false)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Utils.Write("\n[", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkYellow;
Utils.Write("PREGUNTA", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.Yellow;
Utils.Write("]\n", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkYellow;
Utils.Write(msg, fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Utils.Write("\n\n» ", fast ? 6 : 18);
return Console.ReadLine() ?? string.Empty;
}
public static void Ok(string msg, bool fast = false)
{
Console.ForegroundColor = ConsoleColor.Green;
Utils.Write("[", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Utils.Write("OK", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.Green;
Utils.Write("] ", fast ? 6 : 18);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Utils.Write(msg + "\n", fast ? 6 : 18);
}
}
internal class Utils
{
public static void Write(string msg, int sleep = 18)
{
for (int i = 0; i < msg.Length; i++)
{
Console.Write(msg[i]);
Thread.Sleep(sleep);
}
}
public static void WriteNum(int num, int sleep = 18)
{
Console.Write(num);
Thread.Sleep(sleep);
}
public static string GetLocalVersion()
{
try
{
var a = Assembly.GetExecutingAssembly().GetName().Version;
return $"{a?.Major}.{a?.Minor}.{a?.Build}";
}
catch
{
return "desconocida";
}
}
public static async Task CheckUpdates()
{
Console.Title = $"Custom Installer v{GetLocalVersion()} | The Ghost";
HttpClient httpClient = new();
var latestVersion = await httpClient.GetStringAsync("https://otko.pp.ua/CI/version.txt");
Logger.Info("Comprobando actualizaciones...", true);
if (latestVersion != GetLocalVersion())
{
Logger.Info($"Hay una actualización disponible ({GetLocalVersion()} -> {latestVersion}), instalando...");
await InstallUpdate();
}
else
{
Logger.Ok("Estás usando la última versión.", true);
Console.Clear();
Logger.Info("Iniciando comprobaciones...");
}
}
public static async Task InstallUpdate()
{
string currentDir = Directory.GetCurrentDirectory() + "\\";
string temp = Path.GetTempPath();
using (var client = new HttpClient())
{
byte[] updaterBytes = await client.GetByteArrayAsync("https://otko.pp.ua/CI/updater.bat");
await File.WriteAllBytesAsync(currentDir + "updater.bat", updaterBytes);
byte[] installerBytes = await client.GetByteArrayAsync("https://otko.pp.ua/CI/Custom%20Installer.exe");
await File.WriteAllBytesAsync(temp + "Custom Installer.exe", installerBytes);
}
ProcessStartInfo start = new();
string selfName = AppDomain.CurrentDomain.FriendlyName + ".exe";
start.Arguments = string.Format($"\"{selfName}\" \"{temp + "Custom Installer.exe"}\"");
start.FileName = currentDir + "updater.bat";
Process.Start(start);
Environment.Exit(0);
}
public static async Task Download(HttpClient client, string fileName, Uri fileLink, string downloadsDir)
{
using (var response = await client.GetAsync(fileLink, HttpCompletionOption.ResponseHeadersRead))
{
string? originalName = response.Content.Headers.ContentDisposition?.FileName;
using var fileStream = File.Create(Path.Combine(downloadsDir, originalName ?? fileName + ".exe"));
await response.Content.CopyToAsync(fileStream);
}
Logger.Ok(fileName, true);
}
public static void ListConfig(Dictionary<string, object> configJson)
{
Logger.Info("Configuración actual\n");
configJson.Select((c, i) => new { c.Key, Index = i + 1 })
.ToList()
.ForEach(item => Logger.List(item.Key, item.Index, true));
}
public static void ListKeys(Dictionary<string, string> category)
{
Logger.Info("Configuración actual\n");
category.Select((c, i) => new { c.Key, Index = i + 1 })
.ToList()
.ForEach(item => Logger.List(item.Key, item.Index, true));
}
public static Dictionary<string, string> ToDictionary(JObject category)
{
return category.ToObject<Dictionary<string, string>>() ?? [];
}
public static Dictionary<string, string>.ValueCollection GetValues(string category, Dictionary<string, object> configJson)
{
return ToDictionary((JObject)configJson[category]).Values ?? new Dictionary<string, string>().Values;
}
public static Dictionary<string, string>.KeyCollection GetKeys(string category, Dictionary<string, object> configJson)
{
return ToDictionary((JObject)configJson[category]).Keys ?? new Dictionary<string, string>().Keys;
}
public static Dictionary<string, string> GetCategoryData(string category, Dictionary<string, object> configJson)
{
return ToDictionary((JObject)configJson[category]) ?? [];
}
}
}