mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-21 19:25:56 +00:00
Merge remote-tracking branch 'origin/codex/client-only-mode' into codex/release-all-pending
This commit is contained in:
commit
7c096a4844
19 files changed
+412
-50
No files matched your search
@@ -180,7 +180,7 @@ fn backup_dir(meta: &Path) -> PathBuf {
|
||||
meta.join("backups").join("client")
|
||||
}
|
||||
|
||||
fn host_ok(h: &str) -> bool {
|
||||
pub fn host_ok(h: &str) -> bool {
|
||||
!h.is_empty() && h.len() <= 253 && h.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':')) && !h.starts_with('-')
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ pub mod backup;
|
||||
pub mod cleanbase;
|
||||
pub mod companions;
|
||||
pub mod client;
|
||||
pub mod remote_client;
|
||||
pub mod clientdl;
|
||||
pub mod config;
|
||||
pub mod console;
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
//! A player's client connection, independent of any local server installation.
|
||||
use std::path::{Path, PathBuf};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::{client, fsx, Error, Result};
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct Profile {
|
||||
pub host: String,
|
||||
pub client_path: Option<String>,
|
||||
}
|
||||
|
||||
pub fn load(dir: &Path) -> Result<Profile> {
|
||||
match std::fs::read(dir.join("connection.json")) {
|
||||
Ok(bytes) => Ok(serde_json::from_slice(&bytes)?),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Profile::default()),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(dir: &Path, profile: &Profile) -> Result<()> {
|
||||
if !profile.host.is_empty() && !client::host_ok(&profile.host) {
|
||||
return Err(Error::Invalid("Enter a server IP address or hostname, without a URL or port.".into()));
|
||||
}
|
||||
if let Some(path) = &profile.client_path {
|
||||
let client = PathBuf::from(path);
|
||||
if client::detect(&client, None).is_none() {
|
||||
return Err(Error::Invalid("This folder does not look like a game client (it needs Data and the game executable).".into()));
|
||||
}
|
||||
if client::is_running(&client) {
|
||||
return Err(Error::Invalid("Close the game before changing its connection.".into()));
|
||||
}
|
||||
if !profile.host.is_empty() { client::set_realmlist(&client, dir, &profile.host)?; }
|
||||
}
|
||||
fsx::atomic_write_json(&dir.join("connection.json"), profile)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn connection_needs_no_server_and_survives_restart() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let profile = Profile { host: "192.168.1.10".into(), client_path: None };
|
||||
save(dir.path(), &profile).unwrap();
|
||||
assert_eq!(load(dir.path()).unwrap().host, profile.host);
|
||||
assert!(!dir.path().join("install.json").exists());
|
||||
}
|
||||
#[test]
|
||||
fn linking_client_sets_host_without_touching_other_game_settings() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let game = dir.path().join("game");
|
||||
std::fs::create_dir_all(game.join("Data/enUS")).unwrap();
|
||||
std::fs::write(game.join("Wow.exe"), b"fixture").unwrap();
|
||||
std::fs::write(game.join("Data/enUS/realmlist.wtf"), "set realmlist 127.0.0.1\n").unwrap();
|
||||
std::fs::create_dir_all(game.join("WTF")).unwrap();
|
||||
std::fs::write(game.join("WTF/Config.wtf"), "SET realmName \"My realm\"\n").unwrap();
|
||||
let profile = Profile { host: "192.168.1.10".into(), client_path: Some(game.to_string_lossy().into()) };
|
||||
save(dir.path(), &profile).unwrap();
|
||||
assert!(std::fs::read_to_string(game.join("Data/enUS/realmlist.wtf")).unwrap().contains(&profile.host));
|
||||
assert_eq!(std::fs::read_to_string(game.join("WTF/Config.wtf")).unwrap(), "SET realmName \"My realm\"\n");
|
||||
assert!(save(dir.path(), &Profile { host: "bad\nset realmlist other".into(), ..profile }).is_err());
|
||||
assert_eq!(load(dir.path()).unwrap().host, "192.168.1.10");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user