// // NostaleProcess.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.ComponentModel; using System.Diagnostics; using System.Reactive.Linq; using CommunityToolkit.Mvvm.ComponentModel; using NosSmooth.LocalBinding; using ReactiveUI; using Reloaded.Memory.Exceptions; namespace PacketLogger.Models; /// /// A NosTale process. /// public class NostaleProcess : ObservableObject { private bool _wasInGame; private string? _lastCharacterName; /// /// Initializes a new instance of the class. /// /// The process. /// The browser manager. public NostaleProcess(Process process, NosBrowserManager browserManager) { Process = process; BrowserManager = browserManager; ObserveChanges(); } /// /// Gets the name of the character. /// public string CharacterString => _lastCharacterName ?? "- (Not in game)"; /// /// Gets the process string. /// public string ProcessString => $"{Process.Id} | {Process.ProcessName}"; /// /// Gets the process. /// public Process Process { get; init; } /// /// Gets the browser manager. /// public NosBrowserManager BrowserManager { get; init; } /// /// Look for changes in the process, fire property changed. /// internal void ObserveChanges() { try { if (BrowserManager.IsInGame != _wasInGame) { OnPropertyChanging(nameof(BrowserManager)); OnPropertyChanged(nameof(BrowserManager)); } var currentCharacterName = BrowserManager.IsInGame ? BrowserManager.PlayerManager.Player.Name : null; var changed = _lastCharacterName != currentCharacterName; if (changed) { OnPropertyChanging(nameof(CharacterString)); } _wasInGame = BrowserManager.IsInGame; _lastCharacterName = currentCharacterName; if (changed) { OnPropertyChanged(nameof(CharacterString)); } } catch (MemoryPermissionException) { // ignored, wait a bit, NosTale is probably just starting. } } }