Coder Social home page Coder Social logo

bevy_pkv's Introduction

bevy_pkv

crates.io MIT/Apache 2.0 docs.rs ci

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

# #[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:

fn setup(mut pkv: ResMut<PkvStore>) {
    if let Ok(username) = pkv.get::<String>("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:

#[derive(Serialize, Deserialize)]
struct User {
    name: String,
}

fn setup(mut pkv: ResMut<PkvStore>) {
    if let Ok(user) = pkv.get::<User>("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 for further usage

Usage without Bevy

Disable the default features when adding the dependency:

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.

I intend to support the main branch of Bevy in the bevy-main branch.

bevy bevy_pkv
0.13 0.10, main
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

bevy_pkv's People

Contributors

aultus-defora avatar eerii avatar feelingsonice avatar hyperalch avatar idanarye avatar johanhelsing avatar kees-van-beilen avatar leinnan avatar phaestusfox avatar richardhozak avatar tristancacqueray avatar zigguratv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.