-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.xaml.cs
215 lines (178 loc) · 8.62 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using ESLTracker.Utils;
using ESLTracker.Properties;
using System.Windows.Input;
using ESLTracker.BusinessLogic.GameClient;
using ESLTracker.Utils.SimpleInjector;
using ESLTracker.Windows;
using ESLTracker.BusinessLogic.Cards;
using ESLTracker.BusinessLogic.General;
using ESLTracker.Controls;
using ESLTracker.BusinessLogic.DataFile;
namespace ESLTracker
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public bool IsApplicationClosing { get; set; } = false;
SingleInstanceApp singleInstance;
private const string NewVersionAvailable = "New version of tracker is available.";
private const string OpenChangelog = "Open changelog";
private const string Download = "Download";
private const string CardsDatabaseUpdated = "Cards database has been updated to latest version (v{0} from {1}: {2})";
static App()
{
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
public void CloseApplication()
{
IsApplicationClosing = true;
this.Shutdown();
}
private void Application_Exit(object sender, ExitEventArgs e)
{
singleInstance.Dispose();
}
private void HandleUnhandledException(Exception ex, string source)
{
string filename = "./crash" + DateTime.Now.ToString("yyyyMMddHHmm") + ".txt";
string verInfo = String.Join(";",Assembly.GetEntryAssembly().CustomAttributes.Where(ca => ca.AttributeType == typeof(AssemblyInformationalVersionAttribute)).FirstOrDefault()?.ConstructorArguments);
System.IO.File.WriteAllText(filename, "APP VERSION: "+ verInfo +Environment.NewLine);
System.IO.File.AppendAllText(filename, ex.ToString());
MessageBox.Show("Application encountered unhandled exception. Log file has been created in " + "./crash" + DateTime.Now.ToString("yyyyMMddHHmm") + ".txt with details.", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
}
private void Application_Startup(object sender, StartupEventArgs e)
{
SplashScreenManager splash = new SplashScreenManager();
splash.ShowSplash();
splash.UpdateProgress("Loading loggers");
splash.UpdateProgress("Configuring dependecies");
var container = new MasserContainer();
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.PresentationTraceSources.Refresh();
System.Diagnostics.PresentationTraceSources.DataBindingSource.Listeners.Add(new System.Diagnostics.ConsoleTraceListener());
System.Diagnostics.PresentationTraceSources.DataBindingSource.Listeners.Add(new DebugTraceListener());
System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Warning | System.Diagnostics.SourceLevels.Error;
}
#endif
AppDomain.CurrentDomain.UnhandledException += (s, ex) =>
HandleUnhandledException((Exception)ex.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException");
DispatcherUnhandledException += (s, ex) =>
HandleUnhandledException(ex.Exception, "Application.Current.DispatcherUnhandledException");
TaskScheduler.UnobservedTaskException += (s, ex) =>
HandleUnhandledException(ex.Exception, "TaskScheduler.UnobservedTaskException");
splash.UpdateProgress("Checking other instances");
if (CheckSingleInstance())
{
splash.UpdateProgress("Other instance is running. Cancel start-up process");
IsApplicationClosing = true;
return; //if alrady running just stop
}
splash.UpdateProgress("Checking for new version");
IVersionService vc = container.GetInstance<IVersionService>();
var settings = container.GetInstance<ISettings>();
var newVersion = vc.CheckNewAppVersionAvailable();
if (newVersion.IsAvailable)
{
var userMessages = container.GetInstance<UserInfoMessages>();
userMessages.AddMessage(
NewVersionAvailable,
new Dictionary<string, string> {
{ OpenChangelog, settings.VersionCheck_LatestBuildUserUrl },
{ Download, newVersion.DownloadUrl }
});
}
splash.UpdateProgress("Checking for card database updates");
if (vc.IsNewCardsDBAvailable())
{
ICardsDatabase cardsDB = vc.GetLatestCardsDB();
if (cardsDB != null)
{
var userMessages = container.GetInstance<UserInfoMessages>();
userMessages.AddMessage(
string.Format(CardsDatabaseUpdated,
new object[] { cardsDB.Version, cardsDB.VersionDate.ToShortDateString(), cardsDB.VersionInfo }
)
);
}
}
splash.UpdateProgress("Checking data file");
CheckDataFile(container.GetInstance<FileLoader>());
#if DEBUG
var dependecies = container.AnalizeDependecies();
#endif
container.Verify(); //must be outside container constrcutor, otherwise lot of XAML errors in unit tests
//executed after check for new cards db, otheriwse issues when deserailising tracker db - cards from old version can be missing
bool isShiftPressed = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
if (settings.General_StartGameWithTracker && !isShiftPressed)
{
splash.UpdateProgress("Starting game");
container.GetInstance<ILauncherService>().StartGame();
}
splash.UpdateProgress("Creating main form");
MainWindow main = container.GetInstance<MainWindow>();
this.MainWindow = main;
this.MainWindow.Show();
//init overlays
splash.UpdateProgress("Creating overlays");
var overlaysRepo = container.GetInstance<OverlayWindowRepository>();
IEnumerable<OverlayWindowBase> overlayWindowsList = container.GetInstance<IEnumerable<OverlayWindowBase>>();
overlaysRepo.RegisterWindows(overlayWindowsList);
Task.Run(() => overlaysRepo.UpdateOverlayAsync(main, container.GetInstance<IWinAPI>()));
splash.CloseSplash();
this.MainWindow.Activate();
}
private static void CheckDataFile(FileLoader fileLoader)
{
try
{
//try to open data file
fileLoader.LoadDatabase(true);
}
catch (DataFileException ex)
{
bool shutdown = true;
if (ex.CanContinue)
{
MessageBoxResult res = MessageBox.Show(ex.Message, "Datafile problem", MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No);
shutdown = res == MessageBoxResult.No;
}
else
{
MessageBox.Show("Application encountered problems opening data file: " + ex.Message);
}
if (shutdown)
{
Environment.Exit(1); //app.shutdown still init mainwindow and othe cmponents :/
}
}
}
private bool CheckSingleInstance()
{
singleInstance = new SingleInstanceApp();
if (!singleInstance.CheckInstance())
{
MessageBox.Show("ESL Tracker is alrady running", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK,MessageBoxOptions.ServiceNotification);
Application.Current.Shutdown();
return true;
}
return false;
}
}
}