17个版本 (6个重大更新)
新 0.7.0 | 2024年8月23日 |
---|---|
0.6.0 | 2024年7月9日 |
0.5.0 | 2024年2月18日 |
0.2.2 | 2023年12月29日 |
#485 在 游戏开发
每月75 次下载
48KB
521 行
Bevy Local Commands
Bevy插件,用于管理本地shell命令。
使用方法
添加插件
// ...
.add_plugins(BevyLocalCommandsPlugin)
// ...
运行shell命令
fn run_command(mut commands: Commands) {
commands.spawn(LocalCommand::new("bash").args(["-c", "sleep 1 && echo slept"]));
}
查看已启动的命令并终止运行中的命令
fn kill_started_command(mut active_processes: Query<&mut Process>) {
for mut process in active_processes.iter_mut() {
warn!("Killing process {}", process.id());
process.kill().unwrap();
}
}
接收命令输出
fn get_command_output(mut process_output_event: EventReader<ProcessOutput>) {
for output in process_output_event.read() {
info!("Output for command {:?}", output.entity);
for line in output.lines() {
info!("Line Output: {}", line);
}
}
}
发送命令输入
fn send_command_input(
mut process_output_event: EventReader<ProcessOutput>,
mut active_processes: Query<&mut Process>,
) {
for output in process_output_event.read() {
for line in output.lines() {
if line.ends_with("Prompt String: ") {
let mut process = active_processes.get_mut(output.entity).unwrap();
process.println("Text to send").expect("Failed to write to process");
}
}
}
}
查看命令完成情况
fn get_completed(mut process_completed_event: EventReader<ProcessCompleted>) {
for completed in process_completed_event.read() {
info!(
"Command completed (Entity - {}, Success - {})",
completed.entity,
completed.exit_status.success()
);
}
}
重试
fn retries(mut commands: Commands) {
commands.spawn((
LocalCommand::new("bash").args(["-c", "sleep 1 && invalid-command --that=fails"]),
Retry::Attempts(3) // Attempt the command 3 times before giving up
));
}
清理
fn cleanup_on_completion(mut commands: Commands) {
commands.spawn((
LocalCommand::new("bash").args(["-c", "sleep 1"]),
Cleanup::DespawnEntity // Will despawn the entity upon process completion
// Cleanup::RemoveComponents // Will remove only this crate's components upon process completion
));
}
延迟
fn delay_process_start(mut commands: Commands) {
commands.spawn((
LocalCommand::new("bash").args(["-c", "sleep 1"]),
Delay::Fixed(Duration::from_secs(2)), // Start the process after a 2s delay (applies to each retry)
));
}
链式调用
fn chain_multiple_commands(mut commands: Commands) {
commands.spawn((
Chain::new(vec![
LocalCommand::new("sh").args(["-c", "echo 'First command'"]),
LocalCommand::new("sh").args(["-c", "echo 'Second command'"]),
LocalCommand::new("sh").args(["-c", "echo 'Third command'"]),
]),
Retry::Attempts(2), // Retry applies to any link in the chain
Delay::Fixed(Duration::from_secs(3)), // Wait 3s between retries and chain commands
Cleanup::RemoveComponents // Remove Chain, Retry, Delay, and Cleanup components upon completion
));
}
待办事项
- Mac测试(不确定是否可行)
Bevy兼容性
bevy | bevy_local_commands |
---|---|
0.14 | 0.6 |
0.13 | 0.5 |
0.12 | 0.4 |
0.11 | 0.1 |
依赖
~35–71MB
~1M SLoC