-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
153 lines (132 loc) · 4.58 KB
/
MainForm.cs
File metadata and controls
153 lines (132 loc) · 4.58 KB
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
using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.IO;
using System.Net.NetworkInformation;
using System.Windows.Forms;
namespace WoWSimsApp
{
public partial class MainForm : Form
{
private ProcessManager processManager;
private BinaryHandler binaryHandler;
private TrayIconManager trayIconManager;
private WowApplicationHelper wowApplicationHelper;
private ToolStripMenuItem updateMenuItem;
public MainForm()
{
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.WriteTo.File( Path.Combine( Application.LocalUserAppDataPath, "WoWSims", "logs", "app.log" ), rollingInterval: RollingInterval.Day )
.CreateLogger();
// Configure logging
var loggerFactory = LoggerFactory.Create( builder =>
{
builder.AddSerilog();
} );
var logger = loggerFactory.CreateLogger<MainForm>();
// Attach global exception handlers
AppDomain.CurrentDomain.UnhandledException += ( sender, args ) =>
{
logger.LogError( args.ExceptionObject as Exception, "Unhandled exception occurred" );
};
Application.ThreadException += ( sender, args ) =>
{
logger.LogError( args.Exception, "Thread exception occurred" );
};
// Check for internet connection
if (!IsInternetAvailable())
{
MessageBox.Show("This application requires an internet connection to function. Please check your connection and restart the application.",
"Internet Connection Required",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
Application.Exit();
return;
}
InitializeComponent();
processManager = new ProcessManager();
binaryHandler = new BinaryHandler( processManager );
trayIconManager = new TrayIconManager();
wowApplicationHelper = new WowApplicationHelper();
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
this.FormBorderStyle = FormBorderStyle.None;
InitializeTrayIcons();
// Subscribe to update availability changes
binaryHandler.UpdateAvailabilityChanged += OnUpdateAvailabilityChanged;
// Run update check
binaryHandler.CheckForUpdates( true, false );
}
private static bool IsInternetAvailable()
{
try
{
using (var ping = new Ping())
{
var reply = ping.Send("www.google.com", 3000); // Ping Google with a timeout of 3 seconds
return reply != null && reply.Status == IPStatus.Success;
}
}
catch
{
return false;
}
}
private void OnUpdateAvailabilityChanged( bool isUpdateAvailable )
{
if (updateMenuItem != null)
{
updateMenuItem.Text = isUpdateAvailable ? "Update Sim" : "Check for Updates";
}
}
private void InitializeTrayIcons()
{
trayIconManager.SetDoubleClick( () => processManager.LaunchSim() );
var launchMenu = trayIconManager.AddMenuItem( "Open", 0, null, () => { } );
foreach (var classEntry in SimData.Classes)
{
var classMenu = new ToolStripMenuItem( classEntry.Name, classEntry.Icon );
for (int i = 0; i < classEntry.Specs.Count; i++)
{
string spec = classEntry.Specs[i];
classMenu.DropDownItems.Add( spec, classEntry.SpecIcons[i], ( s, e ) =>
{
string url = $"{Constants.LocalHostUrl}{classEntry.Name.ToLowerInvariant()}/{spec.ToLowerInvariant()}".Replace( ' ', '_' );
processManager.OpenUrl( url );
} );
}
launchMenu.DropDownItems.Add( classMenu );
}
var characterMenu = trayIconManager.AddMenuItem( "Characters", 96, null, () => { } );
updateMenuItem = trayIconManager.AddMenuItem(
binaryHandler.IsUpdateAvailable ? "Update Sim" : "Check for Updates",
99,
null,
() => binaryHandler.CheckForUpdates( false, true ) );
trayIconManager.AddMenuItem( "Exit", 100, null, () =>
{
processManager.KillProcess();
Application.Exit();
} );
trayIconManager.SetOnOpen( () => refreshCharaters() );
void refreshCharaters()
{
characterMenu.DropDownItems.Clear();
var characters = wowApplicationHelper.GetCharacters();
foreach (var character in characters)
{
var localCharacter = character;
characterMenu.DropDownItems.Add( $"{character.Name} ({character.Realm})", SimData.GetSpecIcon( character.Class, character.Spec ), ( s, e ) =>
{
MessageBox.Show( "Character data has been copied to clipboard.\n\nImport -> Addon\n\nand paste to start siming your character.", "Sim Character", MessageBoxButtons.OK, MessageBoxIcon.Information );
string url = $"{Constants.LocalHostUrl}{SimData.CharacterToUrlCombo( character )}".Replace( ' ', '_' );
Clipboard.SetText( character.Json );
processManager.OpenUrl( url );
} );
}
}
}
}
}