Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing; addition of GdResVec #11

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ The following features are currently available. More will be listed in the `In D

Features that will certainly be expanded upon:

- Provide more submodules in the `serde_gd` module to support iterable `Gd<Resouce>` collections.
- Make the `gdron` and `gdbin` formats interchangeable for release mode with a custom `EditorExportPlugin`.
- Ensure everything works smoothly in compiled games, especially pointers to `.tres` resources after they are changed into `.res` format.

Expand Down Expand Up @@ -111,7 +110,9 @@ Both file are recognizable by Godot editor, can be loaded through it and attache
## Bundled resources
What if we have a Resource which contains another resource, which we would want to save as a bundled resource? There are two modules that handle this case:
- `gd_props::serde_gd::gd_option` - for `Option<Gd<T>>` fields,
- `gd_props::serde_gd::gd` - for `Gd<T>` fields.
- `gd_props::serde_gd::gd` - for `Gd<T>` fields,
- `gd_props::serde_gd::gd_resvec` - for vector collections of `Gd<T>` using `GdResVec` (see `Supplementary types` section),
- `gd_props::serde_gd::gd_hashmap` - for `HashMap<K, Gd<T>` fields.

There are some requirements for this to work:
- `T` needs to be User-defined `GodotClass` inheriting from `Resource`,
Expand Down Expand Up @@ -161,8 +162,10 @@ Upon saving, we receive file as below:
## External Resources
If you desire to preserve a sub-resource as an External Resource, akin to regular resource saving in Godot, `gd-props` provides two additional modules:

- `gd_props::serde_gd::ext_option` - designed for `Option<Gd<T>>` fields.
- `gd_props::serde_gd::ext` - designed for `Gd<T>` fields.
- `gd_props::serde_gd::ext_option` - designed for `Option<Gd<T>>` fields,
- `gd_props::serde_gd::ext` - designed for `Gd<T>` fields,
- `gd_props::serde_gd::ext_resvec` - for vector collections of `Gd<T>` using `GdResVec` (see `Supplementary types` section),
- `gd_props::serde_gd::ext_hashmap` - for `HashMap<K, Gd<T>` fields.

To enable this functionality, a few requirements must be met:

Expand Down Expand Up @@ -262,4 +265,19 @@ unsafe impl ExtensionLibrary for MyExtension {
}
}
}
```
```

## Supplementary types

For now, this crate introduces only one utility type: `GdResVec`, a vector-like collection of `Gd` pointers to classes
inheriting from `Resource`.

If you need to hold a collection of subresources in one field, deciding whether to use Godot's `Array`, which allows you
to export the collection to the Godot editor, or `Vec`, which is easier to work with in Rust, can be challenging.

To alleviate this hurdle and avoid the declaration of additional `serde_gd` modules, the `GdResVec` has been created,
combining the best of both worlds:

- It holds the pointers in a `Vec`, dereferences to it in Rust, and can be used as a regular vector.
- It is transferred to the Godot side as a typed `Array`, making it easily accessible there by GDScript methods.
- It implements Property and Export, allowing it to be exported.
18 changes: 15 additions & 3 deletions gd-props-defs/src/gd_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::BufRead;

use godot::builtin::GString;
use godot::engine::file_access::ModeFlags;
use godot::engine::{FileAccess, GFile, Resource, ResourceLoader};
use godot::engine::{FileAccess, GFile, Resource, ResourceLoader, ResourceUid};
use godot::obj::Gd;

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -121,10 +121,22 @@ impl GdMetaExt {
None
}
fn try_load_from_uid(&self, resource_loader: &mut Gd<ResourceLoader>) -> Option<Gd<Resource>> {
resource_loader.load(GString::from(&self.uid))
let resource_uid = ResourceUid::singleton();
let id = resource_uid.text_to_id(GString::from(&self.uid));
if resource_uid.has_id(id) {
resource_loader
.load_ex(GString::from(&self.uid))
.type_hint(GString::from(&self.gd_class))
.done()
} else {
None
}
}
fn try_load_from_path(&self, resource_loader: &mut Gd<ResourceLoader>) -> Option<Gd<Resource>> {
resource_loader.load(GString::from(&self.path))
resource_loader
.load_ex(GString::from(&self.path))
.type_hint(GString::from(&self.gd_class))
.done()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gd-props-defs/src/gdprop_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where
GdPropFormat::GdRon => T::load_ron(path),
GdPropFormat::GdBin => T::load_bin(path),
GdPropFormat::None => {
godot_warn!("Unrecognized format for: {}", &path);
godot_warn!("unrecognized format for: {}", &path);
godot::engine::global::Error::ERR_FILE_UNRECOGNIZED.to_variant()
}
}
Expand Down
7 changes: 7 additions & 0 deletions gd-props-defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ pub mod errors;
pub(crate) mod gd_meta;
pub(crate) mod gdprop;
pub(crate) mod gdprop_io;

/// Module containing serialization and deserialization methods for Godot objects and their collections.
pub mod serde_gd;

/// Supplementary types for easier handling of pointers of [Resource](godot::enigne::Resource)-inheriting
/// [GodotClass](godot::obj::GodotClass)es.
pub mod types;

/// Traits containing logic of `gd-props` custom resource formats.
pub mod traits {
pub use super::gdprop::GdProp;
pub use super::gdprop_io::{GdPropLoader, GdPropSaver};
Expand Down
Loading