LennyPhoenix / GodotDiscordSDK

A Discord Game SDK wrapper for Godot, written in C.
MIT License
54 stars 5 forks source link

Error while trying to display User Profile Picture #4

Closed JezSonic closed 3 years ago

JezSonic commented 3 years ago

Describe the bug

When running test project, Godot can't load my profile picture, cause user image given by SDK have different size than Godot needs and it threws error "got x bytes of data instead of excepted y"

To Reproduce

Just run test project

Expected behaviour

TextureRect displays User Image properly

Screenshots

image

Additional context

Changing image format and multipling dimensions to make image bytes match number of bytes that Godot is waiting for doesn't fix this. There's no error, byt image is not displayed

Please confirm the following: (Place an X between the square brackets)

LennyPhoenix commented 3 years ago

Could you try adding these lines of code to the fetch_callback:

print(dimensions.height, ":", dimensions.width)
print(handle.size)

like so: image

The likely issue is that the sdk is returning a square version of the image, but get_dimensions returns the original image dimensions.

JezSonic commented 3 years ago

Could you try adding these lines of code to the fetch_callback:

print(dimensions.height, ":", dimensions.width)
print(handle.size)

like so: image

The likely issue is that the sdk is returning a square version of the image, but get_dimensions returns the original image dimensions.

sure, let me finish lessons and i'll do it

JezSonic commented 3 years ago

image

You have everything there, i also have updated issue

LennyPhoenix commented 3 years ago

Ok, so it looks like the issue is that ImageDimensions is the original image dimensions, but get_data will return the resized image bytes to fit the size we passed to the ImageHandle, so all we have to do is replace the fetch callback with this:

func fetch_callback(result: int, handle: Discord.ImageHandle) -> void:
    if result != Discord.Result.OK:
        print(
            "Failed to fetch image handle: ",
            enum_to_string(Discord.Result, result)
        )
    else:
        print("Fetched image handle, ", handle.id, ", ", handle.size)

        var data = images.get_data(handle)
        if data is int:
            print(
                "Failed to get image data: ",
                enum_to_string(Discord.Result, data)
            )
        else:
            var image: = Image.new()
            image.create_from_data(
                handle.size, handle.size,
                false,
                Image.FORMAT_RGBA8,
                data
            )
            image.unlock()
            var tex: = ImageTexture.new()
            tex.create_from_image(image)
            texture_rect.texture = tex
            OS.window_size = Vector2(handle.size, handle.size)
LennyPhoenix commented 3 years ago

Let me know if it still doesnt work after replacing this function ^

I'm going to add something to the docs about this to prevent issues in future. Done: image

JezSonic commented 3 years ago

:/ image

LennyPhoenix commented 3 years ago

Could you try saving the resulting image to a png?

image.save_png("res://avatar.png")

image

JezSonic commented 3 years ago

avatar there you have the result

LennyPhoenix commented 3 years ago

Could you please send you whole main.gd file? I can't seem to replicate this issue myself, but I'll try using an alt account with smaller pfp later.

LennyPhoenix commented 3 years ago

OK, I think I've figure out the issue, so I'm going to implement a fix now, let me know if it still doesn't work.

JezSonic commented 3 years ago

Ok, thanks

JezSonic commented 3 years ago

Could you please send you whole main.gd file? I can't seem to replicate this issue myself, but I'll try using an alt account with smaller pfp later.

I can't do it now... I'll do it as fast as possible.

JezSonic commented 3 years ago

Btw. If you prefer we can talk on discord, you have my nick and tag on the screen (Futrzak#7813)