use std::sync::
{
RwLock,
atomic::{ AtomicBool, Ordering },
};
#[cfg(feature = "client_base")]
use std::sync::atomic::AtomicUsize;
#[cfg(feature = "client_voice")]
use std::
{
sync::Mutex,
collections::BTreeSet,
};
#[cfg(feature = "client_base")]
use crate::consts::SharedKeys;
static SERVER_USERNAME: RwLock<String> = RwLock::new(String::new());
#[cfg(feature = "server")]
static VOICE_CHAT: AtomicBool = AtomicBool::new(false);
#[cfg(feature = "client_base")]
static KEYS: RwLock<Option<SharedKeys>> = RwLock::new(None);
#[cfg(feature = "client_base")]
static ASKING_PASSWORD: AtomicBool = AtomicBool::new(false);
#[cfg(feature = "client_base")]
static EXTRA_SPACE: AtomicBool = AtomicBool::new(false);
#[cfg(feature = "client_base")]
static SENDING_MESSAGES: AtomicBool = AtomicBool::new(false);
#[cfg(feature = "client_base")]
static SEQ: AtomicUsize = AtomicUsize::new(0);
#[cfg(feature = "client_base")]
static SERVER_SEQ: AtomicUsize = AtomicUsize::new(0);
#[cfg(feature = "client_base")]
static SERVER_ADDRESS: RwLock<String> = RwLock::new(String::new());
#[cfg(feature = "client_base")]
static OBFUSCATION_KEY: RwLock<[u8; 32]> = RwLock::new([0; 32]);
#[cfg(feature = "client_base")]
static CHANNEL: RwLock<String> = RwLock::new(String::new());
#[cfg(feature = "client_voice")]
static MUTE: AtomicBool = AtomicBool::new(false); #[cfg(feature = "client_voice")]
static MUTED: Mutex<BTreeSet<usize>> = Mutex::new(BTreeSet::new());
#[cfg(feature = "client_base")]
static SOCKS5: AtomicBool = AtomicBool::new(false);
pub fn get_server_username() -> String {
SERVER_USERNAME.read().unwrap().to_owned()
}
pub fn set_server_username(username: &str) {
*SERVER_USERNAME.write().unwrap() = username.to_owned();
}
#[cfg(feature = "server")]
pub fn enable_voice_chat() {
VOICE_CHAT.store(true, Ordering::Relaxed);
}
#[cfg(feature = "server")]
pub fn voice_chat_enabled() -> bool {
VOICE_CHAT.load(Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn set_keys(keys: SharedKeys) {
let mut shared_key = KEYS.write().unwrap();
*shared_key = Some(keys);
}
#[cfg(feature = "client_base")]
pub fn get_keys() -> Option<SharedKeys> {
let shared_key = KEYS.read().unwrap();
shared_key.clone()
}
#[cfg(feature = "client_base")]
pub fn set_asking_password(value: bool) {
ASKING_PASSWORD.store(value, Ordering::Relaxed);
}
#[cfg(feature = "client_base")]
pub fn get_asking_password() -> bool {
ASKING_PASSWORD.load(Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn set_extra_space(value: bool) {
EXTRA_SPACE.store(value, Ordering::Relaxed);
}
#[cfg(feature = "client_base")]
pub fn get_extra_space() -> bool {
EXTRA_SPACE.load(Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn get_sending_messages() -> bool {
SENDING_MESSAGES.load(Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn set_sending_messages(value: bool) {
SENDING_MESSAGES.store(value, Ordering::Relaxed);
}
#[cfg(feature = "client_base")]
pub fn get_seq() -> usize {
SEQ.load(Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn set_seq(value: usize) {
SEQ.store(value, Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn get_server_seq() -> usize {
SERVER_SEQ.load(Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn set_server_seq(value: usize) {
SERVER_SEQ.store(value, Ordering::Relaxed)
}
#[cfg(feature = "client_base")]
pub fn get_server_address() -> String {
SERVER_ADDRESS.read().unwrap().to_owned()
}
#[cfg(feature = "client_base")]
pub fn set_server_address(address: &str) {
*SERVER_ADDRESS.write().unwrap() = address.to_owned();
}
#[cfg(feature = "client_base")]
pub fn get_obfuscation_key() -> [u8; 32] {
*OBFUSCATION_KEY.read().unwrap()
}
#[cfg(feature = "client_base")]
pub fn set_obfuscation_key(key: &[u8; 32]) {
*OBFUSCATION_KEY.write().unwrap() = *key;
}
#[cfg(feature = "client_base")]
pub fn get_channel() -> String {
CHANNEL.read().unwrap().clone()
}
#[cfg(feature = "client_base")]
pub fn set_channel(channel: String) {
*CHANNEL.write().unwrap() = channel;
}
#[cfg(feature = "client_voice")]
pub fn toggle_mute(id: Option<usize>) -> bool {
if let Some(id) = id {
let mut muted = MUTED.lock().unwrap();
if muted.remove(&id)
{
false
} else
{
muted.insert(id);
true
}
} else {
!MUTE.fetch_xor(true, Ordering::Relaxed)
}
}
#[cfg(feature = "client_voice")]
pub fn is_muted(id: Option<usize>) -> bool {
if let Some(id) = id
{
MUTED.lock().unwrap().contains(&id)
} else
{
MUTE.load(Ordering::Relaxed)
}
}
#[cfg(feature = "client_base")]
pub fn enable_socks5() {
SOCKS5.store(true, Ordering::Relaxed);
}
#[cfg(feature = "client_base")]
pub fn socks5_enabled() -> bool {
SOCKS5.load(Ordering::Relaxed)
}