-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathProgram.cs
177 lines (152 loc) · 8.18 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
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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace youtube_dl_gui {
static class Program {
private static readonly GuidAttribute ProgramGUID = (GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), true)[0];
//public static readonly string ProgramPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
public static readonly string ProgramPath = Environment.CurrentDirectory;
public static readonly string LocalAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
"\\youtube_dl_gui";
public static readonly string UserAgent = "User-Agent: youtube-dl-gui/" + Properties.Settings.Default.CurrentVersion;
public static bool IsDebug = false;
public static bool UseIni = false;
static Mutex mtx;
public static readonly Language lang = new Language();
public static readonly Verification verif = new Verification();
static frmMain MainForm;
[STAThread]
static int Main(string[] args) {
mtx = new Mutex(true, ProgramGUID.Value);
DebugOnlyMethod();
if (mtx.WaitOne(TimeSpan.Zero, true) || IsDebug) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (File.Exists(Environment.CurrentDirectory + "\\youtube-dl-gui-updater.exe")) {
File.Delete(Environment.CurrentDirectory + "\\youtube-dl-gui-updater.exe");
}
if (File.Exists(Environment.CurrentDirectory + "\\youtube-dl-gui.old.exe")) {
File.Delete(Environment.CurrentDirectory + "\\youtube-dl-gui.old.exe");
}
if (File.Exists(Ini.Path) && Ini.KeyExists("useIni") && Ini.ReadBool("useIni")) {
UseIni = true;
}
else {
UseIni = false;
}
Config.Settings = new Config();
Config.Settings.Load(ConfigType.Initialization);
if (IsDebug) {
LoadClasses();
MainForm = new frmMain();
Application.Run(MainForm);
}
else {
if (Config.Settings.Initialization.firstTime) {
if (MessageBox.Show("youtube-dl-gui is a visual extension to youtube-dl and is not affiliated with the developers of youtube-dl in any way.\n\nThis program (and I) does not condone piracy or illegally downloading of any video you do not own the rights to or is not in public domain.\n\nAny help regarding any problems when downloading anything illegal (in my jurisdiction) will be ignored. This message will not appear again.\n\nHave you read the above?", "youtube-dl-gui", MessageBoxButtons.YesNo) == DialogResult.Yes) {
Config.Settings.Initialization.firstTime = false;
if (MessageBox.Show("Downloads are saved to your downloads folder by default, would you like to specify a different location now?\n(You can change this in the settings at any time)", "youtube-dl-gui", MessageBoxButtons.YesNo) == DialogResult.Yes) {
using (FolderBrowserDialog fbd = new FolderBrowserDialog()) {
fbd.Description = "Select a location to save downloads to";
fbd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
if (fbd.ShowDialog() == DialogResult.OK) {
Config.Settings.Downloads.downloadPath = fbd.SelectedPath;
}
else {
Config.Settings.Downloads.downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
}
}
}
else {
Config.Settings.Downloads.downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
}
Config.Settings.Initialization.Save();
Config.Settings.Downloads.Save();
}
else {
return 1;
}
}
LoadClasses();
if (CheckArgs(args)) {
Environment.Exit(0);
return 0;
}
MainForm = new frmMain();
Application.Run(MainForm);
mtx.ReleaseMutex();
}
return 0;
}
else {
int hwnd = Win32.FindWindow(null, "youtube-dl-gui");
if (hwnd == 0) {
hwnd = Win32.FindWindow(null, "youtube-dl-gui (ini)");
}
if (hwnd != 0) {
Win32.CopyDataStruct DataStruct = new Win32.CopyDataStruct();
try {
// check args?
Win32.SendMessage((IntPtr)hwnd, Win32.WM_SHOWFORM, IntPtr.Zero, ref DataStruct);
}
finally {
DataStruct.Dispose();
}
}
return 0;
}
}
[System.Diagnostics.Conditional("DEBUG")]
static void DebugOnlyMethod() {
IsDebug = true;
}
static void LoadClasses() {
Config.Settings.Load(ConfigType.All);
verif.RefreshLocation();
if (Config.Settings.Initialization.LanguageFile != string.Empty) {
if (File.Exists(Environment.CurrentDirectory + "\\lang\\" + Config.Settings.Initialization.LanguageFile + ".ini")) {
lang.LoadLanguage(Environment.CurrentDirectory + "\\lang\\" + Config.Settings.Initialization.LanguageFile + ".ini");
}
}
else {
lang.LoadInternalEnglish();
}
}
static bool CheckArgs(string[] args) {
if (args.Length > 0) {
if (args[0].StartsWith("ytdl:")) {
string url = args[0].Substring(5);
frmDownloader Downloader = new frmDownloader();
DownloadInfo NewInfo = new DownloadInfo() {
DownloadURL = url
};
switch (args[1]) {
case "-video":
NewInfo.Type = DownloadType.Video;
NewInfo.VideoQuality = (VideoQualityType)Config.Settings.Saved.videoQuality;
Downloader.ShowDialog();
return true;
case "-audio":
NewInfo.Type = DownloadType.Audio;
if (Config.Settings.Downloads.AudioDownloadAsVBR) {
NewInfo.AudioVBRQuality = (AudioVBRQualityType)Config.Settings.Saved.audioQuality;
}
else {
NewInfo.AudioCBRQuality = (AudioCBRQualityType)Config.Settings.Saved.audioQuality;
}
Downloader.ShowDialog();
return true;
default:
NewInfo.Dispose();
Downloader.Dispose();
return false;
}
}
}
return false;
}
}
}