Exemplo n.º 1
0
    /// <summary>
    /// Gets the user avatar as a Texture2D and starts it with the supplied monobehaviour. It will first check the cache if the image exists, if it does it will return the image. Otherwise it will download the image from Discord and store it in the cache, calling the callback once done.
    /// <para>An alias of <see cref="DiscordUser.CacheAvatar(MonoBehaviour, DiscordAvatarSize, AvatarDownloadCallback)"/> and will return the new <see cref="DiscordUser"/> instance.</para>
    /// </summary>
    /// <param name="coroutineCaller">The target object that will start the coroutine</param>
    /// <param name="size">The target size of the avatar. Default is 128x128</param>
    /// <param name="callback">The callback for when the texture completes. Default is no-callback, but its highly recommended to use a callback</param>
    /// <returns>Returns the generated <see cref="DiscordUser"/> for this <see cref="DiscordRPC.User"/> object.</returns>
    public static DiscordUser GetAvatar(this DiscordRPC.User user, MonoBehaviour coroutineCaller, DiscordAvatarSize size = DiscordAvatarSize.x128, DiscordUser.AvatarDownloadCallback callback = null)
    {
        var du = new DiscordUser(user);

        du.GetAvatar(coroutineCaller, size, callback);
        return(du);
    }
Exemplo n.º 2
0
 public DiscordUser(DiscordRPC.User user)
 {
     _username      = user.Username;
     _discriminator = user.Discriminator;
     _snowflake     = user.ID;
     _avatarHash    = user.Avatar;
     _cdnEndpoint   = user.CdnEndpoint;
 }
Exemplo n.º 3
0
    private static IEnumerator EnumerateAvatarTexture(this DiscordRPC.User user, DiscordRPC.User.AvatarSize size = DiscordRPC.User.AvatarSize.x128, TextureFormat format = TextureFormat.RGBA32, bool isJPEG = false, AvatarDownloadCallback callback = null)
    {
        if (string.IsNullOrEmpty(user.Avatar))
        {
            yield return(GetDefaultAvatarTexture(user, size, callback));
        }
        else
        {
            //Generate the path name
            string filename = string.Format("{0}-{1}{2}.{3}", user.Discriminator, user.Avatar, size.ToString(), isJPEG ? "jpeg" : "png");
            string path     = Path.Combine(_cache, filename);

            //The holder texture is null, so we should create new one
            Texture2D holder = new Texture2D((int)size, (int)size, format, false);

            //Check if the file exists
            if (File.Exists(path))
            {
                //Load the image
                var bytes = File.ReadAllBytes(path);
                holder.LoadImage(bytes);
            }
            else
            {
                using (WWW www = new WWW(user.GetAvatarURL(isJPEG ? DiscordRPC.User.AvatarFormat.JPEG : DiscordRPC.User.AvatarFormat.PNG, size)))
                {
                    //Download the texture
                    yield return(www);

                    //Update the holder
                    www.LoadImageIntoTexture(holder);

                    //Save the texture, making a cache for later
                    if (!Directory.Exists(_cache))
                    {
                        Directory.CreateDirectory(_cache);
                    }
                    var bytes = isJPEG ? holder.EncodeToJPG(JpegQuality) : holder.EncodeToPNG();
                    File.WriteAllBytes(path, bytes);
                }
            }

            //Execute the callback (if any)
            if (callback != null)
            {
                callback.Invoke(user, holder);
            }
        }
    }
Exemplo n.º 4
0
    private static IEnumerator GetDefaultAvatarTexture(this DiscordRPC.User user, DiscordRPC.User.AvatarSize size = DiscordRPC.User.AvatarSize.x128, AvatarDownloadCallback callback = null)
    {
        int discrim = user.Discriminator % 5;

        string filename = string.Format("default-{0}{1}.png", discrim, size.ToString());
        string path     = Path.Combine(_cache, filename);

        Texture2D texture;

        //Check if the file exists
        if (File.Exists(path))
        {
            //Load the image
            var bytes = File.ReadAllBytes(path);
            texture = new Texture2D((int)size, (int)size, TextureFormat.RGBA32, false);
            texture.LoadImage(bytes);
        }
        else
        {
            string url = string.Format("https://{0}/embed/avatars/{1}.png?size={2}", user.CdnEndpoint, discrim, size);
            using (WWW www = new WWW(url))
            {
                //Download the texture
                yield return(www);

                //Set the texture
                texture = www.texture;

                //Save the texture, making a cache for later
                if (!Directory.Exists(_cache))
                {
                    Directory.CreateDirectory(_cache);
                }
                var bytes = texture.EncodeToPNG();
                File.WriteAllBytes(path, bytes);
            }
        }

        //Execute the callback (if any)
        if (callback != null)
        {
            callback.Invoke(user, texture);
        }
    }
Exemplo n.º 5
0
    /// <summary>
    /// Gets the default avatar for the given user. Will check the cache first, and if none are available it will then download the default from discord.
    /// <para>An alias of <see cref="DiscordUser.CacheDefaultAvatarCoroutine(DiscordAvatarSize, DiscordUser.AvatarDownloadCallback)"/> and will return the new <see cref="DiscordUser"/> instance in the callback.</para>
    /// </summary>
    /// <param name="size">The size of the target avatar</param>
    /// <param name="callback">The callback that will be made when the picture finishes downloading.</param>
    /// <returns></returns>
    public static IEnumerator GetDefaultAvatarCoroutine(this DiscordRPC.User user, DiscordAvatarSize size = DiscordAvatarSize.x128, DiscordUser.AvatarDownloadCallback callback = null)
    {
        var du = new DiscordUser(user);

        return(du.GetDefaultAvatarCoroutine(size, callback));
    }
Exemplo n.º 6
0
 private static void GetAvatarTexture(this DiscordRPC.User user, MonoBehaviour reference, DiscordRPC.User.AvatarSize size = DiscordRPC.User.AvatarSize.x128, TextureFormat format = TextureFormat.RGBA32, bool isJPEG = false, AvatarDownloadCallback callback = null)
 {
     reference.StartCoroutine(EnumerateAvatarTexture(user, size, format, isJPEG, callback));
 }