foreigner icon indicating copy to clipboard operation
foreigner copied to clipboard

Make use of existing Godot dynamic library loading functionality?

Open follower opened this issue 5 years ago • 0 comments

Re: src/crossplatform.cpp

Existing Godot dynamic library loading functionality

At some point I discovered that Godot already has abstracted/cross-platform dynamic library loading & symbol lookup functionality (including special handling for iOS exports), declared virtual here:

  • https://github.com/godotengine/godot/blob/416cd715a3f51b7d74676fa7d4c8e34c3c2823c9/core/os/os.h#L258-L260

And implemented per platform, e.g.:

  • https://github.com/godotengine/godot/blob/416cd715a3f51b7d74676fa7d4c8e34c3c2823c9/drivers/unix/os_unix.cpp#L403-L448

  • https://github.com/godotengine/godot/blob/416cd715a3f51b7d74676fa7d4c8e34c3c2823c9/platform/windows/os_windows.cpp#L2246-L2295

  • https://github.com/godotengine/godot/blob/416cd715a3f51b7d74676fa7d4c8e34c3c2823c9/platform/osx/os_osx.mm#L1789-L1806

Related commits: here & here.

No access via GDNative

Unfortunately this functionality isn't exposed via GDNative--it's unclear if this is intentional or merely a result of the methods being virtual.

Rather than re-implement the wheel, it seems like it would be good to make use of the existing functionality, by e.g. doing one of:

  • Copy/paste the relevant code into Foreigner (assuming license compatibility). :D

  • Request open_dynamic_library(), close_dynamic_library() & get_dynamic_library_symbol_handle() are exposed to GDNative.

  • Turn Foreigner into a module so we can use OS::get_singleton()->open_dynamic_library(path, native_handle, true) (I assume).

  • Get Foreigner included in the core engine. :)

  • Get GDNative modified so there's a way to get access to a library but without requiring the library to implement any "standard" methods.

  • Figure out some way to bypass the existing "validity" checks in GDNative. :)

    • Preload a shim to implement/dummy/mock required "standard" methods?

    The code already admits it cheats anyway: :)

    • https://github.com/godotengine/godot/blob/620030b600b375545ecab5190615e08919e56da4/modules/gdnative/gdnative.cpp#L325-L344

    So if we could bypass that check it'd be golden...

Related: call_native()

Semi-related would be that potentially we might also then piggyback on top of the existing GDNative::call_native() functionality:

  • https://github.com/godotengine/godot/blob/620030b600b375545ecab5190615e08919e56da4/modules/gdnative/gdnative.cpp#L454-L478

Conclusion

Primarily this issue is to point out "hey this exists" but might also show potential approaches for integration of FFI functionality into Godot more directly.

Edits: Added commit links. Add src/crossplatform.cpp link.

follower avatar Mar 04 '20 05:03 follower