[solved] Problem with CTypes and Unicode characters

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
eZombo
Regular
Posts: 29
Joined: Thu Mar 09, 2017 2:45 pm
Soundcloud: eerron
itch: ezombo
Contact:

[solved] Problem with CTypes and Unicode characters

#1 Post by eZombo »

A Ren'Py game I'm working on has been released on Kartridge, including integration of their Badges (essentially achievements on their platform). This required loading a library to get access to their Badge system, which I have done via CTypes and so far this has worked out fine.

However, we recently received a bug report from a player from Russia that they can't launch the game. The problem seems to be that CTypes can't load the .dll when there are Cyrillic characters in the file path. On my own PC I haven't been able to recreate the exact same issue (they get a traceback with the error "WindowsError: exception: access violation reading 0x00000000" on the line of the first function call from the .dll while I just get a pop-up that it couldn't open the file at the file path, with all Cyrillic characters replaced with question marks) but at least I could confirm that the game doesn't launch if it's installed in a directory with non-English characters.

I have tried various ways to make sure that the variable for the file path that is used to load the .dll is in Unicode, but I still haven't been able to make it work so far.

Here is the code that is used to load the library:

Code: Select all

init python:
	import ctypes
	import os
	path = os.path.abspath(os.path.join(config.basedir, "game\kartridge\kartridge-sdk-x86.dll"))
        _kartridge = ctypes.CDLL(path)
Because I can't predict where the game will be installed, I've used config.basedir followed by the location of the .dll within the game folder.
After this, _kartridge is used whenever the game needs to call a function related to the Badges, but because it works fine as long as the file path uses English characters, the issue probably lies in the code above.

At this point I'm not sure if there's a problem with what I'm doing or if this is a CTypes issue, so any help would be appreciated!
Last edited by eZombo on Sat Oct 24, 2020 1:54 pm, edited 1 time in total.

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Problem with CTypes and Unicode characters

#2 Post by Vladya »

First of all, you don't escaped '\' characters .

Second, you can use the more convenient 'renpy.loader.transfn' that accepts the path relative to the 'game' folder.

With this in mind, I used the code below and it works for me.
("bass api" was in the vicinity. It is important for us to check the fact of loading, not the specific library).

Code: Select all

init python:

    import ctypes
    from os import path

    path_to_dynamic_lib = path.abspath(
        renpy.loader.transfn("кириллические символы в пути/bass.dll")
    )
    bassAPI = ctypes.CDLL(path_to_dynamic_lib)

label start:

    $ errCode = bassAPI.BASS_ErrorGetCode()
    "Error code is [errCode]."  # Testing for a random function.
    return


User avatar
eZombo
Regular
Posts: 29
Joined: Thu Mar 09, 2017 2:45 pm
Soundcloud: eerron
itch: ezombo
Contact:

Re: Problem with CTypes and Unicode characters

#3 Post by eZombo »

Thanks for the reply!
I tried out your solution, but somehow it still can't find the file on my end if there are non-English characters in the file path (it works otherwise).

Could this maybe be an issue with the system language or encoding?

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Problem with CTypes and Unicode characters

#4 Post by Vladya »

System language - I doubt it.
Encoding - maybe...

What Ren'Py version do you use? (If the version is older than 6.99.12, then the string type there is 'str' by default).

User avatar
eZombo
Regular
Posts: 29
Joined: Thu Mar 09, 2017 2:45 pm
Soundcloud: eerron
itch: ezombo
Contact:

Re: Problem with CTypes and Unicode characters

#5 Post by eZombo »

I'm using 7.3.5.606

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Problem with CTypes and Unicode characters

#6 Post by Vladya »

Let's try to read dll using the "open" to definitively eliminate the possibility of an error from the "ctypes" side. And based on this, we will determine where to look for the problem further.

Code: Select all

init python:

    from os import path

    path_to_dynamic_lib = path.abspath(
        renpy.loader.transfn("кириллические символы в пути/mydll.dll")
    )

    with open(path_to_dynamic_lib, "rb") as _mydll:
        while True:
            if not _mydll.read((2**20)):
                break

User avatar
eZombo
Regular
Posts: 29
Joined: Thu Mar 09, 2017 2:45 pm
Soundcloud: eerron
itch: ezombo
Contact:

Re: Problem with CTypes and Unicode characters

#7 Post by eZombo »

That seems to stop the error, thanks! The game launches without problem now.
Is this an alternative way to access the dll or was this just to find out if it's a problem with ctypes?

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Problem with CTypes and Unicode characters

#8 Post by Vladya »

No. This does not solve the problem, but we now know for sure that the problem is not the lack of access to the file.

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Problem with CTypes and Unicode characters

#9 Post by Vladya »

Can I take a look at this dll?

User avatar
eZombo
Regular
Posts: 29
Joined: Thu Mar 09, 2017 2:45 pm
Soundcloud: eerron
itch: ezombo
Contact:

Re: Problem with CTypes and Unicode characters

#10 Post by eZombo »

Ah yes, I'm aware that it didn't solve the problem, I was just glad that the game launched at all.

You can find the dll here in the Kartridge SDK https://github.com/kongregate/kartridge ... s/releases
While looking up this link I also saw that they released a newer version than the one I'm using that apparently fixes an issue with Unicode characters in usernames (which was the problem the player who sent in the report had), so the issue might have been actually this?

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Problem with CTypes and Unicode characters

#11 Post by Vladya »

I suppose it is so. In the last update it is written that they fixed the conflict with Cyrillic in the username. Try to download the latest version.
[#166596895]: Fix crash caused by unicode characters in Windows usernames

User avatar
eZombo
Regular
Posts: 29
Joined: Thu Mar 09, 2017 2:45 pm
Soundcloud: eerron
itch: ezombo
Contact:

Re: Problem with CTypes and Unicode characters

#12 Post by eZombo »

Yeah, that's what I'm doing right now. Hopefully it will solve the problem. Thanks for the help!

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot]