-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
294 lines (274 loc) · 12.9 KB
/
App.xaml.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
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using IWshRuntimeLibrary;
namespace SkinText {
/// <summary>
/// Lógica de interacción para App.xaml
/// </summary>
public partial class App : Application {
private static void FirstRun(string appdatapath, string curFileName) {
try {
//MessageBox.Show("DEBUG: creating folder");
Directory.CreateDirectory(appdatapath);
//MessageBox.Show("DEBUG: copying exe file");
System.IO.File.Copy(curFileName, appdatapath + "\\" + "SkinText.exe", true);
//MessageBox.Show("DEBUG: creating default skin folder");
Directory.CreateDirectory(appdatapath + @"\Default");
//MessageBox.Show("DEBUG: creating desktop shortcut");
CreateShortcut(appdatapath);
if (!System.IO.File.Exists(appdatapath + "\\" + "config.ini"))
{
CustomMethods.CurrentSkin = @"\Default";
//MessageBox.Show("DEBUG: setting default skin");
CustomMethods.SaveCurrentState();
//skininfo.ini Area
//MessageBox.Show("DEBUG: creating default skin config");
CustomMethods.CreateModifySkin("Default", "Default", "FrostHive", "2.0.0", "DefaultIcon", "SkinText Default Skin");
//skininfo.ini Area
}
}
catch (Exception ex) {
//MessageBox.Show("Error creating Initial Config", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
#if DEBUG
MessageBox.Show("DEBUG: "+ex.ToString());
#endif
throw;
}
}
/*
start
check for parameters
get appdata path
check if runing from appdata
if yes run acording to parameters
if not then check if appdata exists
{
create-copy-update-register
}
if error run as admin the above function, close current
open appdata exe
close, delete current exe
*/
private static void CreateShortcut(string appdatapath) {
string link = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ Path.DirectorySeparatorChar + "SkinText.lnk";
WshShell shell = new WshShell();
IWshShortcut shortcut = shell.CreateShortcut(link) as IWshShortcut;
//shortcut.Description = "SkinText \"smart\" Shortcut";
//shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = appdatapath+@"\SkinText.exe";
shortcut.WorkingDirectory = appdatapath;
//shortcut...
shortcut.Save();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
private void Application_Startup(object sender, StartupEventArgs e) {
//TODO: change curFileNamePath it dont work with different names
string curFileNamePath = Process.GetCurrentProcess().MainModule.FileName;
string curFileName = Path.GetFileName(curFileNamePath);
string appdatapath = CustomMethods.GAppPath;
CustomMethods.AppDataPath = appdatapath;
string curPath = Path.GetDirectoryName(curFileNamePath);
#if DEBUG
//MessageBox.Show("DEBUG:\r\n AppDatapath: " +appdatapath+"\r\n curPath: " + curPath);
#endif
CheckCmdParameters(e, appdatapath, curFileName);
//check if runing from appdata or if portable
if (curPath == appdatapath) {
#if DEBUG
//MessageBox.Show("DEBUG: runing from appdata");
#endif
//if yes run acording to parameters (Open Files)
}
else {
if (!Directory.Exists(appdatapath)) {
MessageBox.Show("Welcome to the SkinText Family! \r\nFor the first run we need to do a quick config", "Welcome!", MessageBoxButton.OK, MessageBoxImage.Information);
try {
FirstRun(appdatapath, curFileName);
MessageBox.Show("Config complete!", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
RunAppdata(appdatapath);
DeleteSelf();
}
catch (Exception ex) {
MessageBox.Show("Error creating Initial Config, requesting admin rights", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
#if DEBUG
MessageBox.Show("DEBUG: "+ex.ToString());
//throw;
#endif
FirstRunAdmin(curFileName);
}
CloseThis();
}
//update
UpdateFile(curFileNamePath, curFileName, appdatapath);
//if update not neccesary it will not auto-close and continue to use current exe isntead of appdata exe
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
private static void UpdateFile(string curFileNamePath, string curFileName, string appdatapath) {
if (!System.IO.File.Exists(appdatapath + "\\" + "SkinText.exe")) {
try {
//File.Copy(curFileName, appdatapath + "\\" + "SkinText.exe", true);
//FileAssociaton(appdatapath + "\\" + "SkinText.exe");
FirstRun(appdatapath, curFileName);
MessageBox.Show("Detected a missing file and created it", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
RunAppdata(appdatapath);
DeleteSelf();
}
catch (Exception ex) {
#if DEBUG
MessageBox.Show("DEBUG: "+ex.ToString());
//throw;
#endif
FirstRunAdmin(curFileName);
}
CloseThis();
}
else {
try {
//check version for update
FileVersionInfo oldFileVersionInfo = FileVersionInfo.GetVersionInfo(appdatapath + "\\" + "SkinText.exe");
FileVersionInfo currentFileVersionInfo = FileVersionInfo.GetVersionInfo(curFileNamePath);
bool update = false;
if (currentFileVersionInfo.FileMajorPart > oldFileVersionInfo.FileMajorPart) {
update = true;
}
else {
if (currentFileVersionInfo.FileMajorPart == oldFileVersionInfo.FileMajorPart) {
if (currentFileVersionInfo.FileMinorPart > oldFileVersionInfo.FileMinorPart) {
update = true;
}
else {
if (currentFileVersionInfo.FileMinorPart == oldFileVersionInfo.FileMinorPart) {
if (currentFileVersionInfo.FileBuildPart > oldFileVersionInfo.FileBuildPart) {
update = true;
}/* Since FileBuildPart seems to be random, it's beter to not check it
else {
if (currentFileVersionInfo.FileBuildPart == oldFileVersionInfo.FileBuildPart) {
if (currentFileVersionInfo.FilePrivatePart > oldFileVersionInfo.FilePrivatePart) {
update = true;
}
}
}*/
}
}
}
}
if (update) {
//File.Copy(curFileName, appdatapath + "\\" + "SkinText.exe", true);
FirstRun(appdatapath, curFileName);
MessageBox.Show("Thanks for updating!");
RunAppdata(appdatapath);
DeleteSelf();
CloseThis();
}
}
catch (Exception ex) {
#if DEBUG
MessageBox.Show("DEBUG: "+ex.ToString());
//throw;
#endif
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
private static void CheckCmdParameters(StartupEventArgs e, string appdatapath, string curFileName) {
// Application is running
// Process command line args
bool firstRun = false;
bool Update = false;
for (int i = 0; i != e.Args.Length; ++i) {
firstRun |= (e.Args[i] == "-FirstRun");
Update |= (e.Args[i] == "-Update");
}
// Create main application window, starting minimized if specified
if (firstRun) {
try {
FirstRun(appdatapath, curFileName);
MessageBox.Show("Config complete!", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
RunAppdata(appdatapath);
DeleteSelf();
CloseThis();
}
catch (Exception ex) {
//if admin and still crashes
MessageBox.Show("Detected a missing file and could not fix it, file associations will not work", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
#if DEBUG
MessageBox.Show("DEBUG: "+ex.ToString());
//throw;
#endif
}
}
else {
if (e.Args.Length>0) {
if (System.IO.File.Exists(e.Args[0])) {
CustomMethods.FilepathAssociated = e.Args[0];
/*if (Path.GetExtension(e.Args[0]).ToUpperInvariant() == ".SKTSKIN") {
//open skin package
CustomMethods.FilepathAssociated = e.Args[0];
}
else {
//if double clicked an assosiated file
CustomMethods.FilepathAssociated = e.Args[0];
}*/
}
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
private static void RunAppdata(string appdatapath) {
try {
using (Process process = new Process()) {
process.StartInfo.FileName = appdatapath + "\\" + "SkinText.exe";
process.Start();
//process.WaitForExit();
}
}
catch (Exception ex) {
#if DEBUG
MessageBox.Show("DEBUG: "+ex.ToString());
//throw;
#endif
}
}
private static void CloseThis() {
//Application.Current.Shutdown();
Process.GetCurrentProcess().Kill();
//Environment.Exit(0);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
private static void FirstRunAdmin(string curFileName) {
try {
//Request admin rights
using (Process process = new Process()) {
process.StartInfo.FileName = curFileName;
process.StartInfo.Arguments = "-FirstRun";
process.StartInfo.Verb = "runas";
process.Start();
//process.WaitForExit();
//process.Dispose();
}
}
catch (Exception ex) {
//if failed to launch as admin
MessageBox.Show("Detected a missing file and could not fix it, file associations will not work", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
#if DEBUG
MessageBox.Show("DEBUG: "+ex.ToString());
//throw;
#endif
}
}
private static void DeleteSelf() {
ProcessStartInfo Info = new ProcessStartInfo {
Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" +
Process.GetCurrentProcess().MainModule.FileName + "\"",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
FileName = "cmd.exe"
};
Process.Start(Info);
}
}
}