This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
forked from JulienKluge/Spedit
/
Program.cs
205 lines (194 loc) · 8.61 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Spedit.Interop;
using Spedit.UI;
using Spedit.Properties;
using Spedit.Utils;
namespace Spedit
{
public static class Program
{
public const string ProgramInternalVersion = "13";
public static MainWindow MainWindow;
public static OptionsControl Options;
public static ConfigList ConfigList;
public static readonly List<string> SupportedExtensions = new List<string>(new[] { "inc", "txt", "cfg", "ini", "sma", "vdf", "json" });
public static bool RCCKMade;
[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
Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#if !DEBUG
ProfileOptimization.SetProfileRoot(Environment.CurrentDirectory);
ProfileOptimization.StartProfile("Startup.Profile");
#endif
//todo implement own updater
Options = OptionsControl.Load(out ProgramIsNew);
Assembly assembly = Assembly.GetExecutingAssembly();
NeutralResourcesLanguageAttribute a = (NeutralResourcesLanguageAttribute)assembly.GetCustomAttribute(typeof(NeutralResourcesLanguageAttribute));
Culture.cultures = Culture.GetAvailableCultures();
CultureInfo defCultureInfo = new CultureInfo(string.IsNullOrEmpty(Options.Language) ? a.CultureName : Options.Language);
CultureInfo.DefaultThreadCurrentUICulture = defCultureInfo;
CultureInfo.DefaultThreadCurrentCulture = defCultureInfo;
ConfigList = ConfigLoader.Load();
foreach (Config config in ConfigList.Configs.Where(config => config.Name == Options.SelectedConfig))
{
ConfigList.CurrentConfig = ConfigList.Configs.IndexOf(config);
break;
}
if (!Options.UseHardwareAcceleration)
{
RenderOptions.ProcessRenderMode = 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();
new PipeInteropServer(MainWindow).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.Run(MainWindow);
OptionsControl.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('.', ' ');
//todo fix this?
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
}
}
}
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) + " kb");
outString.AppendLine("Installed UI Culture: " + CultureInfo.InstalledUICulture);
outString.AppendLine("Current UI Culture: " + CultureInfo.CurrentUICulture);
outString.AppendLine("Current Culture: " + CultureInfo.CurrentCulture);
outString.AppendLine();
int eNumber = 1;
for (; ; )
{
if (e == null)
{
break;
}
outString.AppendLine("Exception " + eNumber);
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);
}
e = e.InnerException;
eNumber++;
}
return (eNumber - 1) + Environment.NewLine + outString;
}
}
}