Enhance app version management and improve plugin list filtering with advanced search capabilities
This commit is contained in:
1 parent
61becf8d22
commit
0240ab9c50
19 files changed
+1771
-176
No files matched your search
@@ -153,6 +153,8 @@ pub async fn perform_scan(app_handle: &AppHandle, path: &str) -> Result<ScanResu
|
||||
file_path: meta.file_path,
|
||||
file_hash: meta.file_hash,
|
||||
changelog: None, // Will be populated during update check
|
||||
compatibility_status: None, // Will be populated during update check
|
||||
platform_compatibility: None, // Will be populated during update check
|
||||
repository_source: None, // Will be populated during update check
|
||||
repository_id: None, // Will be populated during update check
|
||||
repository_url: None, // Will be populated during update check
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
use tauri::{AppHandle, Emitter};
|
||||
use futures::stream::StreamExt;
|
||||
|
||||
use crate::models::plugin::Plugin;
|
||||
use crate::models::plugin::{Plugin, PluginCompatibilityStatus};
|
||||
use crate::models::repository::{RepositorySource, RepositoryPlugin, BulkUpdateProgress, SingleUpdateResult};
|
||||
use super::version_utils::compare_plugin_versions;
|
||||
use crate::models::server::{ServerInfo, ServerType};
|
||||
use super::version_utils::is_version_compatible;
|
||||
use crate::platform_matcher::is_version_compatible_with_server;
|
||||
|
||||
/// Check for updates for multiple plugins
|
||||
pub async fn check_for_plugin_updates(
|
||||
app_handle: AppHandle,
|
||||
installed_plugins: Vec<Plugin>,
|
||||
repositories_to_check: Vec<RepositorySource>
|
||||
repositories_to_check: Vec<RepositorySource>,
|
||||
server_info: &ServerInfo,
|
||||
) -> Result<Vec<Plugin>, String> {
|
||||
if installed_plugins.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
@@ -37,6 +39,7 @@ pub async fn check_for_plugin_updates(
|
||||
let app_handle_clone = app_handle.clone();
|
||||
let repos_clone = repositories_to_check.clone();
|
||||
let plugin_clone = plugin.clone();
|
||||
let server_info_clone = server_info.clone();
|
||||
|
||||
// Report progress
|
||||
let progress = BulkUpdateProgress {
|
||||
@@ -53,7 +56,7 @@ pub async fn check_for_plugin_updates(
|
||||
batch_futures.push(async move {
|
||||
// Process plugin
|
||||
println!("Processing update for plugin {}/{}: {}", index + 1, total, plugin_clone.name);
|
||||
let result = process_plugin_update(plugin_clone.clone(), &repos_clone).await;
|
||||
let result = process_plugin_update(plugin_clone.clone(), &repos_clone, &server_info_clone).await;
|
||||
(index, result)
|
||||
});
|
||||
}
|
||||
@@ -97,6 +100,7 @@ pub async fn check_single_plugin_update(
|
||||
app_handle: AppHandle,
|
||||
plugin_to_check: Plugin,
|
||||
repositories_to_check: Vec<RepositorySource>,
|
||||
server_info: Option<&ServerInfo>,
|
||||
) -> Result<(), String> { // Returns Result<(), String> because result is sent via event
|
||||
// Begin check
|
||||
if let Err(e) = app_handle.emit("single_update_check_started", plugin_to_check.name.clone()) {
|
||||
@@ -104,7 +108,19 @@ pub async fn check_single_plugin_update(
|
||||
}
|
||||
|
||||
// Process update
|
||||
let updated_plugin = process_plugin_update(plugin_to_check.clone(), &repositories_to_check).await;
|
||||
let updated_plugin = match server_info {
|
||||
Some(info) => process_plugin_update(plugin_to_check.clone(), &repositories_to_check, info).await,
|
||||
None => {
|
||||
// Create a minimal server info if none provided
|
||||
let default_info = ServerInfo {
|
||||
server_type: ServerType::Unknown,
|
||||
minecraft_version: None,
|
||||
plugins_directory: String::new(),
|
||||
plugins_count: 0
|
||||
};
|
||||
process_plugin_update(plugin_to_check.clone(), &repositories_to_check, &default_info).await
|
||||
}
|
||||
};
|
||||
|
||||
// Create result
|
||||
let result = SingleUpdateResult {
|
||||
@@ -122,26 +138,57 @@ pub async fn check_single_plugin_update(
|
||||
}
|
||||
|
||||
/// Process update check for a single plugin
|
||||
async fn process_plugin_update(plugin: Plugin, repositories: &[RepositorySource]) -> Plugin {
|
||||
async fn process_plugin_update(plugin: Plugin, repositories: &[RepositorySource], server_info: &ServerInfo) -> Plugin {
|
||||
let mut updated_plugin = plugin.clone();
|
||||
println!("Checking for updates for plugin: {}", plugin.name);
|
||||
|
||||
// If plugin already has repository info, check that specific repo
|
||||
if let (Some(repo_source), Some(repo_id)) = (&plugin.repository_source, &plugin.repository_id) {
|
||||
println!("Plugin has existing repository info: {:?}, ID: {}", repo_source, repo_id);
|
||||
match crate::lib_get_plugin_details_from_repository(repo_id, repo_source.clone(), None).await {
|
||||
let result = crate::lib_get_plugin_details_from_repository(repo_id, repo_source.clone(), Some(&server_info.server_type)).await;
|
||||
|
||||
match result {
|
||||
Ok(repo_plugin) => {
|
||||
println!("Successfully got details from repository for {}", plugin.name);
|
||||
update_plugin_from_repo(&mut updated_plugin, &repo_plugin);
|
||||
// Verify that the retrieved plugin still matches
|
||||
if is_likely_match(&plugin, &repo_plugin) {
|
||||
update_plugin_from_repo(&mut updated_plugin, &repo_plugin, server_info);
|
||||
} else {
|
||||
println!("Warning: Retrieved plugin no longer matches! Clearing repository info and trying again");
|
||||
// Clear repository info and try smart matching
|
||||
updated_plugin.repository_source = None;
|
||||
updated_plugin.repository_id = None;
|
||||
updated_plugin = retry_with_smart_matching(updated_plugin, repositories, server_info).await;
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Failed to get update details for {}: {}", plugin.name, e);
|
||||
// If we failed with the saved repository, try smart matching instead
|
||||
updated_plugin = retry_with_smart_matching(updated_plugin, repositories, server_info).await;
|
||||
}
|
||||
}
|
||||
|
||||
// If we have no latest_version set but we have a version, set the latest_version
|
||||
// to the current version to show it's up-to-date
|
||||
if updated_plugin.latest_version.is_none() {
|
||||
updated_plugin.latest_version = Some(updated_plugin.version.clone());
|
||||
updated_plugin.has_update = false;
|
||||
}
|
||||
|
||||
return updated_plugin;
|
||||
}
|
||||
|
||||
// Otherwise, try smart matching with repositories
|
||||
updated_plugin = retry_with_smart_matching(updated_plugin, repositories, server_info).await;
|
||||
|
||||
println!("Update check complete for plugin: {}", plugin.name);
|
||||
updated_plugin
|
||||
}
|
||||
|
||||
/// Helper function to perform smart matching when no repository info exists or previous check failed
|
||||
async fn retry_with_smart_matching(plugin: Plugin, repositories: &[RepositorySource], server_info: &ServerInfo) -> Plugin {
|
||||
let mut updated_plugin = plugin.clone();
|
||||
|
||||
println!("No repository info for {}. Trying smart matching...", plugin.name);
|
||||
let matches = match crate::search_with_variations(&plugin.name, repositories).await {
|
||||
Ok(matches) => {
|
||||
@@ -150,94 +197,636 @@ async fn process_plugin_update(plugin: Plugin, repositories: &[RepositorySource]
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Error searching for plugin {}: {}", plugin.name, e);
|
||||
// If we failed to find matches, set latest_version to current version
|
||||
// to indicate it's up-to-date as far as we know
|
||||
updated_plugin.latest_version = Some(updated_plugin.version.clone());
|
||||
updated_plugin.has_update = false;
|
||||
return updated_plugin;
|
||||
}
|
||||
};
|
||||
|
||||
// Find best match
|
||||
for repo_plugin in matches {
|
||||
if is_likely_match(&plugin, &repo_plugin) {
|
||||
println!("Found likely match for {} in {:?}: {} ({})",
|
||||
plugin.name, repo_plugin.repository, repo_plugin.name, repo_plugin.version);
|
||||
update_plugin_from_repo(&mut updated_plugin, &repo_plugin);
|
||||
break;
|
||||
let mut found_match = false;
|
||||
let mut best_match_score = 0.0f32;
|
||||
let mut best_match_index = 0;
|
||||
|
||||
// First pass: evaluate all matches to find the best one
|
||||
for (i, repo_plugin) in matches.iter().enumerate() {
|
||||
let match_score = calculate_match_score(&plugin, repo_plugin);
|
||||
if match_score > best_match_score {
|
||||
best_match_score = match_score;
|
||||
best_match_index = i;
|
||||
}
|
||||
}
|
||||
|
||||
println!("Update check complete for plugin: {}", plugin.name);
|
||||
// Second pass: if we found a likely match, use it
|
||||
if best_match_score >= 3.0f32 && best_match_index < matches.len() {
|
||||
let repo_plugin = &matches[best_match_index];
|
||||
|
||||
println!("Found likely match for {} in {:?}: {} ({})",
|
||||
plugin.name, repo_plugin.repository, repo_plugin.name, repo_plugin.version);
|
||||
|
||||
update_plugin_from_repo(&mut updated_plugin, repo_plugin, server_info);
|
||||
found_match = true;
|
||||
}
|
||||
|
||||
// If no match was found, set latest_version to current version
|
||||
if !found_match {
|
||||
updated_plugin.latest_version = Some(updated_plugin.version.clone());
|
||||
updated_plugin.has_update = false;
|
||||
}
|
||||
|
||||
updated_plugin
|
||||
}
|
||||
|
||||
/// Calculate a match score between an installed plugin and a repository plugin
|
||||
fn calculate_match_score(installed: &Plugin, repo: &RepositoryPlugin) -> f32 {
|
||||
if is_likely_match(installed, repo) {
|
||||
// Get detailed score for debugging and sorting purposes
|
||||
let mut score = 0.0f32;
|
||||
|
||||
// Name similarity
|
||||
let similarity = calculate_name_similarity(&installed.name, &repo.name);
|
||||
if similarity == 1.0f32 {
|
||||
score += 5.0f32; // Exact name match
|
||||
} else if similarity > 0.9f32 {
|
||||
score += 3.0f32; // Strong name match
|
||||
} else if similarity > 0.7f32 {
|
||||
score += 2.0f32; // Moderate name match
|
||||
} else if similarity > 0.5f32 {
|
||||
score += 1.0f32; // Weak name match
|
||||
}
|
||||
|
||||
// Main class match
|
||||
if let Some(main_class) = &installed.main_class {
|
||||
if main_class.to_lowercase().contains(&repo.name.to_lowercase()) {
|
||||
score += 3.0f32;
|
||||
}
|
||||
}
|
||||
|
||||
// Author match
|
||||
if !installed.authors.is_empty() && !repo.authors.is_empty() {
|
||||
for installed_author in &installed.authors {
|
||||
for repo_author in &repo.authors {
|
||||
if installed_author.to_lowercase() == repo_author.to_lowercase() {
|
||||
score += 2.0f32;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Website match
|
||||
if let Some(website) = &installed.website {
|
||||
if website.contains(&repo.page_url) || repo.page_url.contains(website) {
|
||||
score += 2.0f32;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for matching plugin name and author
|
||||
let repo_name_lower = repo.name.to_lowercase();
|
||||
|
||||
// Handle the case where installed.depend is an Option<Vec<String>>
|
||||
if let Some(depend) = &installed.depend {
|
||||
if !depend.is_empty() && depend.len() > 1 {
|
||||
// Score bump if repo_name appears in dependencies
|
||||
let depend_str = depend.join(",").to_lowercase();
|
||||
if depend_str.contains(&repo_name_lower) {
|
||||
score += 3.0f32;
|
||||
}
|
||||
}
|
||||
|
||||
// Score bump if the plugin name matches or is contained in any dependency
|
||||
if depend.iter().any(|dep| repo_name_lower.contains(&dep.to_lowercase())) {
|
||||
score += 5.0f32;
|
||||
}
|
||||
}
|
||||
|
||||
// More detailed scoring can be added here
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
0.0f32 // Not a match
|
||||
}
|
||||
|
||||
/// Update plugin with repository information
|
||||
fn update_plugin_from_repo(plugin: &mut Plugin, repo_plugin: &RepositoryPlugin) {
|
||||
fn update_plugin_from_repo(plugin: &mut Plugin, repo_plugin: &RepositoryPlugin, server_info: &ServerInfo) {
|
||||
// Get server platform information
|
||||
let server_loader = server_type_to_loader_name(&server_info.server_type);
|
||||
|
||||
// Extract platform suffixes from repo version
|
||||
let repo_platform = crate::services::update_manager::version_utils::extract_platform_suffix(&repo_plugin.version);
|
||||
|
||||
// Set repository information
|
||||
plugin.repository_source = Some(repo_plugin.repository.clone());
|
||||
plugin.repository_id = Some(repo_plugin.id.clone());
|
||||
plugin.repository_url = Some(repo_plugin.page_url.clone());
|
||||
|
||||
// Set platform compatibility information
|
||||
if !repo_plugin.loaders.is_empty() {
|
||||
// Clean and format the loader names for display
|
||||
let formatted_loaders: Vec<String> = repo_plugin.loaders.iter()
|
||||
.map(|loader| {
|
||||
let loader_lower = loader.to_lowercase();
|
||||
// Standard naming conversion
|
||||
if loader_lower == "paper" { "Paper".to_string() }
|
||||
else if loader_lower == "spigot" { "Spigot".to_string() }
|
||||
else if loader_lower == "bukkit" { "Bukkit".to_string() }
|
||||
else if loader_lower == "forge" { "Forge".to_string() }
|
||||
else if loader_lower == "neoforge" { "NeoForge".to_string() }
|
||||
else if loader_lower == "fabric" { "Fabric".to_string() }
|
||||
else if loader_lower == "quilt" { "Quilt".to_string() }
|
||||
else if loader_lower == "velocity" { "Velocity".to_string() }
|
||||
else if loader_lower == "bungeecord" { "BungeeCord".to_string() }
|
||||
else if loader_lower == "waterfall" { "Waterfall".to_string() }
|
||||
else if loader_lower == "sponge" { "Sponge".to_string() }
|
||||
else { loader.clone() } // Just use original if not recognized
|
||||
})
|
||||
.collect();
|
||||
|
||||
plugin.platform_compatibility = Some(formatted_loaders);
|
||||
println!(" Setting platform compatibility for {}: {:?}", plugin.name, plugin.platform_compatibility);
|
||||
}
|
||||
|
||||
// If repo version has a specific platform suffix, check against server type
|
||||
let is_platform_specific = if let Some(platform) = repo_platform {
|
||||
// Check if platform suffix is compatible with server type
|
||||
let platform_compatible =
|
||||
if let Some(server_platform) = server_loader {
|
||||
// Both have specified platforms, check if compatible
|
||||
is_platform_compatible(&platform, server_platform)
|
||||
} else {
|
||||
// Server platform unknown, be conservative
|
||||
false
|
||||
};
|
||||
|
||||
if !platform_compatible {
|
||||
println!(" Repo version {} has incompatible platform suffix: {}",
|
||||
repo_plugin.version, platform);
|
||||
// Mark as incompatible but still track the version
|
||||
plugin.compatibility_status = Some(PluginCompatibilityStatus::IncompatibleLoader);
|
||||
}
|
||||
|
||||
// Return whether this version is platform-specific
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
// For Modrinth, check if the version looks like an ID instead of a version number
|
||||
let latest_version = if repo_plugin.repository == RepositorySource::Modrinth &&
|
||||
repo_plugin.version.len() >= 8 &&
|
||||
!repo_plugin.version.contains('.') {
|
||||
// This looks like an ID rather than a version, use "Latest" as a fallback
|
||||
// TODO: Potentially fetch actual version number if needed later
|
||||
"Latest".to_string()
|
||||
} else {
|
||||
repo_plugin.version.clone()
|
||||
};
|
||||
|
||||
// Set latest version if newer
|
||||
if latest_version != plugin.version {
|
||||
let has_update = compare_plugin_versions(&plugin.version, &latest_version);
|
||||
// Set latest version and check compatibility
|
||||
let has_newer_version = crate::services::update_manager::version_utils::compare_plugin_versions(
|
||||
&plugin.version, &latest_version);
|
||||
|
||||
if has_newer_version {
|
||||
println!(" Detected newer version for {}: {} -> {}", plugin.name, plugin.version, latest_version);
|
||||
|
||||
// Check loader compatibility using the platform matcher
|
||||
let is_loader_compatible = if is_platform_specific {
|
||||
// We already checked platform compatibility above
|
||||
plugin.compatibility_status != Some(PluginCompatibilityStatus::IncompatibleLoader)
|
||||
} else {
|
||||
// No platform suffix in version, check loaders list
|
||||
is_version_compatible_with_server(
|
||||
&repo_plugin.loaders,
|
||||
&server_info.server_type
|
||||
)
|
||||
};
|
||||
|
||||
// Check Minecraft version compatibility
|
||||
let is_mc_version_compatible = if let Some(mc_version) = &server_info.minecraft_version {
|
||||
// Only check if we have a server Minecraft version
|
||||
is_version_compatible(mc_version, &repo_plugin.supported_versions)
|
||||
} else {
|
||||
// If we don't know the server version, assume it's compatible
|
||||
true
|
||||
};
|
||||
|
||||
plugin.latest_version = Some(latest_version);
|
||||
plugin.has_update = has_update;
|
||||
// Only set has_update to true if all compatibility checks pass
|
||||
plugin.has_update = is_loader_compatible && is_mc_version_compatible;
|
||||
plugin.changelog = repo_plugin.changelog.clone();
|
||||
|
||||
// Set compatibility status based on checks
|
||||
if !is_loader_compatible {
|
||||
plugin.compatibility_status = Some(PluginCompatibilityStatus::IncompatibleLoader);
|
||||
println!(" Update found ({}) for {}, but loader is incompatible with server type {:?}",
|
||||
plugin.latest_version.as_deref().unwrap_or("?"),
|
||||
plugin.name,
|
||||
server_info.server_type
|
||||
);
|
||||
} else if !is_mc_version_compatible {
|
||||
plugin.compatibility_status = Some(PluginCompatibilityStatus::IncompatibleMinecraftVersion);
|
||||
println!(" Update found ({}) for {}, but it's incompatible with MC version {:?}",
|
||||
plugin.latest_version.as_deref().unwrap_or("?"),
|
||||
plugin.name,
|
||||
server_info.minecraft_version.as_deref().unwrap_or("?")
|
||||
);
|
||||
} else {
|
||||
plugin.compatibility_status = Some(PluginCompatibilityStatus::Compatible);
|
||||
println!(" Compatible update found ({}) for {}",
|
||||
plugin.latest_version.as_deref().unwrap_or("?"),
|
||||
plugin.name
|
||||
);
|
||||
}
|
||||
|
||||
} else {
|
||||
// Plugin version matches or is newer than repo - treat as up-to-date
|
||||
plugin.latest_version = Some(plugin.version.clone());
|
||||
plugin.has_update = false;
|
||||
plugin.compatibility_status = Some(PluginCompatibilityStatus::Compatible); // Assume compatible if up-to-date
|
||||
}
|
||||
|
||||
// If status wasn't set (e.g., error during check before this point), mark as Unknown
|
||||
if plugin.compatibility_status.is_none() {
|
||||
plugin.compatibility_status = Some(PluginCompatibilityStatus::Unknown);
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine if a repository plugin is likely a match for the installed plugin
|
||||
/// Convert server type to loader name
|
||||
fn server_type_to_loader_name(server_type: &ServerType) -> Option<&'static str> {
|
||||
match server_type {
|
||||
ServerType::Paper => Some("paper"),
|
||||
ServerType::Spigot => Some("spigot"),
|
||||
ServerType::Bukkit => Some("bukkit"),
|
||||
ServerType::Forge => Some("forge"),
|
||||
ServerType::Fabric => Some("fabric"),
|
||||
ServerType::Velocity => Some("velocity"),
|
||||
ServerType::BungeeCord => Some("bungeecord"),
|
||||
ServerType::Waterfall => Some("waterfall"),
|
||||
ServerType::Vanilla => None, // Vanilla doesn't support plugins
|
||||
ServerType::Unknown => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if version platform is compatible with server platform
|
||||
fn is_platform_compatible(version_platform: &str, server_platform: &str) -> bool {
|
||||
// Direct platform match
|
||||
if version_platform.eq_ignore_ascii_case(server_platform) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Platform compatibility matrix
|
||||
match server_platform {
|
||||
"paper" => {
|
||||
// Paper is compatible with Spigot and Bukkit plugins
|
||||
version_platform == "spigot" ||
|
||||
version_platform == "bukkit"
|
||||
},
|
||||
"spigot" => {
|
||||
// Spigot is compatible with Bukkit plugins
|
||||
version_platform == "bukkit"
|
||||
},
|
||||
"bungeecord" => {
|
||||
// BungeeCord is compatible with Waterfall plugins and vice versa
|
||||
version_platform == "waterfall"
|
||||
},
|
||||
"waterfall" => {
|
||||
version_platform == "bungeecord"
|
||||
},
|
||||
_ => false // No other cross-compatibility
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine if a repository plugin is likely a match for an installed plugin
|
||||
/// Uses multiple weighted factors and confidence scoring
|
||||
fn is_likely_match(installed: &Plugin, repo: &RepositoryPlugin) -> bool {
|
||||
// Require multiple criteria to match to avoid false positives
|
||||
let mut match_points = 0;
|
||||
println!("Evaluating match: '{}' (installed) vs '{}' (repo)", installed.name, repo.name);
|
||||
|
||||
// Name similarity check
|
||||
// Multi-factor weighted scoring system
|
||||
let mut confidence_score = 0.0f32;
|
||||
let mut factor_count = 0;
|
||||
let mut applied_weights = 0.0f32;
|
||||
|
||||
// Track applied factors for debugging
|
||||
let mut applied_factors = Vec::new();
|
||||
|
||||
// ====== NAME SIMILARITY (HIGHEST WEIGHT) ======
|
||||
let name_similarity = calculate_name_similarity(&installed.name, &repo.name);
|
||||
let name_weight = 10.0f32;
|
||||
applied_weights += name_weight;
|
||||
factor_count += 1;
|
||||
confidence_score += name_similarity * name_weight;
|
||||
applied_factors.push(format!("Name similarity: {:.2} (weight: {:.1})", name_similarity, name_weight));
|
||||
|
||||
// Strong name match (over 0.9 similarity)
|
||||
if name_similarity > 0.9 {
|
||||
match_points += 2;
|
||||
}
|
||||
// Moderate name match
|
||||
else if name_similarity > 0.7 {
|
||||
match_points += 1;
|
||||
// Exact name match is very strong signal
|
||||
if name_similarity > 0.95f32 {
|
||||
applied_factors.push("STRONG SIGNAL: Nearly exact name match".to_string());
|
||||
}
|
||||
|
||||
// Author check - if any author matches
|
||||
// ====== DESCRIPTION SIMILARITY ======
|
||||
if let (Some(installed_desc), Some(repo_desc)) = (&installed.description, &repo.description) {
|
||||
if !installed_desc.is_empty() && !repo_desc.is_empty() {
|
||||
let desc_similarity = calculate_text_similarity(installed_desc, repo_desc);
|
||||
let desc_weight = 3.0f32;
|
||||
applied_weights += desc_weight;
|
||||
factor_count += 1;
|
||||
confidence_score += desc_similarity * desc_weight;
|
||||
applied_factors.push(format!("Description similarity: {:.2} (weight: {:.1})", desc_similarity, desc_weight));
|
||||
}
|
||||
}
|
||||
|
||||
// ====== MAIN CLASS MATCH (VERY STRONG SIGNAL) ======
|
||||
if let Some(main_class) = &installed.main_class {
|
||||
let main_class_lower = main_class.to_lowercase();
|
||||
let repo_name_lower = repo.name.to_lowercase();
|
||||
|
||||
// Check if repository plugin name is contained in the main class
|
||||
// This is a very strong signal that they are the same plugin
|
||||
let main_class_signal = if main_class_lower.contains(&repo_name_lower) {
|
||||
// Direct containment is strongest
|
||||
let weight = 8.0f32;
|
||||
applied_weights += weight;
|
||||
factor_count += 1;
|
||||
confidence_score += 1.0f32 * weight;
|
||||
applied_factors.push(format!("Main class contains repo name (weight: {:.1})", weight));
|
||||
true
|
||||
} else if repo_name_lower.contains(&main_class_lower.split('.').last().unwrap_or("").to_lowercase()) {
|
||||
// Final component of package matches
|
||||
let weight = 6.0f32;
|
||||
applied_weights += weight;
|
||||
factor_count += 1;
|
||||
confidence_score += 0.9f32 * weight;
|
||||
applied_factors.push(format!("Repo name contains last part of main class (weight: {:.1})", weight));
|
||||
true
|
||||
} else {
|
||||
// Check for partial matching (e.g., WordlEdit matches com.sk89q.worldedit)
|
||||
let main_class_parts: Vec<&str> = main_class_lower.split('.').collect();
|
||||
for part in main_class_parts {
|
||||
if part.len() > 3 && repo_name_lower.contains(part) {
|
||||
let weight = 5.0f32;
|
||||
applied_weights += weight;
|
||||
factor_count += 1;
|
||||
confidence_score += 0.8f32 * weight;
|
||||
applied_factors.push(format!("Main class part '{}' found in repo name (weight: {:.1})", part, weight));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
};
|
||||
|
||||
if main_class_signal {
|
||||
applied_factors.push("STRONG SIGNAL: Main class match".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// ====== AUTHOR MATCH ======
|
||||
if !installed.authors.is_empty() && !repo.authors.is_empty() {
|
||||
let mut best_author_match = 0.0f32;
|
||||
let mut matched_author = String::new();
|
||||
|
||||
for installed_author in &installed.authors {
|
||||
let installed_author_lower = installed_author.to_lowercase();
|
||||
|
||||
for repo_author in &repo.authors {
|
||||
if installed_author.to_lowercase() == repo_author.to_lowercase() {
|
||||
match_points += 2;
|
||||
let repo_author_lower = repo_author.to_lowercase();
|
||||
|
||||
// Try different author name formats and variations
|
||||
if installed_author_lower == repo_author_lower {
|
||||
best_author_match = 1.0f32;
|
||||
matched_author = repo_author.clone();
|
||||
break;
|
||||
}
|
||||
|
||||
// Handle partial name matches (e.g., "JohnDoe" vs "John Doe" or "jdoe")
|
||||
let similarity = calculate_name_similarity(&installed_author_lower, &repo_author_lower);
|
||||
if similarity > best_author_match {
|
||||
best_author_match = similarity;
|
||||
matched_author = repo_author.clone();
|
||||
}
|
||||
}
|
||||
|
||||
if best_author_match > 0.0f32 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if best_author_match > 0.0f32 {
|
||||
let author_weight = if best_author_match >= 0.9f32 { 6.0f32 } else { 3.0f32 };
|
||||
applied_weights += author_weight;
|
||||
factor_count += 1;
|
||||
confidence_score += best_author_match * author_weight;
|
||||
applied_factors.push(format!("Author match: {} ({:.2} similarity, weight: {:.1})",
|
||||
matched_author, best_author_match, author_weight));
|
||||
|
||||
if best_author_match >= 0.9f32 {
|
||||
applied_factors.push("STRONG SIGNAL: Author exact match".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ====== WEBSITE MATCH ======
|
||||
if let Some(website) = &installed.website {
|
||||
let website_lower = website.to_lowercase();
|
||||
let repo_url_lower = repo.page_url.to_lowercase();
|
||||
|
||||
if website_lower.contains(&repo_url_lower) || repo_url_lower.contains(&website_lower) {
|
||||
let weight = 4.0f32;
|
||||
applied_weights += weight;
|
||||
factor_count += 1;
|
||||
confidence_score += 1.0f32 * weight;
|
||||
applied_factors.push(format!("Website match (weight: {:.1})", weight));
|
||||
applied_factors.push("STRONG SIGNAL: Website match".to_string());
|
||||
} else {
|
||||
// Try to extract domains for partial matching
|
||||
let website_domain = extract_domain(&website_lower);
|
||||
let repo_domain = extract_domain(&repo_url_lower);
|
||||
|
||||
if let (Some(w_domain), Some(r_domain)) = (website_domain, repo_domain) {
|
||||
if w_domain == r_domain {
|
||||
let weight = 3.0f32;
|
||||
applied_weights += weight;
|
||||
factor_count += 1;
|
||||
confidence_score += 0.9f32 * weight;
|
||||
applied_factors.push(format!("Domain match: {} (weight: {:.1})", w_domain, weight));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Website match
|
||||
if let Some(website) = &installed.website {
|
||||
if website.contains(&repo.page_url) || repo.page_url.contains(website) {
|
||||
match_points += 2;
|
||||
// ====== DEPENDENCY CHECKS ======
|
||||
// Check if plugins have similar dependencies
|
||||
// This helps distinguish plugins with similar names but different functionality
|
||||
if let Some(depend) = &installed.depend {
|
||||
if !depend.is_empty() {
|
||||
// Create a string of dependencies for matching
|
||||
let depend_str = depend.join(",").to_lowercase();
|
||||
let repo_name_lower = repo.name.to_lowercase();
|
||||
|
||||
// Watch for potential false matches where repo name matches a dependency
|
||||
// This prevents matching a plugin to its dependency (like WorldEdit to FastAsyncWorldEdit)
|
||||
let mut dependency_penalty = false;
|
||||
for dep in depend {
|
||||
if repo_name_lower.contains(&dep.to_lowercase()) {
|
||||
dependency_penalty = true;
|
||||
let weight = -5.0f32; // Strong negative signal
|
||||
applied_weights += weight.abs();
|
||||
confidence_score += weight; // Negative score
|
||||
applied_factors.push(format!("NEGATIVE: Repo name matches dependency: {} (weight: {:.1})", dep, weight));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if description contains dependency references
|
||||
if !dependency_penalty && !repo_name_lower.contains("api") && !repo_name_lower.contains("lib") {
|
||||
if let Some(desc) = &repo.description {
|
||||
let desc_lower = desc.to_lowercase();
|
||||
|
||||
// Check if any dependencies are mentioned in the description
|
||||
let mut found_dependencies = Vec::new();
|
||||
for dep in depend {
|
||||
let dep_lower = dep.to_lowercase();
|
||||
if desc_lower.contains(&dep_lower) {
|
||||
found_dependencies.push(dep.clone());
|
||||
}
|
||||
}
|
||||
|
||||
if !found_dependencies.is_empty() {
|
||||
let weight = 3.0f32;
|
||||
let match_score = (found_dependencies.len() as f32 / depend.len() as f32).min(1.0);
|
||||
applied_weights += weight;
|
||||
factor_count += 1;
|
||||
confidence_score += match_score * weight;
|
||||
applied_factors.push(format!("Dependencies in description: {} (weight: {:.1})",
|
||||
found_dependencies.join(", "), weight));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Require at least 2 match points to consider it a likely match
|
||||
// This helps avoid incorrect matches
|
||||
match_points >= 2
|
||||
// ====== FINAL CONFIDENCE CALCULATION ======
|
||||
let normalized_confidence = if applied_weights > 0.0f32 {
|
||||
confidence_score / applied_weights
|
||||
} else {
|
||||
0.0f32
|
||||
};
|
||||
|
||||
// Calculate a weighted score that considers both total confidence and breadth of signals
|
||||
let signal_breadth_factor = (factor_count as f32 / 5.0).min(1.0); // Normalize to max of 5 factors
|
||||
let final_confidence = normalized_confidence * 0.7 + signal_breadth_factor * 0.3;
|
||||
|
||||
// Threshold for likely match
|
||||
let is_match = final_confidence >= 0.6 && factor_count >= 2;
|
||||
|
||||
// Debug output
|
||||
println!("Match analysis for '{}' vs '{}':", installed.name, repo.name);
|
||||
for factor in &applied_factors {
|
||||
println!(" {}", factor);
|
||||
}
|
||||
println!(" Factors: {}, Applied weights: {:.1}, Confidence score: {:.1}",
|
||||
factor_count, applied_weights, confidence_score);
|
||||
println!(" Normalized confidence: {:.2}, Signal breadth: {:.2}, Final confidence: {:.2}",
|
||||
normalized_confidence, signal_breadth_factor, final_confidence);
|
||||
println!(" MATCH RESULT: {}", if is_match { "LIKELY MATCH ✓" } else { "NOT A MATCH ✗" });
|
||||
|
||||
is_match
|
||||
}
|
||||
|
||||
/// Calculate similarity between plugin names (simple implementation)
|
||||
/// Extract domain from a URL for website matching
|
||||
fn extract_domain(url: &str) -> Option<String> {
|
||||
// Strip protocol
|
||||
let without_protocol = if url.starts_with("http://") {
|
||||
&url[7..]
|
||||
} else if url.starts_with("https://") {
|
||||
&url[8..]
|
||||
} else {
|
||||
url
|
||||
};
|
||||
|
||||
// Find end of domain (first slash or end of string)
|
||||
let domain_end = without_protocol.find('/').unwrap_or(without_protocol.len());
|
||||
let domain = &without_protocol[0..domain_end];
|
||||
|
||||
// Remove www. prefix if present
|
||||
let domain = if domain.starts_with("www.") {
|
||||
&domain[4..]
|
||||
} else {
|
||||
domain
|
||||
};
|
||||
|
||||
if domain.is_empty() || !domain.contains('.') {
|
||||
None
|
||||
} else {
|
||||
Some(domain.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate similarity between two text fields
|
||||
fn calculate_text_similarity(text1: &str, text2: &str) -> f32 {
|
||||
let t1 = text1.to_lowercase();
|
||||
let t2 = text2.to_lowercase();
|
||||
|
||||
// For short texts, use character-level similarity
|
||||
if t1.len() < 10 || t2.len() < 10 {
|
||||
return calculate_levenshtein_similarity(&t1, &t2);
|
||||
}
|
||||
|
||||
// Otherwise use a bag-of-words approach
|
||||
let words1: Vec<&str> = t1.split_whitespace().collect();
|
||||
let words2: Vec<&str> = t2.split_whitespace().collect();
|
||||
|
||||
if words1.is_empty() || words2.is_empty() {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Count word occurrences
|
||||
let mut word_counts1 = std::collections::HashMap::new();
|
||||
let mut word_counts2 = std::collections::HashMap::new();
|
||||
|
||||
for word in &words1 {
|
||||
*word_counts1.entry(*word).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
for word in &words2 {
|
||||
*word_counts2.entry(*word).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
// Calculate intersection size
|
||||
let mut intersection = 0;
|
||||
let mut important_word_matches = 0;
|
||||
|
||||
for (word, count1) in &word_counts1 {
|
||||
if word.len() <= 3 {
|
||||
continue; // Skip short common words
|
||||
}
|
||||
|
||||
if let Some(count2) = word_counts2.get(word) {
|
||||
intersection += count1.min(count2);
|
||||
|
||||
// Consider longer words more important
|
||||
if word.len() >= 5 {
|
||||
important_word_matches += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate Jaccard similarity
|
||||
let union = words1.len() + words2.len() - intersection;
|
||||
let jaccard = if union > 0 {
|
||||
intersection as f32 / union as f32
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
// Boost score if important words match
|
||||
let importance_bonus = (important_word_matches as f32 * 0.1).min(0.3);
|
||||
|
||||
(jaccard + importance_bonus).min(1.0)
|
||||
}
|
||||
|
||||
/// Calculate similarity between plugin names using an improved algorithm
|
||||
fn calculate_name_similarity(name1: &str, name2: &str) -> f32 {
|
||||
let name1_lower = name1.to_lowercase();
|
||||
let name2_lower = name2.to_lowercase();
|
||||
@@ -247,30 +836,184 @@ fn calculate_name_similarity(name1: &str, name2: &str) -> f32 {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
// Contains check
|
||||
if name1_lower.contains(&name2_lower) || name2_lower.contains(&name1_lower) {
|
||||
return 0.9;
|
||||
// First, normalize the names by removing common irrelevant words
|
||||
let name1_normalized = normalize_plugin_name(&name1_lower);
|
||||
let name2_normalized = normalize_plugin_name(&name2_lower);
|
||||
|
||||
// Check normalized equality
|
||||
if name1_normalized == name2_normalized {
|
||||
return 0.95; // Almost perfect match after normalization
|
||||
}
|
||||
|
||||
// Simple word matching
|
||||
let words1: Vec<&str> = name1_lower.split_whitespace().collect();
|
||||
let words2: Vec<&str> = name2_lower.split_whitespace().collect();
|
||||
// Contains check with improved weighting
|
||||
if name1_normalized.contains(&name2_normalized) || name2_normalized.contains(&name1_normalized) {
|
||||
// Calculate containment score based on length ratios
|
||||
let length_ratio = name1_normalized.len() as f32 / name2_normalized.len().max(1) as f32;
|
||||
let normalized_ratio = if length_ratio > 1.0 { 1.0 / length_ratio } else { length_ratio };
|
||||
|
||||
let mut matched_words = 0;
|
||||
let total_words = words1.len().max(words2.len());
|
||||
// Higher score for closer length ratios
|
||||
return 0.7 + (normalized_ratio * 0.2);
|
||||
}
|
||||
|
||||
// Split by common separators and compute word-level similarity
|
||||
let words1: Vec<&str> = name1_normalized
|
||||
.split(|c| c == ' ' || c == '-' || c == '_')
|
||||
.filter(|w| !w.is_empty() && w.len() > 2)
|
||||
.collect();
|
||||
|
||||
let words2: Vec<&str> = name2_normalized
|
||||
.split(|c| c == ' ' || c == '-' || c == '_')
|
||||
.filter(|w| !w.is_empty() && w.len() > 2)
|
||||
.collect();
|
||||
|
||||
if words1.is_empty() || words2.is_empty() {
|
||||
// Fallback to character-level similarity for short names or single words
|
||||
return calculate_levenshtein_similarity(&name1_normalized, &name2_normalized);
|
||||
}
|
||||
|
||||
// Word-level similarity calculation with TF-IDF style weighting
|
||||
let mut similarity_score = 0.0f32;
|
||||
let mut total_weight = 0.0f32;
|
||||
|
||||
for word1 in &words1 {
|
||||
// Words with higher length get more weight (similar to IDF)
|
||||
let word_weight = (word1.len() as f32).min(5.0) / 5.0;
|
||||
total_weight += word_weight;
|
||||
|
||||
let mut best_word_match: f32 = 0.0f32;
|
||||
|
||||
for word2 in &words2 {
|
||||
if word1 == word2 && word1.len() > 2 { // Ignore short words
|
||||
matched_words += 1;
|
||||
break;
|
||||
// For each word, find the best matching word in the other name
|
||||
let word_similarity = if word1 == word2 {
|
||||
1.0f32 // Exact word match
|
||||
} else {
|
||||
calculate_levenshtein_similarity(word1, word2)
|
||||
};
|
||||
|
||||
best_word_match = best_word_match.max(word_similarity);
|
||||
}
|
||||
|
||||
similarity_score += best_word_match * word_weight;
|
||||
}
|
||||
|
||||
// Normalize by total weight
|
||||
if total_weight > 0.0 {
|
||||
similarity_score /= total_weight;
|
||||
}
|
||||
|
||||
// Extra boost for plugins with first word matching
|
||||
// This helps for plugins like "WorldEdit" vs "FastWorldEdit"
|
||||
if !words1.is_empty() && !words2.is_empty() && words1[0] == words2[0] {
|
||||
similarity_score = (similarity_score + 0.2).min(0.9);
|
||||
}
|
||||
|
||||
similarity_score
|
||||
}
|
||||
|
||||
/// Normalize a plugin name by removing common prefixes, suffixes, and irrelevant words
|
||||
fn normalize_plugin_name(name: &str) -> String {
|
||||
let mut normalized = name.to_lowercase();
|
||||
|
||||
// Remove common version indicators
|
||||
let version_patterns = [
|
||||
" v1", " v2", " v3", " v4", " v5",
|
||||
" 1.0", " 2.0", " 3.0", " 4.0", " 5.0",
|
||||
" (1.", " (2.", " (3.",
|
||||
];
|
||||
|
||||
for pattern in &version_patterns {
|
||||
if let Some(pos) = normalized.find(pattern) {
|
||||
normalized = normalized[0..pos].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
// Remove common irrelevant words
|
||||
let irrelevant_words = [
|
||||
"plugin", "mod", "minecraft", "bukkit", "spigot", "paper", "forge", "fabric",
|
||||
"the", "for", "and", "lite", "full", "version", "free", "premium", "pro"
|
||||
];
|
||||
|
||||
for word in &irrelevant_words {
|
||||
let pattern = format!(" {} ", word);
|
||||
normalized = normalized.replace(&pattern, " ");
|
||||
|
||||
// Also check at the beginning and end
|
||||
let start_pattern = format!("{} ", word);
|
||||
let end_pattern = format!(" {}", word);
|
||||
|
||||
normalized = normalized.replace(&start_pattern, "");
|
||||
normalized = normalized.replace(&end_pattern, "");
|
||||
}
|
||||
|
||||
// Trim and remove duplicate spaces
|
||||
normalized = normalized.trim().to_string();
|
||||
while normalized.contains(" ") {
|
||||
normalized = normalized.replace(" ", " ");
|
||||
}
|
||||
|
||||
normalized
|
||||
}
|
||||
|
||||
/// Calculate Levenshtein similarity between two strings
|
||||
fn calculate_levenshtein_similarity(s1: &str, s2: &str) -> f32 {
|
||||
if s1.is_empty() || s2.is_empty() {
|
||||
return if s1.is_empty() && s2.is_empty() { 1.0 } else { 0.0 };
|
||||
}
|
||||
|
||||
// Optimization for strings with large length difference
|
||||
let len_diff = (s1.len() as isize - s2.len() as isize).abs() as usize;
|
||||
if len_diff > s1.len() / 2 || len_diff > s2.len() / 2 {
|
||||
return 0.2; // Low similarity for strings with very different lengths
|
||||
}
|
||||
|
||||
// Early exact matching check
|
||||
if s1 == s2 {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
// Convert strings to character vectors for easier access
|
||||
let s1_chars: Vec<char> = s1.chars().collect();
|
||||
let s2_chars: Vec<char> = s2.chars().collect();
|
||||
let len1 = s1_chars.len();
|
||||
let len2 = s2_chars.len();
|
||||
|
||||
// Create matrix for dynamic programming
|
||||
let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];
|
||||
|
||||
// Initialize first row and column
|
||||
for i in 0..=len1 {
|
||||
matrix[i][0] = i;
|
||||
}
|
||||
for j in 0..=len2 {
|
||||
matrix[0][j] = j;
|
||||
}
|
||||
|
||||
// Fill the matrix
|
||||
for i in 1..=len1 {
|
||||
for j in 1..=len2 {
|
||||
let cost = if s1_chars[i-1] == s2_chars[j-1] { 0 } else { 1 };
|
||||
|
||||
matrix[i][j] = [
|
||||
matrix[i-1][j] + 1, // deletion
|
||||
matrix[i][j-1] + 1, // insertion
|
||||
matrix[i-1][j-1] + cost, // substitution
|
||||
].iter().min().unwrap().clone();
|
||||
|
||||
// Transposition (swap two adjacent characters)
|
||||
if i > 1 && j > 1 && s1_chars[i-1] == s2_chars[j-2] && s1_chars[i-2] == s2_chars[j-1] {
|
||||
matrix[i][j] = matrix[i-2][j-2] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if total_words > 0 {
|
||||
matched_words as f32 / total_words as f32
|
||||
// Calculate normalized similarity
|
||||
let distance = matrix[len1][len2] as f32;
|
||||
let max_length = len1.max(len2) as f32;
|
||||
|
||||
// Normalize to a similarity score (1.0 = identical, 0.0 = completely different)
|
||||
if max_length > 0.0 {
|
||||
(max_length - distance) / max_length
|
||||
} else {
|
||||
0.0
|
||||
1.0
|
||||
}
|
||||
}
|
||||
@@ -3,36 +3,283 @@ use regex::Regex;
|
||||
|
||||
/// Normalize a version string to be semver compatible
|
||||
pub fn normalize_version(version_str: &str) -> String {
|
||||
// If already starts with a digit, assume semantic version format
|
||||
if version_str.chars().next().map_or(false, |c| c.is_ascii_digit()) {
|
||||
// Clean up any common prefixes like 'v'
|
||||
let cleaned = version_str.trim_start_matches(|c| c == 'v' || c == 'V');
|
||||
return cleaned.to_string();
|
||||
// Handle empty strings or "Latest"
|
||||
if version_str.is_empty() || version_str.eq_ignore_ascii_case("latest") {
|
||||
return "0.0.0".to_string(); // Default version if none provided
|
||||
}
|
||||
|
||||
// Return as-is for now
|
||||
version_str.to_string()
|
||||
// Remove 'v' prefix if present
|
||||
let cleaned = version_str.trim_start_matches(|c| c == 'v' || c == 'V');
|
||||
|
||||
// Remove double 'v' prefixes (like "vv1.0.0")
|
||||
let cleaned = if cleaned.starts_with("v") || cleaned.starts_with("V") {
|
||||
cleaned.trim_start_matches(|c| c == 'v' || c == 'V')
|
||||
} else {
|
||||
cleaned
|
||||
};
|
||||
|
||||
cleaned.to_string()
|
||||
}
|
||||
|
||||
/// Sanitize version string for comparison by removing platform suffixes
|
||||
fn sanitize_version_for_comparison(version_str: &str) -> String {
|
||||
// First normalize the version
|
||||
let normalized = normalize_version(version_str);
|
||||
|
||||
// Remove common platform-specific suffixes
|
||||
let platform_suffixes = [
|
||||
"-paper", "-spigot", "-bukkit", "-forge", "-fabric", "-neoforge",
|
||||
"-sponge", "-velocity", "-waterfall", "-bungeecord", "-quilt"
|
||||
];
|
||||
|
||||
let mut result = normalized.to_string();
|
||||
|
||||
// Case-insensitive suffix removal
|
||||
for suffix in platform_suffixes.iter() {
|
||||
let suffix_lower = suffix.to_lowercase();
|
||||
let version_lower = result.to_lowercase();
|
||||
|
||||
if version_lower.contains(&suffix_lower) {
|
||||
// Find the position of the suffix (case-insensitive)
|
||||
if let Some(pos) = version_lower.find(&suffix_lower) {
|
||||
// Remove the suffix (with original casing)
|
||||
result = result[0..pos].to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove any build metadata (anything after +)
|
||||
if let Some(plus_pos) = result.find('+') {
|
||||
result = result[0..plus_pos].to_string();
|
||||
}
|
||||
|
||||
// Handle snapshot versions with build numbers
|
||||
if result.contains("-SNAPSHOT") || result.contains("-snapshot") {
|
||||
// Extract just the version part before any build info
|
||||
if let Some(snapshot_pos) = result.to_lowercase().find("-snapshot") {
|
||||
result = result[0..snapshot_pos].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize dev build formats that use dash-separated numbers
|
||||
let build_regex = Regex::new(r"-build\d+").unwrap();
|
||||
result = build_regex.replace_all(&result, "").to_string();
|
||||
|
||||
// Handle other common non-numeric suffixes
|
||||
let common_suffixes = ["-RELEASE", "-dev", "-final", "-stable"];
|
||||
for suffix in common_suffixes.iter() {
|
||||
let suffix_lower = suffix.to_lowercase();
|
||||
let version_lower = result.to_lowercase();
|
||||
|
||||
if version_lower.ends_with(&suffix_lower) {
|
||||
let suffix_len = suffix_lower.len();
|
||||
result = result[0..result.len() - suffix_len].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
// If we've removed everything, return the original normalized version
|
||||
if result.is_empty() {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
// Make sure it ends with at least one digit for semver parsing
|
||||
if !result.chars().last().map_or(false, |c| c.is_ascii_digit()) {
|
||||
if let Some(last_digit_pos) = result.rfind(|c: char| c.is_ascii_digit()) {
|
||||
result = result[0..=last_digit_pos].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
// If we've removed version numbers entirely, return the original
|
||||
if !result.chars().any(|c| c.is_ascii_digit()) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Extracts platform suffix from a version string
|
||||
pub fn extract_platform_suffix(version_str: &str) -> Option<String> {
|
||||
// Platform suffixes to check for
|
||||
let platform_suffixes = [
|
||||
"-paper", "-spigot", "-bukkit", "-forge", "-fabric", "-neoforge",
|
||||
"-sponge", "-velocity", "-waterfall", "-bungeecord", "-quilt"
|
||||
];
|
||||
|
||||
let version_lower = version_str.to_lowercase();
|
||||
|
||||
for suffix in platform_suffixes.iter() {
|
||||
let suffix_lower = suffix.to_lowercase();
|
||||
if version_lower.contains(&suffix_lower) {
|
||||
// Return the actual platform with original case
|
||||
return Some(suffix_lower[1..].to_string()); // Remove the leading '-'
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Determine if a version is a pre-release (snapshot, beta, etc.)
|
||||
fn is_prerelease_version(version_str: &str) -> bool {
|
||||
let version_lower = version_str.to_lowercase();
|
||||
|
||||
// Check for common pre-release indicators
|
||||
version_lower.contains("-snapshot") ||
|
||||
version_lower.contains("-alpha") ||
|
||||
version_lower.contains("-beta") ||
|
||||
version_lower.contains("-pre") ||
|
||||
version_lower.contains("-rc") ||
|
||||
version_lower.contains("dev") ||
|
||||
version_lower.contains("test") ||
|
||||
version_lower.contains("nightly")
|
||||
}
|
||||
|
||||
/// Compare two plugin versions to determine if an update is available
|
||||
/// Returns true if repo_str represents a newer version than installed_str
|
||||
pub fn compare_plugin_versions(installed_str: &str, repo_str: &str) -> bool {
|
||||
// Normalize version strings
|
||||
let installed_version = normalize_version(installed_str);
|
||||
let repo_version = normalize_version(repo_str);
|
||||
// Special case: identical strings are never upgrades
|
||||
if installed_str == repo_str {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extract platform suffixes
|
||||
let installed_platform = extract_platform_suffix(installed_str);
|
||||
let repo_platform = extract_platform_suffix(repo_str);
|
||||
|
||||
// If platforms differ and both are specified, it's not considered an upgrade
|
||||
// (we don't want to suggest forge versions for paper plugins)
|
||||
if let (Some(installed_p), Some(repo_p)) = (&installed_platform, &repo_platform) {
|
||||
if installed_p != repo_p {
|
||||
println!("Platforms differ: {} vs {}, not an upgrade", installed_p, repo_p);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for downgrades from release to prerelease
|
||||
let installed_is_prerelease = is_prerelease_version(installed_str);
|
||||
let repo_is_prerelease = is_prerelease_version(repo_str);
|
||||
|
||||
// Don't consider a pre-release version an upgrade from a stable version
|
||||
if !installed_is_prerelease && repo_is_prerelease {
|
||||
println!("Not upgrading from release {} to pre-release {}", installed_str, repo_str);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Sanitize versions for comparison by removing platform-specific suffixes
|
||||
let sanitized_installed = sanitize_version_for_comparison(installed_str);
|
||||
let sanitized_repo = sanitize_version_for_comparison(repo_str);
|
||||
|
||||
// Log for debugging
|
||||
println!("Comparing versions: '{}'({}') vs '{}'('{}')",
|
||||
installed_str, sanitized_installed, repo_str, sanitized_repo);
|
||||
|
||||
// Try to parse as semver
|
||||
match (Version::parse(&installed_version), Version::parse(&repo_version)) {
|
||||
match (Version::parse(&sanitized_installed), Version::parse(&sanitized_repo)) {
|
||||
(Ok(installed), Ok(repo)) => {
|
||||
// Simple semver comparison
|
||||
// Properly formatted semver comparison
|
||||
println!(" Using semver comparison: {} vs {}", installed, repo);
|
||||
repo > installed
|
||||
},
|
||||
_ => {
|
||||
// Fallback to simple string comparison for non-semver versions
|
||||
repo_version != installed_version
|
||||
// Fallback to more sophisticated string comparison for non-semver versions
|
||||
if sanitized_installed == sanitized_repo {
|
||||
// Same base version, check if repo has a higher build number or qualifier
|
||||
compare_version_qualifiers(installed_str, repo_str)
|
||||
} else {
|
||||
// Try numeric component-by-component comparison
|
||||
compare_version_components(&sanitized_installed, &sanitized_repo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compare version qualifiers and build numbers when base versions are the same
|
||||
fn compare_version_qualifiers(installed_str: &str, repo_str: &str) -> bool {
|
||||
// Try to extract build numbers
|
||||
let installed_build = extract_build_number(installed_str);
|
||||
let repo_build = extract_build_number(repo_str);
|
||||
|
||||
if let (Some(i_build), Some(r_build)) = (installed_build, repo_build) {
|
||||
return r_build > i_build;
|
||||
}
|
||||
|
||||
// If qualifiers differ, use a rank-based system
|
||||
let installed_rank = get_qualifier_rank(installed_str);
|
||||
let repo_rank = get_qualifier_rank(repo_str);
|
||||
|
||||
if installed_rank != repo_rank {
|
||||
return repo_rank > installed_rank;
|
||||
}
|
||||
|
||||
// Default case - not considered an upgrade
|
||||
false
|
||||
}
|
||||
|
||||
/// Assign rank values to different version qualifiers
|
||||
fn get_qualifier_rank(version_str: &str) -> i32 {
|
||||
let lower_str = version_str.to_lowercase();
|
||||
|
||||
if lower_str.contains("alpha") { return 10; }
|
||||
if lower_str.contains("beta") { return 20; }
|
||||
if lower_str.contains("rc") { return 30; }
|
||||
if lower_str.contains("pre") { return 40; }
|
||||
if lower_str.contains("snapshot") { return 50; }
|
||||
if lower_str.contains("nightly") { return 60; }
|
||||
if lower_str.contains("dev") { return 70; }
|
||||
if lower_str.contains("release") { return 100; }
|
||||
|
||||
// Default for stable releases
|
||||
90
|
||||
}
|
||||
|
||||
/// Extract build number from a version string
|
||||
fn extract_build_number(version_str: &str) -> Option<i32> {
|
||||
// Try to find patterns like "build123" or "-b123" or ".123"
|
||||
let build_patterns = [
|
||||
Regex::new(r"build(\d+)").ok()?,
|
||||
Regex::new(r"-b(\d+)").ok()?,
|
||||
Regex::new(r"\.(\d+)$").ok()?,
|
||||
Regex::new(r"-(\d+)$").ok()?,
|
||||
];
|
||||
|
||||
for pattern in &build_patterns {
|
||||
if let Some(captures) = pattern.captures(version_str) {
|
||||
if let Some(build_match) = captures.get(1) {
|
||||
if let Ok(build) = build_match.as_str().parse::<i32>() {
|
||||
return Some(build);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Compare version strings component by component
|
||||
fn compare_version_components(version1: &str, version2: &str) -> bool {
|
||||
// Split versions into numeric and non-numeric parts
|
||||
let v1_parts: Vec<&str> = version1.split(|c: char| !c.is_ascii_digit()).filter(|s| !s.is_empty()).collect();
|
||||
let v2_parts: Vec<&str> = version2.split(|c: char| !c.is_ascii_digit()).filter(|s| !s.is_empty()).collect();
|
||||
|
||||
// Compare corresponding numeric parts
|
||||
let max_parts = v1_parts.len().max(v2_parts.len());
|
||||
|
||||
for i in 0..max_parts {
|
||||
let n1 = v1_parts.get(i).and_then(|s| s.parse::<i32>().ok()).unwrap_or(0);
|
||||
let n2 = v2_parts.get(i).and_then(|s| s.parse::<i32>().ok()).unwrap_or(0);
|
||||
|
||||
if n2 > n1 {
|
||||
return true; // Version 2 is higher
|
||||
}
|
||||
if n1 > n2 {
|
||||
return false; // Version 1 is higher
|
||||
}
|
||||
}
|
||||
|
||||
// If all numeric parts are equal, compare by string length
|
||||
// (Consider more segments to be a higher version, like 1.2.3 > 1.2)
|
||||
v2_parts.len() > v1_parts.len()
|
||||
}
|
||||
|
||||
/// Extract version pattern from a string
|
||||
pub fn extract_version_pattern(input: &str) -> Option<String> {
|
||||
// Look for version pattern like 1.19.2 in string
|
||||
@@ -46,15 +293,34 @@ pub fn extract_version_pattern(input: &str) -> Option<String> {
|
||||
}
|
||||
|
||||
/// Check if a plugin version is compatible with a specific Minecraft version
|
||||
pub fn is_version_compatible(plugin_version: &str, minecraft_version: &str) -> bool {
|
||||
// Try to parse the Minecraft version
|
||||
if let Ok(mc_version) = Version::parse(minecraft_version) {
|
||||
// Try to parse as a version requirement
|
||||
if let Ok(req) = VersionReq::parse(plugin_version) {
|
||||
return req.matches(&mc_version);
|
||||
pub fn is_version_compatible(plugin_version: &str, minecraft_versions: &Vec<String>) -> bool {
|
||||
// If no versions specified, assume compatible
|
||||
if minecraft_versions.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if the Minecraft version is in the list of supported versions
|
||||
for supported_version in minecraft_versions {
|
||||
if plugin_version == supported_version {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try to parse the Minecraft version
|
||||
if let Ok(mc_version) = semver::Version::parse(plugin_version) {
|
||||
// Try to parse as a version requirement
|
||||
if let Ok(req) = semver::VersionReq::parse(supported_version) {
|
||||
if req.matches(&mc_version) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If version formats are incompatible, make best guess
|
||||
if plugin_version.contains(supported_version) || supported_version.contains(plugin_version) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// If version formats are incompatible, make best guess
|
||||
plugin_version.contains(minecraft_version)
|
||||
// No compatibility match found
|
||||
false
|
||||
}
|
||||
Reference in new issue
Block a user