Skip to content

Commit

Permalink
feat: data persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Jul 25, 2024
1 parent 3c4748a commit 0d926c8
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
target
.data
.direnv
.envrc
Expand Down
96 changes: 91 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ embedded = ["include_dir"]
bevy = { version = "0.14", features = ["serialize", "wayland"] }
leafwing-input-manager = { git = "https://github.com/Leafwing-Studios/leafwing-input-manager" }

# Local crates
macros = { path = "macros" }

# Other dependencies
include_dir = { version = "0.7.4", optional = true }
include_dir = { version = "0.7", optional = true }
log = { version = "*", features = [
"max_level_debug",
"release_max_level_warn",
] }
serde = { version = "1.0", features = ["derive"] }
toml = { version = "0.8" }

[profile.dev]
opt-level = 1
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
general-deps = with pkgs; [
# Rust
cargo-watch
cargo-expand
rust-analyzer-unwrapped
# Toml
taplo
Expand Down
101 changes: 101 additions & 0 deletions macros/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "macros"
description = "procedural macros for the game"
repository = "https://github.com/eerii/hello-bevy"
authors = ["Eri <[email protected]>"]
license = "Apache-2.0 OR MIT"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
darling = { version = "0.20" }
quote = { version = "1.0" }
syn = { version = "2.0" }
proc-macro2 = { version = "1.0" }
72 changes: 72 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// For a more complete solution, look at https://github.com/umut-sahin/bevy-persistent

use darling::{ast::NestedMeta, FromMeta};
use proc_macro as pm;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse2, DeriveInput};

const DATA_PATH: &str = ".data";

#[derive(Debug, FromMeta)]
struct PersistentArgs {
name: String,
}

#[proc_macro_attribute]
pub fn persistent(args: pm::TokenStream, input: pm::TokenStream) -> pm::TokenStream {
let input: TokenStream = input.into();
let DeriveInput { ident, .. } = parse2(input.clone()).unwrap();

let attr_args = match NestedMeta::parse_meta_list(args.into()) {
Ok(v) => v,
Err(e) => {
return pm::TokenStream::from(darling::Error::from(e).write_errors());
},
};
let args = match PersistentArgs::from_list(&attr_args) {
Ok(v) => v,
Err(e) => {
return pm::TokenStream::from(e.write_errors());
},
};
let path = format!("{}/{}.toml", DATA_PATH, args.name);
let name = ident.to_string();

let output = quote! {
#[derive(Resource, Serialize, Deserialize)]
#input

impl Persistent for #ident {
fn load() -> Self {
let mut data = Self::default();
data.reload();
data
}

fn reload(&mut self) {
*self = match std::fs::read_to_string(#path) {
Ok(data) => toml::from_str(&data).unwrap_or_default(),
Err(_) => Self::default(),
};
}

fn persist(&self) {
let data = toml::to_string(self).unwrap();
let _ = std::fs::write(#path, data);
debug!("{} updated, saved in {}", #name, #path);
}

fn update(&mut self, f: impl Fn(&mut #ident)) {
f(self);
self.persist()
}

fn reset(&mut self) {
*self = Self::default();
self.persist()
}
}
};
output.into()
}
Loading

0 comments on commit 0d926c8

Please sign in to comment.