cache validation

This commit is contained in:
Novattz
2026-01-17 17:57:49 +01:00
parent 40b9ec9b01
commit 7a07399946
2 changed files with 149 additions and 37 deletions

View File

@@ -2,9 +2,10 @@ mod storage;
mod version; mod version;
pub use storage::{ pub use storage::{
get_creamlinux_version_dir, get_smokeapi_version_dir, is_cache_initialized, get_creamlinux_version_dir, get_smokeapi_version_dir,
list_creamlinux_files, list_smokeapi_dlls, read_versions, update_creamlinux_version, list_creamlinux_files, list_smokeapi_files, read_versions,
update_smokeapi_version, update_creamlinux_version, update_smokeapi_version, validate_smokeapi_cache,
validate_creamlinux_cache,
}; };
pub use version::{ pub use version::{
@@ -22,39 +23,87 @@ use std::collections::HashMap;
pub async fn initialize_cache() -> Result<(), String> { pub async fn initialize_cache() -> Result<(), String> {
info!("Initializing cache..."); info!("Initializing cache...");
// Check if cache is already initialized let versions = read_versions()?;
if is_cache_initialized()? { let mut needs_smokeapi = false;
info!("Cache already initialized"); let mut needs_creamlinux = false;
return Ok(());
// Check if SmokeAPI is properly cached
if versions.smokeapi.latest.is_empty() {
info!("No SmokeAPI version in manifest");
needs_smokeapi = true
} else {
// Validate that all files exist
match validate_smokeapi_cache(&versions.smokeapi.latest) {
Ok(true) => {
info!("SmokeAPI cache validated successfully");
}
Ok(false) => {
info!("SmokeAPI cache incomplete, re-downloading");
needs_smokeapi = true;
}
Err(e) => {
warn!("Failed to validate SmokeAPI cache: {}, re-downloading", e);
needs_smokeapi = true;
}
}
} }
info!("Cache not initialized, downloading unlockers..."); // Check if CreamLinux is properly cached
if versions.creamlinux.latest.is_empty() {
info!("No CreamLinux version in manifest");
needs_creamlinux = true;
} else {
match validate_creamlinux_cache(&versions.creamlinux.latest) {
Ok(true) => {
info!("CreamLinux cache validated successfully");
}
Ok(false) => {
info!("CreamLinux cache incomplete, re-downloading");
needs_creamlinux = true;
}
Err(e) => {
warn!("Failed to validate CreamLinux cache: {}, re-downloading", e);
needs_creamlinux = true;
}
}
}
// Download SmokeAPI // Download SmokeAPI
match SmokeAPI::download_to_cache().await { if needs_smokeapi {
Ok(version) => { info!("Downloading SmokeAPI...");
info!("Downloaded SmokeAPI version: {}", version); match SmokeAPI::download_to_cache().await {
update_smokeapi_version(&version)?; Ok(version) => {
} info!("Downloaded SmokeAPI version: {}", version);
Err(e) => { update_smokeapi_version(&version)?;
error!("Failed to download SmokeAPI: {}", e); }
return Err(format!("Failed to download SmokeAPI: {}", e)); Err(e) => {
error!("Failed to download SmokeAPI: {}", e);
return Err(format!("Failed to download SmokeAPI: {}", e));
}
} }
} }
// Download CreamLinux // Download CreamLinux
match CreamLinux::download_to_cache().await { if needs_creamlinux {
Ok(version) => { info!("Downloading CreamLinux...");
info!("Downloaded CreamLinux version: {}", version); match CreamLinux::download_to_cache().await {
update_creamlinux_version(&version)?; Ok(version) => {
} info!("Downloaded CreamLinux version: {}", version);
Err(e) => { update_creamlinux_version(&version)?;
error!("Failed to download CreamLinux: {}", e); }
return Err(format!("Failed to download CreamLinux: {}", e)); Err(e) => {
error!("Failed to download CreamLinux: {}", e);
return Err(format!("Failed to download CreamLinux: {}", e));
}
} }
} }
info!("Cache initialization complete"); if !needs_smokeapi && !needs_creamlinux {
info!("Cache already initialized and validated");
} else {
info!("Cache initialization complete");
}
Ok(()) Ok(())
} }

View File

@@ -204,12 +204,6 @@ pub fn update_creamlinux_version(new_version: &str) -> Result<(), String> {
Ok(()) Ok(())
} }
// Check if the cache is initialized (has both unlockers cached)
pub fn is_cache_initialized() -> Result<bool, String> {
let versions = read_versions()?;
Ok(!versions.smokeapi.latest.is_empty() && !versions.creamlinux.latest.is_empty())
}
// Get the SmokeAPI DLL path for the latest cached version // Get the SmokeAPI DLL path for the latest cached version
#[allow(dead_code)] #[allow(dead_code)]
pub fn get_smokeapi_dll_path() -> Result<PathBuf, String> { pub fn get_smokeapi_dll_path() -> Result<PathBuf, String> {
@@ -233,8 +227,8 @@ pub fn get_creamlinux_files_dir() -> Result<PathBuf, String> {
get_creamlinux_version_dir(&versions.creamlinux.latest) get_creamlinux_version_dir(&versions.creamlinux.latest)
} }
// List all SmokeAPI DLL files in the cached version directory /// List all SmokeAPI files in the cached version directory
pub fn list_smokeapi_dlls() -> Result<Vec<PathBuf>, String> { pub fn list_smokeapi_files() -> Result<Vec<PathBuf>, String> {
let versions = read_versions()?; let versions = read_versions()?;
if versions.smokeapi.latest.is_empty() { if versions.smokeapi.latest.is_empty() {
return Ok(Vec::new()); return Ok(Vec::new());
@@ -249,17 +243,20 @@ pub fn list_smokeapi_dlls() -> Result<Vec<PathBuf>, String> {
let entries = fs::read_dir(&version_dir) let entries = fs::read_dir(&version_dir)
.map_err(|e| format!("Failed to read SmokeAPI directory: {}", e))?; .map_err(|e| format!("Failed to read SmokeAPI directory: {}", e))?;
let mut dlls = Vec::new(); let mut files = Vec::new();
for entry in entries { for entry in entries {
if let Ok(entry) = entry { if let Ok(entry) = entry {
let path = entry.path(); let path = entry.path();
if path.extension().and_then(|s| s.to_str()) == Some("dll") { // Get both .dll and .so files
dlls.push(path); if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
if ext == "dll" || ext == "so" {
files.push(path);
}
} }
} }
} }
Ok(dlls) Ok(files)
} }
// List all CreamLinux files in the cached version directory // List all CreamLinux files in the cached version directory
@@ -289,4 +286,70 @@ pub fn list_creamlinux_files() -> Result<Vec<PathBuf>, String> {
} }
Ok(files) Ok(files)
}
/// Validate that all required files exist for SmokeAPI
pub fn validate_smokeapi_cache(version: &str) -> Result<bool, String> {
let version_dir = get_smokeapi_version_dir(version)?;
if !version_dir.exists() {
return Ok(false);
}
// Required files for SmokeAPI
let required_files = vec![
"smoke_api32.dll",
"smoke_api64.dll",
"libsmoke_api32.so",
"libsmoke_api64.so",
];
let mut missing_files = Vec::new();
for file in &required_files {
let file_path = version_dir.join(file);
if !file_path.exists() {
missing_files.push(file.to_string());
}
}
if !missing_files.is_empty() {
info!("Missing required files in cache: {:?}", missing_files);
return Ok(false);
}
Ok(true)
}
/// Validate that all required files exist for CreamLinux
pub fn validate_creamlinux_cache(version: &str) -> Result<bool, String> {
let version_dir = get_creamlinux_version_dir(version)?;
if !version_dir.exists() {
return Ok(false);
}
// Required files for CreamLinux
let required_files = vec![
"cream.sh",
"cream_api.ini",
"lib32Creamlinux.so",
"lib64Creamlinux.so",
];
let mut missing_files = Vec::new();
for file in &required_files {
let file_path = version_dir.join(file);
if !file_path.exists() {
missing_files.push(file.to_string());
}
}
if !missing_files.is_empty() {
info!("Missing required files in cache: {:?}", missing_files);
return Ok(false);
}
Ok(true)
} }