Closed JDeeth closed 8 months ago
OK, the command is dropped when X-Plane shuts down, and XPLMCreateCommand
returns the existing XPLMCommandRef
if there's already a command with the same name. So the workaround simplifies a little:
impl OwnedCommandData {
pub fn new<H: CommandHandler>(
name: &str,
description: &str,
handler: H,
) -> Result<Self, CommandCreateError> {
let name_c = CString::new(name)?;
let description_c = CString::new(description)?;
Ok(OwnedCommandData {
id: unsafe { XPLMCreateCommand(name_c.as_ptr(), description_c.as_ptr()) },
handler: Box::new(handler),
})
}
}
This is a bit perplexing!
If you register an
OwnedCommand
, when the plugin loads first time, it creates the command. On reloading, it fails (and doesn't register the command handler) because the command already exists. Seems XP12 doesn't delete command IDs when all the handlers are deregistered anymore (if it ever did)?Win11, XP12.0.8-rc3, rust-xplm 0.3.1, aircraft plugin.
First run:
Second run, after loading a different aircraft in between:
Cargo.toml
:lib.rs
:My workaround is to clone
rust-xplm
locally and depend on this instead, and altercommand::OwnedCommandData::new
to use the existing command if it exists:Perhaps it's worth separating the creation of commands from the registration/deregistration of command handlers?
Just as a point of comparison, my C++ wrapper for commands, where registering handlers for existing commands is accommodated alongside creating new commands: https://github.com/JDeeth/PPL/blob/xplm210/src/command.h
Registering your own handler for an X-Plane stock command is fine - you can return 1 or 0 to let the default handler run or not. I don't know what happens if a plugin registers a new handler for a command created by another plugin - which handler takes precedence or if one replaces the other…!