Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/johanhelsing/bevy_pkv
Cross-platform (including wasm) persistent key value store plugin for rust games/apps
https://github.com/johanhelsing/bevy_pkv
bevy bevy-plugin key-value localstorage persistence rust sled wasm
Last synced: about 1 month ago
JSON representation
Cross-platform (including wasm) persistent key value store plugin for rust games/apps
- Host: GitHub
- URL: https://github.com/johanhelsing/bevy_pkv
- Owner: johanhelsing
- License: other
- Created: 2022-03-15T11:20:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-04T17:12:38.000Z (3 months ago)
- Last Synced: 2024-09-27T07:04:00.889Z (about 2 months ago)
- Topics: bevy, bevy-plugin, key-value, localstorage, persistence, rust, sled, wasm
- Language: Rust
- Homepage:
- Size: 89.8 KB
- Stars: 133
- Watchers: 6
- Forks: 23
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bevy_pkv
[![crates.io](https://img.shields.io/crates/v/bevy_pkv.svg)](https://crates.io/crates/bevy_pkv)
![MIT/Apache 2.0](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)
[![docs.rs](https://img.shields.io/docsrs/bevy_pkv)](https://docs.rs/bevy_pkv)
[![ci](https://github.com/johanhelsing/bevy_pkv/actions/workflows/ci.yml/badge.svg)](https://github.com/johanhelsing/bevy_pkv/actions/workflows/ci.yml)`bevy_pkv` is a cross-platform persistent key value store for rust apps.
Use it for storing things like settings, save games etc.
Currently, the Bevy dependency is optional, so it may be used in other games/apps as well.
## Usage with Bevy
Add a store resource to your app
```rust no_run
# #[cfg(feature = "bevy")] { // ignore this line
use bevy::prelude::*;
use bevy_pkv::PkvStore;fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(PkvStore::new("FooCompany", "BarGame"))
// ...insert systems etc.
.run();
}
# }
```This will create or load a store in the appropriate location for your system, and make it available to bevy systems:
```rust ignore
fn setup(mut pkv: ResMut) {
if let Ok(username) = pkv.get::("username") {
info!("Welcome back {username}");
} else {
pkv.set_string("username", "alice")
.expect("failed to store username");// alternatively, using the slightly less efficient generic api:
pkv.set("username", &"alice".to_string())
.expect("failed to store username");
}
}
```Using your own types implementing `serde::Serialize` and `Deserialize`:
```rust ignore
#[derive(Serialize, Deserialize)]
struct User {
name: String,
}fn setup(mut pkv: ResMut) {
if let Ok(user) = pkv.get::("user") {
info!("Welcome back {}", user.name);
} else {
let user = User {
name: "bob".to_string(),
};
pkv.set("user", &user).expect("failed to store user");
}
}
```See the [examples](./examples) for further usage
## Usage without Bevy
Disable the default features when adding the dependency:
```toml
bevy_pkv = {version = "0.9", default-features = false}
```## Implementation details
### Native
`redb` and `rmp_serde` (MessagePack) is used for storage. It's creating a `bevy_pkv.redb` db in the appropriate application data directory for your system.
Alternatively, disable default-features and enable the `rocksdb` feature to use a RocksDB-based implementation or `sled` feature to use sled db.
### Wasm
`Window.localStorage` and `serde_json` is used for storage. Perhaps IndexedDb and something else would have been a better choice, but its API is complicated, and I wanted a simple implementation and a simple synchronous API.
## Bevy version support
The `main` branch targets the latest bevy release.
|bevy|bevy\_pkv|
|----|---|
|0.14|0.11, main|
|0.13|0.10|
|0.12|0.9|
|0.11|0.8|
|0.10|0.7|
|0.9 |0.6|
|0.8 |0.5|
|0.7 |0.2, 0.3, 0.4|
|0.6 |0.1|## License
MIT or Apache-2.0