Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
201 lines (174 loc) · 6.97 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
201 lines (174 loc) · 6.97 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
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
using MapyCZforTS_CS.Properties;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
namespace MapyCZforTS_CS
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Proxy? proxy;
private readonly bool init = false;
public MainWindow()
{
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
{
if (e.ExceptionObject is Exception ex)
Utils.Log($"Unhandled exception occured:{Environment.NewLine}{ex.StackTrace}", Utils.LOG_LEVEL.ERROR);
_onClosing();
};
Utils.Log("Initializing UI", Utils.LOG_LEVEL.VERBOSE);
InitializeComponent();
App.MW = this;
AppName.Content = $"Mapy.cz for Train Simulator v{Assembly.GetExecutingAssembly().GetName().Version}";
App.Mapsets.ForEach(x => mapsetInput.Items.Add(x.Name) );
mapsetInput.SelectedIndex = Settings.Default.Mapset;
apiKeyInput.Text = Settings.Default.ApiKey;
portInput.Value = Settings.Default.Port;
cachingCheckbox.IsChecked = Settings.Default.Cache;
loggingCheckbox.IsChecked = Settings.Default.AdvancedLogging;
if (string.IsNullOrWhiteSpace(apiKeyInput.Text))
{
toggleProxy.IsEnabled = false;
toggleProxy.ToolTip = Localization.Strings.ButtonProxyTooltip;
}
Utils.Log($"Loading settings:{Environment.NewLine}\tMapset: {App.Mapsets[Settings.Default.Mapset]}{Environment.NewLine}\tPort: {Settings.Default.Port}{Environment.NewLine}\tUse cache: {Settings.Default.Cache}{Environment.NewLine}\tAdvanced log: {Settings.Default.AdvancedLogging}", Utils.LOG_LEVEL.VERBOSE);
init = true;
}
public void StopProxy()
{
if (proxy?.ProxyRunning == true)
{
Utils.DisableProxy();
proxy.Stop();
proxy = null;
toggleProxy.Content = Localization.Strings.ButtonEnableProxy;
}
}
private void apiKeyInput_SelectionChanged(object sender, RoutedEventArgs e)
{
string apiKey = apiKeyInput.Text.Trim();
if (string.IsNullOrEmpty(apiKey))
{
if (proxy?.ProxyRunning != true)
{
toggleProxy.IsEnabled = false;
toggleProxy.ToolTip = Localization.Strings.ButtonProxyTooltip;
}
return;
}
toggleProxy.IsEnabled = true;
toggleProxy.ToolTip = null;
Settings.Default.ApiKey = apiKey;
Settings.Default.Save();
}
private void mapsetInput_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
byte mapset = (byte)mapsetInput.SelectedIndex;
byte maxZoom = App.Mapsets[mapset].MaxZoom;
byte posNum = (byte)(maxZoom > 16 ? 22 - maxZoom : maxZoom - 12);
string posSide = maxZoom > 16 ? Localization.Strings.FromRight : Localization.Strings.FromLeft;
MaxZoomText.Text = string.Format(Localization.Strings.MaxZoom, maxZoom, posNum, posSide);
if (!init)
return;
Settings.Default.Mapset = mapset;
Settings.Default.Save();
Utils.Log($"UI -> Changed mapset to {App.Mapsets[mapset]}");
Utils.CleanIECache();
}
private void toggleProxy_Click(object sender, RoutedEventArgs e)
{
if (proxy?.ProxyRunning == true)
{
StopProxy();
}
else
{
App.InvalidApiKey = false;
proxy = Utils.EnableProxy();
proxy.Start();
toggleProxy.Content = Localization.Strings.ButtonDisableProxy;
}
Utils.CleanIECache();
}
private void openLog_Click(object sender, RoutedEventArgs e)
{
if (!File.Exists(Utils.LogPath))
return;
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(Utils.LogPath) { UseShellExecute = true });
}
private void clearCache_Click(object sender, RoutedEventArgs e)
{
try
{
Utils.Log($"CACHE -> Deleting local image cache per user request");
if (Directory.Exists(MapTile.SourceCache))
Directory.Delete(MapTile.SourceCache, true);
if (Directory.Exists(MapTile.OutCache))
Directory.Delete(MapTile.OutCache, true);
Utils.Log($"CACHE -> Succesfully deleted local image cache per user request");
}
catch (Exception ex)
{
Utils.Log($"CACHE -> Failed to delete local cache: {ex}", Utils.LOG_LEVEL.ERROR);
}
}
private void loggingCheckbox_Checked(object sender, RoutedEventArgs e)
{
if (init)
{
Settings.Default.AdvancedLogging = loggingCheckbox.IsChecked == true;
Settings.Default.Save();
Utils.Log(Settings.Default.AdvancedLogging ? "UI -> Enabled advanced logging" : "UI -> Disabled advanced logging");
}
}
private void cachingCheckbox_Checked(object sender, RoutedEventArgs e)
{
if (init)
{
Settings.Default.Cache = cachingCheckbox.IsChecked == true;
Settings.Default.Save();
Utils.Log(Settings.Default.Cache ? "UI -> Enabled caching" : "UI -> Disabled caching");
}
}
private void portInput_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (init)
{
int newPort = (int)portInput.Value;
Settings.Default.Port = newPort;
Settings.Default.Save();
Utils.Log($"UI -> Port changed to {newPort}");
if (proxy != null)
{
proxy.ChangePort(newPort);
Utils.SetProxyPort(
Registry.CurrentUser.CreateSubKey(Path.Join("Software", "Microsoft", "Windows", "CurrentVersion", "Internet Settings")),
newPort
);
}
}
}
private void _onClosing()
{
if (proxy?.ProxyRunning == true)
{
Utils.Log("Closing application");
Utils.DisableProxy();
proxy.Stop();
proxy = null;
}
}
protected override void OnClosing(CancelEventArgs e)
{
_onClosing();
base.OnClosing(e);
}
}
}