migueldeicaza / SwiftGodot

New Godot bindings for Swift
https://migueldeicaza.github.io/SwiftGodotDocs/tutorials/swiftgodot-tutorials/
MIT License
1.19k stars 77 forks source link

Import options not working on `EditorImportPlugin` subclass #470

Open sekimondre opened 6 months ago

sekimondre commented 6 months ago

Environment

Bug Description

Import options are not working as intended for Import plugins written in Swift. These are the options provided through the _getImportOptions(path:_, presetIndex:_) method of a EditorImportPlugin. The noticeable effects are:

  1. Import options don't show up in the editor's "Import" dock.
  2. An empty dictionary { } is received through the options parameter of the _import(sourceFile:_,savePath:_,options:_,platformVariants:_, genFiles:_) method.

How to reproduce

  1. Create an EditorImportPlugin subclass:

    @Godot(.tool)
    class TestPlugin: EditorImportPlugin {}
  2. Override and provide a default implementation to all necessary methods.

    • _getImporterName
    • _getVisibleName
    • _getRecognizedExtensions
    • _getResourceType
    • _getSaveExtension
    • _getImportOrder
    • _getPriority
    • _getPresetCount
    • _getPresetName
  3. Override _getImportOptions:

    override func _getImportOptions(
    path: String,
    presetIndex: Int32
    ) -> VariantCollection<GDictionary> {
    let opt1 = GDictionary()
    opt1["name"] = Variant("use_this_option")
    opt1["default_value"] = Variant(false)
    let opt2 = GDictionary()
    opt2["name"] = Variant("another_option")
    opt2["default_value"] = Variant(true)
    return [
        opt1,
        opt2
    ]
    }
  4. Override the _import function. Try to print the options parameter or any values that should be in the dictionary.

  5. Create a Godot plugin in res://addons, provide a plugin.cfg file, and enable the import plugin with a GDScript:

    
    @tool
    extends EditorPlugin

var plugin: TestPlugin

func _get_plugin_name() -> String: return "Test Importer"

func _enter_tree(): plugin = TestPlugin.new() add_import_plugin(plugin)

func _exit_tree(): remove_import_plugin(plugin) plugin = null



Finally, build the library, reload the editor, and enable the plugin on the "Project Settings...". Select a file on the editor's filesystem with the plugin's recognizable extension and open the "Import" tab. Check that no options are shown.

Press the "Reimport" button to trigger an import and check for any values you tried to print to the console.

### Considerations

I tried writing the exact same plugin (same values, same overrides, ...) in GDScript and it works. Options show as intended.

Sometimes, options from another import plugin used leak into the `_import` call (visible by printing to console). Seems like some caching behavior is happening in the editor across plugins.

If someone can make it work is Swift, let me know.

### Reference
For more information on Import plugins options, refer to the official documentation:
https://docs.godotengine.org/en/stable/tutorials/plugins/editor/import_plugins.html#options-and-presets
migueldeicaza commented 6 months ago

~Can you annotate the overwritten method with @Callable?~