Config save and load start
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
use std::{ops::RangeInclusive, path::PathBuf, str::FromStr};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tokio::{
|
||||
fs::{File, create_dir},
|
||||
fs::{File, create_dir, read, read_dir},
|
||||
io::{self, AsyncWriteExt},
|
||||
sync::{RwLock, watch},
|
||||
};
|
||||
@@ -9,21 +11,23 @@ use tokio_stream::wrappers::BroadcastStream;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
config::{MinecraftType, MinecraftVersion, StreamSource, Version, stream::InstanceEvent},
|
||||
config::{self, MinecraftType, MinecraftVersion, StreamSource, Version, stream::InstanceEvent},
|
||||
error::{CreationError, ServerError, SubscribeError},
|
||||
instance::InstanceHandle,
|
||||
manifests::vanilla::{VanillaManifestV2, VanillaManifestV2Version, VanillaReleaseManifest},
|
||||
server,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct MineGuardConfig {
|
||||
uuid: Uuid,
|
||||
server_dir: PathBuf,
|
||||
jar_path: PathBuf,
|
||||
mc_version: MinecraftVersion,
|
||||
mc_type: MinecraftType,
|
||||
pub server_dir: PathBuf,
|
||||
pub jar_path: PathBuf,
|
||||
pub mc_version: MinecraftVersion,
|
||||
pub mc_type: MinecraftType,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MineGuardServer {
|
||||
pub handle: RwLock<InstanceHandle>,
|
||||
pub config: RwLock<MineGuardConfig>,
|
||||
@@ -42,6 +46,15 @@ impl MineGuardConfig {
|
||||
}
|
||||
|
||||
impl MineGuardServer {
|
||||
async fn load_cfg_handle(
|
||||
config: MineGuardConfig,
|
||||
handle: InstanceHandle,
|
||||
) -> Result<Self, CreationError> {
|
||||
Ok(Self {
|
||||
config: RwLock::new(config),
|
||||
handle: RwLock::new(handle),
|
||||
})
|
||||
}
|
||||
pub async fn create(
|
||||
mc_version: MinecraftVersion,
|
||||
mc_type: MinecraftType,
|
||||
@@ -62,6 +75,11 @@ impl MineGuardServer {
|
||||
.await
|
||||
.map_err(|_| CreationError::DirectoryError)?;
|
||||
|
||||
let internal_dir = server_root.join(".mineguard");
|
||||
create_dir(internal_dir)
|
||||
.await
|
||||
.map_err(|_| CreationError::DirectoryError)?;
|
||||
|
||||
let mut url = String::new();
|
||||
|
||||
if mc_type == MinecraftType::Vanilla {
|
||||
@@ -162,4 +180,69 @@ impl MineGuardServer {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn write_config(&self) -> Result<(), ServerError> {
|
||||
let config_r = self.config.read().await;
|
||||
let root_path = config_r.server_dir.clone();
|
||||
let config_clone = config_r.clone();
|
||||
drop(config_r);
|
||||
|
||||
let config_path = root_path.join(".mineguard/config.json");
|
||||
|
||||
let json = serde_json::to_vec_pretty(&config_clone).map_err(|_| ServerError::FileIO)?;
|
||||
|
||||
File::create(config_path)
|
||||
.await
|
||||
.map_err(|_| ServerError::FileIO)?
|
||||
.write_all(&json)
|
||||
.await
|
||||
.map_err(|_| ServerError::FileIO)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load(path: &PathBuf) -> Result<Self, CreationError> {
|
||||
let config_path = path.join(".mineguard/config.json");
|
||||
|
||||
let data = read(config_path)
|
||||
.await
|
||||
.map_err(|_| CreationError::DirectoryError)?;
|
||||
|
||||
let config: MineGuardConfig =
|
||||
serde_json::from_slice(&data).map_err(|_| CreationError::CreationError)?;
|
||||
let handle = InstanceHandle::new_with_config(config.clone())
|
||||
.map_err(|_| CreationError::CreationError)?;
|
||||
|
||||
MineGuardServer::load_cfg_handle(config, handle).await
|
||||
}
|
||||
|
||||
pub async fn load_all(path: PathBuf) -> Result<Vec<Self>, CreationError> {
|
||||
let mut dirs = Vec::new();
|
||||
let mut entries = read_dir(path)
|
||||
.await
|
||||
.map_err(|_| CreationError::DirectoryError)?;
|
||||
|
||||
while let Some(entry) = entries
|
||||
.next_entry()
|
||||
.await
|
||||
.map_err(|_| CreationError::DirectoryError)?
|
||||
{
|
||||
let meta = entry
|
||||
.metadata()
|
||||
.await
|
||||
.map_err(|_| CreationError::DirectoryError)?;
|
||||
if meta.is_dir() {
|
||||
dirs.push(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
let mut servers: Vec<Self> = Vec::new();
|
||||
|
||||
for v in dirs {
|
||||
println!("{}", v.to_str().unwrap());
|
||||
servers.push(Self::load(&v).await?);
|
||||
}
|
||||
|
||||
Ok(servers)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user