Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ crate-type = ["cdylib"]
[[example]]
name = "menus"
crate-type = ["cdylib"]

[[example]]
name = "paths"
crate-type = ["cdylib"]
42 changes: 42 additions & 0 deletions examples/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extern crate xplm;

use xplm::paths::{aircraft_path, plugin_path, prefs_path, system_path};
use xplm::plugin::{Plugin, PluginInfo};
use xplm::{debugln, xplane_plugin};

struct PathsDemo;

impl Plugin for PathsDemo {
type Error = std::convert::Infallible;

fn start() -> Result<Self, Self::Error> {
debugln!(
"[Rust-XPLM] X-Plane root folder: {}",
system_path().unwrap().display()
);
debugln!(
"[Rust-XPLM] X-Plane prefs file: {}",
prefs_path().unwrap().display()
);
debugln!(
"[Rust-XPLM] Plugin path: {}",
plugin_path().unwrap().display()
);
debugln!(
"[Rust-XPLM] Aircraft path: {}",
aircraft_path().unwrap().display()
);

Ok(PathsDemo)
}

fn info(&self) -> PluginInfo {
PluginInfo {
name: String::from("Paths Demo Rust Plugin"),
signature: String::from("org.samcrow.xplm.examples.paths"),
description: String::from("Demonstrates paths functions"),
}
}
}

xplane_plugin!(PathsDemo);
12 changes: 11 additions & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr;

use super::feature;

/// Copies up to 256 bytes (including null termination) to
/// the provided destination. If the provided source string is too long, it will be
/// truncated.
Expand All @@ -14,7 +16,15 @@ pub unsafe fn copy_to_c_buffer(mut src: String, dest: *mut c_char) {
ptr::copy_nonoverlapping(src_c.as_ptr(), dest, src_c_length);
}

/// Enables native paths
pub fn path_init() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn path_init() {
fn path_init() {

Unless I am missing something, there should be no reason anymore for this to be public

// Feature specified to exist in SDK 2.1
let native_path_feature =
feature::find_feature("XPLM_USE_NATIVE_PATHS").expect("No native paths feature");
native_path_feature.set_enabled(true);
}

/// Performs initialization required for the XPLM crate to work correctly
pub fn xplm_init() {
super::paths::path_init();
path_init();
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use std::ffi::CString;

/// FFI utilities
mod ffi;
/// Path conversion
mod paths;
/// Plugin macro
mod plugin_macro;

Expand All @@ -34,6 +32,8 @@ pub mod flight_loop;
pub mod geometry;
/// User interface menus
pub mod menu;
/// X-Plane and plugin paths
pub mod paths;
Comment on lines +35 to +36
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add docs to the path module that we always return native paths because we turn on the resprective feature during xplm_init.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this... these functions return PathBuf objects from the Rust standard library - isn't that platform-agnostic? I thought the "native paths" feature was a convenience for C programmers!

/// Plugin creation and management
pub mod plugin;
/// X-Plane and XPLM version info
Expand Down
57 changes: 49 additions & 8 deletions src/paths.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
use super::feature;

/// Enables native paths
pub fn path_init() {
// Feature specified to exist in SDK 2.1
let native_path_feature =
feature::find_feature("XPLM_USE_NATIVE_PATHS").expect("No native paths feature");
native_path_feature.set_enabled(true);
use std::{path::PathBuf, ptr::null_mut, str::Utf8Error};

use xplm_sys::{
XPLMGetMyID, XPLMGetNthAircraftModel, XPLMGetPluginInfo, XPLMGetPrefsPath, XPLMGetSystemPath,
};

use super::ffi::StringBuffer;

/// Get path to this plugin
pub fn plugin_path() -> Result<PathBuf, Utf8Error> {
let mut path = StringBuffer::new(512);
unsafe {
XPLMGetPluginInfo(
XPLMGetMyID(),
null_mut(),
path.as_mut_ptr(),
null_mut(),
null_mut(),
);
}
Ok(PathBuf::from(path.as_str()?))
}

/// Get path to X-Plane's preferences file
/// (usually "Output/preferences/Set X-Plane.prf")
pub fn prefs_path() -> Result<PathBuf, Utf8Error> {
let mut path = StringBuffer::new(512);
unsafe {
XPLMGetPrefsPath(path.as_mut_ptr());
}
Ok(PathBuf::from(path.as_str()?))
}

/// Get path to X-Plane root folder
pub fn system_path() -> Result<PathBuf, Utf8Error> {
let mut path = StringBuffer::new(512);
unsafe {
XPLMGetSystemPath(path.as_mut_ptr());
}
Ok(PathBuf::from(path.as_str()?))
}

/// Get path to loaded aircraft's .acf file
pub fn aircraft_path() -> Result<PathBuf, Utf8Error> {
let mut path = StringBuffer::new(512);
unsafe {
XPLMGetNthAircraftModel(0, StringBuffer::new(256).as_mut_ptr(), path.as_mut_ptr());
}
Ok(PathBuf::from(path.as_str()?))
}