-
Notifications
You must be signed in to change notification settings - Fork 25
/
Program.cs
241 lines (231 loc) · 9.88 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
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
using Spedit.Interop;
using Spedit.Interop.Updater;
using Spedit.UI;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Runtime;
namespace Spedit
{
public static class Program
{
public const string ProgramInternalVersion = "13";
public static MainWindow MainWindow;
public static OptionsControl OptionsObject;
public static TranslationProvider Translations;
public static Config[] Configs;
public static int SelectedConfig = 0;
public static UpdateInfo UpdateStatus;
public static bool RCCKMade = false;
[STAThread]
public static void Main(string[] args)
{
bool mutexReserved;
using (Mutex appMutex = new Mutex(true, "SpeditGlobalMutex", out mutexReserved))
{
if (mutexReserved)
{
bool ProgramIsNew = false;
#if !DEBUG
try
{
#endif
SplashScreen splashScreen = new SplashScreen("Resources/Icon256x.png");
splashScreen.Show(false, true);
Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#if !DEBUG
ProfileOptimization.SetProfileRoot(Environment.CurrentDirectory);
ProfileOptimization.StartProfile("Startup.Profile");
#endif
UpdateStatus = new UpdateInfo();
OptionsObject = OptionsControlIOObject.Load(out ProgramIsNew);
Translations = new TranslationProvider();
Translations.LoadLanguage(OptionsObject.Language, true);
for (int i = 0; i < args.Length; ++i)
{
if (args[i].ToLowerInvariant() == "-rcck") //ReCreateCryptoKey
{
OptionsObject.ReCreateCryptoKey();
MakeRCCKAlert();
}
}
Configs = ConfigLoader.Load();
for (int i = 0; i < Configs.Length; ++i)
{
if (Configs[i].Name == OptionsObject.Program_SelectedConfig)
{
Program.SelectedConfig = i;
break;
}
}
if (!OptionsObject.Program_UseHardwareAcceleration)
{
RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;
}
#if !DEBUG
if (ProgramIsNew)
{
if (Translations.AvailableLanguageIDs.Length > 0)
{
splashScreen.Close(new TimeSpan(0, 0, 1));
var languageWindow = new UI.Interop.LanguageChooserWindow(Translations.AvailableLanguageIDs, Translations.AvailableLanguages);
languageWindow.ShowDialog();
string potentialSelectedLanguageID = languageWindow.SelectedID;
if (!string.IsNullOrWhiteSpace(potentialSelectedLanguageID))
{
OptionsObject.Language = potentialSelectedLanguageID;
Translations.LoadLanguage(potentialSelectedLanguageID);
}
splashScreen.Show(false, true);
}
}
#endif
MainWindow = new MainWindow(splashScreen);
PipeInteropServer pipeServer = new PipeInteropServer(MainWindow);
pipeServer.Start();
#if !DEBUG
}
catch (Exception e)
{
File.WriteAllText("CRASH_" + Environment.TickCount.ToString() + ".txt", BuildExceptionString(e, "SPEDIT LOADING"));
MessageBox.Show("An error occured while loading." + Environment.NewLine + "A crash report was written in the editor-directory.",
"Error while Loading",
MessageBoxButton.OK,
MessageBoxImage.Error);
Environment.Exit(Environment.ExitCode);
}
#endif
Application app = new Application();
#if !DEBUG
try
{
if (OptionsObject.Program_CheckForUpdates)
{
UpdateCheck.Check(true);
}
#endif
app.Startup += App_Startup;
app.Run(MainWindow);
OptionsControlIOObject.Save();
#if !DEBUG
}
catch (Exception e)
{
File.WriteAllText("CRASH_" + Environment.TickCount.ToString() + ".txt", BuildExceptionString(e, "SPEDIT MAIN"));
MessageBox.Show("An error occured." + Environment.NewLine + "A crash report was written in the editor-directory.",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
Environment.Exit(Environment.ExitCode);
}
#endif
}
else
{
try
{
StringBuilder sBuilder = new StringBuilder();
bool addedFiles = false;
for (int i = 0; i < args.Length; ++i)
{
if (!string.IsNullOrWhiteSpace(args[i]))
{
FileInfo fInfo = new FileInfo(args[i]);
if (fInfo.Exists)
{
string ext = fInfo.Extension.ToLowerInvariant().Trim(new char[] { '.', ' ' });
if (ext == "sp" || ext == "inc" || ext == "txt" || ext == "smx")
{
addedFiles = true;
sBuilder.Append(fInfo.FullName);
if ((i + 1) != args.Length)
{ sBuilder.Append("|"); }
}
}
}
}
if (addedFiles)
{ PipeInteropClient.ConnectToMasterPipeAndSendData(sBuilder.ToString()); }
}
catch (Exception) { } //dont fuck the user up with irrelevant data
}
}
}
public static void MakeRCCKAlert()
{
if (RCCKMade)
{
return;
}
RCCKMade = true;
MessageBox.Show("All FTP/RCon passwords are now encrypted wrong!" + Environment.NewLine + "You have to replace them!",
"Created new crypto key", MessageBoxButton.OK, MessageBoxImage.Information);
}
public static void ClearUpdateFiles()
{
string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.exe", SearchOption.TopDirectoryOnly);
for (int i = 0; i < files.Length; ++i)
{
FileInfo fInfo = new FileInfo(files[i]);
if (fInfo.Name.StartsWith("updater_", StringComparison.CurrentCultureIgnoreCase))
{
fInfo.Delete();
}
}
}
private static void App_Startup(object sender, StartupEventArgs e)
{
Tuple<MahApps.Metro.AppTheme, MahApps.Metro.Accent> appStyle = MahApps.Metro.ThemeManager.DetectAppStyle(Application.Current);
MahApps.Metro.ThemeManager.ChangeAppStyle(Application.Current,
MahApps.Metro.ThemeManager.GetAccent("Green"),
MahApps.Metro.ThemeManager.GetAppTheme("BaseDark")); // or appStyle.Item1
}
private static string BuildExceptionString(Exception e, string SectionName)
{
StringBuilder outString = new StringBuilder();
outString.AppendLine("Section: " + SectionName);
outString.AppendLine(".NET Version: " + Environment.Version);
outString.AppendLine("OS: " + Environment.OSVersion.VersionString);
outString.AppendLine("64 bit OS: " + ((Environment.Is64BitOperatingSystem) ? "TRUE" : "FALSE"));
outString.AppendLine("64 bit mode: " + ((Environment.Is64BitProcess) ? "TRUE" : "FALSE"));
outString.AppendLine("Dir: " + Environment.CurrentDirectory);
outString.AppendLine("Working Set: " + (Environment.WorkingSet / 1024).ToString() + " kb");
outString.AppendLine("Installed UI Culture: " + System.Globalization.CultureInfo.InstalledUICulture ?? "null");
outString.AppendLine("Current UI Culture: " + System.Globalization.CultureInfo.CurrentUICulture ?? "null");
outString.AppendLine("Current Culture: " + System.Globalization.CultureInfo.CurrentCulture ?? "null");
outString.AppendLine();
Exception current = e;
int eNumber = 1;
for (; ; )
{
if (e == null)
{
break;
}
outString.AppendLine("Exception " + eNumber.ToString());
outString.AppendLine("Message:");
outString.AppendLine(e.Message);
outString.AppendLine("Stacktrace:");
outString.AppendLine(e.StackTrace);
outString.AppendLine("Source:");
outString.AppendLine(e.Source ?? "null");
outString.AppendLine("HResult Code:");
outString.AppendLine(e.HResult.ToString());
outString.AppendLine("Helplink:");
outString.AppendLine(e.HelpLink ?? "null");
if (e.TargetSite != null)
{
outString.AppendLine("Targetsite Name:");
outString.AppendLine(e.TargetSite.Name ?? "null");
}
e = e.InnerException;
eNumber++;
}
return (eNumber - 1).ToString() + Environment.NewLine + outString.ToString();
}
}
}