-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: static_resources_dir and more (#16)
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
Showing
8 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters