add default config

This commit is contained in:
shenjack 2025-02-11 22:06:49 +08:00
parent 79b306d089
commit f8cd207923
Signed by: shenjack
GPG Key ID: 7B1134A979775551
6 changed files with 60 additions and 45 deletions

View File

@ -102,7 +102,7 @@ impl BotConfig {
pub fn new_from_cli() -> Self {
// let config_file_path = env::args().nth(1).expect("No config path given");
// -c <config_file_path>
let mut config_file_path = String::new();
let mut config_file_path = "./config.toml".to_string();
let mut args = env::args();
while let Some(arg) = args.next() {
if arg == "-c" {

View File

@ -15,31 +15,31 @@ use crate::{version_str, StopGetter};
/// icalingua 客户端的兼容版本号
pub const ICA_PROTOCOL_VERSION: &str = "2.12.26";
mod status {
use crate::data_struct::ica::all_rooms::Room;
pub use crate::data_struct::ica::online_data::OnlineData;
// mod status {
// use crate::data_struct::ica::all_rooms::Room;
// pub use crate::data_struct::ica::online_data::OnlineData;
#[derive(Debug, Clone)]
pub struct MainStatus {
/// 是否启用 ica
pub enable: bool,
/// qq 是否登录
pub qq_login: bool,
/// 当前已加载的消息数量
pub current_loaded_messages_count: u64,
/// 房间数据
pub rooms: Vec<Room>,
/// 在线数据 (Icalingua 信息)
pub online_status: OnlineData,
}
// #[derive(Debug, Clone)]
// pub struct MainStatus {
// /// 是否启用 ica
// pub enable: bool,
// /// qq 是否登录
// pub qq_login: bool,
// /// 当前已加载的消息数量
// pub current_loaded_messages_count: u64,
// /// 房间数据
// pub rooms: Vec<Room>,
// /// 在线数据 (Icalingua 信息)
// pub online_status: OnlineData,
// }
impl MainStatus {
pub fn update_rooms(&mut self, room: Vec<Room>) { self.rooms = room; }
pub fn update_online_status(&mut self, status: OnlineData) { self.online_status = status; }
}
}
// impl MainStatus {
// pub fn update_rooms(&mut self, room: Vec<Room>) { self.rooms = room; }
// pub fn update_online_status(&mut self, status: OnlineData) { self.online_status = status; }
// }
// }
static ICA_STATUS: OnceLock<status::MainStatus> = OnceLock::new();
// static ICA_STATUS: OnceLock<status::MainStatus> = OnceLock::new();
pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientResult<(), IcaError> {
let span = span!(Level::INFO, "Icalingua Client");

View File

@ -101,10 +101,41 @@ macro_rules! async_any_callback_with_state {
}};
}
const CLI_HELP_MSG: &str = r#"{VERSION}
-d
debug
-t
trace
-h
-c <config_file_path>
"#;
fn main() -> anyhow::Result<()> {
let start_up_time = SystemTime::now();
STARTUP_TIME.set(start_up_time).expect("WTF, why did you panic?");
// -d -> debug
// none -> info
let args = std::env::args();
let args = args.collect::<Vec<String>>();
if args.contains(&"-h".to_string()) {
println!("{}", CLI_HELP_MSG.replace("{VERSION}", version_str().as_str()));
return Ok(());
}
let level = {
if args.contains(&"-d".to_string()) {
Level::DEBUG
} else if args.contains(&"-t".to_string()) {
Level::TRACE
} else {
Level::INFO
}
};
tracing_subscriber::fmt().with_max_level(level).init();
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_name("shenbot-rs")
@ -115,21 +146,6 @@ fn main() -> anyhow::Result<()> {
}
async fn inner_main() -> anyhow::Result<()> {
// -d -> debug
// none -> info
let level = {
let args = std::env::args();
let args = args.collect::<Vec<String>>();
if args.contains(&"-d".to_string()) {
Level::DEBUG
} else if args.contains(&"-t".to_string()) {
Level::TRACE
} else {
Level::INFO
}
};
tracing_subscriber::fmt().with_max_level(level).init();
let span = span!(Level::INFO, "Shenbot Main");
let _enter = span.enter();

View File

@ -1,7 +1,4 @@
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use std::{path::Path, str::FromStr};
use toml_edit::{value, DocumentMut, Key, Table, TomlError, Value};
use tracing::{event, Level};
@ -46,7 +43,7 @@ impl PluginConfigFile {
pub fn default_init() -> anyhow::Result<Self> {
let config_path = MainStatus::global_config().py().config_path.clone();
let path = Path::new(&config_path);
Self::from_config_path(&path)
Self::from_config_path(path)
}
pub fn reload_from_default(&mut self) -> bool {
@ -165,7 +162,7 @@ impl PluginConfigFile {
pub fn write_to_default(&self) -> Result<(), std::io::Error> {
let config_path = MainStatus::global_config().py().config_path.clone();
let config_path = Path::new(&config_path);
self.write_to_file(&config_path)
self.write_to_file(config_path)
}
pub fn write_to_file(&self, path: &Path) -> Result<(), std::io::Error> {

View File

@ -439,7 +439,8 @@ pub fn init_py() {
#[cfg(target_os = "windows")]
use std::os::windows::ffi::OsStrExt;
let wide_path = OsStr::new(&virtual_env).encode_wide().chain(Some(0)).collect::<Vec<u16>>();
let wide_path =
OsStr::new(&virtual_env).encode_wide().chain(Some(0)).collect::<Vec<u16>>();
// 设置 prefix 和 exec_prefix
pyo3::ffi::PyConfig_SetString(

View File

@ -4,6 +4,7 @@
- 现在支持通过读取环境变量里的 `VIRTUAL_ENV` 来获取虚拟环境路径
- 用于在虚拟环境中运行插件
- 现在加入了默认的配置文件路径 `./config.toml`
## 0.8.1