当前位置:网站首页>.net6 encounter with the League of heroes - create a game assistant according to the official LCU API

.net6 encounter with the League of heroes - create a game assistant according to the official LCU API

2022-07-26 13:42:00 .net is a little handsome

I have seen many small assistant tools of hero alliance developed by myself on the Internet , At that time, I was suffering from no api, I want to be one myself . Later, I found that the fist itself provided LCU API It will be exposed when the client is running .

Now let's learn about the implementation of the tool .

Query data :http Agreement to access resuful Interface , Query some static data , Such as account information , Ranking information , Achievements, etc .

     websocket Binding interface , Get the dynamic information sent by the server , Such as game progress , Select hero real-time data .

We know from the above that , The League of heroes client will definitely get up when it starts web service , And there will be some resuful Interface and websocket Sending of information .

How can we get it ? As long as we open the hero League client and start it with the administrator cmd, Input :

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

You can see some command line information started by the hero alliance client . It is mainly to obtain the service port and token.

 

 

  Next, we just need to parse the information . Get the port in the string ,token, Process number and other information .

using (Process p = new Process())
            {
                p.StartInfo.FileName = _cmdPath;
                p.StartInfo.UseShellExecute = false; // Whether to use the operating system shell start-up 
                p.StartInfo.RedirectStandardInput = true; // Accept input from the calling program 
                p.StartInfo.RedirectStandardOutput = true; // Get the output information from the calling program 
                p.StartInfo.RedirectStandardError = true; // Redirect standard error output 
                p.StartInfo.CreateNoWindow = true; // Don't show program window 
                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;
            }
}

Next we need to start http Service and websocket Listener Service .

httpclient initialization :

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 initialization :

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));
        }

Next, we just need to start these services , Then according to the fist official LCU API You can access some local data .LCU API : https://lcu.vivide.re/

I use it here .net 6 +WPF+VS2022 Made a LOL Tools , You can use it as a reference , Free open source , If you think you can still play a planet ball .

github Address :BruceQiu1996/NPhoenix: Hero League plug-in , It supports modifying the segment , Modify career background , Automatically accept the match , Set Rune , Check the record of teammates , Superior horse analysis, etc (github.com)

Tool support :

 | Choose heroes in seconds | Automatically accept the match | Ranking list of national service data

  Check the hero contraposition suppression | Check the hero advantage line | 5v5 Rune configuration | Modify the segment | Modify career background

  Send horse information to the chat interface | Check the Summoner's record | Check the Summoner's unique hero | Check the Summoner's record details

  Choose your favorite hero in chaos | The Rune of chaos is recommended

Here are some screenshots of the tool :

 

 

 

 

 

原网站

版权声明
本文为[.net is a little handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207261339045296.html