Initial auto create impl

This commit is contained in:
2025-12-07 20:31:05 +01:00
parent 6b2757a52f
commit 668870b92e
6 changed files with 167 additions and 20 deletions

View File

@@ -1,12 +1,18 @@
use std::{path::PathBuf, str::FromStr};
use std::{ops::RangeInclusive, path::PathBuf, str::FromStr};
use tokio::sync::RwLock;
use tokio::{
fs::{File, create_dir},
io::{self, AsyncWriteExt},
sync::RwLock,
};
use uuid::Uuid;
use crate::{
config::{MinecraftType, MinecraftVersion, Version},
error::CreationError,
instance::InstanceHandle,
manifests::vanilla::{VanillaManifestV2, VanillaManifestV2Version, VanillaReleaseManifest},
server,
};
pub struct MineGuardConfig {
@@ -18,8 +24,8 @@ pub struct MineGuardConfig {
}
pub struct MineGuardServer {
handle: RwLock<InstanceHandle>,
config: RwLock<MineGuardConfig>,
pub handle: RwLock<InstanceHandle>,
pub config: RwLock<MineGuardConfig>,
}
impl MineGuardConfig {
@@ -35,7 +41,7 @@ impl MineGuardConfig {
}
impl MineGuardServer {
pub fn create(
pub async fn create(
mc_version: MinecraftVersion,
mc_type: MinecraftType,
directory: PathBuf,
@@ -44,6 +50,74 @@ impl MineGuardServer {
return Err(CreationError::DirectoryError);
}
todo!()
let uuid = Uuid::new_v4();
let server_root = directory.join(uuid.to_string());
let jar_path_rel =
PathBuf::from_str("server.jar").map_err(|_| CreationError::DirectoryError)?;
let jar_path_full = server_root.join(jar_path_rel.clone());
create_dir(server_root.clone())
.await
.map_err(|_| CreationError::DirectoryError)?;
let mut url = String::new();
if mc_type == MinecraftType::Vanilla {
let vanilla_manifest = VanillaManifestV2::load()
.await
.map_err(|_| CreationError::ManifestError)?;
let find_ver = match vanilla_manifest
.find(mc_version.clone())
.map_err(|_| CreationError::ManifestError)?
{
Some(val) => val,
None => return Err(CreationError::VersionError),
};
let release_manifest = VanillaReleaseManifest::load(find_ver)
.await
.map_err(|_| CreationError::ManifestError)?;
url = release_manifest.server_url();
}
let resp = reqwest::get(url)
.await
.map_err(|_| CreationError::NetworkError)?;
let mut body = resp
.bytes()
.await
.map_err(|_| CreationError::NetworkError)?;
let mut out = File::create(jar_path_full)
.await
.map_err(|_| CreationError::DirectoryError)?;
out.write_all_buf(&mut body)
.await
.map_err(|_| CreationError::DirectoryError)?;
let config = MineGuardConfig {
uuid: uuid,
server_dir: server_root,
jar_path: jar_path_rel,
mc_version: mc_version,
mc_type: mc_type,
};
let handle = InstanceHandle::new_with_params(
config.server_dir.clone(),
config.jar_path.clone(),
config.mc_version.clone(),
config.mc_type.clone(),
)
.map_err(|_| CreationError::CreationError)?;
let server = MineGuardServer {
config: RwLock::new(config),
handle: RwLock::new(handle),
};
Ok(server)
}
}