bp3d_os/assets/
mod.rs

1// Copyright (c) 2025, BlockProject 3D
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7//
8//     * Redistributions of source code must retain the above copyright notice,
9//       this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above copyright notice,
11//       this list of conditions and the following disclaimer in the documentation
12//       and/or other materials provided with the distribution.
13//     * Neither the name of BlockProject 3D nor the names of its contributors
14//       may be used to endorse or promote products derived from this software
15//       without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29//! This module provides cross-platform functions to get application resources.
30
31use std::path::PathBuf;
32
33#[cfg(target_vendor = "apple")]
34mod apple;
35
36#[cfg(target_os = "linux")]
37mod linux;
38
39#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
40mod bsd;
41
42#[cfg(target_os = "windows")]
43mod windows;
44
45#[cfg(target_vendor = "apple")]
46use apple::{get_exe_path, get_resources_dir};
47
48#[cfg(target_os = "linux")]
49use linux::{get_exe_path, get_resources_dir};
50
51#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
52use bsd::{get_exe_path, get_resources_dir};
53
54#[cfg(target_os = "windows")]
55use windows::{get_exe_path, get_resources_dir};
56
57/// Returns the path to an asset of the application.
58///
59/// # Platform specific behavior
60///
61/// On supported platforms this returns an asset bundled in the application. Supported platforms are:
62/// - Any Linux/Unix when app is packaged as an AppImage,
63/// - macOS (when app is packaged as a .app),
64/// - iOS
65///
66/// In the case a platform/packaging method isn't supported this function still returns a path based
67/// on executable location.
68///
69/// Returns None if there is a system issue, ex: the system didn't return a proper path to the current
70/// executing application. This should rarely occur.
71pub fn get_app_bundled_asset(file_name: &str) -> Option<PathBuf> {
72    let res = get_resources_dir()
73        .map(|v| v.join(file_name))
74        .or_else(|| get_exe_path().map(|v| v.join("..").join("Assets").join(file_name)));
75    if res.as_ref().map(|v| !v.exists()).unwrap_or(false) {
76        let res = get_exe_path().map(|v| v.join("Assets").join(file_name));
77        if res.as_ref().map(|v| !v.exists()).unwrap_or(false) {
78            return None;
79        }
80        return res;
81    }
82    res
83}
84
85/// Returns the path to the executable. This is useful for loading assets in CLI based applications.
86///
87/// # Platform specific behavior
88///
89/// On supported platforms this returns the path to the current executing program.
90/// Supported platforms are:
91/// - Any Linux/Unix with a procfs,
92/// - macOS,
93/// - iOS
94///
95/// Returns None if there is a system issue, ex: the system didn't return a proper path to the current
96/// executing application. This should rarely occur.
97pub fn get_executable_path() -> Option<PathBuf> {
98    get_exe_path()
99}
100
101#[cfg(test)]
102mod tests {
103    use crate::assets::{get_app_bundled_asset, get_executable_path};
104
105    #[test]
106    fn test() {
107        assert!(get_executable_path().is_some());
108    }
109
110    #[test]
111    fn test2() {
112        assert!(get_app_bundled_asset("file.txt").is_none());
113    }
114}