2023-01-05 13:25:55 +01:00
/**************************************************************************/
/* gdextension_export_plugin.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
2022-03-10 08:48:25 +01:00
# ifndef GDEXTENSION_EXPORT_PLUGIN_H
# define GDEXTENSION_EXPORT_PLUGIN_H
2022-07-17 19:24:37 +02:00
# include "editor/export/editor_export.h"
2022-03-10 08:48:25 +01:00
class GDExtensionExportPlugin : public EditorExportPlugin {
protected :
2022-05-19 17:00:06 +02:00
virtual void _export_file ( const String & p_path , const String & p_type , const HashSet < String > & p_features ) ;
Add support for scene/resource customization in export plugins
EditorExportPlugin adds a set of callbacks to allow customizing scenes, resources or subresources in all files exported:
* Can take scene files, resource files and subresources in all of them.
* Uses a cache for the converted files if nothing changes, so this work only happens if a file is modified.
* Uses hashing to differentiate export configuration caches.
* Removed the previous conversion code to binary, as this one uses existing stuff.
This API is useful in several scenarios:
* Needed by the "server" export platform to get rid of textures, meshes, audio, etc.
* Needed by text to binary converters.
* Needed by eventual optimizations such as shader precompiling on export, mesh merging and optimization, etc.
This is a draft, feedback is very welcome.
2022-08-31 11:12:42 +02:00
virtual String _get_name ( ) const { return " GDExtension " ; }
2022-03-10 08:48:25 +01:00
} ;
2022-05-19 17:00:06 +02:00
void GDExtensionExportPlugin : : _export_file ( const String & p_path , const String & p_type , const HashSet < String > & p_features ) {
2022-12-07 12:11:28 +01:00
if ( p_type ! = " GDExtension " ) {
2022-03-10 08:48:25 +01:00
return ;
}
Ref < ConfigFile > config ;
config . instantiate ( ) ;
Error err = config - > load ( p_path ) ;
2022-05-24 09:54:37 +02:00
ERR_FAIL_COND_MSG ( err , " Failed to load GDExtension file: " + p_path ) ;
2022-03-10 08:48:25 +01:00
2022-05-24 09:54:37 +02:00
ERR_FAIL_COND_MSG ( ! config - > has_section_key ( " configuration " , " entry_symbol " ) , " Failed to export GDExtension file, missing entry symbol: " + p_path ) ;
2022-03-10 08:48:25 +01:00
String entry_symbol = config - > get_value ( " configuration " , " entry_symbol " ) ;
2022-10-26 18:23:09 +02:00
PackedStringArray tags ;
2022-12-07 12:11:28 +01:00
String library_path = GDExtension : : find_extension_library (
2022-10-26 18:23:09 +02:00
p_path , config , [ p_features ] ( String p_feature ) { return p_features . has ( p_feature ) ; } , & tags ) ;
if ( ! library_path . is_empty ( ) ) {
add_shared_object ( library_path , tags ) ;
if ( p_features . has ( " iOS " ) & & ( library_path . ends_with ( " .a " ) | | library_path . ends_with ( " .xcframework " ) ) ) {
String additional_code = " extern void register_dynamic_symbol(char *name, void *address); \n "
" extern void add_ios_init_callback(void (*cb)()); \n "
" \n "
" extern \" C \" void $ENTRY(); \n "
" void $ENTRY_init() { \n "
" if (&$ENTRY) register_dynamic_symbol((char *) \" $ENTRY \" , (void *)$ENTRY); \n "
" } \n "
" struct $ENTRY_struct { \n "
" $ENTRY_struct() { \n "
" add_ios_init_callback($ENTRY_init); \n "
" } \n "
" }; \n "
" $ENTRY_struct $ENTRY_struct_instance; \n \n " ;
additional_code = additional_code . replace ( " $ENTRY " , entry_symbol ) ;
add_ios_cpp_code ( additional_code ) ;
String linker_flags = " -Wl,-U,_ " + entry_symbol ;
add_ios_linker_flags ( linker_flags ) ;
2022-03-10 08:48:25 +01:00
}
2022-10-26 18:23:09 +02:00
} else {
Vector < String > features_vector ;
2022-05-24 09:54:37 +02:00
for ( const String & E : p_features ) {
2022-10-26 18:23:09 +02:00
features_vector . append ( E ) ;
2022-05-24 09:54:37 +02:00
}
2022-12-07 13:22:49 +01:00
ERR_FAIL_MSG ( vformat ( " No suitable library found for GDExtension: %s. Possible feature flags for your platform: %s " , p_path , String ( " , " ) . join ( features_vector ) ) ) ;
2022-05-24 09:54:37 +02:00
}
2022-03-10 08:48:25 +01:00
List < String > dependencies ;
2022-05-24 09:54:37 +02:00
if ( config - > has_section ( " dependencies " ) ) {
config - > get_section_keys ( " dependencies " , & dependencies ) ;
}
2022-03-10 08:48:25 +01:00
2022-10-26 18:23:09 +02:00
for ( const String & E : dependencies ) {
Vector < String > dependency_tags = E . split ( " . " ) ;
2022-03-10 08:48:25 +01:00
bool all_tags_met = true ;
2022-10-26 18:23:09 +02:00
for ( int i = 0 ; i < dependency_tags . size ( ) ; i + + ) {
String tag = dependency_tags [ i ] . strip_edges ( ) ;
2022-03-10 08:48:25 +01:00
if ( ! p_features . has ( tag ) ) {
all_tags_met = false ;
break ;
}
}
if ( all_tags_met ) {
Dictionary dependency = config - > get_value ( " dependencies " , E ) ;
for ( const Variant * key = dependency . next ( nullptr ) ; key ; key = dependency . next ( key ) ) {
2022-10-26 18:23:09 +02:00
String dependency_path = * key ;
2022-03-10 08:48:25 +01:00
String target_path = dependency [ * key ] ;
2022-10-26 18:23:09 +02:00
if ( dependency_path . is_relative_path ( ) ) {
dependency_path = p_path . get_base_dir ( ) . path_join ( dependency_path ) ;
2022-03-10 08:48:25 +01:00
}
2022-10-26 18:23:09 +02:00
add_shared_object ( dependency_path , dependency_tags , target_path ) ;
2022-03-10 08:48:25 +01:00
}
break ;
}
}
}
# endif // GDEXTENSION_EXPORT_PLUGIN_H