|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Security.Cryptography; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Avalonia.Media; |
| 8 | +using AvaloniaControls; |
| 9 | +using AvaloniaControls.ControlServices; |
| 10 | +using Material.Icons; |
| 11 | +using SnesConnectorLibrary; |
| 12 | +using SnesConnectorLibrary.Requests; |
| 13 | +using SNI; |
| 14 | +using TrackerCouncil.Smz3.Data.Options; |
| 15 | +using TrackerCouncil.Smz3.Shared.Enums; |
| 16 | +using TrackerCouncil.Smz3.UI.ViewModels; |
| 17 | + |
| 18 | +namespace TrackerCouncil.Smz3.UI.Services; |
| 19 | + |
| 20 | +public class SetupWindowService(OptionsFactory optionsFactory, ISnesConnectorService snesConnectorService) : ControlService |
| 21 | +{ |
| 22 | + private const string Z3Hash = "03a63945398191337e896e5771f77173"; |
| 23 | + private const string SMHash = "21f3e98df4780ee1c667b84e57d88675"; |
| 24 | + |
| 25 | + private SetupWindowViewModel _model = new(); |
| 26 | + private RandomizerOptions _randomizerOptions = null!; |
| 27 | + private CancellationTokenSource _cancellationTokenSource = new(); |
| 28 | + |
| 29 | + public SetupWindowViewModel GetViewModel() |
| 30 | + { |
| 31 | + snesConnectorService.GameDetected += SnesConnectorServiceOnConnected; |
| 32 | + _randomizerOptions = optionsFactory.Create(); |
| 33 | + SetZeldaRomPath(_randomizerOptions.GeneralOptions.Z3RomPath); |
| 34 | + SetMetroidRomPath(_randomizerOptions.GeneralOptions.SMRomPath); |
| 35 | + _randomizerOptions.GeneralOptions.HasOpenedSetupWindow = true; |
| 36 | + _randomizerOptions.Save(); |
| 37 | + snesConnectorService.CreateLuaScriptsFolder(_randomizerOptions.AutoTrackerScriptsOutputPath); |
| 38 | + return _model; |
| 39 | + } |
| 40 | + |
| 41 | + public bool SetRomPaths(IEnumerable<string> romPaths) |
| 42 | + { |
| 43 | + var anyInvalidRom = false; |
| 44 | + |
| 45 | + foreach (var path in romPaths) |
| 46 | + { |
| 47 | + if (CheckFileHash(path, Z3Hash)) |
| 48 | + { |
| 49 | + _model.ZeldaRomPath = path; |
| 50 | + _model.IsValidZeldaRom = true; |
| 51 | + } |
| 52 | + else if (CheckFileHash(path, SMHash)) |
| 53 | + { |
| 54 | + _model.MetroidRomPath = path; |
| 55 | + _model.IsValidMetroidRom = true; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + anyInvalidRom = true; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return !anyInvalidRom; |
| 64 | + } |
| 65 | + |
| 66 | + public async Task TestAutoTracking() |
| 67 | + { |
| 68 | + if (_model.IsConnecting) |
| 69 | + { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + _model.IsConnecting = true; |
| 74 | + _cancellationTokenSource = new CancellationTokenSource(); |
| 75 | + _model.AutoTrackerOpacity = 0.2f; |
| 76 | + _model.AutoTrackerIconKind = MaterialIconKind.CircleOutline; |
| 77 | + _model.AutoTrackerBrush = Brushes.White; |
| 78 | + _model.AutoTrackerMessage = "Connecting..."; |
| 79 | + |
| 80 | + snesConnectorService.Connect(GetSnesConnectorSettings()); |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + await Task.Delay(TimeSpan.FromSeconds(20), _cancellationTokenSource.Token); |
| 85 | + |
| 86 | + // If the task wasn't cancelled, then it didn't connect at all fully |
| 87 | + _model.AutoTrackerOpacity = 1; |
| 88 | + _model.AutoTrackerIconKind = MaterialIconKind.CloseCircleOutline; |
| 89 | + _model.AutoTrackerBrush = Brushes.IndianRed; |
| 90 | + _model.AutoTrackerMessage = "Unable to connect"; |
| 91 | + await _cancellationTokenSource.CancelAsync(); |
| 92 | + } |
| 93 | + catch |
| 94 | + { |
| 95 | + // Do nothing |
| 96 | + } |
| 97 | + |
| 98 | + _model.IsConnecting = false; |
| 99 | + snesConnectorService.Disconnect(); |
| 100 | + } |
| 101 | + |
| 102 | + public void SaveSettings() |
| 103 | + { |
| 104 | + if (_model.IsValidZeldaRom) |
| 105 | + { |
| 106 | + _randomizerOptions.GeneralOptions.Z3RomPath = _model.ZeldaRomPath; |
| 107 | + } |
| 108 | + |
| 109 | + if (_model.IsValidMetroidRom) |
| 110 | + { |
| 111 | + _randomizerOptions.GeneralOptions.SMRomPath = _model.MetroidRomPath; |
| 112 | + } |
| 113 | + |
| 114 | + _randomizerOptions.GeneralOptions.SnesConnectorSettings = GetSnesConnectorSettings(); |
| 115 | + |
| 116 | + if (!_model.TrackerVoiceEnabled) |
| 117 | + { |
| 118 | + _randomizerOptions.GeneralOptions.SpeechRecognitionMode = SpeechRecognitionMode.Disabled; |
| 119 | + _randomizerOptions.GeneralOptions.TrackerVoiceFrequency = TrackerVoiceFrequency.Disabled; |
| 120 | + } |
| 121 | + |
| 122 | + _randomizerOptions.GeneralOptions.SelectedProfiles = GetSelectedProfiles(); |
| 123 | + _randomizerOptions.Save(); |
| 124 | + } |
| 125 | + |
| 126 | + public void OnClose() |
| 127 | + { |
| 128 | + snesConnectorService.GameDetected -= SnesConnectorServiceOnConnected; |
| 129 | + snesConnectorService.Disconnect(); |
| 130 | + } |
| 131 | + |
| 132 | + public void OpenLuaFolder() |
| 133 | + { |
| 134 | + CrossPlatformTools.OpenDirectory(_randomizerOptions.AutoTrackerScriptsOutputPath); |
| 135 | + } |
| 136 | + |
| 137 | + private SnesConnectorSettings GetSnesConnectorSettings() |
| 138 | + { |
| 139 | + var snesConnectorSettings = new SnesConnectorSettings(); |
| 140 | + |
| 141 | + if (_model.AutoTrackingDisable) |
| 142 | + { |
| 143 | + snesConnectorSettings.ConnectorType = SnesConnectorType.None; |
| 144 | + } |
| 145 | + else if (_model.AutoTrackingLua) |
| 146 | + { |
| 147 | + snesConnectorSettings.ConnectorType = SnesConnectorType.Lua; |
| 148 | + } |
| 149 | + else if (_model.AutoTrackingEmoTracker) |
| 150 | + { |
| 151 | + snesConnectorSettings.ConnectorType = SnesConnectorType.LuaEmoTracker; |
| 152 | + } |
| 153 | + else if (_model.AutoTrackingUsb2Snes) |
| 154 | + { |
| 155 | + snesConnectorSettings.ConnectorType = SnesConnectorType.Usb2Snes; |
| 156 | + snesConnectorSettings.Usb2SnesAddress = _model.ConnectorIpAddress; |
| 157 | + } |
| 158 | + else if (_model.AutoTrackingSni) |
| 159 | + { |
| 160 | + snesConnectorSettings.ConnectorType = SnesConnectorType.Sni; |
| 161 | + snesConnectorSettings.SniAddress = _model.ConnectorIpAddress; |
| 162 | + } |
| 163 | + |
| 164 | + return snesConnectorSettings; |
| 165 | + } |
| 166 | + |
| 167 | + private List<string?> GetSelectedProfiles() |
| 168 | + { |
| 169 | + var profiles = new List<string?>(); |
| 170 | + |
| 171 | + if (_model.TrackerSassEnabled) |
| 172 | + { |
| 173 | + profiles.Add("Sassy"); |
| 174 | + } |
| 175 | + |
| 176 | + if (_model.TrackerCursingEnabled) |
| 177 | + { |
| 178 | + profiles.Add("Rated T for Teen"); |
| 179 | + } |
| 180 | + |
| 181 | + if (_model.TrackerBcuEnabled) |
| 182 | + { |
| 183 | + profiles.Add("BCU"); |
| 184 | + } |
| 185 | + |
| 186 | + return profiles; |
| 187 | + } |
| 188 | + |
| 189 | + private void SetZeldaRomPath(string? path) |
| 190 | + { |
| 191 | + if (File.Exists(path) && CheckFileHash(path, Z3Hash)) |
| 192 | + { |
| 193 | + _model.ZeldaRomPath = path; |
| 194 | + _model.IsValidZeldaRom = true; |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + _model.ZeldaRomPath = "Not Selected"; |
| 199 | + _model.IsValidZeldaRom = false; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private void SetMetroidRomPath(string? path) |
| 204 | + { |
| 205 | + if (File.Exists(path) && CheckFileHash(path, SMHash)) |
| 206 | + { |
| 207 | + _model.MetroidRomPath = path; |
| 208 | + _model.IsValidMetroidRom = true; |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + _model.MetroidRomPath = "Not Selected"; |
| 213 | + _model.IsValidMetroidRom = false; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + private bool CheckFileHash(string path, string expectedHash) |
| 218 | + { |
| 219 | + var bytes = File.ReadAllBytes(path); |
| 220 | + var hash = MD5.HashData(bytes); |
| 221 | + var hashString = BitConverter.ToString(hash).Replace("-", ""); |
| 222 | + return expectedHash.Equals(hashString, StringComparison.OrdinalIgnoreCase); |
| 223 | + } |
| 224 | + |
| 225 | + private void SnesConnectorServiceOnConnected(object? sender, EventArgs e) |
| 226 | + { |
| 227 | + _ = TestGetMemoryAsync(); |
| 228 | + } |
| 229 | + |
| 230 | + private async Task TestGetMemoryAsync() |
| 231 | + { |
| 232 | + for (var i = 0; i < 5 && !_cancellationTokenSource.IsCancellationRequested; i++) |
| 233 | + { |
| 234 | + var response = await snesConnectorService.MakeMemoryRequestAsync(new SnesSingleMemoryRequest() |
| 235 | + { |
| 236 | + MemoryRequestType = SnesMemoryRequestType.RetrieveMemory, |
| 237 | + SnesMemoryDomain = SnesMemoryDomain.ConsoleRAM, |
| 238 | + AddressFormat = AddressFormat.Snes9x, |
| 239 | + SniMemoryMapping = MemoryMapping.Unknown, |
| 240 | + Address = 0x7e0000, |
| 241 | + Length = 0x1 |
| 242 | + }); |
| 243 | + |
| 244 | + if (response is { Successful: true, HasData: true }) |
| 245 | + { |
| 246 | + _model.AutoTrackerOpacity = 1; |
| 247 | + _model.AutoTrackerIconKind = MaterialIconKind.CheckCircleOutline; |
| 248 | + _model.AutoTrackerBrush = Brushes.Lime; |
| 249 | + _model.AutoTrackerMessage = "Connection successful!"; |
| 250 | + break; |
| 251 | + } |
| 252 | + else |
| 253 | + { |
| 254 | + _model.AutoTrackerOpacity = 1; |
| 255 | + _model.AutoTrackerIconKind = MaterialIconKind.CloseCircleOutline; |
| 256 | + _model.AutoTrackerBrush = Brushes.IndianRed; |
| 257 | + _model.AutoTrackerMessage = "Invalid response"; |
| 258 | + await Task.Delay(TimeSpan.FromSeconds(2f)); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + await _cancellationTokenSource.CancelAsync(); |
| 263 | + } |
| 264 | +} |
0 commit comments