gdext icon indicating copy to clipboard operation
gdext copied to clipboard

Type-safe API to query Godot features

Open Bromeon opened this issue 2 years ago • 2 comments

We already have the GdextBuild class which gives access to API and runtime versions.

There are more capabilities that can be queried, e.g. using OS::has_feature().

The feature tags are however stringly typed, and it would be nice to have a type-safe API. Instead of a single enum grouping all values, we could categorize features into multiple specific enums. The docs contain a bullet list:

Godot has a special system to tag availability of features. Each feature is represented as a string, which can refer to many of the following:

  • Platform name.
  • Platform architecture (64-bit or 32-bit, x86 or ARM).
  • Platform type (desktop, mobile, Web).
  • Supported texture compression algorithms on the platform.
  • Whether a build is debug or release (debug includes the editor).
  • Whether the project is running from the editor or a "standalone" binary.
  • Many more things.

We would need to make sure that enum variants are truly exclusive, otherwise we'd need to model things differently. Knowing the exact semantics may require some research.

Bromeon avatar Dec 19 '23 19:12 Bromeon

I was looking at this a bit, it's hella hard to track down every feature listed, but I'm fairly confident that relatively few are fully exclusive. There's also the ability to set custom settings, so we'd also have to still have some way to support that.

Maybe something like a very simple FeatureTag attribute or trait that we could implement on the builtin tags and that users could add to their own types so that they don't have to use strings at all, even for custom tags.

vortexofdoom avatar Feb 02 '24 17:02 vortexofdoom

The predefined tags are often arranged in flags style, rather than exclusive enums, e.g.

// "atomic" ones (single bit)
x86_32
x86_64
arm32
arm64
...

// abstract over archs
arm = arm32 | arm64
x86 = x86_32 | x86_64

// abstract over bits
32 = x86_32 | arm32 | ...
64 = x86_64 | arm64 | ...

And for practical scenarios, I'm not convinced that creating a complex type system around this is worth it. We should probably just start with constants to avoid stringly-typed problems like typos.

pub struct FeatureTag { ... }

impl FeatureTag {
    /// Custom features (could even validate they don't overlap with builtins).
    pub fn user_defined(tag: &str);

    /// All predefined ones.
    pub const ANDROID: FeatureTag;
    pub const BSD: FeatureTag;
    ...
    pub const BITS_64: FeatureTag; // "64"
    ...
}

Bromeon avatar Feb 02 '24 18:02 Bromeon