引入新的配置项

This commit is contained in:
2026-02-03 22:19:47 +08:00
parent 55c1e49142
commit 54b86a44e1
6 changed files with 287 additions and 17 deletions

116
Cargo.lock generated
View File

@@ -23,6 +23,7 @@ dependencies = [
"reqwest", "reqwest",
"serde", "serde",
"spin", "spin",
"sysinfo",
"tokio", "tokio",
"tokio-tungstenite", "tokio-tungstenite",
"toml", "toml",
@@ -1005,6 +1006,15 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "ntapi"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c70f219e21142367c70c0b30c6a9e3a14d55b4d12a204d897fbec83a0363f081"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.19" version = "0.2.19"
@@ -1014,6 +1024,25 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "objc2-core-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags",
]
[[package]]
name = "objc2-io-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.21.3" version = "1.21.3"
@@ -1519,6 +1548,20 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "sysinfo"
version = "0.38.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe840c5b1afe259a5657392a4dbb74473a14c8db999c3ec2f4ae812e028a94da"
dependencies = [
"libc",
"memchr",
"ntapi",
"objc2-core-foundation",
"objc2-io-kit",
"windows",
]
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.24.0" version = "3.24.0"
@@ -1928,6 +1971,22 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]] [[package]]
name = "winapi-util" name = "winapi-util"
version = "0.1.11" version = "0.1.11"
@@ -1937,6 +1996,33 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
dependencies = [
"windows-collections",
"windows-core",
"windows-future",
"windows-numerics",
]
[[package]]
name = "windows-collections"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610"
dependencies = [
"windows-core",
]
[[package]] [[package]]
name = "windows-core" name = "windows-core"
version = "0.62.2" version = "0.62.2"
@@ -1950,6 +2036,17 @@ dependencies = [
"windows-strings", "windows-strings",
] ]
[[package]]
name = "windows-future"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
dependencies = [
"windows-core",
"windows-link",
"windows-threading",
]
[[package]] [[package]]
name = "windows-implement" name = "windows-implement"
version = "0.60.2" version = "0.60.2"
@@ -1978,6 +2075,16 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-numerics"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
dependencies = [
"windows-core",
"windows-link",
]
[[package]] [[package]]
name = "windows-result" name = "windows-result"
version = "0.4.1" version = "0.4.1"
@@ -2031,6 +2138,15 @@ dependencies = [
"windows_x86_64_msvc", "windows_x86_64_msvc",
] ]
[[package]]
name = "windows-threading"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
dependencies = [
"windows-link",
]
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.53.1" version = "0.53.1"

View File

@@ -33,6 +33,7 @@ tokio-tungstenite = "0.28.0"
bytes = "1.11.0" bytes = "1.11.0"
spin = "0.10.0" spin = "0.10.0"
chardetng = "0.1.17" chardetng = "0.1.17"
sysinfo = "0.38.0"
[build-dependencies] [build-dependencies]
cxx-build = "1.0.192" cxx-build = "1.0.192"

View File

@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
fn main() { fn main() {
cxx_build::bridges(["src/lib.rs", "src/config.rs"]) cxx_build::bridges(["src/lib.rs", "src/config.rs", "src/login.rs"])
.std("c++20") .std("c++20")
.compile("dnf_utils"); .compile("dnf_utils");
println!("cargo:rerun-if-changed=src/lib.rs"); println!("cargo:rerun-if-changed=src/lib.rs");
@@ -29,7 +29,8 @@ fn main() {
prost_build::Config::new() prost_build::Config::new()
.out_dir(out_dir) .out_dir(out_dir)
.compile_protos(&proto_list, &["proto/".to_string()]).unwrap(); .compile_protos(&proto_list, &["proto/".to_string()])
.unwrap();
//tonic_prost_build::configure() //tonic_prost_build::configure()
// .out_dir(out_dir) // .out_dir(out_dir)
@@ -51,7 +52,6 @@ fn main() {
).unwrap(); ).unwrap();
} }
fn get_filenames(dir: &str) -> Result<Vec<String>, std::io::Error> { fn get_filenames(dir: &str) -> Result<Vec<String>, std::io::Error> {
println!("cargo:rerun-if-changed=proto"); println!("cargo:rerun-if-changed=proto");
let entries = fs::read_dir(Path::new(dir))?; let entries = fs::read_dir(Path::new(dir))?;

View File

@@ -40,6 +40,14 @@ mod ffi {
fn is_decompose(cfg: &Box<Config>) -> bool; fn is_decompose(cfg: &Box<Config>) -> bool;
fn set_decompose(cfg: &mut Box<Config>, value: bool); fn set_decompose(cfg: &mut Box<Config>, value: bool);
/// 倍攻
fn is_attack_power(cfg: &Box<Config>) -> bool;
fn set_attack_power(cfg: &mut Box<Config>, value: bool);
/// 装备变换
fn is_equip_transform(cfg: &Box<Config>) -> bool;
fn set_equip_transform(cfg: &mut Box<Config>, value: bool);
/// 从配置文件中读取一个配置项 /// 从配置文件中读取一个配置项
fn get_config_value( fn get_config_value(
path: &CxxString, path: &CxxString,
@@ -73,17 +81,23 @@ pub struct Config {
is_forest: bool, is_forest: bool,
/// 分解 /// 分解
is_decompose: bool, is_decompose: bool,
/// 倍攻
is_attack_power: bool,
/// 装备变换
is_equip_transform: bool,
} }
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
is_daily_missions: false, is_daily_missions: true,
is_archipelago: false, is_archipelago: true,
is_spiral_abyss: false, is_spiral_abyss: true,
is_demonic_energy: false, is_demonic_energy: true,
is_forest: false, is_forest: true,
is_decompose: false, is_decompose: true,
is_attack_power: true,
is_equip_transform: true,
} }
} }
} }
@@ -94,7 +108,6 @@ impl Config {
// 你也可以加一个总注释、版本号等 // 你也可以加一个总注释、版本号等
format!( format!(
r#"# 自动生成的配置文件 r#"# 自动生成的配置文件
# 实现可能修改配置存储结构请不要使用程序化工具修改此配置文件如果非要这么做请链接utils.lib后使用其提供的接口。
# 做不做每日任务 # 做不做每日任务
is_daily_missions = {is_daily_missions} is_daily_missions = {is_daily_missions}
@@ -113,6 +126,12 @@ is_forest = {is_forest}
# 分解 # 分解
is_decompose = {is_decompose} is_decompose = {is_decompose}
# 倍攻
is_attack_power = {is_attack_power}
# 装备变换
is_equip_transform = {is_equip_transform}
"#, "#,
is_daily_missions = self.is_daily_missions, is_daily_missions = self.is_daily_missions,
is_archipelago = self.is_archipelago, is_archipelago = self.is_archipelago,
@@ -120,15 +139,17 @@ is_decompose = {is_decompose}
is_demonic_energy = self.is_demonic_energy, is_demonic_energy = self.is_demonic_energy,
is_forest = self.is_forest, is_forest = self.is_forest,
is_decompose = self.is_decompose, is_decompose = self.is_decompose,
is_attack_power = self.is_attack_power,
is_equip_transform = self.is_equip_transform,
) )
} }
pub fn save_with_comments(&self, path: impl AsRef<Path>) -> io::Result<()> { pub fn save_with_comments(&self, path: impl AsRef<Path>) -> io::Result<()> {
/*let path = path.as_ref(); let path = path.as_ref();
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?; fs::create_dir_all(parent)?;
} }
fs::write(path, self.to_toml_with_comments())*/ fs::write(path, self.to_toml_with_comments())?;
Ok(()) Ok(())
} }
@@ -137,7 +158,7 @@ is_decompose = {is_decompose}
/// - toml 缺字段:会自动用 Default 补齐(因为 #[serde(default)] /// - toml 缺字段:会自动用 Default 补齐(因为 #[serde(default)]
/// - 可选:如果你想“读取后把缺的字段补齐回写”,这里也做了回写 /// - 可选:如果你想“读取后把缺的字段补齐回写”,这里也做了回写
pub fn load_or_create(path: impl AsRef<Path>) -> Result<Self, Box<dyn std::error::Error>> { pub fn load_or_create(path: impl AsRef<Path>) -> Result<Self, Box<dyn std::error::Error>> {
/*let path = path.as_ref(); let path = path.as_ref();
if !path.exists() { if !path.exists() {
let cfg = Self::default(); let cfg = Self::default();
@@ -150,15 +171,15 @@ is_decompose = {is_decompose}
// 可选:自动回写补全(比如用户旧版配置少字段时,补齐并带注释写回去) // 可选:自动回写补全(比如用户旧版配置少字段时,补齐并带注释写回去)
// 如果你不想覆盖用户文件,把下面两行删掉即可。 // 如果你不想覆盖用户文件,把下面两行删掉即可。
cfg.save_with_comments(path)?;*/ cfg.save_with_comments(path)?;
let cfg = Config { /*let cfg = Config {
is_daily_missions: true, is_daily_missions: true,
is_archipelago: true, is_archipelago: true,
is_spiral_abyss: false, is_spiral_abyss: false,
is_demonic_energy: true, is_demonic_energy: true,
is_forest: true, is_forest: true,
is_decompose: true, is_decompose: true,
}; };*/
Ok(cfg) Ok(cfg)
} }
@@ -227,6 +248,20 @@ pub(crate) fn set_decompose(this: &mut Box<Config>, flag: bool) {
this.is_decompose = flag; this.is_decompose = flag;
} }
fn is_attack_power(cfg: &Box<Config>) -> bool {
cfg.is_attack_power
}
fn set_attack_power(cfg: &mut Box<Config>, value: bool) {
cfg.is_attack_power = value;
}
fn is_equip_transform(cfg: &Box<Config>) -> bool {
cfg.is_equip_transform
}
fn set_equip_transform(cfg: &mut Box<Config>, value: bool) {
cfg.is_equip_transform = value;
}
pub(crate) fn set_config_value( pub(crate) fn set_config_value(
path: &CxxString, path: &CxxString,
path_name: &CxxString, path_name: &CxxString,

View File

@@ -7,6 +7,7 @@ use encoding_rs::Encoding;
use futures_util::SinkExt; use futures_util::SinkExt;
use prost::Message as WebMessage; use prost::Message as WebMessage;
use spin::Mutex; use spin::Mutex;
use std::ffi::CString;
use std::sync::OnceLock; use std::sync::OnceLock;
use std::sync::mpsc; use std::sync::mpsc;
use tokio::net::TcpStream; use tokio::net::TcpStream;
@@ -17,6 +18,7 @@ static LOGGER_SENDER: OnceLock<mpsc::Sender<Starpoles>> = OnceLock::new();
mod config; mod config;
mod log; mod log;
mod login;
mod network; mod network;
pub mod protobuf; pub mod protobuf;
pub mod utils; pub mod utils;

116
src/login.rs Normal file
View File

@@ -0,0 +1,116 @@
use crate::log::get_current_dir;
use crate::utils::cxx_string_to_string;
use cxx::CxxString;
use log::info;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{fs, io};
use sysinfo::{ProcessesToUpdate, System};
use toml::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
#[derive(Default)]
pub struct User {
username: String,
password: String,
server: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
#[derive(Default)]
pub struct Login {
/// 协议登录exe路径
login_exe: String,
game_path: String,
list: Vec<User>,
}
#[cxx::bridge]
mod ffi {
extern "Rust" {
type Login;
fn load_login_config() -> Result<Box<Login>>;
fn get_login_size(cfg: &Box<Login>) -> usize;
fn get_login_exe(cfg: &Box<Login>) -> String;
fn login_game(cfg: &Box<Login>, index: usize) -> bool;
fn get_username(cfg: &Box<Login>, index: usize) -> String;
fn get_password(cfg: &Box<Login>, index: usize) -> String;
fn get_server(cfg: &Box<Login>, index: usize) -> String;
fn is_process_running() -> bool;
}
}
pub(crate) fn load_login_config() -> Result<Box<Login>, Box<dyn std::error::Error>> {
let path = PathBuf::from("C:\\Windows\\Starpoles\\login.toml"); /* get_current_dir().join("config").join("login.toml");*/
if !path.exists() {
fs::create_dir_all(path.parent().unwrap())?;
let cfg = Login::default();
fs::write(path, toml::to_string(&cfg).unwrap())?;
return Ok(Box::new(cfg));
}
let text = fs::read_to_string(path)?;
let cfg: Login = toml::from_str(&text)?;
Ok(Box::new(cfg))
}
pub(crate) fn get_login_size(p0: &Box<Login>) -> usize {
p0.list.len()
}
pub(crate) fn get_login_exe(p0: &Box<Login>) -> String {
p0.login_exe.clone()
}
pub(crate) fn get_username(p0: &Box<Login>, p1: usize) -> String {
p0.list[p1].username.clone()
}
pub(crate) fn get_password(p0: &Box<Login>, p1: usize) -> String {
p0.list[p1].password.clone()
}
pub(crate) fn get_server(p0: &Box<Login>, p1: usize) -> String {
p0.list[p1].server.clone()
}
pub(crate) fn login_game(p0: &Box<Login>, p1: usize) -> bool {
info!(
"协议登录:{}user:{}password:{},server:{}",
p0.login_exe, p0.list[p1].username, p0.list[p1].password, p0.list[p1].server
);
let status = Command::new(p0.login_exe.clone())
.args([
"-g",
&p0.game_path,
"-u",
&p0.list[p1].username,
"-p",
&p0.list[p1].password,
"-a",
&p0.list[p1].server,
])
.status()
.unwrap();
status.success()
}
fn is_process_running() -> bool {
let mut sys = System::new();
sys.refresh_processes(ProcessesToUpdate::All, true);
sys.processes()
.values()
.any(|p| p.name().eq_ignore_ascii_case("dnf.exe"))
}