-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCore.cs
359 lines (303 loc) · 13.1 KB
/
Core.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
using FFmpeg_Utilizer.Data;
using FFmpeg_Utilizer.Modules;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace FFmpeg_Utilizer
{
public static class Core
{
public static string softwareName = "FFmpeg Utilizer";
public static string authorRealName = "Victor Rimsby";
public static string authorName = "N0tiC";
public static string companyName = "ByteVault Studio";
public static string authorContact = "contact@bytevaultstudio.se";
public static string companyWebsite = "http://bytevaultstudio.se/";
public static string softwareGIT = "Git /n0tic/FFmpeg-UI-Utilizer";
public static string softwareGITURL = "https://github.com/n0tic/FFmpeg-UI-Utilizer";
#region Version
public static BuildTypes buildType = BuildTypes.Alpha;
public static int majorVersion = 0;
public static int minorVersion = 1;
public static int buildVersion = 4;
public enum BuildTypes
{
Alpha,
Beta,
Normal
}
public static string GetVersion() => majorVersion.ToString() + "." + minorVersion.ToString() + "." + buildVersion.ToString() + " " + buildType.ToString();
#endregion Version
#region Files, Folders and Paths
public static bool IsValidFilename(string testName)
{
string strTheseAreInvalidFileNameChars = new string(System.IO.Path.GetInvalidFileNameChars());
Regex regInvalidFileName = new Regex("[" + Regex.Escape(strTheseAreInvalidFileNameChars) + "]");
if (regInvalidFileName.IsMatch(testName)) { return false; };
return true;
}
public static void OpenDirectory(string path)
{
if (Directory.Exists(path))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = path,
FileName = "explorer.exe"
};
Process.Start(startInfo);
}
else
{
main.notice.SetNotice("The directory does not exist.", NoticeModule.TypeNotice.Error);
return;
}
}
public static void CreateAllDefaultFolders()
{
try
{
foreach (SubFolders folder in Enum.GetValues(typeof(SubFolders)))
{
if (!Directory.Exists(GetSubfolder(folder))) Directory.CreateDirectory(GetSubfolder(folder));
}
}
catch (ArgumentNullException) { }
catch (ArgumentException) { }
catch (PathTooLongException) { }
catch (DirectoryNotFoundException) { }
catch (InvalidOperationException) { }
catch (IOException) { }
catch (UnauthorizedAccessException) { }
catch (NotSupportedException) { }
}
public enum Tools
{
ffmpeg,
ffplay
}
public static string GetTool(Tools tool) => tool.ToString() + ".exe";
public enum SubFolders
{
Tools,
Downloads,
tmp,
Output
}
public static string GetDirectory() => @AppDomain.CurrentDomain.BaseDirectory;
public static string GetSubfolder(SubFolders folder) => GetDirectory() + folder.ToString() + @"\";
public static bool DoesDirectoryExist(SubFolders folder) => Directory.Exists(GetDirectory() + folder.ToString());
public static bool DoesAllDefaultDirectoryExist()
{
if (!Directory.Exists(GetSubfolder(SubFolders.Tools)) ||
!Directory.Exists(GetSubfolder(SubFolders.tmp)) ||
!Directory.Exists(GetSubfolder(SubFolders.Downloads)) ||
!Directory.Exists(GetSubfolder(SubFolders.Output)))
return false;
else return true;
}
public static bool DoesAllToolsExist()
{
if (!File.Exists(GetSubfolder(SubFolders.Tools) + GetTool(Tools.ffmpeg)) ||
!File.Exists(GetSubfolder(SubFolders.Tools) + GetTool(Tools.ffplay)))
return false;
else return true;
}
public static bool IsFileLocked(string file)
{
FileInfo fileInfo = new FileInfo(file);
FileStream stream = null;
try
{
stream = fileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
//File Used
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
public static void WriteToFile(string data, string file = "tmp.txt")
{
try
{
using (StreamWriter sw = File.AppendText(GetSubfolder(SubFolders.tmp) + file))
{
sw.WriteLine(data);
sw.Close();
}
}
catch (UnauthorizedAccessException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); }
catch (ArgumentNullException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); }
catch (ArgumentException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); }
catch (DirectoryNotFoundException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); }
catch (PathTooLongException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); }
catch (NotSupportedException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); }
catch (IOException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); }
}
public static bool WriteToFile(List<string> data, string file = "tmp.txt")
{
try
{
if (File.Exists(GetSubfolder(SubFolders.tmp) + file)) File.Delete(GetSubfolder(SubFolders.tmp) + file);
using (StreamWriter sw = File.AppendText(GetSubfolder(SubFolders.tmp) + file))
{
foreach (string f in data)
{
if (!f.Contains("#"))
sw.WriteLine("file '" + f + "'");
else
sw.WriteLine(f);
}
sw.Close();
}
return true;
}
catch (UnauthorizedAccessException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); return false; }
catch (ArgumentNullException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); return false; }
catch (ArgumentException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); return false; }
catch (DirectoryNotFoundException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); return false; }
catch (PathTooLongException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); return false; }
catch (NotSupportedException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); return false; }
catch (IOException x) { main.notice.SetNotice(x.Message, NoticeModule.TypeNotice.Error); return false; }
}
#endregion Files, Folders and Paths
#region Network
public static bool IsInternetConnectionAvailable()
{
try
{
using (var client = new WebClient().OpenRead("http://www.google.com/")) return true;
}
catch { return false; }
}
public static string GetUTCTime()
{
System.Int32 unixTimestamp = (System.Int32)(System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1))).TotalSeconds;
return unixTimestamp.ToString();
}
#endregion Network
#region External FFmpeg Builds
/*
* To query the latest available versions, visit the URLs below and parse the single-line plain-text files received:
https://www.gyan.dev/ffmpeg/builds/git-version
https://www.gyan.dev/ffmpeg/builds/release-version
https://www.gyan.dev/ffmpeg/builds/tools-version
https://www.gyan.dev/ffmpeg/builds/last-build-update
https://www.gyan.dev/ffmpeg/builds/next-build-update
https://www.gyan.dev/ffmpeg/builds/changelog-counter
https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip // Essential release
*/
public static string FFmpeg64BitURLDownload = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip";
public static string FFmpeg64BitURLVersion = "https://www.gyan.dev/ffmpeg/builds/release-version";
#endregion External FFmpeg Builds
#region Word Notation
private static readonly string[] suffixes = { "Bytes/s", "KB/s", "MB/s", "GB/s", "TB/s", "PB/s" };
public static string WordNotation(Int64 bytes)
{
int counter = 0;
decimal number = (decimal)bytes;
while (Math.Round(number / 1024) >= 1)
{
number /= 1024;
counter++;
}
return string.Format("{0:n1} {1}", number, suffixes[counter]);
}
#endregion Word Notation
#region Application
#region Move Window
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
public static void MoveWindow(Form main, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(main.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
#endregion Move Window
public enum Tabs
{
Encoder = 0,
CutMerge = 1,
Cut = 3,
Merge = 2,
M3U8 = 4,
Argument = 5,
Settings = 6,
Updater = 7
}
public enum TabColor
{
Selected = 0,
NotSelected = 1,
Indicator = 2,
White = 3,
NotSectedText = 4
}
private static readonly List<Color> colors = new List<Color>() { Color.FromArgb(43, 52, 67), Color.FromArgb(53, 64, 82), Color.FromArgb(25, 124, 237), Color.FromArgb(255, 255, 255), Color.FromArgb(159, 168, 179) };
public static List<Tab> tabs = new List<Tab>();
public static void AddTab(Panel tab, Panel indicator, Label text, Panel mainPanel, bool active = true) => tabs.Add(new Tab(tab, indicator, text, mainPanel, active));
public static Main main;
public static void ChangeTab(Tabs tab)
{
for (int i = 0; i < tabs.Count; i++)
{
if (i == (int)tab)
{
if (!tabs[i].active) main.notice.SetNotice("Network features disabled. No network connection detected.", Modules.NoticeModule.TypeNotice.Warning);
tabs[i].tab.BackColor = colors[(int)TabColor.Selected];
tabs[i].indicator.BackColor = colors[(int)TabColor.Indicator];
tabs[i].text.ForeColor = colors[(int)TabColor.White];
tabs[i].text.BackColor = colors[(int)TabColor.Selected];
if (tabs[i].mainPanel != null)
{
tabs[i].mainPanel.Enabled = true;
tabs[i].mainPanel.Visible = true;
}
}
else
{
tabs[i].tab.BackColor = colors[(int)TabColor.NotSelected];
tabs[i].indicator.BackColor = colors[(int)TabColor.NotSelected];
tabs[i].text.ForeColor = colors[(int)TabColor.NotSectedText];
tabs[i].text.BackColor = colors[(int)TabColor.NotSelected];
if (tabs[i].mainPanel != null && (int)tab != 1)
{
tabs[i].mainPanel.Enabled = false;
tabs[i].mainPanel.Visible = false;
}
}
if ((int)tab == 1 || (int)tab == 2 || (int)tab == 3)
{
tabs[2].tab.Visible = true;
tabs[3].tab.Visible = true;
}
else
{
tabs[2].tab.Visible = false;
tabs[3].tab.Visible = false;
}
}
}
#endregion Application
}
}