Skip to content

Commit

Permalink
feat!: static_resources_dir and more (#16)
Browse files Browse the repository at this point in the history
Copy the game's assets folder to the executable directory.
Place the newly created file in the user's data directory which is writable.
  • Loading branch information
ssrlive authored Jan 22, 2025
1 parent b65e77f commit 3e8dcfd
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 8 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ rusqlite = { version = "0.33", features = ["bundled"] }
arboard = "3.4" # System clipboard
image = "0.25"
toml = "0.8"
dirs = "6"

bevy = "0.15"
leafwing-input-manager = "0.16"
bevy_kira_audio = "0.22"
benimator = "4.1"
winit = "0.30" # The version needs to be consistent with the one used in bevy

[build-dependencies]
fs_extra = "1.2"

# Enable a small amount of optimization in debug mode.
[profile.dev]
opt-level = 1
Expand Down
35 changes: 35 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fn main() {
println!("cargo:rerun-if-changed=build.rs");
copy_assets_to_work_dir();
}

fn copy_assets_to_work_dir() {
use std::path::Path;

let out_dir = get_cargo_target_dir().unwrap();
let src = "assets";
let dst = out_dir.join(src);

if dst.exists() {
std::fs::remove_dir_all(dst).unwrap();
}

let mut options = fs_extra::dir::CopyOptions::new();
options.copy_inside = true;
fs_extra::dir::copy(src, Path::new(&out_dir), &options).unwrap();
}

fn get_cargo_target_dir() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR")?);
let profile = std::env::var("PROFILE")?;
let mut target_dir = None;
let mut current_path = out_dir.as_path();
while let Some(parent) = current_path.parent() {
if parent.ends_with(&profile) {
target_dir = Some(parent);
break;
}
current_path = parent;
}
Ok(target_dir.ok_or("not found")?.to_path_buf())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod events;
mod input_map;
mod plugins;
mod resources;
mod settings;
mod solve;
mod state;
mod systems;
Expand Down
14 changes: 9 additions & 5 deletions src/plugins/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ pub struct ConfigPlugin;

impl Plugin for ConfigPlugin {
fn build(&self, app: &mut App) {
if !Path::new(CONFIG_FILE_PATH).is_file() {
if !Path::new(&*CONFIG_FILE_PATH).is_file() {
let default_config_toml = toml::to_string(&Config::default()).unwrap();
fs::write(CONFIG_FILE_PATH, default_config_toml).unwrap();
fs::write(&*CONFIG_FILE_PATH, default_config_toml).unwrap();
save_config(&Config::default());
}

Expand All @@ -25,17 +25,21 @@ impl Plugin for ConfigPlugin {
}
}

const CONFIG_FILE_PATH: &str = "config.toml";
static CONFIG_FILE_PATH: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
let mut path = crate::settings::app_writeable_dir();
path.push("config.toml");
path.to_str().unwrap().to_string()
});

fn load_config() -> Config {
let config_toml = fs::read_to_string(CONFIG_FILE_PATH).unwrap();
let config_toml = fs::read_to_string(&*CONFIG_FILE_PATH).unwrap();
let config: Config = toml::from_str(config_toml.as_str()).unwrap();
config
}

fn save_config(config: &Config) {
let config_toml = toml::to_string(&config).unwrap();
fs::write(CONFIG_FILE_PATH, config_toml).unwrap();
fs::write(&*CONFIG_FILE_PATH, config_toml).unwrap();
}

fn save_config_system(config: Res<Config>) {
Expand Down
12 changes: 12 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub(crate) fn static_resources_dir() -> std::path::PathBuf {
let mut path = std::env::current_exe().unwrap();
path.pop();
path
}

pub(crate) fn app_writeable_dir() -> std::path::PathBuf {
let mut path = dirs::data_dir().unwrap();
path.push(env!("CARGO_PKG_NAME"));
std::fs::create_dir_all(&path).unwrap();
path
}
6 changes: 4 additions & 2 deletions src/systems/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use std::{collections::HashMap, fs, path::Path, sync::Mutex};

/// Sets up the database, initializes it, and loads levels from files into the database.
pub fn setup_database(mut commands: Commands) {
let database = database::Database::from_file(Path::new("db.sqlite3"));
let db_path = crate::settings::app_writeable_dir().join("db.sqlite3");
let database = database::Database::from_file(Path::new(&db_path));
database.initialize();
info!("Loading levels from files");
for path in fs::read_dir("assets/levels/").unwrap() {
let levels_dir = crate::settings::static_resources_dir().join("assets/levels/");
for path in fs::read_dir(&levels_dir).unwrap() {
let path = path.unwrap().path();
if !path.is_file() {
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/systems/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use std::{cmp::Ordering, collections::HashSet, time::Duration};
/// Sets the window icon for all windows
pub fn set_windows_icon(winit_windows: NonSend<WinitWindows>) {
let (icon_rgba, icon_width, icon_height) = {
let image = image::open("assets/textures/box.png")
let icon_path = crate::settings::static_resources_dir().join("assets/textures/box.png");
let image = image::open(&icon_path)
.expect("failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
Expand Down

0 comments on commit 3e8dcfd

Please sign in to comment.