qwqwQAQ

看了网上很多自己开发的英雄联盟的小助手工具,当时苦于没有api,自己也想做一个。后来发现了其实拳头本身就提供了LCU API在客户端运行的时候会暴露出来。

现在我们就来了解下工具的实现。

查询数据:http协议访问resuful接口,查询一些静态数据,如账号信息,排位信息,战绩等。

     websocket绑定接口,获取服务端发送过来的动态信息,如游戏进程,选英雄实时数据。

从上面我们得知,英雄联盟客户端在启动的时候一定会起来一个web服务,并且会有一些resuful接口和websocket信息的发送。

那我们如何获取到呢?只要我们打开英雄联盟客户端并且用管理员启动cmd,输入:

wmic PROCESS WHERE name='LeagueClientUx.exe' GET commandline 

就能看到一些英雄联盟客户端启动的命令行信息。主要是获取其中的服务端口和token.

 

 

 接下来我们只要解析其中的信息。获取字符串中的端口,token,进程号等信息。

using (Process p = new Process())
            {
                p.StartInfo.FileName = _cmdPath;
                p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
                p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
                p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
                p.StartInfo.RedirectStandardError = true; //重定向标准错误输出
                p.StartInfo.CreateNoWindow = true; //不显示程序窗口
                p.Start();
                p.StandardInput.WriteLine(_excuteShell.TrimEnd('&') + "&exit");
                p.StandardInput.AutoFlush = true;
                string authenticate = await p.StandardOutput.ReadToEndAsync();
                p.WaitForExit();
                p.Close();

                var authenticate = await GetAuthenticate();
                    if (!string.IsNullOrEmpty(authenticate) && authenticate.Contains("--remoting-auth-token="))
                    {
                        var tokenResults = authenticate.Split("--remoting-auth-token=");
                        var portResults = authenticate.Split("--app-port=");
                        var PidResults = authenticate.Split("--app-pid=");
                        var installLocations = authenticate.Split("--install-directory=");
                        Constant.Token = tokenResults[1].Substring(0, tokenResults[1].IndexOf("\""));
                        Constant.Port = int.TryParse(portResults[1].Substring(0, portResults[1].IndexOf("\"")), out var temp) ? temp : 0;
                        Constant.Pid = int.TryParse(PidResults[1].Substring(0, PidResults[1].IndexOf("\"")), out var temp1) ? temp1 : 0;
            }
}

接下来我们需要启动http服务和websocket监听服务。

httpclient初始化:

public Task Initialize(int port, string token)
        {
            Port = port;
            Token = token;
            CreateHttpClient();
            var authTokenBytes = Encoding.ASCII.GetBytes($"riot:{token}");
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authTokenBytes));
            _httpClient.BaseAddress = new Uri($"https://127.0.0.1:{port}/");

            return Task.CompletedTask;
        }

websocket初始化:

public Task Initialize(int port, string token)
        {
            _webSocket = new WebSocket($"wss://127.0.0.1:{port}/", "wamp");
            _webSocket.SetCredentials("riot", token, true);
            _webSocket.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12;
            _webSocket.SslConfiguration.ServerCertificateValidationCallback = (response, cert, chain, errors) => true;

            _webSocket.OnMessage += WssOnOnMessage;

            return Task.CompletedTask;
        }

        private void WssOnOnMessage(object sender, MessageEventArgs e)
        {
            if (!e.IsText) return;

            var eventArray = JArray.Parse(e.Data);
            var eventNumber = eventArray[0].ToObject<int>();
            if (eventNumber != ClientEventNumber) return;
            var leagueEvent = eventArray[ClientEventData].ToObject<EventArgument>();
            if (string.IsNullOrWhiteSpace(leagueEvent?.Uri))
                return;

            MessageReceived?.Invoke(this, leagueEvent);
            if (!_subscribers.TryGetValue(leagueEvent.Uri, out List<EventHandler<EventArgument>> eventHandlers))
            {
                return;
            }

            eventHandlers.ForEach(eventHandler => eventHandler?.Invoke(this, leagueEvent));
        }

接下来我们只要启动这些服务,然后根据拳头官方的LCU API就能够访问到本地的一些数据了。LCU API : https://lcu.vivide.re/

我这边用.net 6 +WPF+VS2022制作了一个LOL工具,大家可以作为参考,开源免费,如果觉得还可以打个星球球了。

github地址:BruceQiu1996/NPhoenix: 英雄联盟插件,支持修改段位,修改生涯背景,自动接受对局,设置符文,查看队友战绩,上等马分析等等 (github.com)

工具支持:

相关文章: