~ruther/NosTale-PacketLogger

ref: 86b762f16a33f04280b48e0c61c4585ce1e4b3ec NosTale-PacketLogger/src/PacketLogger/Models/NostaleProcess.cs -rw-r--r-- 2.5 KiB
86b762f1 — František Boháček feat: add settings and filter profiles 2 years ago
                                                                                
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
//
//  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;

/// <summary>
/// A NosTale process.
/// </summary>
public class NostaleProcess : ObservableObject
{
    private bool _wasInGame;
    private string? _lastCharacterName;

    /// <summary>
    /// Initializes a new instance of the <see cref="NostaleProcess"/> class.
    /// </summary>
    /// <param name="process">The process.</param>
    /// <param name="browserManager">The browser manager.</param>
    public NostaleProcess(Process process, NosBrowserManager browserManager)
    {
        Process = process;
        BrowserManager = browserManager;

        ObserveChanges();
    }

    /// <summary>
    /// Gets the name of the character.
    /// </summary>
    public string CharacterString => _lastCharacterName ?? "- (Not in game)";

    /// <summary>
    /// Gets the process string.
    /// </summary>
    public string ProcessString => $"{Process.Id} | {Process.ProcessName}";

    /// <summary>
    /// Gets the process.
    /// </summary>
    public Process Process { get; init; }

    /// <summary>
    /// Gets the browser manager.
    /// </summary>
    public NosBrowserManager BrowserManager { get; init; }

    /// <summary>
    /// Look for changes in the process, fire property changed.
    /// </summary>
    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.
        }
    }
}
Do not follow this link