-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathglobal.rs
More file actions
168 lines (141 loc) · 4.66 KB
/
global.rs
File metadata and controls
168 lines (141 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::{
collections::HashMap,
fs::File,
io::{ErrorKind, Read, Seek, Write},
path::PathBuf,
time::Duration,
};
use anyhow::{bail, Context, Result};
use azalea::{
app::{App, Plugin},
prelude::*,
protocol::address::ServerAddr,
};
use serde::{Deserialize, Serialize};
use serde_tuple::{Deserialize_tuple as DeserializeTuple, Serialize_tuple as SerializeTuple};
use serde_with::DurationSeconds;
use smart_default::SmartDefault;
use uuid::Uuid;
/// Global Swarm Settings that apply to every account
pub struct GlobalSettingsPlugin;
impl Plugin for GlobalSettingsPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(GlobalSettings::load().expect("Failed to load global settings"));
}
}
#[serde_as]
#[derive(Clone, Deserialize, Serialize, SmartDefault, Resource)]
#[serde(default)]
pub struct GlobalSettings {
/// Chat command prefix.
#[default("!")]
pub command_prefix: String,
/// Command cooldown in seconds.
#[default(Duration::from_secs(10))]
#[serde_as(as = "DurationSeconds")]
pub command_cooldown: Duration,
/// Discord client token for commands and events. (Optional)
pub discord_token: String,
/// Minecraft server ender pearl view distance in blocks.
/// Better to under-estimate than to over-estimate.
#[default(60)] /* Vanilla/Spigot/Paper/Folia Default */
pub pearl_view_distance: i32,
/// Minecraft server address.
#[default(ServerAddr{
host: str!("play.vengeancecraft.net"),
port: 25565
})]
pub server_address: ServerAddr,
/// `ViaProxy` server version. (Optional)
pub server_version: String,
/// Automatically whitelist players that enter visual range.
#[default(false)]
pub whitelist_in_range: bool,
/// Disable commands for non-whitelisted players.
#[default(false)]
pub whitelist_only: bool,
/// API Server for local integrations.
#[cfg(feature = "api")]
#[serde(rename = "api_server")]
pub http_api: ApiServer,
/// Chat encryption using the NCR (No Chat Reports) mod.
#[serde(rename = "chat_encryption")]
pub chat: ChatEncryption,
/// Minecraft accounts with their linked Discord ID and API Password.
pub users: HashMap<Uuid, User>,
}
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize, SmartDefault)]
#[serde(default)]
pub struct ApiServer {
#[default(false)]
pub enabled: bool,
/// API Server bind address. (default local only & random port)
#[default("127.0.0.1:0")]
pub bind_addr: String,
}
#[derive(Clone, Eq, PartialEq, Deserialize, Serialize, SmartDefault)]
#[serde(default)]
pub struct ChatEncryption {
/// Encryption key (default is public)
#[default("blfrngArk3chG6wzncOZ5A==")]
pub key: String,
/// Encryption response mode. (`OnDemand`, `Always`, or `Never`)
#[default(EncryptionMode::OnDemand)]
pub mode: EncryptionMode,
}
#[serde_as]
#[derive(Clone, Default, Eq, PartialEq, DeserializeTuple, SerializeTuple)]
pub struct User {
pub discord_id: String,
pub api_password: String,
}
#[derive(Clone, Default, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum EncryptionMode {
#[default]
OnDemand,
Always,
Never,
}
impl GlobalSettings {
/// # Errors
/// Will return `Err` if `std::env::current_exe` or `std::env::current_dir` fails.
pub fn path() -> Result<PathBuf> {
let path = if cfg!(debug_assertions) {
let path = std::env::current_exe()?;
path.parent().context("None")?.to_path_buf()
} else {
std::env::current_dir()?
};
Ok(path.join("global-settings.toml"))
}
/// # Errors
/// Will return `Err` if `File::open`, `toml::to_string_pretty`, or `File::write_all` fails.
pub fn load() -> Result<Self> {
let path = Self::path()?;
match File::open(&path) {
Err(error) if error.kind() == ErrorKind::NotFound => Ok(Self::default()),
Err(error) => bail!(error),
Ok(mut file) => {
let mut text = String::new();
file.read_to_string(&mut text)?;
file.rewind()?;
Ok(toml::from_str(&text)?)
}
}
}
/// # Errors
/// Will return `Err` if `File::open`, `File::read_to_string`, `File::rewind`, or `toml::from_str` fails.
pub fn save(&self) -> Result<()> {
let path = Self::path()?;
let mut file = File::options()
.write(true)
.create(true)
.truncate(true)
.open(&path)?;
let text = toml::to_string(&self)?;
let buf = text.as_bytes();
file.write_all(buf)?;
Ok(())
}
}