Ejemplo n.º 1
0
        public static WebRequest PrepareRequest(
            RichPresence presence,
            string applicationID,
            int port = 6463)
        {
            if (presence != null)
            {
                if (presence.HasSecrets())
                {
                    throw new BadPresenceException("Cannot send a presence with secrets as HTTP endpoint does not suppport events.");
                }
                if (presence.HasParty() && presence.Party.Max < presence.Party.Size)
                {
                    throw new BadPresenceException("Presence maximum party size cannot be smaller than the current size.");
                }
            }
            int    id   = Process.GetCurrentProcess().Id;
            string json = JsonConvert.SerializeObject((object)new PresenceCommand()
            {
                PID      = id,
                Presence = presence
            }.PreparePayload(DateTime.UtcNow.ToFileTime()));

            return(new WebRequest("http://127.0.0.1:" + (object)port + "/rpc?v=" + (object)RpcConnection.VERSION + "&client_id=" + applicationID, json));
        }
Ejemplo n.º 2
0
        public static WebRequest PrepareRequest(RichPresence presence, string applicationID, int port = 6463)
        {
            //Validate the presence
            if (presence != null)
            {
                //Send valid presence
                //Validate the presence with our settings
                if (presence.HasSecrets())
                {
                    throw new BadPresenceException("Cannot send a presence with secrets as HTTP endpoint does not suppport events.");
                }

                if (presence.HasParty() && presence.Party.Max < presence.Party.Size)
                {
                    throw new BadPresenceException("Presence maximum party size cannot be smaller than the current size.");
                }
            }

            //Prepare some params
            int pid = System.Diagnostics.Process.GetCurrentProcess().Id;

            //Prepare the payload
            PresenceCommand command = new PresenceCommand()
            {
                PID = pid, Presence = presence
            };
            var payload = command.PreparePayload(DateTime.UtcNow.ToFileTime());

            string json = JsonConvert.SerializeObject(payload);

            string url = "http://127.0.0.1:" + port + "/rpc?v=" + RpcConnection.VERSION + "&client_id=" + applicationID;

            return(new WebRequest(url, json));
        }
Ejemplo n.º 3
0
        public static bool TrySetRichPresence(RichPresence presence, out RichPresence response, string applicationID, int port = 6463)
        {
            //Validate the presence
            if (presence != null)
            {
                //Send valid presence
                //Validate the presence with our settings
                if (presence.HasSecrets())
                {
                    throw new BadPresenceException("Cannot send a presence with secrets as HTTP endpoint does not suppport events.");
                }

                if (presence.HasParty() && presence.Party.Max < presence.Party.Size)
                {
                    throw new BadPresenceException("Presence maximum party size cannot be smaller than the current size.");
                }
            }

            //Iterate over the ports until the first succesfull one
            for (int p = port; p < 6472; p++)
            {
                //Prepare the url and json
                using (WebClient client = new WebClient())
                {
                    try
                    {
                        WebRequest request = PrepareRequest(presence, applicationID, p);
                        client.Headers.Add("content-type", "application/json");

                        var result = client.UploadString(request.URL, request.Data);
                        if (TryParseResponse(result, out response))
                        {
                            return(true);
                        }
                    }
                    catch (Exception)
                    {
                        //Something went wrong, but we are just going to ignore it and try the next port.
                    }
                }
            }

            //we failed, return null
            response = null;
            return(false);
        }
Ejemplo n.º 4
0
 public static bool TrySetRichPresence(
     RichPresence presence,
     out RichPresence response,
     string applicationID,
     int port = 6463)
 {
     if (presence != null)
     {
         if (presence.HasSecrets())
         {
             throw new BadPresenceException("Cannot send a presence with secrets as HTTP endpoint does not suppport events.");
         }
         if (presence.HasParty() && presence.Party.Max < presence.Party.Size)
         {
             throw new BadPresenceException("Presence maximum party size cannot be smaller than the current size.");
         }
     }
     for (int port1 = port; port1 < 6472; ++port1)
     {
         using (WebClient webClient = new WebClient())
         {
             try
             {
                 WebRequest webRequest = WebRPC.PrepareRequest(presence, applicationID, port1);
                 webClient.Headers.Add("content-type", "application/json");
                 if (WebRPC.TryParseResponse(webClient.UploadString(webRequest.URL, webRequest.Data), out response))
                 {
                     return(true);
                 }
             }
             catch (Exception ex)
             {
             }
         }
     }
     response = (RichPresence)null;
     return(false);
 }