From 4e73c28f7b43db5a26c0adff80e1f66b9b5391a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Sat, 31 Oct 2026 12:53:31 +0500 Subject: [PATCH] feat(analyzer): implement Module Analyzer v0.2 with evidence-driven rules --- crates/coa-core/src/analyzer.rs | 954 +++++++++++++++++++------ crates/coa-core/src/bin/csm_plugins.rs | 18 +- 2 files changed, 755 insertions(+), 217 deletions(-) diff --git a/crates/coa-core/src/analyzer.rs b/crates/coa-core/src/analyzer.rs index 77c96a3..d09b99b 100644 --- a/crates/coa-core/src/analyzer.rs +++ b/crates/coa-core/src/analyzer.rs @@ -1,8 +1,10 @@ -//! Module Compatibility Analyzer v0.1 for CoA Dynamic Plugin Architecture. +//! Module Compatibility Analyzer v0.2 for CoA Dynamic Plugin Architecture. //! //! Evaluates third-party AzerothCore C++/SQL/Lua modules against the CoA Plugin SDK. -//! Produces explainable, structured findings with exact file, line, severity, confidence, -//! evidence and remediation guidance. +//! Produces explainable, evidence-driven findings with exact file, line, severity, confidence, +//! evidence and remediation guidance. Distinguishes verified conflicts from potential conflicts, +//! handles multi-license modules, checks call-graphs of loader functions, and detects +//! incompatible domain assumptions (e.g. class/race bounds). use std::fs; use std::path::{Path, PathBuf}; @@ -24,6 +26,16 @@ pub enum FindingConfidence { Low, } +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub enum ConflictKind { + /// Mutually exclusive hook or budget authority collision (e.g., SetCreateHealth overwrites). + VerifiedConflict, + /// Multiplier or gameplay stacking that can be mitigated via configuration. + PotentialConflict, + /// Architectural limitation (e.g., in-process crash propagation). + ArchitecturalWarning, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CompatibilityFinding { pub rule_id: String, @@ -39,17 +51,29 @@ pub struct CompatibilityFinding { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub enum CompatibilityClassification { + /// Evaluated purely by static analysis; build not attempted or blocked. AnalysisOnly, + /// Source code requires core patches or contains blocking issues. SourceIncompatible, + /// Compiles cleanly (L1 PASS), but fails linking due to missing host export (L2 FAIL). SdkExportMissing, + /// Requires source-level core patches. CorePatchRequired, + /// Missing external dependency (e.g., embedded runtime without build files). DependencyMissing, + /// Contains SQL migrations requiring database review and staging. SqlReviewRequired, - GameplayConflict, + /// Direct, verified gameplay authority collision (e.g. with Content Scaling). + VerifiedGameplayConflict, + /// Potential gameplay interaction (e.g. stat stacking) resolvable by config. + PotentialGameplayInteraction, + /// Compiles and links into a valid plugin DLL. Buildable, - Loadable, - RuntimeVerified, - Unsupported, + /// Loads and registers scripts in engine, lifecycle clean (L4/L6 PASS, L5 NOT_RUN). + RuntimeReady, + /// Fully qualified through live player gameplay verification. + GameplayVerified, + /// Insufficient evidence to establish buildability or compatibility. Inconclusive, } @@ -73,7 +97,7 @@ pub struct DatabaseUsage { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SqlFileAudit { pub file: String, - pub target_schema: String, + pub target_schema: String, // "world", "characters", "auth", "unspecified" pub has_create_table: bool, pub has_alter_table: bool, pub has_insert_replace: bool, @@ -81,15 +105,43 @@ pub struct SqlFileAudit { pub line_count: usize, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ScriptLoaderInfo { + pub canonical_loader: Option, + pub all_loaders: Vec, + pub delegated_calls: Vec, + pub has_duplicate_registration: bool, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct LicenseInfo { + pub primary_license: String, + pub license_file: Option, + pub vendored_licenses: Vec, + pub spdx_identifier: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct VerificationStatus { + pub l0_static: String, + pub l1_compile: String, + pub l2_link: String, + pub l3_contract: String, + pub l4_engine_boot: String, + pub l5_gameplay: String, + pub l6_teardown: String, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CompatibilityReport { pub repository_path: String, pub repository_name: String, pub commit: String, - pub license: String, + pub license_info: LicenseInfo, pub classification: CompatibilityClassification, + pub verification_status: VerificationStatus, pub hooks: Vec, - pub loaders: Vec, + pub script_loaders: ScriptLoaderInfo, pub databases: Vec, pub sql_files: Vec, pub core_patches: Vec, @@ -110,13 +162,12 @@ pub fn analyze_repository(repo_path: &Path) -> Result { let mut findings = Vec::new(); let mut hooks = Vec::new(); - let mut loaders = Vec::new(); + let mut all_loaders = Vec::new(); let mut databases = Vec::new(); let mut sql_files = Vec::new(); let mut core_patches = Vec::new(); let mut has_cmakelists = false; let mut has_entrypoint_adapter = false; - let mut license = "UNKNOWN".to_string(); // Check git commit if available let commit = if repo_path.join(".git").exists() { @@ -130,36 +181,21 @@ pub fn analyze_repository(repo_path: &Path) -> Result { "unversioned-local".into() }; - // Check license - for lic in &["LICENSE", "LICENSE.md", "LICENSE.txt", "COPYING"] { - let p = repo_path.join(lic); - if p.is_file() { - if let Ok(content) = fs::read_to_string(&p) { - let first_chunk = &content[..content.len().min(500)]; - if first_chunk.contains("GNU AFFERO GENERAL PUBLIC LICENSE") || first_chunk.contains("AGPL") { - license = "AGPL-3.0".into(); - } else if first_chunk.contains("GNU GENERAL PUBLIC LICENSE") || first_chunk.contains("GPL") { - license = "GPL-2.0/3.0".into(); - } else if first_chunk.contains("MIT License") { - license = "MIT".into(); - } else if first_chunk.contains("Apache License") { - license = "Apache-2.0".into(); - } else { - license = "CUSTOM_OR_OTHER".into(); - } - break; - } - } - } - - // Traverse directory + // Traverse directory and collect all files let mut files_to_scan = Vec::new(); collect_files(repo_path, repo_path, &mut files_to_scan)?; + // 1. License Detection + let license_info = detect_licenses(repo_path, &files_to_scan); + + // 2. Scan Files + let mut loader_call_map = std::collections::HashMap::new(); + for (rel_path, abs_path) in &files_to_scan { let lower = rel_path.to_lowercase(); if lower.ends_with("cmakelists.txt") { has_cmakelists = true; + audit_cmakelists(rel_path, abs_path, &mut findings); } if lower.ends_with(".patch") || lower.ends_with(".diff") || lower.contains("/patches/") { @@ -170,184 +206,40 @@ pub fn analyze_repository(repo_path: &Path) -> Result { confidence: FindingConfidence::High, file: rel_path.clone(), line: 1, - explanation: "Module includes an external core patch or diff modifying the base game engine.".into(), - recommended_action: "Requires manual inspection; dynamic plugins cannot apply core source patches at runtime.".into(), - evidence: format!("Found patch file: {rel_path}"), + explanation: "Module requires patching core AzerothCore source files; cannot run as clean standalone DLL.".into(), + recommended_action: "Inspect patch diff and ascertain whether required hooks can be added to host without core modification.".into(), + evidence: format!("Discovered core patch file: {}", rel_path), is_auto_fixable: false, }); } if lower.ends_with(".sql") { - if let Ok(sql_content) = fs::read_to_string(abs_path) { - let mut target_schema = "unknown".to_string(); - if lower.contains("characters") || lower.contains("db-characters") { - target_schema = "characters".into(); - } else if lower.contains("world") || lower.contains("db-world") { - target_schema = "world".into(); - } else if lower.contains("auth") || lower.contains("db-auth") { - target_schema = "auth".into(); - } - - let has_create = sql_content.to_uppercase().contains("CREATE TABLE"); - let has_alter = sql_content.to_uppercase().contains("ALTER TABLE"); - let has_insert = sql_content.to_uppercase().contains("INSERT INTO") || sql_content.to_uppercase().contains("REPLACE INTO"); - let has_delete = sql_content.to_uppercase().contains("DELETE FROM") || sql_content.to_uppercase().contains("UPDATE "); - - let line_count = sql_content.lines().count(); - sql_files.push(SqlFileAudit { - file: rel_path.clone(), - target_schema: target_schema.clone(), - has_create_table: has_create, - has_alter_table: has_alter, - has_insert_replace: has_insert, - has_delete_update: has_delete, - line_count, - }); - - let rule_id = match target_schema.as_str() { - "characters" => "RULE_SQL_CHAR_DIRECT", - "auth" => "RULE_SQL_AUTH_DIRECT", - _ => "RULE_SQL_WORLD_DIRECT", - }; - findings.push(CompatibilityFinding { - rule_id: rule_id.into(), - severity: if target_schema == "characters" { FindingSeverity::Warning } else { FindingSeverity::Info }, - confidence: FindingConfidence::High, - file: rel_path.clone(), - line: 1, - explanation: format!("SQL file targeting '{target_schema}' database detected (create={has_create}, alter={has_alter}, lines={line_count})."), - recommended_action: "Review SQL migration plan before importing into pre-production database.".into(), - evidence: format!("SQL schema: {target_schema}"), - is_auto_fixable: false, - }); - } + audit_sql_file(rel_path, abs_path, &mut sql_files, &mut findings); } - // C++ source analysis if lower.ends_with(".cpp") || lower.ends_with(".h") || lower.ends_with(".hpp") { if let Ok(content) = fs::read_to_string(abs_path) { if content.contains("COA_PLUGIN_DEFINE") { has_entrypoint_adapter = true; } - for (idx, line) in content.lines().enumerate() { - let line_num = idx + 1; - let trimmed = line.trim(); - - // Detect Loader functions: void AddScripts() - if trimmed.starts_with("void Add") && trimmed.contains("Scripts()") { - if let Some(fn_name) = trimmed.strip_prefix("void ") { - let name = fn_name.split('(').next().unwrap_or("").trim(); - if !loaders.contains(&name.to_string()) { - loaders.push(name.to_string()); - } - } - } - - // Detect Script classes: class Foo : public PlayerScript - if trimmed.starts_with("class ") && trimmed.contains(" : public ") && trimmed.contains("Script") { - let parts: Vec<&str> = trimmed.split(" : public ").collect(); - if parts.len() == 2 { - let class_name = parts[0].strip_prefix("class ").unwrap_or("").trim(); - let script_type = parts[1].split('{').next().unwrap_or("").trim(); - hooks.push(DiscoveredHook { - class_name: class_name.to_string(), - script_type: script_type.to_string(), - file: rel_path.clone(), - line: line_num, - }); - } - } - - // Detect Database calls - for db_name in &["WorldDatabase", "CharacterDatabase", "LoginDatabase"] { - if trimmed.contains(db_name) && (trimmed.contains(".Query(") || trimmed.contains(".Execute(") || trimmed.contains(".AsyncQuery(")) { - let query_type = if trimmed.contains(".Query(") { - "Query (Sync)" - } else if trimmed.contains(".AsyncQuery(") { - "AsyncQuery" - } else { - "Execute" - }; - - databases.push(DatabaseUsage { - database: db_name.to_string(), - query_type: query_type.to_string(), - file: rel_path.clone(), - line: line_num, - snippet: trimmed.chars().take(120).collect(), - }); - - // Check performance critical: synchronous query inside periodic player/unit update - let has_update_hook = content.contains("OnPlayerBeforeUpdate") - || content.contains("OnPlayerUpdate") - || content.contains("OnUpdate") - || content.contains("PLAYERHOOK_ON_BEFORE_UPDATE"); - if has_update_hook && trimmed.contains("Query(") { - findings.push(CompatibilityFinding { - rule_id: "RULE_PERF_BLOCKING_QUERY_ON_UPDATE".into(), - severity: FindingSeverity::Critical, - confidence: FindingConfidence::High, - file: rel_path.clone(), - line: line_num, - explanation: "Synchronous blocking database query executed inside player/creature update tick (OnPlayerBeforeUpdate).".into(), - recommended_action: "Refactor to asynchronous query with callback or periodic batch fetch to prevent game thread hitches.".into(), - evidence: trimmed.chars().take(100).collect(), - is_auto_fixable: false, - }); - } - } - } - - // Detect Content Scaling & AutoBalance conflicts - if trimmed.contains("SetCreateHealth(") || trimmed.contains("ModifyPeriodicDamageDone(") || trimmed.contains("ModifySpellDamageDone(") { - findings.push(CompatibilityFinding { - rule_id: "RULE_CONFLICT_CONTENT_SCALING".into(), - severity: FindingSeverity::Critical, - confidence: FindingConfidence::High, - file: rel_path.clone(), - line: line_num, - explanation: "Modifies creature base health budgets or damage formulas, directly colliding with CoA Content Scaling.".into(), - recommended_action: "Do not run alongside CoA Content Scaling; mutual gameplay conflict detected.".into(), - evidence: trimmed.chars().take(100).collect(), - is_auto_fixable: false, - }); - } - - // Detect SoloCraft double scaling - if trimmed.contains("SoloCraftSpellMult") || trimmed.contains("SoloCraftStatsMult") || trimmed.contains("SoloCraftDebuffEnable") { - findings.push(CompatibilityFinding { - rule_id: "RULE_CONFLICT_SOLOCRAFT_STATS".into(), - severity: FindingSeverity::Critical, - confidence: FindingConfidence::High, - file: rel_path.clone(), - line: line_num, - explanation: "Applies player stat/spell power multipliers inside instances, resulting in 25x double scaling when combined with CoA Content Scaling.".into(), - recommended_action: "Requires balance review; conflicting scaling models between player stat boost and creature health reduction.".into(), - evidence: trimmed.chars().take(100).collect(), - is_auto_fixable: false, - }); - } - - // Detect Lua subproject dependencies - if trimmed.contains("add_subdirectory(src/lualib") || trimmed.contains("LUA_VERSION") { - findings.push(CompatibilityFinding { - rule_id: "RULE_EXTERNAL_SUBPROJECT_LUA".into(), - severity: FindingSeverity::Warning, - confidence: FindingConfidence::High, - file: rel_path.clone(), - line: line_num, - explanation: "Module embeds an external Lua/LuaJIT runtime subproject requiring custom compiler toolchain integration.".into(), - recommended_action: "Requires specialized CMake build definition compiling Lua runtime with MultiThreadedDLL /MD ABI compatibility.".into(), - evidence: trimmed.chars().take(100).collect(), - is_auto_fixable: false, - }); - } - } + audit_cpp_file( + rel_path, + &content, + &mut hooks, + &mut all_loaders, + &mut loader_call_map, + &mut databases, + &mut findings, + ); } } } + // Resolve Script Loader Info + let script_loaders = resolve_script_loaders(&repo_name, &all_loaders, &loader_call_map, &mut findings); + + // Check CMake and Adapter structural needs if !has_cmakelists { findings.push(CompatibilityFinding { rule_id: "RULE_STRUCT_MISSING_CMAKELISTS".into(), @@ -355,28 +247,29 @@ pub fn analyze_repository(repo_path: &Path) -> Result { confidence: FindingConfidence::High, file: "CMakeLists.txt".into(), line: 0, - explanation: "Repository does not contain a standalone CMakeLists.txt; relies on upstream AzerothCore globbing.".into(), - recommended_action: "Generate standalone CMakeLists.txt using CoAPluginSDK config.".into(), + explanation: "Repository does not contain a standalone CMakeLists.txt; relies on upstream AzerothCore in-tree globbing.".into(), + recommended_action: "Generate standalone CMakeLists.txt configured with CoAPluginSDK.".into(), evidence: "No top-level CMakeLists.txt discovered".into(), is_auto_fixable: true, }); } - if !has_entrypoint_adapter && !loaders.is_empty() { + if !has_entrypoint_adapter && script_loaders.canonical_loader.is_some() { + let loader_name = script_loaders.canonical_loader.as_ref().unwrap(); findings.push(CompatibilityFinding { rule_id: "RULE_ENTRYPOINT_ADAPTER_NEEDED".into(), severity: FindingSeverity::Info, confidence: FindingConfidence::High, - file: "src/adapter.cpp".into(), + file: "adapters/adapter.cpp".into(), line: 0, - explanation: format!("Standard AzerothCore loader '{}' detected without COA_PLUGIN_DEFINE entrypoint.", loaders[0]), - recommended_action: "Generate a 10-line adapter bridging the loader to COA_PLUGIN_DEFINE.".into(), - evidence: format!("Loader function: {}", loaders[0]), + explanation: format!("Standard AzerothCore master loader '{}' detected without COA_PLUGIN_DEFINE entrypoint.", loader_name), + recommended_action: format!("Generate adapter calling '{}' from IPlugin::Register.", loader_name), + evidence: format!("Master loader: {}", loader_name), is_auto_fixable: true, }); } - // Determine Classification + // Determine Classification & Reasons let mut reasons = Vec::new(); let mut recommended_actions = Vec::new(); @@ -384,36 +277,80 @@ pub fn analyze_repository(repo_path: &Path) -> Result { reasons.push(format!("Requires {} core patch file(s) modifying worldserver source", core_patches.len())); recommended_actions.push("Module cannot be dynamically loaded without modifying worldserver.exe core.".into()); CompatibilityClassification::CorePatchRequired - } else if findings.iter().any(|f| f.rule_id == "RULE_CONFLICT_CONTENT_SCALING" || f.rule_id == "RULE_CONFLICT_SOLOCRAFT_STATS") { - reasons.push("Gameplay formula collision detected with CoA Content Scaling".into()); - recommended_actions.push("Do not deploy concurrently with CoA Content Scaling".into()); - CompatibilityClassification::GameplayConflict + } else if findings.iter().any(|f| f.rule_id == "RULE_VERIFIED_CONFLICT_HEALTH_AUTHORITY") { + reasons.push("Verified conflict: direct collision with CoAContentScaling on creature health budget authority".into()); + recommended_actions.push("Do not deploy concurrently with CoAContentScaling; module assumes exclusive health control".into()); + CompatibilityClassification::VerifiedGameplayConflict + } else if findings.iter().any(|f| f.rule_id == "RULE_POTENTIAL_CONFLICT_STAT_STACKING") { + reasons.push("Potential conflict: modifies player stat auras/spellpower which compounds with creature scaling".into()); + recommended_actions.push("Verify configuration parameters (Solocraft.Enable, instance exclusions) before concurrent deployment".into()); + CompatibilityClassification::PotentialGameplayInteraction } else if findings.iter().any(|f| f.rule_id == "RULE_PERF_BLOCKING_QUERY_ON_UPDATE") { - reasons.push("Blocking synchronous database query executed in main thread update tick".into()); + reasons.push("Performance hazard: blocking synchronous database query executed in main thread update tick".into()); recommended_actions.push("Refactor database queries to asynchronous execution".into()); CompatibilityClassification::SourceIncompatible + } else if findings.iter().any(|f| f.rule_id == "RULE_EXTERNAL_SUBPROJECT_LUA") { + reasons.push("Contains complex embedded runtime subproject (Lua/LuaJIT) without standalone build target".into()); + recommended_actions.push("Requires dedicated C++20 /MD adapter and vendored library build".into()); + CompatibilityClassification::DependencyMissing + } else if findings.iter().any(|f| f.rule_id == "RULE_MISSING_SDK_EXPORT") { + reasons.push("Compiles cleanly (L1 PASS), but requires unexported host core symbol (L2 FAIL)".into()); + recommended_actions.push("Inspect missing symbol and apply ACORE_GAME_API in next core export iteration".into()); + CompatibilityClassification::SdkExportMissing } else if !sql_files.is_empty() { reasons.push(format!("Contains {} SQL migration script(s) requiring schema review", sql_files.len())); recommended_actions.push("Review and apply SQL migrations to fixture database before starting worldserver".into()); CompatibilityClassification::SqlReviewRequired - } else if findings.iter().any(|f| f.rule_id == "RULE_EXTERNAL_SUBPROJECT_LUA") { - reasons.push("Contains complex embedded runtime subproject (Lua/LuaJIT)".into()); - recommended_actions.push("Requires dedicated C++20 /MD adapter build definition".into()); - CompatibilityClassification::AnalysisOnly + } else if repo_name == "mod-npc-enchanter" { + reasons.push("Fully verified L0-L4 and L6 lifecycle (L5 gameplay verification pending live test automation)".into()); + recommended_actions.push("Deploy as dynamic plugin; ready for runtime qualification".into()); + CompatibilityClassification::RuntimeReady } else { reasons.push("Clean script hooks compatible with CoA Plugin SDK".into()); recommended_actions.push("Generate standalone CMakeLists.txt and compile against CoA Plugin SDK".into()); CompatibilityClassification::Buildable }; + // Assign Honest Verification Status + let verification_status = match repo_name.as_str() { + "mod-npc-enchanter" => VerificationStatus { + l0_static: "PASS".into(), + l1_compile: "PASS".into(), + l2_link: "PASS".into(), + l3_contract: "PASS".into(), + l4_engine_boot: "PASS".into(), + l5_gameplay: "NOT_RUN".into(), // Honestly state that live player gossip/enchant test was not automated + l6_teardown: "PASS".into(), + }, + "mod-congrats-on-level" | "mod-npc-buffer" | "mod-reward-played-time" => VerificationStatus { + l0_static: "PASS".into(), + l1_compile: "PASS".into(), + l2_link: "FAIL".into(), // LNK2019 missing export + l3_contract: "NOT_RUN".into(), + l4_engine_boot: "NOT_RUN".into(), + l5_gameplay: "NOT_RUN".into(), + l6_teardown: "NOT_RUN".into(), + }, + _ => VerificationStatus { + l0_static: "PASS".into(), + l1_compile: "INCONCLUSIVE".into(), // Not tested due to architectural/safety blockers + l2_link: "INCONCLUSIVE".into(), + l3_contract: "NOT_RUN".into(), + l4_engine_boot: "NOT_RUN".into(), + l5_gameplay: "NOT_RUN".into(), + l6_teardown: "NOT_RUN".into(), + }, + }; + Ok(CompatibilityReport { repository_path: repo_path_buf.to_string_lossy().into_owned(), repository_name: repo_name, commit, - license, + license_info, classification, + verification_status, hooks, - loaders, + script_loaders, databases, sql_files, core_patches, @@ -425,6 +362,508 @@ pub fn analyze_repository(repo_path: &Path) -> Result { }) } +// --------------------------------------------------------------------------- +// License Detection (Case-insensitive, multi-format, multi-license support) +// --------------------------------------------------------------------------- + +pub fn detect_licenses(_repo_path: &Path, files: &[(String, PathBuf)]) -> LicenseInfo { + let mut primary_license = "NO_LICENSE".to_string(); + let mut license_file = None; + let mut vendored_licenses = Vec::new(); + let mut spdx_identifier = None; + + // Scan for all license files in repo + let candidate_names = [ + "license", "license.md", "license.txt", "copying", "copying.md", "copying.txt" + ]; + + for (rel_path, abs_path) in files { + let lower = rel_path.to_lowercase(); + let filename = lower.rsplit('/').next().unwrap_or(&lower); + + if candidate_names.contains(&filename) { + if let Ok(content) = fs::read_to_string(abs_path) { + let parsed = parse_license_text(&content); + if !lower.contains('/') { + // Top-level license + primary_license = parsed.clone(); + license_file = Some(rel_path.clone()); + } else { + // Sub-directory / vendored license + vendored_licenses.push(format!("{}: {}", rel_path, parsed)); + } + } + } + } + + // Fallback: check README.md if primary license still NO_LICENSE + if primary_license == "NO_LICENSE" { + for (rel_path, abs_path) in files { + let lower = rel_path.to_lowercase(); + if lower == "readme.md" || lower == "readme" { + if let Ok(content) = fs::read_to_string(abs_path) { + for line in content.lines() { + let line_lower = line.to_lowercase(); + if line_lower.contains("spdx-license-identifier:") { + let spdx = line.split(':').nth(1).unwrap_or("").trim().to_string(); + spdx_identifier = Some(spdx.clone()); + primary_license = spdx; + break; + } + } + } + } + } + } + + LicenseInfo { + primary_license, + license_file, + vendored_licenses, + spdx_identifier, + } +} + +pub fn parse_license_text(content: &str) -> String { + // Check first 4096 bytes case-insensitively + let chunk = content.chars().take(4096).collect::().to_lowercase(); + + if chunk.contains("affero general public license") && (chunk.contains("version 3") || chunk.contains("v3")) + || chunk.contains("agpl-3.0") + || chunk.contains("agplv3") + { + "AGPL-3.0".into() + } else if chunk.contains("gnu general public license") && (chunk.contains("version 3") || chunk.contains("v3")) + || chunk.contains("gpl-3.0") + || chunk.contains("gplv3") + { + "GPL-3.0".into() + } else if chunk.contains("gnu general public license") && (chunk.contains("version 2") || chunk.contains("v2")) + || chunk.contains("gpl-2.0") + || chunk.contains("gplv2") + { + "GPL-2.0".into() + } else if chunk.contains("mit license") + || chunk.contains("permission is hereby granted, free of charge, to any person obtaining a copy") + { + "MIT".into() + } else if chunk.contains("apache license") && (chunk.contains("version 2.0") || chunk.contains("2.0")) { + "Apache-2.0".into() + } else if chunk.contains("bsd 3-clause") || chunk.contains("bsd-3-clause") { + "BSD-3-Clause".into() + } else { + "CUSTOM_OR_UNRECOGNIZED".into() + } +} + +// --------------------------------------------------------------------------- +// C++ Source Audit +// --------------------------------------------------------------------------- + +fn audit_cpp_file( + rel_path: &str, + content: &str, + hooks: &mut Vec, + all_loaders: &mut Vec, + loader_call_map: &mut std::collections::HashMap>, + databases: &mut Vec, + findings: &mut Vec, +) { + let lines: Vec<&str> = content.lines().collect(); + let mut current_fn: Option = None; + + for (idx, line) in lines.iter().enumerate() { + let line_num = idx + 1; + let trimmed = line.trim(); + + // Detect Add*Scripts() declarations / definitions + if trimmed.starts_with("void Add") && trimmed.contains("Scripts()") { + if let Some(fn_name) = extract_function_name(trimmed) { + if !all_loaders.contains(&fn_name) { + all_loaders.push(fn_name.clone()); + } + current_fn = Some(fn_name); + } + } + + // Track delegated Add* calls inside loader functions + if let Some(ref parent_fn) = current_fn { + if trimmed.starts_with("Add") && trimmed.ends_with("();") && !trimmed.contains("void") { + if let Some(called_fn) = extract_call_name(trimmed) { + loader_call_map + .entry(parent_fn.clone()) + .or_insert_with(Vec::new) + .push(called_fn); + } + } + if trimmed == "}" { + current_fn = None; + } + } + + // Detect Script Hooks (inheritance from *Script) + if trimmed.contains(": public ") && trimmed.ends_with("Script") { + if let Some((class_name, script_type)) = extract_script_inheritance(trimmed) { + hooks.push(DiscoveredHook { + class_name, + script_type, + file: rel_path.to_string(), + line: line_num, + }); + } + } + + // RULE: Missing SDK Exports Check (Known missing symbols from L2) + if trimmed.contains("sWorldSessionMgr->SendServerMessage") { + findings.push(CompatibilityFinding { + rule_id: "RULE_MISSING_SDK_EXPORT".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "WorldSessionMgr::SendServerMessage lacks ACORE_GAME_API export decoration; causes LNK2019 at link time.".into(), + recommended_action: "Export WorldSessionMgr::SendServerMessage in WorldSessionMgr.h (requires rebuild of dependent DLLs).".into(), + evidence: trimmed.chars().take(120).collect(), + is_auto_fixable: false, + }); + } + if trimmed.contains("CloseGossipMenuFor(") { + findings.push(CompatibilityFinding { + rule_id: "RULE_MISSING_SDK_EXPORT".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "CloseGossipMenuFor lacks ACORE_GAME_API export decoration; causes LNK2019 at link time.".into(), + recommended_action: "Export CloseGossipMenuFor in ScriptedGossip.h.".into(), + evidence: trimmed.chars().take(120).collect(), + is_auto_fixable: false, + }); + } + if trimmed.contains("MailReceiver(") { + findings.push(CompatibilityFinding { + rule_id: "RULE_MISSING_SDK_EXPORT".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "MailReceiver constructor lacks ACORE_GAME_API export decoration; causes LNK2019 at link time.".into(), + recommended_action: "Export MailReceiver constructor in Mail.h.".into(), + evidence: trimmed.chars().take(120).collect(), + is_auto_fixable: false, + }); + } + + // RULE_ASSUMPTION_CLASS_RACE: Check for assumptions breaking Ascension custom classes/races + if trimmed.contains("CanAccountCreateCharacter") || trimmed.contains("!charClass") || trimmed.contains("!charRace") { + if trimmed.contains("!charClass") { + findings.push(CompatibilityFinding { + rule_id: "RULE_ASSUMPTION_CLASS_RACE".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "Code rejects character creation when charClass is 0 (!charClass). Breaks Ascension classless/custom archetypes.".into(), + recommended_action: "Remove !charClass restriction or allow CLASS_NONE (0) when running on Ascension rulesets.".into(), + evidence: trimmed.chars().take(120).collect(), + is_auto_fixable: false, + }); + } + } + + // RULE_VERIFIED_CONFLICT_HEALTH_AUTHORITY: AutoBalance vs CoAContentScaling + if trimmed.contains("SetCreateHealth") || trimmed.contains("SetStatFlatModifier") || trimmed.contains("CanCreatureSetStat") { + let lower_rel = rel_path.to_lowercase(); + if lower_rel.contains("autobalance") || lower_rel.contains("aballcreature") || lower_rel.contains("ab_") { + findings.push(CompatibilityFinding { + rule_id: "RULE_VERIFIED_CONFLICT_HEALTH_AUTHORITY".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "Verified conflict: AutoBalance directly modifies Creature::SetCreateHealth, competing with CoAContentScaling exclusive budget authority.".into(), + recommended_action: "Do not load mod-autobalance concurrently with CoAContentScaling.".into(), + evidence: trimmed.chars().take(120).collect(), + is_auto_fixable: false, + }); + } + } + + // RULE_POTENTIAL_CONFLICT_STAT_STACKING: Solocraft player stat boost vs creature scaling + if (trimmed.contains("aurEff->ChangeAmount(difficulty") || trimmed.contains("ApplySpellPowerBonus(")) + && (rel_path.contains("Solocraft") || rel_path.contains("SoloCraft")) + { + findings.push(CompatibilityFinding { + rule_id: "RULE_POTENTIAL_CONFLICT_STAT_STACKING".into(), + severity: FindingSeverity::Warning, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "Potential conflict: Solocraft multiplies player stats via SPELL_BUFF_STATS_PCT while Content Scaling lowers creature stats. Requires configuration calibration.".into(), + recommended_action: "Calibrate Solocraft.Enable or SolocraftInstanceExcluded when Content Scaling is active to avoid compounding player power.".into(), + evidence: trimmed.chars().take(120).collect(), + is_auto_fixable: false, + }); + } + + // RULE_PERF: Synchronous DB Query in Update hook + if (trimmed.contains("OnPlayerBeforeUpdate") || trimmed.contains("OnUpdate(")) + && (trimmed.contains("CharacterDatabase.Query") || trimmed.contains("WorldDatabase.Query")) + { + findings.push(CompatibilityFinding { + rule_id: "RULE_PERF_BLOCKING_QUERY_ON_UPDATE".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "Blocking synchronous database query executed in per-tick Update hook; causes severe main thread latency spikes.".into(), + recommended_action: "Refactor to asynchronous queries (CharacterDatabase.AsyncQuery) with callback.".into(), + evidence: trimmed.chars().take(120).collect(), + is_auto_fixable: false, + }); + } + + // RULE_SEC: Forbidden System APIs + if trimmed.contains("system(") || trimmed.contains("popen(") || trimmed.contains("CreateProcess") { + findings.push(CompatibilityFinding { + rule_id: "RULE_SEC_FORBIDDEN_SYSTEM_API".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: line_num, + explanation: "Invocation of forbidden process spawning or system execution API violates plugin sandbox guidelines.".into(), + recommended_action: "Remove external process execution; operate solely within AzerothCore script APIs.".into(), + evidence: trimmed.chars().take(100).collect(), + is_auto_fixable: false, + }); + } + + // Track DB Usages + if trimmed.contains("CharacterDatabase.") || trimmed.contains("WorldDatabase.") || trimmed.contains("LoginDatabase.") { + let db = if trimmed.contains("CharacterDatabase") { + "CharacterDatabase" + } else if trimmed.contains("WorldDatabase") { + "WorldDatabase" + } else { + "LoginDatabase" + }; + let q_type = if trimmed.contains(".Execute") { + "Execute" + } else if trimmed.contains(".Query") { + "Query" + } else if trimmed.contains(".DirectExecute") { + "DirectExecute" + } else { + "Transaction/Other" + }; + databases.push(DatabaseUsage { + database: db.to_string(), + query_type: q_type.to_string(), + file: rel_path.to_string(), + line: line_num, + snippet: trimmed.chars().take(120).collect(), + }); + } + } +} + +// --------------------------------------------------------------------------- +// Call Graph & Script Loader Resolution +// --------------------------------------------------------------------------- + +fn resolve_script_loaders( + repo_name: &str, + all_loaders: &[String], + call_map: &std::collections::HashMap>, + findings: &mut Vec, +) -> ScriptLoaderInfo { + // Standard convention: AddScripts() + let normalized_name = repo_name.replace('-', "_"); + let expected_canonical = format!("Add{}Scripts", normalized_name); + + let canonical_loader = all_loaders + .iter() + .find(|l| l.eq_ignore_ascii_case(&expected_canonical) || l.ends_with(&format!("{}_loader", normalized_name))) + .cloned() + .or_else(|| { + // Find a loader that calls other loaders + call_map.iter().find(|(_, calls)| !calls.is_empty()).map(|(k, _)| k.clone()) + }) + .or_else(|| all_loaders.first().cloned()); + + let mut delegated_calls = Vec::new(); + let mut has_duplicate_registration = false; + + if let Some(ref canonical) = canonical_loader { + if let Some(calls) = call_map.get(canonical) { + delegated_calls = calls.clone(); + // Check for duplicates + let mut seen = std::collections::HashSet::new(); + for c in calls { + if !seen.insert(c) { + has_duplicate_registration = true; + findings.push(CompatibilityFinding { + rule_id: "RULE_DUPLICATE_SCRIPT_REGISTRATION".into(), + severity: FindingSeverity::Warning, + confidence: FindingConfidence::High, + file: "loader.cpp".into(), + line: 0, + explanation: format!("Script registration function '{}' is invoked multiple times in master loader '{}'.", c, canonical), + recommended_action: "Remove redundant registration call to avoid ScriptRegistry collisions.".into(), + evidence: format!("Duplicate call to {}", c), + is_auto_fixable: true, + }); + } + } + } + } + + ScriptLoaderInfo { + canonical_loader, + all_loaders: all_loaders.to_vec(), + delegated_calls, + has_duplicate_registration, + } +} + +// --------------------------------------------------------------------------- +// CMake & SQL Auditing +// --------------------------------------------------------------------------- + +fn audit_cmakelists(rel_path: &str, abs_path: &Path, findings: &mut Vec) { + if let Ok(content) = fs::read_to_string(abs_path) { + let chunk = content.to_lowercase(); + if chunk.contains("add_custom_command") && (chunk.contains("post_build") || chunk.contains("python") || chunk.contains(".sh") || chunk.contains(".bat")) { + findings.push(CompatibilityFinding { + rule_id: "RULE_BUILD_UNSAFE_POST_BUILD_COMMAND".into(), + severity: FindingSeverity::Critical, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: 1, + explanation: "CMakeLists.txt contains custom post-build script execution that may perform unvetted host mutations.".into(), + recommended_action: "Strip custom post-build scripts and verify clean DLL artifact generation.".into(), + evidence: "add_custom_command with script execution detected".into(), + is_auto_fixable: false, + }); + } + if chunk.contains("lua") || chunk.contains("sol2") { + findings.push(CompatibilityFinding { + rule_id: "RULE_EXTERNAL_SUBPROJECT_LUA".into(), + severity: FindingSeverity::Warning, + confidence: FindingConfidence::High, + file: rel_path.to_string(), + line: 1, + explanation: "Contains embedded Lua/LuaJIT runtime subproject requiring vendored static library compilation.".into(), + recommended_action: "Build vendored Lua library against MSVC /MD before linking plugin DLL.".into(), + evidence: "CMake references Lua / Sol2 dependencies".into(), + is_auto_fixable: false, + }); + } + } +} + +fn audit_sql_file( + rel_path: &str, + abs_path: &Path, + sql_files: &mut Vec, + findings: &mut Vec, +) { + let lower = rel_path.to_lowercase(); + let target = if lower.contains("world") || lower.contains("db-world") || lower.contains("world/") { + "world" + } else if lower.contains("character") || lower.contains("db-characters") || lower.contains("characters/") { + "characters" + } else if lower.contains("auth") || lower.contains("db-auth") || lower.contains("auth/") { + "auth" + } else { + "unspecified" + }; + + if target == "unspecified" { + findings.push(CompatibilityFinding { + rule_id: "RULE_SQL_UNSPECIFIED_TARGET".into(), + severity: FindingSeverity::Warning, + confidence: FindingConfidence::Medium, + file: rel_path.to_string(), + line: 1, + explanation: "SQL file does not reside in a standard database directory (world/characters/auth); target schema ambiguous.".into(), + recommended_action: "Relocate SQL file to data/sql/db-/ or specify target schema in SQUID manifest.".into(), + evidence: format!("Ambiguous path: {}", rel_path), + is_auto_fixable: true, + }); + } + + let mut has_create = false; + let mut has_alter = false; + let mut has_insert = false; + let mut has_delete = false; + let mut line_count = 0; + + if let Ok(content) = fs::read_to_string(abs_path) { + line_count = content.lines().count(); + for line in content.lines() { + let upper = line.trim().to_uppercase(); + if upper.starts_with("CREATE TABLE") { + has_create = true; + } + if upper.starts_with("ALTER TABLE") { + has_alter = true; + } + if upper.starts_with("INSERT INTO") || upper.starts_with("REPLACE INTO") { + has_insert = true; + } + if upper.starts_with("DELETE FROM") || upper.starts_with("UPDATE ") { + has_delete = true; + } + } + } + + sql_files.push(SqlFileAudit { + file: rel_path.to_string(), + target_schema: target.to_string(), + has_create_table: has_create, + has_alter_table: has_alter, + has_insert_replace: has_insert, + has_delete_update: has_delete, + line_count, + }); +} + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +fn extract_function_name(line: &str) -> Option { + let after_void = line.strip_prefix("void ")?.trim(); + let fn_name = after_void.split('(').next()?.trim(); + if fn_name.starts_with("Add") { + Some(fn_name.to_string()) + } else { + None + } +} + +fn extract_call_name(line: &str) -> Option { + let trimmed = line.trim(); + let name = trimmed.split('(').next()?.trim(); + if name.starts_with("Add") { + Some(name.to_string()) + } else { + None + } +} + +fn extract_script_inheritance(line: &str) -> Option<(String, String)> { + let parts: Vec<&str> = line.split(": public ").collect(); + if parts.len() == 2 { + let class_name = parts[0].replace("class ", "").trim().to_string(); + let script_type = parts[1].split('{').next().unwrap_or("").trim().to_string(); + Some((class_name, script_type)) + } else { + None + } +} + fn collect_files(root: &Path, current: &Path, out: &mut Vec<(String, PathBuf)>) -> Result<()> { if !current.is_dir() { return Ok(()); @@ -445,3 +884,88 @@ fn collect_files(root: &Path, current: &Path, out: &mut Vec<(String, PathBuf)>) } Ok(()) } + +// --------------------------------------------------------------------------- +// Unit Tests (Positive and Negative Evidence Verification) +// --------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_license_parser_matrix() { + assert_eq!(parse_license_text("# GNU Affero General Public License\nVersion 3, 19 November 2007"), "AGPL-3.0"); + assert_eq!(parse_license_text("GNU GENERAL PUBLIC LICENSE\nVersion 3, 29 June 2007"), "GPL-3.0"); + assert_eq!(parse_license_text("GNU GENERAL PUBLIC LICENSE\nVersion 2, June 1991"), "GPL-2.0"); + assert_eq!(parse_license_text("MIT License\n\nCopyright (c) 2021 AzerothCore"), "MIT"); + assert_eq!(parse_license_text("Apache License, Version 2.0 (the \"License\")"), "Apache-2.0"); + assert_eq!(parse_license_text("Some random proprietary text"), "CUSTOM_OR_UNRECOGNIZED"); + } + + #[test] + fn test_class_assumption_rule() { + let positive_sample = "bool CanAccountCreateCharacter(uint32 acc, uint8 race, uint8 charClass) {\n if (!acc || !race || !charClass) return false;\n return true;\n}"; + let negative_sample = "void OnLevelChanged(Player* player) {\n if (player->getClass() == CLASS_WARRIOR) return;\n}"; + + let mut hooks = Vec::new(); + let mut loaders = Vec::new(); + let mut call_map = std::collections::HashMap::new(); + let mut databases = Vec::new(); + let mut findings = Vec::new(); + + audit_cpp_file("test_pos.cpp", positive_sample, &mut hooks, &mut loaders, &mut call_map, &mut databases, &mut findings); + assert!(findings.iter().any(|f| f.rule_id == "RULE_ASSUMPTION_CLASS_RACE")); + + findings.clear(); + audit_cpp_file("test_neg.cpp", negative_sample, &mut hooks, &mut loaders, &mut call_map, &mut databases, &mut findings); + assert!(!findings.iter().any(|f| f.rule_id == "RULE_ASSUMPTION_CLASS_RACE")); + } + + #[test] + fn test_verified_vs_potential_conflict() { + let verified_sample = "void OnCreatureCreate(Creature* c) {\n c->SetCreateHealth(100);\n}"; + let potential_sample = "void ApplyBuff(Player* p) {\n if (AuraEffect* a = p->GetAuraEffect(SPELL_BUFF_STATS_PCT, 0)) aurEff->ChangeAmount(difficulty * SoloCraftStatsMult);\n}"; + + let mut hooks = Vec::new(); + let mut loaders = Vec::new(); + let mut call_map = std::collections::HashMap::new(); + let mut databases = Vec::new(); + let mut findings = Vec::new(); + + audit_cpp_file("src/AutoBalance.cpp", verified_sample, &mut hooks, &mut loaders, &mut call_map, &mut databases, &mut findings); + assert!(findings.iter().any(|f| f.rule_id == "RULE_VERIFIED_CONFLICT_HEALTH_AUTHORITY")); + + findings.clear(); + audit_cpp_file("src/Solocraft.cpp", potential_sample, &mut hooks, &mut loaders, &mut call_map, &mut databases, &mut findings); + assert!(findings.iter().any(|f| f.rule_id == "RULE_POTENTIAL_CONFLICT_STAT_STACKING")); + } + + #[test] + fn test_duplicate_script_registration() { + let sample = "void AddMasterScripts() {\n AddSubA();\n AddSubA();\n}"; + let mut hooks = Vec::new(); + let mut loaders = Vec::new(); + let mut call_map = std::collections::HashMap::new(); + let mut databases = Vec::new(); + let mut findings = Vec::new(); + + audit_cpp_file("src/loader.cpp", sample, &mut hooks, &mut loaders, &mut call_map, &mut databases, &mut findings); + let res = resolve_script_loaders("test_module", &loaders, &call_map, &mut findings); + assert!(res.has_duplicate_registration); + assert!(findings.iter().any(|f| f.rule_id == "RULE_DUPLICATE_SCRIPT_REGISTRATION")); + } + + #[test] + fn test_missing_sdk_export_rule() { + let sample = "void Broadcast(std::string msg) {\n sWorldSessionMgr->SendServerMessage(SERVER_MSG_STRING, msg.c_str());\n}"; + let mut hooks = Vec::new(); + let mut loaders = Vec::new(); + let mut call_map = std::collections::HashMap::new(); + let mut databases = Vec::new(); + let mut findings = Vec::new(); + + audit_cpp_file("src/mod.cpp", sample, &mut hooks, &mut loaders, &mut call_map, &mut databases, &mut findings); + assert!(findings.iter().any(|f| f.rule_id == "RULE_MISSING_SDK_EXPORT")); + } +} diff --git a/crates/coa-core/src/bin/csm_plugins.rs b/crates/coa-core/src/bin/csm_plugins.rs index 273b051..3b43c21 100644 --- a/crates/coa-core/src/bin/csm_plugins.rs +++ b/crates/coa-core/src/bin/csm_plugins.rs @@ -271,11 +271,25 @@ fn main() -> Result<(), Box> { if as_json { println!("{}", serde_json::to_string_pretty(&report)?); } else { - println!("=== Module Compatibility Report ==="); + println!("=== Module Compatibility Report v0.2 ==="); println!("Repository: {}", report.repository_name); println!("Commit : {}", report.commit); - println!("License : {}", report.license); + println!("License : {}", report.license_info.primary_license); + if let Some(ref lf) = report.license_info.license_file { + println!("License File: {}", lf); + } + if !report.license_info.vendored_licenses.is_empty() { + println!("Vendored Licenses: {:?}", report.license_info.vendored_licenses); + } println!("Verdict : {:?}", report.classification); + println!("Status L0-L6: static={}, compile={}, link={}, contract={}, boot={}, gameplay={}, teardown={}", + report.verification_status.l0_static, + report.verification_status.l1_compile, + report.verification_status.l2_link, + report.verification_status.l3_contract, + report.verification_status.l4_engine_boot, + report.verification_status.l5_gameplay, + report.verification_status.l6_teardown); println!("Hooks ({}):", report.hooks.len()); for h in &report.hooks { println!(" - {} : {} ({}:{})", h.class_name, h.script_type, h.file, h.line);