~ruther/NosSmooth.Local

ref: ce0260d5b61a99ecbbd13177283632c4a7a2e28c NosSmooth.Local/src/Core/NosSmooth.LocalBinding/Structs/NetworkManager.cs -rw-r--r-- 3.2 KiB
ce0260d5 — František Boháček feat: update to managed and raw client 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
//
//  NetworkManager.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 NosSmooth.LocalBinding.Errors;
using NosSmooth.LocalBinding.Extensions;
using NosSmooth.LocalBinding.Options;
using Reloaded.Memory.Sources;
using Remora.Results;

namespace NosSmooth.LocalBinding.Structs;

/// <summary>
/// An object used for PacketSend and PacketReceive functions.
/// </summary>
public class NetworkManager : NostaleObject
{
    /// <summary>
    /// Create <see cref="PlayerManager"/> instance.
    /// </summary>
    /// <param name="nosBrowserManager">The NosTale process browser.</param>
    /// <param name="options">The options.</param>
    /// <returns>The player manager or an error.</returns>
    public static Result<NetworkManager> Create(NosBrowserManager nosBrowserManager, NetworkManagerOptions options)
    {
        var networkObjectAddress = nosBrowserManager.Scanner.FindPattern(options.NetworkObjectPattern);
        if (!networkObjectAddress.Found)
        {
            return new BindingNotFoundError(options.NetworkObjectPattern, "NetworkBinding");
        }

        if (nosBrowserManager.Process.MainModule is null)
        {
            return new NotFoundError("Cannot find the main module of the target process.");
        }

        var staticAddress = (nuint)(nosBrowserManager.Process.MainModule.BaseAddress + networkObjectAddress.Offset
            + options.NetworkObjectOffset);
        return new NetworkManager(nosBrowserManager.Memory, staticAddress);
    }

    private readonly IMemory _memory;
    private readonly nuint _staticNetworkManagerAddress;

    /// <summary>
    /// Initializes a new instance of the <see cref="NetworkManager"/> class.
    /// </summary>
    /// <param name="memory">The memory.</param>
    /// <param name="staticNetworkManagerAddress">The pointer to the beginning of the player manager structure.</param>
    public NetworkManager(IMemory memory, nuint staticNetworkManagerAddress)
        : base(memory, staticNetworkManagerAddress)
    {
        _memory = memory;
        _staticNetworkManagerAddress = staticNetworkManagerAddress;
    }

    /// <summary>
    /// Gets the address to the player manager.
    /// </summary>
    public override nuint Address => GetManagerAddress(false);

    private nuint GetManagerAddress(bool third)
    {
        nuint networkManager = _staticNetworkManagerAddress;
        _memory.Read(networkManager, out networkManager);
        _memory.Read(networkManager, out networkManager);
        _memory.Read(networkManager, out networkManager);

        if (third)
        {
            _memory.Read(networkManager + 0x34, out networkManager);
        }

        return networkManager;
    }

    /// <summary>
    /// Gets an address pointer used in PacketSend function.
    /// </summary>
    /// <returns>Pointer to the object.</returns>
    public nuint GetAddressForPacketSend()
        => GetManagerAddress(false);

    /// <summary>
    /// Gets an address pointer used in PacketReceive function.
    /// </summary>
    /// <returns>Pointer to the object.</returns>
    public nuint GetAddressForPacketReceive()
        => GetManagerAddress(true);
}
Do not follow this link