~ruther/NosTale-PacketLogger

ref: 6e4772732055f775c1302818d11360bea1530879 NosTale-PacketLogger/src/PacketLogger/Models/NostaleProcesses.cs -rw-r--r-- 4.1 KiB
6e477273 — Rutherther chore: update pcap to parse incoming packets without knowing encryption key 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
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
//
//  NostaleProcesses.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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Threading.Tasks;
using NosSmooth.Comms.Local;
using NosSmooth.Core.Extensions;
using NosSmooth.LocalBinding;
using NosSmooth.LocalBinding.Errors;
using NosSmooth.LocalBinding.Options;
using ReactiveUI;

namespace PacketLogger.Models;

/// <summary>
/// Keeps and refreshes a collection of NosTale processes.
/// </summary>
public class NostaleProcesses : IDisposable
{
    private readonly IDisposable _cleanUp;
    private readonly List<long> _errorfulProcesses;

    /// <summary>
    /// Initializes a new instance of the <see cref="NostaleProcesses"/> class.
    /// </summary>
    public NostaleProcesses()
    {
        _errorfulProcesses = new List<long>();
        Processes = new ObservableCollection<NostaleProcess>();
        _cleanUp = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
            .ObserveOn(RxApp.TaskpoolScheduler)
            .Subscribe
            (
                _ =>
                {
                    try
                    {
                        Refresh().GetAwaiter().GetResult();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            );
    }

    /// <summary>
    /// Gets NosTale processes.
    /// </summary>
    public ObservableCollection<NostaleProcess> Processes { get; }

    /// <inheritdoc />
    public void Dispose()
    {
        _cleanUp.Dispose();
    }

    private async Task Refresh()
    {
        var nosTaleProcesses = CommsInjector.FindNosTaleProcesses().Select(x => x.Id).ToArray();
        var toRemove = new List<NostaleProcess>();

        foreach (var currentProcess in Processes)
        {
            if (nosTaleProcesses.All(x => x != currentProcess.Process.Id))
            {
                toRemove.Add(currentProcess);
            }
            else
            {
                RxApp.MainThreadScheduler.Schedule(() => currentProcess.ObserveChanges());
            }
        }

        foreach (var remove in toRemove)
        {
            RxApp.MainThreadScheduler.Schedule(() => Processes.Remove(remove));
        }

        foreach (var openProcess in nosTaleProcesses)
        {
            if (Processes.All(x => x.Process.Id != openProcess) && !_errorfulProcesses.Contains(openProcess))
            {
                var process = Process.GetProcessById(openProcess);
                NosBrowserManager nosBrowserManager = new NosBrowserManager
                (
                    process,
                    new PlayerManagerOptions(),
                    new SceneManagerOptions(),
                    new PetManagerOptions(),
                    new NetworkManagerOptions(),
                    new UnitManagerOptions()
                );
                var result = nosBrowserManager.Initialize();
                if (result.IsSuccess || (result.Error is CouldNotInitializeModuleError moduleError
                    && moduleError.Module.Name.Contains("NtClient")))
                {
                    RxApp.MainThreadScheduler.Schedule
                        (() => Processes.Add(new NostaleProcess(process, nosBrowserManager)));
                }
                else
                {
                    _errorfulProcesses.Add(openProcess);
                    Console.WriteLine(result.ToFullString());
                }
            }
        }

        var errorfulToRemove = new List<long>();

        foreach (var errorfulProcess in _errorfulProcesses)
        {
            if (nosTaleProcesses.All(x => x != errorfulProcess))
            {
                errorfulToRemove.Add(errorfulProcess);
            }
        }

        foreach (var errorfulRemove in errorfulToRemove)
        {
            _errorfulProcesses.Remove(errorfulRemove);
        }
    }
}
Do not follow this link