nexus\api/
paths.rs

1//! Paths.
2
3use crate::{
4    util::{path_from_c, str_to_c},
5    AddonApi, PathApi,
6};
7use std::{ffi::c_char, path::PathBuf};
8
9pub type RawGetGameDir = unsafe extern "C-unwind" fn() -> *const c_char;
10
11pub type RawGetAddonDir = unsafe extern "C-unwind" fn(name: *const c_char) -> *const c_char;
12
13pub type RawGetCommonDir = unsafe extern "C-unwind" fn() -> *const c_char;
14
15/// Returns the game directory.
16#[inline]
17pub fn get_game_dir() -> Option<PathBuf> {
18    let PathApi { get_game_dir, .. } = AddonApi::get().path;
19    unsafe { path_from_c(get_game_dir()) }
20}
21
22/// Returns the directory for an addon with the passed name.
23#[inline]
24pub fn get_addon_dir(name: impl AsRef<str>) -> Option<PathBuf> {
25    let PathApi { get_addon_dir, .. } = AddonApi::get().path;
26    let name = str_to_c(name, "failed to convert addon dir name");
27    unsafe { path_from_c(get_addon_dir(name.as_ptr())) }
28}
29
30/// Returns the common addon directory.
31///
32/// Alias for `get_addon_dir("common")`.
33#[inline]
34pub fn get_common_dir() -> Option<PathBuf> {
35    let PathApi { get_common_dir, .. } = AddonApi::get().path;
36    unsafe { path_from_c(get_common_dir()) }
37}