-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProgram.cs
144 lines (127 loc) · 5.35 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
using ESLTracker.BusinessLogic.GameClient;
using ESLTracker.DataModel;
using ESLTracker.Properties;
using ESLTracker.Services;
using ESLTracker.Utils;
using ESLTracker.Utils.DiagnosticsWrappers;
using ESLTracker.Utils.SimpleInjector;
using ESLTracker.ViewModels;
using NLog;
using NLog.Config;
using SimpleInjector;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace ESLTracker
{
static class Program
{
public static bool IsApplicationClosing { get; set; } = false;
static SingleInstanceApp singleInstance;
internal const string UserInfoLogger = "UserInfoLogger";
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})";
[STAThread]
static void Main()
{
InitNLog();
//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");
var container = new MasserContainer();
CheckSingleInstance();
CheckDataFile(container.GetInstance<IFileManager>());
IVersionService vc = container.GetInstance<IVersionService>();
var settings = container.GetInstance<ISettings>();
var newVersion = vc.CheckNewAppVersionAvailable();
if (newVersion.IsAvailable)
{
Logger userInfo = LogManager.GetLogger(App.UserInfoLogger);
userInfo.Info(NewVersionAvailable, new Dictionary<string, string> {
{ OpenChangelog, settings.VersionCheck_LatestBuildUserUrl },
{ Download, newVersion.DownloadUrl }
});
}
if (vc.IsNewCardsDBAvailable())
{
ICardsDatabase cardsDB = vc.GetLatestCardsDB();
Logger log = LogManager.GetLogger(App.UserInfoLogger);
log.Info(CardsDatabaseUpdated, new object[] { cardsDB.Version, cardsDB.VersionDate.ToShortDateString(), cardsDB.VersionInfo });
}
bool isShiftPressed = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
if (settings.General_StartGameWithTracker && !isShiftPressed)
{
var winApi = container.GetInstance<IWinAPI>();
var messanger = container.GetInstance<IMessenger>();
container.GetInstance<ILauncherService>().StartGame();
}
RunApplication(container);
}
private static void InitNLog()
{
ConfigurationItemFactory.Default.Targets
.RegisterDefinition("UserInfoLogger", typeof(ESLTracker.Utils.NLog.UserInfoLoggerTarget));
}
private static void RunApplication(Container container)
{
try
{
ResourceDictionary myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("ControlStyle.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
var app = new App();
var mainWindow = container.GetInstance<MainWindow>();
app.Run(mainWindow);
}
catch (Exception ex)
{
Debugger.Launch();
//Log the exception and exit
}
}
private static void CheckDataFile(IFileManager fileManager)
{
try
{
//try to open data file
fileManager.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 static void CheckSingleInstance()
{
singleInstance = new SingleInstanceApp();
if (!singleInstance.CheckInstance())
{
MessageBox.Show("ESL Tracker is alrady running", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
}
}
}
}