Repository navigation
Expand file tree
/
Copy pathMainWindow.axaml.cs
More file actions
189 lines (162 loc) · 6.89 KB
/
Copy pathMainWindow.axaml.cs
File metadata and controls
189 lines (162 loc) · 6.89 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
using System;
using System.Diagnostics;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using SciLors_Mashed_Trainer.Controls;
using SciLors_Mashed_Trainer.Core;
using SciLors_Mashed_Trainer.Types;
namespace SciLors_Mashed_Trainer {
public partial class MainWindow : Window {
private readonly DispatcherTimer timer = new DispatcherTimer();
private Game? game;
private readonly Player?[] players = new Player?[4];
private readonly UcPlayerInfo[] playerInfos = new UcPlayerInfo[4];
private readonly bool simulate = Array.IndexOf(Environment.GetCommandLineArgs(), "--simulate") >= 0;
private Win32MemoryTarget? win32Target;
private int idleTicks;
public string ProgramVersion => "v0.2.1";
public string ProgramName => "mashed-trainer";
public string ProgramLongName => "SciLor's Mashed Trainer";
public string ProgramTitle => $"{ProgramLongName} {ProgramVersion}";
public MainWindow() {
InitializeComponent();
Title = ProgramTitle;
InitializePlayerGrid();
UpdateIconMenu();
tabMain.IsEnabled = false;
timer.Interval = TimeSpan.FromMilliseconds(50);
timer.Tick += Timer_Tick;
timer.Start();
}
private void InitializePlayerGrid() {
var grid = this.FindControl<Grid>("grdPlayers");
if (grid == null) return;
grid.Children.Clear();
for (int i = 0; i < playerInfos.Length; i++) {
var playerInfo = new UcPlayerInfo {
Header = $"Player {i + 1}"
};
Grid.SetColumn(playerInfo, i);
playerInfos[i] = playerInfo;
grid.Children.Add(playerInfo);
}
}
private void SetStatus(string message, bool isConnected) {
var statusText = this.FindControl<TextBlock>("txtStatus");
var statusLed = this.FindControl<Avalonia.Controls.Shapes.Ellipse>("elpStatusLed");
if (statusText != null) statusText.Text = message;
if (statusLed != null) {
statusLed.Fill = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Color.Parse(isConnected ? "#00E676" : "#FFA000"));
}
}
private void UpdateGameInfo() {
txtGameInfo.Text = game == null ? "" :
$"{(game.IsActive ? "Round running" : "Not in a round")} · {game.PlayerCount} players · {game.MaximumPoints} points to win";
}
private void Timer_Tick(object? sender, EventArgs e) {
try { TickCore(); } finally { UpdateGameInfo(); }
}
private void TickCore() {
var ucGameInfo = this.FindControl<UcGameInfo>("ucGameInfo");
if (simulate) {
if (game == null) {
var mock = new MockMemoryTarget();
AttachToTarget(mock);
SetStatus("Running with Mock Game Target (Simulator)", true);
} else {
game.Update();
}
return;
}
// Detached: look for MFL.exe once per second instead of every tick
if (game == null && idleTicks++ % 20 != 0) return;
Process[] proc = Process.GetProcessesByName("MFL");
if (proc.Length == 1) {
if (game == null) {
try {
win32Target = new Win32MemoryTarget(proc[0]);
AttachToTarget(win32Target);
SetStatus($"Attached to Mashed PID {proc[0].Id}", true);
} catch (Exception ex) {
SetStatus($"Failed to attach: {ex.Message}", false);
}
} else {
game.Update();
var err = win32Target?.LastWriteError;
if (err != null) SetStatus(err, false);
else SetStatus($"Mashed Process PID: {proc[0].Id}", true);
}
} else if (proc.Length > 1) {
SetStatus("Multiple MFL.exe processes found - close all but one.", false);
} else if (game != null) {
CleanUp();
SetStatus("Mashed disconnected. Waiting for MFL.exe...", false);
}
}
private void AttachToTarget(IMemoryTarget target) {
var ucGameInfo = this.FindControl<UcGameInfo>("ucGameInfo");
game = new Game(target);
game.Update();
tabMain.IsEnabled = true;
foreach (Player.PlayerId playerId in Enum.GetValues<Player.PlayerId>()) {
int id = (int)playerId;
players[id] = new Player(game, playerId);
playerInfos[id].Player = players[id];
}
if (ucGameInfo != null) {
ucGameInfo.Game = game;
}
}
public void AttachMockForTesting(MockMemoryTarget mockTarget) {
AttachToTarget(mockTarget);
}
private void CleanUp() {
var ucGameInfo = this.FindControl<UcGameInfo>("ucGameInfo");
if (game != null) {
foreach (var playerInfo in playerInfos) {
playerInfo.Player = null;
}
Array.Clear(players, 0, players.Length);
if (ucGameInfo != null) ucGameInfo.Game = null;
game.Dispose();
game = null;
win32Target = null;
idleTicks = 0;
tabMain.IsEnabled = false;
}
}
protected override void OnClosing(WindowClosingEventArgs e) {
timer.Stop();
CleanUp();
base.OnClosing(e);
}
private void mniExit_Click(object? sender, RoutedEventArgs e) {
Close();
}
private void mniIcon_Click(object? sender, RoutedEventArgs e) {
if (sender is MenuItem { Tag: string pack }) IconPack.Set(pack);
UpdateIconMenu();
}
private void UpdateIconMenu() {
foreach (var item in new[] { mniIconIngame, mniIconClassic })
item.Icon = (string?)item.Tag == IconPack.Current ? new TextBlock { Text = "✓" } : null;
}
private void mniWebsite_Click(object? sender, RoutedEventArgs e) {
OpenUrl("http://www.scilor.com/mashed-trainer.html");
}
private void mniDonate_Click(object? sender, RoutedEventArgs e) {
OpenUrl("http://www.scilor.com/donate.html");
}
private static void OpenUrl(string url) {
try {
Process.Start(new ProcessStartInfo {
FileName = url,
UseShellExecute = true
});
} catch {
// Ignore browser launch failure on minimal environments
}
}
}
}