-
Notifications
You must be signed in to change notification settings - Fork 4
NativeAPI: Expose TypeTreeGenerator_getMonoBehaviorDefinitions
#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -397,4 +397,9 @@ FodyWeavers.xsd | |
| *.msp | ||
|
|
||
| # JetBrains Rider | ||
| *.sln.iml | ||
| *.sln.iml | ||
|
|
||
| # Built binaries | ||
| *.dll | ||
| *.so | ||
| *.dylib | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import os | ||
| import platform | ||
| import ctypes | ||
| from typing import List | ||
| from typing import List, Tuple | ||
|
|
||
|
|
||
| class TypeTreeNodeNative(ctypes.Structure): | ||
|
|
@@ -78,6 +78,11 @@ def init_dll(): | |
| ctypes.POINTER(ctypes.POINTER(TypeTreeNodeNative)), | ||
| ctypes.POINTER(ctypes.c_int), | ||
| ] | ||
| dll.TypeTreeGenerator_getMonoBehaviorDefinitions.argtypes = [ | ||
| ctypes.c_void_p, | ||
| ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)), | ||
| ctypes.POINTER(ctypes.c_int), | ||
| ] | ||
| dll.TypeTreeGenerator_del.argtypes = [ctypes.c_void_p] | ||
| dll.FreeCoTaskMem.argtypes = [ctypes.c_void_p] | ||
| DLL = dll # type: ignore | ||
|
|
@@ -146,5 +151,20 @@ def get_nodes(self, assembly: str, fullname: str) -> List[TypeTreeNode]: | |
| DLL.FreeCoTaskMem(nodes_ptr) | ||
| return nodes | ||
|
|
||
| def get_monobehavior_definitions(self) -> List[Tuple[str, str]]: | ||
| names_ptr = ctypes.POINTER(ctypes.c_char_p)() | ||
| names_cnt = ctypes.c_int() | ||
| assert not DLL.TypeTreeGenerator_getMonoBehaviorDefinitions( | ||
| self.ptr, | ||
| ctypes.byref(names_ptr), | ||
| ctypes.byref(names_cnt), | ||
| ), "failed to get module exports" | ||
| names_array = ctypes.cast( | ||
| names_ptr, ctypes.POINTER(ctypes.c_char_p * names_cnt.value) | ||
| ).contents | ||
| names = [name.decode("ascii") for name in names_array] | ||
| DLL.FreeCoTaskMem(names_ptr) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would leak memory, as the array pointers to the names wouldn't be freed.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which reminds me, that I still have to double check if the raw typetree struct leaks or not.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Unfortunately it does according to the docs :(
Back to this one - when I was trying to free the allocated strings with the following snippet Python seem to crash after a few frees. Any ideas why this is happening? for ptr in ptr_array:
ptr = ctypes.cast(ptr, ctypes.c_void_p).value
DLL.FreeCoTaskMem(ptr)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue is that the iterator doesn't work as intended. So I'm simply going to include free function in the NativeAPI as well, which would also make usage in other languages next to python easier and less hacky. |
||
| return [(module, fullname) for module, fullname in zip(names[::2], names[1::2])] | ||
|
|
||
|
|
||
| __all__ = ("TypeTreeGenerator", "TypeTreeNode", "TypeTreeNodeNative") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might result in errors if the names include non ansi ascii compatible characters.
StringToCoTaskMemUTF8( would be better.
I should use this for the json code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the encoding system locally just now, so that the system's default encoding gets used to prevent possible issues.