File size: 2,366 Bytes
53764a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using SilkroadBot.Domain.Enums;
using SilkroadBot.Domain.Interfaces;
using SilkroadBot.Domain.Adapters;

namespace SilkroadBot.Core.Configuration;

/// <summary>
/// Registry and factory for protocol adapters.
/// Configuration-driven: adapters are selected based on game client type.
/// </summary>
public class ProtocolAdapterRegistry
{
    private readonly Dictionary<GameClientType, IProtocolAdapter> _adapters = new();
    private readonly Dictionary<string, CustomServerConfig> _customServers = new();

    public ProtocolAdapterRegistry()
    {
        // Register built-in adapters
        Register(new ISROProtocolAdapter());
        Register(new TRSROProtocolAdapter());
        Register(new VSROProtocolAdapter());
    }

    /// <summary>
    /// Register a protocol adapter.
    /// </summary>
    public void Register(IProtocolAdapter adapter)
    {
        _adapters[adapter.ClientType] = adapter;
    }

    /// <summary>
    /// Get adapter for a specific client type.
    /// </summary>
    public IProtocolAdapter GetAdapter(GameClientType clientType)
    {
        if (_adapters.TryGetValue(clientType, out var adapter))
            return adapter;
        throw new NotSupportedException($"No adapter registered for {clientType}");
    }

    /// <summary>
    /// Register a custom private server configuration.
    /// </summary>
    public void RegisterCustomServer(string name, CustomServerConfig config)
    {
        _customServers[name] = config;
    }

    /// <summary>
    /// Get all registered adapters.
    /// </summary>
    public IReadOnlyList<IProtocolAdapter> GetAllAdapters() => _adapters.Values.ToList();

    /// <summary>
    /// Get all custom server configurations.
    /// </summary>
    public IReadOnlyDictionary<string, CustomServerConfig> GetCustomServers() => _customServers;
}

/// <summary>
/// Configuration for a custom/private server.
/// </summary>
public record CustomServerConfig
{
    public string Name { get; init; } = string.Empty;
    public string Host { get; init; } = "localhost";
    public int GatewayPort { get; init; } = 15779;
    public int AgentPort { get; init; } = 15780;
    public GameClientType BaseProtocol { get; init; } = GameClientType.VSRO;
    public string ProtocolVersion { get; init; } = "vSRO_188";
    public Dictionary<string, string> CustomOpcodes { get; init; } = new();
}