2016-06-26 16:02:15 +02:00
/*************************************************************************/
/* register_types.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2016-06-26 16:02:15 +02:00
/*************************************************************************/
2019-01-01 12:53:14 +01:00
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2016-06-26 16:02:15 +02:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2016-06-26 16:02:15 +02:00
# include "register_types.h"
2018-09-27 13:11:41 +02:00
2017-09-03 12:40:41 +02:00
# include "gdnative/gdnative.h"
2017-04-09 21:07:53 +02:00
# include "gdnative.h"
2016-06-26 16:02:15 +02:00
2017-11-10 12:36:50 +01:00
# include "arvr/register_types.h"
2017-09-03 12:40:41 +02:00
# include "nativescript/register_types.h"
2018-06-26 20:26:51 +02:00
# include "net/register_types.h"
2017-10-08 23:47:38 +02:00
# include "pluginscript/register_types.h"
2018-07-28 22:01:30 +02:00
# include "videodecoder/register_types.h"
2017-09-03 12:40:41 +02:00
2017-09-03 14:50:33 +02:00
# include "core/engine.h"
2018-09-27 13:11:41 +02:00
# include "core/io/resource_loader.h"
# include "core/io/resource_saver.h"
2017-07-14 01:44:14 +02:00
# include "core/os/os.h"
2017-09-03 14:50:33 +02:00
# include "core/project_settings.h"
# ifdef TOOLS_ENABLED
# include "editor/editor_node.h"
2017-12-15 15:35:16 +01:00
# include "gdnative_library_editor_plugin.h"
# include "gdnative_library_singleton_editor.h"
2017-09-03 14:50:33 +02:00
// Class used to discover singleton gdnative files
2017-11-15 17:24:32 +01:00
static void actual_discoverer_handler ( ) ;
2017-09-03 14:50:33 +02:00
class GDNativeSingletonDiscover : public Object {
// GDCLASS(GDNativeSingletonDiscover, Object)
virtual String get_class ( ) const {
// okay, this is a really dirty hack.
// We're overriding get_class so we can connect it to a signal
// This works because get_class is a virtual method, so we don't
// need to register a new class to ClassDB just for this one
// little signal.
actual_discoverer_handler ( ) ;
return " Object " ;
}
} ;
2017-11-15 17:24:32 +01:00
static Set < String > get_gdnative_singletons ( EditorFileSystemDirectory * p_dir ) {
2017-09-03 14:50:33 +02:00
Set < String > file_paths ;
// check children
for ( int i = 0 ; i < p_dir - > get_file_count ( ) ; i + + ) {
String file_name = p_dir - > get_file ( i ) ;
String file_type = p_dir - > get_file_type ( i ) ;
if ( file_type ! = " GDNativeLibrary " ) {
continue ;
}
Ref < GDNativeLibrary > lib = ResourceLoader : : load ( p_dir - > get_file_path ( i ) ) ;
2017-11-02 17:14:37 +01:00
if ( lib . is_valid ( ) & & lib - > is_singleton ( ) ) {
2017-09-03 14:50:33 +02:00
file_paths . insert ( p_dir - > get_file_path ( i ) ) ;
}
}
// check subdirectories
for ( int i = 0 ; i < p_dir - > get_subdir_count ( ) ; i + + ) {
Set < String > paths = get_gdnative_singletons ( p_dir - > get_subdir ( i ) ) ;
for ( Set < String > : : Element * E = paths . front ( ) ; E ; E = E - > next ( ) ) {
file_paths . insert ( E - > get ( ) ) ;
}
}
return file_paths ;
}
2017-11-15 17:24:32 +01:00
static void actual_discoverer_handler ( ) {
2017-12-09 23:32:40 +01:00
2017-09-03 14:50:33 +02:00
EditorFileSystemDirectory * dir = EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) ;
Set < String > file_paths = get_gdnative_singletons ( dir ) ;
2017-12-05 03:21:04 +01:00
bool changed = false ;
2017-12-09 23:32:40 +01:00
Array current_files ;
2017-12-09 23:36:41 +01:00
if ( ProjectSettings : : get_singleton ( ) - > has_setting ( " gdnative/singletons " ) ) {
2017-12-09 23:32:40 +01:00
current_files = ProjectSettings : : get_singleton ( ) - > get ( " gdnative/singletons " ) ;
}
2017-09-03 14:50:33 +02:00
Array files ;
files . resize ( file_paths . size ( ) ) ;
int i = 0 ;
for ( Set < String > : : Element * E = file_paths . front ( ) ; E ; i + + , E = E - > next ( ) ) {
2017-12-05 03:21:04 +01:00
if ( ! current_files . has ( E - > get ( ) ) ) {
changed = true ;
}
2017-09-03 14:50:33 +02:00
files . set ( i , E - > get ( ) ) ;
}
2017-12-05 03:21:04 +01:00
// Check for removed files
if ( ! changed ) {
2019-02-12 21:10:08 +01:00
for ( int j = 0 ; j < current_files . size ( ) ; j + + ) {
if ( ! file_paths . has ( current_files [ j ] ) ) {
2017-12-05 03:21:04 +01:00
changed = true ;
break ;
}
}
}
if ( changed ) {
ProjectSettings : : get_singleton ( ) - > set ( " gdnative/singletons " , files ) ;
ProjectSettings : : get_singleton ( ) - > save ( ) ;
}
2017-09-03 14:50:33 +02:00
}
2017-11-15 17:24:32 +01:00
static GDNativeSingletonDiscover * discoverer = NULL ;
class GDNativeExportPlugin : public EditorExportPlugin {
protected :
virtual void _export_file ( const String & p_path , const String & p_type , const Set < String > & p_features ) ;
} ;
2017-10-02 17:01:43 +02:00
struct LibrarySymbol {
2018-09-27 13:11:41 +02:00
const char * name ;
2017-10-02 17:01:43 +02:00
bool is_required ;
} ;
2017-11-15 17:24:32 +01:00
void GDNativeExportPlugin : : _export_file ( const String & p_path , const String & p_type , const Set < String > & p_features ) {
if ( p_type ! = " GDNativeLibrary " ) {
return ;
}
Ref < GDNativeLibrary > lib = ResourceLoader : : load ( p_path ) ;
if ( lib . is_null ( ) ) {
return ;
}
Ref < ConfigFile > config = lib - > get_config_file ( ) ;
{
List < String > entry_keys ;
config - > get_section_keys ( " entry " , & entry_keys ) ;
for ( List < String > : : Element * E = entry_keys . front ( ) ; E ; E = E - > next ( ) ) {
String key = E - > get ( ) ;
Vector < String > tags = key . split ( " . " ) ;
bool skip = false ;
for ( int i = 0 ; i < tags . size ( ) ; i + + ) {
bool has_feature = p_features . has ( tags [ i ] ) ;
if ( ! has_feature ) {
skip = true ;
break ;
}
}
if ( skip ) {
continue ;
}
2017-10-02 17:01:43 +02:00
String entry_lib_path = config - > get_value ( " entry " , key ) ;
add_shared_object ( entry_lib_path , tags ) ;
2017-11-15 17:24:32 +01:00
}
}
{
List < String > dependency_keys ;
config - > get_section_keys ( " dependencies " , & dependency_keys ) ;
for ( List < String > : : Element * E = dependency_keys . front ( ) ; E ; E = E - > next ( ) ) {
String key = E - > get ( ) ;
Vector < String > tags = key . split ( " . " ) ;
bool skip = false ;
for ( int i = 0 ; i < tags . size ( ) ; i + + ) {
bool has_feature = p_features . has ( tags [ i ] ) ;
if ( ! has_feature ) {
skip = true ;
break ;
}
}
if ( skip ) {
continue ;
}
2017-10-02 17:01:43 +02:00
Vector < String > dependency_paths = config - > get_value ( " dependencies " , key ) ;
for ( int i = 0 ; i < dependency_paths . size ( ) ; i + + ) {
add_shared_object ( dependency_paths [ i ] , tags ) ;
}
2017-11-15 17:24:32 +01:00
}
}
2017-10-02 17:01:43 +02:00
if ( p_features . has ( " iOS " ) ) {
// Register symbols in the "fake" dynamic lookup table, because dlsym does not work well on iOS.
LibrarySymbol expected_symbols [ ] = {
{ " gdnative_init " , true } ,
{ " gdnative_terminate " , false } ,
{ " nativescript_init " , false } ,
{ " nativescript_frame " , false } ,
{ " nativescript_thread_enter " , false } ,
{ " nativescript_thread_exit " , false } ,
{ " gdnative_singleton " , false }
} ;
String declare_pattern = " extern \" C \" void $name(void)$weak; \n " ;
String additional_code = " extern void register_dynamic_symbol(char *name, void *address); \n "
" extern void add_ios_init_callback(void (*cb)()); \n " ;
String linker_flags = " " ;
2018-09-26 17:38:02 +02:00
for ( unsigned int i = 0 ; i < sizeof ( expected_symbols ) / sizeof ( expected_symbols [ 0 ] ) ; + + i ) {
2017-10-02 17:01:43 +02:00
String full_name = lib - > get_symbol_prefix ( ) + expected_symbols [ i ] . name ;
String code = declare_pattern . replace ( " $name " , full_name ) ;
code = code . replace ( " $weak " , expected_symbols [ i ] . is_required ? " " : " __attribute__((weak)) " ) ;
additional_code + = code ;
if ( ! expected_symbols [ i ] . is_required ) {
if ( linker_flags . length ( ) > 0 ) {
linker_flags + = " " ;
2017-11-15 17:24:32 +01:00
}
2017-10-02 17:01:43 +02:00
linker_flags + = " -Wl,-U,_ " + full_name ;
2017-11-15 17:24:32 +01:00
}
}
2017-10-02 17:01:43 +02:00
additional_code + = String ( " void $prefixinit() { \n " ) . replace ( " $prefix " , lib - > get_symbol_prefix ( ) ) ;
String register_pattern = " if (&$name) register_dynamic_symbol((char *) \" $name \" , (void *)$name); \n " ;
2018-09-26 17:38:02 +02:00
for ( unsigned int i = 0 ; i < sizeof ( expected_symbols ) / sizeof ( expected_symbols [ 0 ] ) ; + + i ) {
2017-10-02 17:01:43 +02:00
String full_name = lib - > get_symbol_prefix ( ) + expected_symbols [ i ] . name ;
additional_code + = register_pattern . replace ( " $name " , full_name ) ;
}
additional_code + = " } \n " ;
additional_code + = String ( " struct $prefixstruct {$prefixstruct() {add_ios_init_callback($prefixinit);}}; \n " ) . replace ( " $prefix " , lib - > get_symbol_prefix ( ) ) ;
additional_code + = String ( " $prefixstruct $prefixstruct_instance; \n " ) . replace ( " $prefix " , lib - > get_symbol_prefix ( ) ) ;
2017-11-15 17:24:32 +01:00
2017-10-02 17:01:43 +02:00
add_ios_cpp_code ( additional_code ) ;
add_ios_linker_flags ( linker_flags ) ;
2017-11-15 17:24:32 +01:00
}
}
2017-09-03 14:50:33 +02:00
2017-09-14 20:01:11 +02:00
static void editor_init_callback ( ) {
2017-12-15 15:35:16 +01:00
GDNativeLibrarySingletonEditor * library_editor = memnew ( GDNativeLibrarySingletonEditor ) ;
2017-09-14 20:01:11 +02:00
library_editor - > set_name ( TTR ( " GDNative " ) ) ;
ProjectSettingsEditor : : get_singleton ( ) - > get_tabs ( ) - > add_child ( library_editor ) ;
2017-09-03 14:50:33 +02:00
discoverer = memnew ( GDNativeSingletonDiscover ) ;
EditorFileSystem : : get_singleton ( ) - > connect ( " filesystem_changed " , discoverer , " get_class " ) ;
2017-11-15 17:24:32 +01:00
Ref < GDNativeExportPlugin > export_plugin ;
export_plugin . instance ( ) ;
EditorExport : : get_singleton ( ) - > add_export_plugin ( export_plugin ) ;
2017-12-15 15:35:16 +01:00
EditorNode : : get_singleton ( ) - > add_editor_plugin ( memnew ( GDNativeLibraryEditorPlugin ( EditorNode : : get_singleton ( ) ) ) ) ;
2017-09-03 14:50:33 +02:00
}
# endif
2017-04-03 16:11:38 +02:00
2017-11-15 17:24:32 +01:00
static godot_variant cb_standard_varcall ( void * p_procedure_handle , godot_array * p_args ) {
2017-07-14 01:44:14 +02:00
godot_gdnative_procedure_fn proc ;
2017-10-14 15:42:10 +02:00
proc = ( godot_gdnative_procedure_fn ) p_procedure_handle ;
2017-09-03 14:50:33 +02:00
2017-10-14 15:42:10 +02:00
return proc ( p_args ) ;
2017-09-03 14:50:33 +02:00
}
2017-07-14 01:44:14 +02:00
GDNativeCallRegistry * GDNativeCallRegistry : : singleton ;
2017-04-03 16:11:38 +02:00
2017-09-03 14:50:33 +02:00
Vector < Ref < GDNative > > singleton_gdnatives ;
2018-06-11 02:59:53 +02:00
Ref < GDNativeLibraryResourceLoader > resource_loader_gdnlib ;
Ref < GDNativeLibraryResourceSaver > resource_saver_gdnlib ;
2017-11-02 17:14:37 +01:00
2017-07-14 01:44:14 +02:00
void register_gdnative_types ( ) {
2017-04-03 16:11:38 +02:00
2017-09-03 14:50:33 +02:00
# ifdef TOOLS_ENABLED
2017-10-02 17:01:43 +02:00
EditorNode : : add_init_callback ( editor_init_callback ) ;
2017-09-03 14:50:33 +02:00
# endif
2017-07-14 01:44:14 +02:00
ClassDB : : register_class < GDNativeLibrary > ( ) ;
ClassDB : : register_class < GDNative > ( ) ;
2018-06-11 02:59:53 +02:00
resource_loader_gdnlib . instance ( ) ;
2017-11-02 17:14:37 +01:00
ResourceLoader : : add_resource_format_loader ( resource_loader_gdnlib ) ;
2019-01-10 12:45:57 +01:00
resource_saver_gdnlib . instance ( ) ;
2017-11-02 17:14:37 +01:00
ResourceSaver : : add_resource_format_saver ( resource_saver_gdnlib ) ;
2017-07-14 01:44:14 +02:00
GDNativeCallRegistry : : singleton = memnew ( GDNativeCallRegistry ) ;
2017-04-03 16:11:38 +02:00
2017-07-14 01:44:14 +02:00
GDNativeCallRegistry : : singleton - > register_native_call_type ( " standard_varcall " , cb_standard_varcall ) ;
2017-09-03 12:40:41 +02:00
2018-06-26 20:26:51 +02:00
register_net_types ( ) ;
2017-11-10 12:36:50 +01:00
register_arvr_types ( ) ;
2017-09-03 12:40:41 +02:00
register_nativescript_types ( ) ;
2017-10-08 23:47:38 +02:00
register_pluginscript_types ( ) ;
2018-07-28 22:01:30 +02:00
register_videodecoder_types ( ) ;
2017-09-03 14:50:33 +02:00
// run singletons
2017-10-21 19:31:23 +02:00
Array singletons = Array ( ) ;
if ( ProjectSettings : : get_singleton ( ) - > has_setting ( " gdnative/singletons " ) ) {
singletons = ProjectSettings : : get_singleton ( ) - > get ( " gdnative/singletons " ) ;
}
2017-09-03 14:50:33 +02:00
singleton_gdnatives . resize ( singletons . size ( ) ) ;
for ( int i = 0 ; i < singletons . size ( ) ; i + + ) {
String path = singletons [ i ] ;
Ref < GDNativeLibrary > lib = ResourceLoader : : load ( path ) ;
2018-07-25 03:11:03 +02:00
singleton_gdnatives . write [ i ] . instance ( ) ;
singleton_gdnatives . write [ i ] - > set_library ( lib ) ;
2017-09-03 14:50:33 +02:00
2018-07-25 03:11:03 +02:00
if ( ! singleton_gdnatives . write [ i ] - > initialize ( ) ) {
2017-09-03 14:50:33 +02:00
// Can't initialize. Don't make a native_call then
continue ;
}
2017-10-14 15:42:10 +02:00
void * proc_ptr ;
Error err = singleton_gdnatives [ i ] - > get_symbol (
2017-11-02 17:14:37 +01:00
lib - > get_symbol_prefix ( ) + " gdnative_singleton " ,
2017-10-14 15:42:10 +02:00
proc_ptr ) ;
if ( err ! = OK ) {
2017-11-02 17:14:37 +01:00
ERR_PRINT ( ( String ( " No godot_gdnative_singleton in \" " + singleton_gdnatives [ i ] - > get_library ( ) - > get_current_library_path ( ) ) + " \" found " ) . utf8 ( ) . get_data ( ) ) ;
2017-10-14 15:42:10 +02:00
} else {
( ( void ( * ) ( ) ) proc_ptr ) ( ) ;
}
2017-09-03 14:50:33 +02:00
}
2017-07-14 01:44:14 +02:00
}
2017-04-03 16:11:38 +02:00
2017-07-14 01:44:14 +02:00
void unregister_gdnative_types ( ) {
2017-09-03 12:40:41 +02:00
2017-09-03 14:50:33 +02:00
for ( int i = 0 ; i < singleton_gdnatives . size ( ) ; i + + ) {
if ( singleton_gdnatives [ i ] . is_null ( ) ) {
continue ;
}
if ( ! singleton_gdnatives [ i ] - > is_initialized ( ) ) {
continue ;
}
2018-07-25 03:11:03 +02:00
singleton_gdnatives . write [ i ] - > terminate ( ) ;
2017-09-03 14:50:33 +02:00
}
2017-10-09 00:31:25 +02:00
singleton_gdnatives . clear ( ) ;
2017-09-03 14:50:33 +02:00
2018-07-28 22:01:30 +02:00
unregister_videodecoder_types ( ) ;
2017-10-08 23:47:38 +02:00
unregister_pluginscript_types ( ) ;
2017-09-03 12:40:41 +02:00
unregister_nativescript_types ( ) ;
2017-11-10 12:36:50 +01:00
unregister_arvr_types ( ) ;
2018-06-26 20:26:51 +02:00
unregister_net_types ( ) ;
2017-09-03 12:40:41 +02:00
2017-07-14 01:44:14 +02:00
memdelete ( GDNativeCallRegistry : : singleton ) ;
2017-08-02 02:46:45 +02:00
2017-09-03 14:50:33 +02:00
# ifdef TOOLS_ENABLED
2017-09-10 23:39:42 +02:00
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) & & discoverer ! = NULL ) {
2017-09-03 14:50:33 +02:00
memdelete ( discoverer ) ;
}
# endif
2018-06-11 02:59:53 +02:00
ResourceLoader : : remove_resource_format_loader ( resource_loader_gdnlib ) ;
resource_loader_gdnlib . unref ( ) ;
2019-01-10 12:45:57 +01:00
ResourceSaver : : remove_resource_format_saver ( resource_saver_gdnlib ) ;
2018-06-11 02:59:53 +02:00
resource_saver_gdnlib . unref ( ) ;
2017-11-02 17:14:37 +01:00
2017-08-02 02:46:45 +02:00
// This is for printing out the sizes of the core types
/*
print_line ( String ( " array: \t " ) + itos ( sizeof ( Array ) ) ) ;
print_line ( String ( " basis: \t " ) + itos ( sizeof ( Basis ) ) ) ;
print_line ( String ( " color: \t " ) + itos ( sizeof ( Color ) ) ) ;
print_line ( String ( " dict: \t " ) + itos ( sizeof ( Dictionary ) ) ) ;
print_line ( String ( " node_path: \t " ) + itos ( sizeof ( NodePath ) ) ) ;
print_line ( String ( " plane: \t " ) + itos ( sizeof ( Plane ) ) ) ;
print_line ( String ( " poolarray: \t " ) + itos ( sizeof ( PoolByteArray ) ) ) ;
print_line ( String ( " quat: \t " ) + itos ( sizeof ( Quat ) ) ) ;
print_line ( String ( " rect2: \t " ) + itos ( sizeof ( Rect2 ) ) ) ;
2017-11-17 03:09:00 +01:00
print_line ( String ( " aabb: \t " ) + itos ( sizeof ( AABB ) ) ) ;
2017-08-02 02:46:45 +02:00
print_line ( String ( " rid: \t " ) + itos ( sizeof ( RID ) ) ) ;
print_line ( String ( " string: \t " ) + itos ( sizeof ( String ) ) ) ;
print_line ( String ( " transform: \t " ) + itos ( sizeof ( Transform ) ) ) ;
print_line ( String ( " transfo2D: \t " ) + itos ( sizeof ( Transform2D ) ) ) ;
print_line ( String ( " variant: \t " ) + itos ( sizeof ( Variant ) ) ) ;
print_line ( String ( " vector2: \t " ) + itos ( sizeof ( Vector2 ) ) ) ;
print_line ( String ( " vector3: \t " ) + itos ( sizeof ( Vector3 ) ) ) ;
*/
2017-10-14 15:42:10 +02:00
}