config files
This commit is contained in:
@@ -22,7 +22,6 @@ pub fn set_tripwire(
|
||||
callsign: Query<&Callsign>,
|
||||
) {
|
||||
let Some(pilot) = &cmd.pilot else {
|
||||
eprintln!("no pilot");
|
||||
return; // no pilot
|
||||
};
|
||||
let Some(operator) = &cmd.operator else {
|
||||
|
||||
95
src/config.rs
Normal file
95
src/config.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
|
||||
use bevy::ecs::system::Resource;
|
||||
use guardian_core::{
|
||||
components::{GrpcBaseUrl, SrsSocketAddr, SttBaseUrl},
|
||||
srs::{Modulation as SrsModulation, Voice},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Resource, Clone, Debug, Deserialize, Serialize, Default)]
|
||||
pub struct Channels(pub HashMap<String, ChannelConfig>);
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
|
||||
pub struct Config {
|
||||
pub srs: SrsConfig,
|
||||
pub grpc: GrpcBaseUrl,
|
||||
pub stt: SttBaseUrl,
|
||||
pub channels: Channels,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
||||
pub struct SrsConfig {
|
||||
pub server: SrsSocketAddr,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
|
||||
pub struct ChannelConfig {
|
||||
pub frequency: u64,
|
||||
pub modulation: Modulation, // 0=AM, 1=FM?
|
||||
pub voice: Voice,
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for Config {
|
||||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
|
||||
|
||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
||||
use std::io::Read;
|
||||
let mut config = std::fs::File::open(value)?;
|
||||
let mut config_str = String::new();
|
||||
config.read_to_string(&mut config_str)?;
|
||||
Ok(toml::from_str(&config_str)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(file: PathBuf) -> Result<Self, Box<dyn std::error::Error + Send + Sync + 'static>> {
|
||||
if file.exists() {
|
||||
return file.try_into();
|
||||
}
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
let mut defaults = Config::default();
|
||||
|
||||
defaults.channels.0.insert(
|
||||
"Overlord1-1".to_string(),
|
||||
ChannelConfig {
|
||||
frequency: 251000000,
|
||||
modulation: Modulation::Am,
|
||||
voice: Voice::David,
|
||||
},
|
||||
);
|
||||
|
||||
// write the file
|
||||
std::fs::File::create(file)?.write_all(toml::to_string_pretty(&defaults)?.as_bytes())?;
|
||||
|
||||
Ok(defaults)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Default)]
|
||||
pub enum Modulation {
|
||||
#[default]
|
||||
Am,
|
||||
Fm,
|
||||
Intercom,
|
||||
Disabled,
|
||||
HaveQuick,
|
||||
Satcom,
|
||||
Mids,
|
||||
}
|
||||
|
||||
impl From<Modulation> for SrsModulation {
|
||||
fn from(value: Modulation) -> Self {
|
||||
match value {
|
||||
Modulation::Am => SrsModulation::Am,
|
||||
Modulation::Fm => SrsModulation::Fm,
|
||||
Modulation::Intercom => SrsModulation::Intercom,
|
||||
Modulation::Disabled => SrsModulation::Disabled,
|
||||
Modulation::HaveQuick => SrsModulation::HaveQuick,
|
||||
Modulation::Satcom => SrsModulation::Satcom,
|
||||
Modulation::Mids => SrsModulation::Mids,
|
||||
}
|
||||
}
|
||||
}
|
||||
45
src/main.rs
45
src/main.rs
@@ -1,5 +1,6 @@
|
||||
mod braa;
|
||||
mod commands;
|
||||
mod config;
|
||||
mod tripwire;
|
||||
|
||||
use bevy::{
|
||||
@@ -7,44 +8,62 @@ use bevy::{
|
||||
ecs::{
|
||||
entity::Entity,
|
||||
query::{With, Without},
|
||||
system::{Commands, Query},
|
||||
system::{Commands, Query, Res},
|
||||
},
|
||||
};
|
||||
use commands::CommandsPlugin;
|
||||
use config::{Channels, Config};
|
||||
use guardian_core::{
|
||||
components::{Awacs, Callsign},
|
||||
srs::{Modulation, Radio},
|
||||
srs::Radio,
|
||||
DefaultPlugins, TokioResource,
|
||||
};
|
||||
use tokio::runtime::Handle;
|
||||
use tripwire::TripwirePlugin;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let config = Config::load("config.toml".into())?;
|
||||
|
||||
App::new()
|
||||
.insert_resource(config.srs.server)
|
||||
.insert_resource(config.stt)
|
||||
.insert_resource(config.grpc)
|
||||
.insert_resource(config.channels)
|
||||
.insert_resource(TokioResource(Handle::current()))
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(TripwirePlugin)
|
||||
.add_plugins(CommandsPlugin)
|
||||
.add_systems(Update, give_awacs_radio)
|
||||
.run();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn give_awacs_radio(
|
||||
mut commands: Commands,
|
||||
channels: Res<Channels>,
|
||||
units: Query<(Entity, &Callsign), (With<Awacs>, Without<Radio>)>,
|
||||
) {
|
||||
for (ent, callsign) in units.iter() {
|
||||
if callsign.as_str() == "Overlord1-1" {
|
||||
commands
|
||||
.entity(ent)
|
||||
.insert(Radio::new(251000000, Modulation::Am));
|
||||
}
|
||||
let Some(channel) = channels.0.get(callsign.as_str()) else {
|
||||
continue; // not configured for this unit
|
||||
};
|
||||
|
||||
if callsign.as_str() == "Magic1-1" {
|
||||
commands
|
||||
.entity(ent)
|
||||
.insert(Radio::new(266000000, Modulation::Am));
|
||||
}
|
||||
commands.entity(ent).insert(Radio::new(
|
||||
channel.frequency,
|
||||
channel.modulation,
|
||||
channel.voice,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// // Initial player tripwire? get from some db so player doesnt need to redo it everytime
|
||||
// fn add_tripwire(mut commands: Commands, players: Query<Entity, Added<Player>>) {
|
||||
// for ent in players.iter() {
|
||||
// commands.entity(ent).insert(Tripwire {
|
||||
// range: 2.0,
|
||||
// reported: vec![],
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -3,7 +3,7 @@ use bevy::{
|
||||
ecs::{
|
||||
component::Component,
|
||||
entity::Entity,
|
||||
query::{Added, With, Without},
|
||||
query::{With, Without},
|
||||
system::{Commands, Query},
|
||||
},
|
||||
};
|
||||
@@ -23,16 +23,6 @@ impl Plugin for TripwirePlugin {
|
||||
}
|
||||
}
|
||||
|
||||
// this will be on a tripwire command event (from srs), instead of Added<Player>, not everyone may want it; opt-in over opt-out
|
||||
// fn add_tripwire(mut commands: Commands, players: Query<Entity, Added<Player>>) {
|
||||
// for ent in players.iter() {
|
||||
// commands.entity(ent).insert(Tripwire {
|
||||
// range: 2.0,
|
||||
// reported: vec![],
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
fn tripwire(
|
||||
mut commands: Commands,
|
||||
awacs: Query<(Entity, &Callsign, &Radio), (With<Awacs>, With<Blue>, With<Radio>)>,
|
||||
|
||||
Reference in New Issue
Block a user