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;
pub use storage::{
get_creamlinux_version_dir, get_smokeapi_version_dir, is_cache_initialized,
list_creamlinux_files, list_smokeapi_dlls, read_versions, update_creamlinux_version,
update_smokeapi_version,
get_creamlinux_version_dir, get_smokeapi_version_dir,
list_creamlinux_files, list_smokeapi_files, read_versions,
update_creamlinux_version, update_smokeapi_version, validate_smokeapi_cache,
validate_creamlinux_cache,
};
pub use version::{
@@ -22,39 +23,87 @@ use std::collections::HashMap;
pub async fn initialize_cache() -> Result<(), String> {
info!("Initializing cache...");
// Check if cache is already initialized
if is_cache_initialized()? {
info!("Cache already initialized");
return Ok(());
let versions = read_versions()?;
let mut needs_smokeapi = false;
let mut needs_creamlinux = false;
// 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
match SmokeAPI::download_to_cache().await {
Ok(version) => {
info!("Downloaded SmokeAPI version: {}", version);
update_smokeapi_version(&version)?;
}
Err(e) => {
error!("Failed to download SmokeAPI: {}", e);
return Err(format!("Failed to download SmokeAPI: {}", e));
if needs_smokeapi {
info!("Downloading SmokeAPI...");
match SmokeAPI::download_to_cache().await {
Ok(version) => {
info!("Downloaded SmokeAPI version: {}", version);
update_smokeapi_version(&version)?;
}
Err(e) => {
error!("Failed to download SmokeAPI: {}", e);
return Err(format!("Failed to download SmokeAPI: {}", e));
}
}
}
// Download CreamLinux
match CreamLinux::download_to_cache().await {
Ok(version) => {
info!("Downloaded CreamLinux version: {}", version);
update_creamlinux_version(&version)?;
}
Err(e) => {
error!("Failed to download CreamLinux: {}", e);
return Err(format!("Failed to download CreamLinux: {}", e));
if needs_creamlinux {
info!("Downloading CreamLinux...");
match CreamLinux::download_to_cache().await {
Ok(version) => {
info!("Downloaded CreamLinux version: {}", version);
update_creamlinux_version(&version)?;
}
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(())
}

View File

@@ -204,12 +204,6 @@ pub fn update_creamlinux_version(new_version: &str) -> Result<(), String> {
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
#[allow(dead_code)]
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)
}
// List all SmokeAPI DLL files in the cached version directory
pub fn list_smokeapi_dlls() -> Result<Vec<PathBuf>, String> {
/// List all SmokeAPI files in the cached version directory
pub fn list_smokeapi_files() -> Result<Vec<PathBuf>, String> {
let versions = read_versions()?;
if versions.smokeapi.latest.is_empty() {
return Ok(Vec::new());
@@ -249,17 +243,20 @@ pub fn list_smokeapi_dlls() -> Result<Vec<PathBuf>, String> {
let entries = fs::read_dir(&version_dir)
.map_err(|e| format!("Failed to read SmokeAPI directory: {}", e))?;
let mut dlls = Vec::new();
let mut files = Vec::new();
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) == Some("dll") {
dlls.push(path);
// Get both .dll and .so files
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
@@ -290,3 +287,69 @@ pub fn list_creamlinux_files() -> Result<Vec<PathBuf>, String> {
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)
}