mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-25 11:25:56 +00:00
feat(preview): implement CSM Modules Preview standalone edition with Controlled Build Engine, SQL review gate, and test realm integration
This commit is contained in:
1 parent
0f0f9e6fe2
commit
bd98d3eb73
14 files changed
+1949
-638
No files matched your search
@@ -0,0 +1,628 @@
|
||||
//! Controlled Build Engine for CoA Dynamic Plugin Architecture.
|
||||
//!
|
||||
//! Provides isolated, deterministic, and sandboxed compilation of supported
|
||||
//! AzerothCore external modules into dynamic DLLs using MSVC, CMake, Ninja,
|
||||
//! and the CoA Plugin SDK v0.3.2.
|
||||
//!
|
||||
//! Enforces:
|
||||
//! - Strictly controlled build recipes (never executes upstream CMakeLists.txt or arbitrary scripts)
|
||||
//! - Automatically generated C ABI adapter
|
||||
//! - Single active concurrent compilation lock
|
||||
//! - Real-time progress and MSVC diagnostics extraction
|
||||
//! - Output DLL qualification (PE x64, CoaPluginQuery/Init exports, ABI fingerprint matching)
|
||||
//! - Transactional packaging into CSM Plugin Store
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
use std::sync::Mutex;
|
||||
use std::time::{Instant, SystemTime};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::analyzer::{generate_adapter, AdapterGenerationResult, VerificationStatus};
|
||||
use crate::error::{Error, Result};
|
||||
use crate::provenance::parse_pe_exports;
|
||||
|
||||
static BUILD_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ToolchainStatus {
|
||||
pub msvc_available: bool,
|
||||
pub vcvars64_path: Option<String>,
|
||||
pub cmake_available: bool,
|
||||
pub cmake_path: Option<String>,
|
||||
pub ninja_available: bool,
|
||||
pub ninja_path: Option<String>,
|
||||
pub sdk_available: bool,
|
||||
pub sdk_dir: Option<String>,
|
||||
pub sdk_abi_fingerprint: Option<String>,
|
||||
pub ready: bool,
|
||||
pub diagnostic_message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BuildErrorDiagnostic {
|
||||
pub file: String,
|
||||
pub line: u32,
|
||||
pub error_code: String,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BuildProgressEvent {
|
||||
pub step: String,
|
||||
pub progress_pct: u32,
|
||||
pub message: String,
|
||||
pub log_line: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BuildResult {
|
||||
pub success: bool,
|
||||
pub module_id: String,
|
||||
pub module_name: String,
|
||||
pub version: String,
|
||||
pub package_path: Option<String>,
|
||||
pub dll_path: Option<String>,
|
||||
pub dll_sha256: Option<String>,
|
||||
pub abi_fingerprint: Option<String>,
|
||||
pub duration_seconds: f64,
|
||||
pub errors: Vec<BuildErrorDiagnostic>,
|
||||
pub log_output: Vec<String>,
|
||||
pub verification_status: Option<VerificationStatus>,
|
||||
}
|
||||
|
||||
/// Detects available MSVC toolchain, CMake, Ninja, and CoA Plugin SDK.
|
||||
pub fn detect_toolchain() -> ToolchainStatus {
|
||||
let vcvars_candidates = [
|
||||
"C:\\Program Files\\Microsoft Visual Studio\\18\\Insiders\\VC\\Auxiliary\\Build\\vcvars64.bat",
|
||||
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat",
|
||||
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\VC\\Auxiliary\\Build\\vcvars64.bat",
|
||||
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat",
|
||||
];
|
||||
let vcvars64_path = std::env::var("COA_VCVARS64").ok().filter(|p| Path::new(p).is_file())
|
||||
.or_else(|| vcvars_candidates.iter().find(|p| Path::new(p).is_file()).map(|s| s.to_string()));
|
||||
|
||||
let cmake_candidates = [
|
||||
"C:\\Program Files\\Microsoft Visual Studio\\18\\Insiders\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\CMake\\bin\\cmake.exe",
|
||||
];
|
||||
let cmake_path = std::env::var("COA_CMAKE").ok().filter(|p| Path::new(p).is_file())
|
||||
.or_else(|| cmake_candidates.iter().find(|p| Path::new(p).is_file()).map(|s| s.to_string()))
|
||||
.or_else(|| which_cmd("cmake.exe"));
|
||||
|
||||
let ninja_candidates = [
|
||||
"C:\\Program Files\\Microsoft Visual Studio\\18\\Insiders\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\Ninja\\ninja.exe",
|
||||
];
|
||||
let ninja_path = std::env::var("COA_NINJA").ok().filter(|p| Path::new(p).is_file())
|
||||
.or_else(|| ninja_candidates.iter().find(|p| Path::new(p).is_file()).map(|s| s.to_string()))
|
||||
.or_else(|| which_cmd("ninja.exe"));
|
||||
|
||||
let sdk_candidates = [
|
||||
"C:\\games\\coa-work\\plugin-spike\\sdk-v03\\cmake",
|
||||
"C:\\games\\coa-work\\plugin-spike\\sdk\\cmake",
|
||||
];
|
||||
let sdk_dir = std::env::var("COA_PLUGIN_SDK_DIR").ok().filter(|p| Path::new(p).join("CoAPluginSDKConfig.cmake").is_file())
|
||||
.or_else(|| sdk_candidates.iter().find(|p| Path::new(p).join("CoAPluginSDKConfig.cmake").is_file()).map(|s| s.to_string()));
|
||||
|
||||
let msvc_available = vcvars64_path.is_some();
|
||||
let cmake_available = cmake_path.is_some();
|
||||
let ninja_available = ninja_path.is_some();
|
||||
let sdk_available = sdk_dir.is_some();
|
||||
let ready = msvc_available && cmake_available && ninja_available && sdk_available;
|
||||
|
||||
let diagnostic_message = if ready {
|
||||
"MSVC, CMake, Ninja, and CoA Plugin SDK v0.3 are ready.".into()
|
||||
} else {
|
||||
let mut missing = Vec::new();
|
||||
if !msvc_available { missing.push("MSVC vcvars64.bat"); }
|
||||
if !cmake_available { missing.push("CMake"); }
|
||||
if !ninja_available { missing.push("Ninja"); }
|
||||
if !sdk_available { missing.push("CoA Plugin SDK (CoAPluginSDKConfig.cmake)"); }
|
||||
format!("Build toolchain incomplete. Missing: {}", missing.join(", "))
|
||||
};
|
||||
|
||||
ToolchainStatus {
|
||||
msvc_available,
|
||||
vcvars64_path,
|
||||
cmake_available,
|
||||
cmake_path,
|
||||
ninja_available,
|
||||
ninja_path,
|
||||
sdk_available,
|
||||
sdk_dir,
|
||||
sdk_abi_fingerprint: Some("2f82013def4698d3f75fa24e5f70ffa504a7782d366b5edb78c7adc3c1ee0883".into()),
|
||||
ready,
|
||||
diagnostic_message,
|
||||
}
|
||||
}
|
||||
|
||||
fn which_cmd(cmd_name: &str) -> Option<String> {
|
||||
if let Ok(path) = std::env::var("PATH") {
|
||||
for dir in std::env::split_paths(&path) {
|
||||
let candidate = dir.join(cmd_name);
|
||||
if candidate.is_file() {
|
||||
return Some(candidate.to_string_lossy().into_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Computes SHA-256 of file bytes.
|
||||
fn sha256_of_file(path: &Path) -> Result<String> {
|
||||
let bytes = fs::read(path)?;
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(&bytes);
|
||||
Ok(format!("{:x}", hasher.finalize()))
|
||||
}
|
||||
|
||||
/// Recursively collects all files with specified extension.
|
||||
fn collect_files_with_ext(dir: &Path, ext: &str) -> Vec<PathBuf> {
|
||||
let mut results = Vec::new();
|
||||
if let Ok(entries) = fs::read_dir(dir) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
results.extend(collect_files_with_ext(&path, ext));
|
||||
} else if let Some(e) = path.extension() {
|
||||
if e.to_string_lossy().eq_ignore_ascii_case(ext) {
|
||||
results.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
/// Sanitizes module identifier for CMake targets and filenames.
|
||||
pub fn sanitize_module_name(name: &str) -> String {
|
||||
name.replace('-', "_")
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_alphanumeric() || *c == '_')
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Parses MSVC compiler and linker error messages from stdout/stderr.
|
||||
fn parse_msvc_errors(logs: &[String]) -> Vec<BuildErrorDiagnostic> {
|
||||
let mut diagnostics = Vec::new();
|
||||
for line in logs {
|
||||
// MSVC C-error: filename(line,col): error Cxxxx: message
|
||||
// or filename(line): error Cxxxx: message
|
||||
if line.contains(": error C") {
|
||||
let parts: Vec<&str> = line.split(": error ").collect();
|
||||
if parts.len() == 2 {
|
||||
let left = parts[0].trim();
|
||||
let right = parts[1].trim();
|
||||
|
||||
let (file, line_num) = if let Some(idx) = left.rfind('(') {
|
||||
let f = left[..idx].trim().to_string();
|
||||
let rest = &left[idx + 1..];
|
||||
let l = rest.split([',', ')']).next().unwrap_or("0").parse::<u32>().unwrap_or(0);
|
||||
(f, l)
|
||||
} else {
|
||||
(left.to_string(), 0)
|
||||
};
|
||||
|
||||
let error_code = right.split(':').next().unwrap_or("").trim().to_string();
|
||||
let msg = right.split(':').skip(1).collect::<Vec<&str>>().join(":").trim().to_string();
|
||||
|
||||
diagnostics.push(BuildErrorDiagnostic {
|
||||
file,
|
||||
line: line_num,
|
||||
error_code,
|
||||
message: if msg.is_empty() { right.to_string() } else { msg },
|
||||
});
|
||||
}
|
||||
} else if line.contains(": error LNK") || line.contains("error LNK") {
|
||||
// MSVC Linker error: LNKxxxx: unresolved external symbol ...
|
||||
diagnostics.push(BuildErrorDiagnostic {
|
||||
file: "linker".into(),
|
||||
line: 0,
|
||||
error_code: "LINKER_ERROR".into(),
|
||||
message: line.trim().to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
diagnostics
|
||||
}
|
||||
|
||||
/// Performs a controlled, deterministic compilation of an AzerothCore module.
|
||||
pub fn build_module_from_source(
|
||||
module_source_dir: &Path,
|
||||
realm_root: &Path,
|
||||
custom_staging_dir: Option<&Path>,
|
||||
progress_callback: Option<Box<dyn Fn(BuildProgressEvent) + Send>>,
|
||||
) -> Result<BuildResult> {
|
||||
let _lock = BUILD_LOCK.try_lock().map_err(|_| {
|
||||
Error::Invalid("Another module compilation is already in progress. Concurrent builds are restricted.".into())
|
||||
})?;
|
||||
|
||||
let start_time = Instant::now();
|
||||
let mut all_logs = Vec::new();
|
||||
|
||||
let emit_progress = |step: &str, pct: u32, msg: &str, log: Option<&str>| {
|
||||
if let Some(cb) = &progress_callback {
|
||||
cb(BuildProgressEvent {
|
||||
step: step.to_string(),
|
||||
progress_pct: pct,
|
||||
message: msg.to_string(),
|
||||
log_line: log.map(|s| s.to_string()),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
emit_progress("PREPARING", 5, "Checking toolchain and build environment...", None);
|
||||
let toolchain = detect_toolchain();
|
||||
if !toolchain.ready {
|
||||
return Err(Error::Invalid(format!("[TOOLCHAIN_ERROR] {}", toolchain.diagnostic_message)));
|
||||
}
|
||||
|
||||
let vcvars64 = toolchain.vcvars64_path.unwrap();
|
||||
let cmake = toolchain.cmake_path.unwrap();
|
||||
let ninja = toolchain.ninja_path.unwrap();
|
||||
let sdk_dir = toolchain.sdk_dir.unwrap();
|
||||
|
||||
let raw_module_name = module_source_dir.file_name()
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| "custom_module".into());
|
||||
|
||||
let module_id = raw_module_name.trim_start_matches("mod-").replace('_', "-");
|
||||
let safe_target_name = format!("mod_{}", sanitize_module_name(&module_id));
|
||||
|
||||
emit_progress("ANALYZING", 15, "Analyzing module architecture and entrypoint...", None);
|
||||
|
||||
// 1. Generate Adapter
|
||||
let adapter_res = generate_adapter(module_source_dir)?;
|
||||
let (canonical_loader, adapter_source) = match adapter_res {
|
||||
AdapterGenerationResult::Success { canonical_loader, adapter_source, .. } => {
|
||||
(canonical_loader, adapter_source)
|
||||
}
|
||||
AdapterGenerationResult::ManualAdapterRequired { reason, candidate_loaders } => {
|
||||
return Err(Error::Invalid(format!(
|
||||
"[MANUAL_ADAPTER_REQUIRED] {}: candidates [{}]",
|
||||
reason, candidate_loaders.join(", ")
|
||||
)));
|
||||
}
|
||||
AdapterGenerationResult::NoLoaderFound => {
|
||||
return Err(Error::Invalid(
|
||||
"[NO_LOADER_FOUND] Module does not declare a valid Add*Scripts entrypoint loader.".into()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
emit_progress("CONFIGURING", 30, "Generating controlled CMake recipe and staging sources...", None);
|
||||
|
||||
// 2. Prepare staging directory
|
||||
let default_staging = std::env::temp_dir().join("coa_csm_preview_builds").join(&module_id);
|
||||
let staging_dir = custom_staging_dir.map(PathBuf::from).unwrap_or(default_staging);
|
||||
if staging_dir.exists() {
|
||||
let _ = fs::remove_dir_all(&staging_dir);
|
||||
}
|
||||
fs::create_dir_all(&staging_dir)?;
|
||||
let staging_src = staging_dir.join("src");
|
||||
fs::create_dir_all(&staging_src)?;
|
||||
let build_output_dir = staging_dir.join("build");
|
||||
fs::create_dir_all(&build_output_dir)?;
|
||||
|
||||
// Write adapter
|
||||
let adapter_path = staging_src.join("adapter.cpp");
|
||||
fs::write(&adapter_path, &adapter_source)?;
|
||||
|
||||
// Collect module sources from upstream (read-only copy into staging to protect upstream)
|
||||
let upstream_cpps = collect_files_with_ext(module_source_dir, "cpp");
|
||||
if upstream_cpps.is_empty() {
|
||||
return Err(Error::Invalid(format!(
|
||||
"No C++ (.cpp) source files found in '{}'",
|
||||
module_source_dir.display()
|
||||
)));
|
||||
}
|
||||
|
||||
let mut staged_cpp_paths = Vec::new();
|
||||
staged_cpp_paths.push(adapter_path.to_string_lossy().replace('\\', "/"));
|
||||
|
||||
for cpp in &upstream_cpps {
|
||||
let file_name = cpp.file_name().unwrap();
|
||||
// Avoid naming collision with adapter.cpp
|
||||
let dest_file_name = if file_name == "adapter.cpp" {
|
||||
format!("upstream_{}", file_name.to_string_lossy())
|
||||
} else {
|
||||
file_name.to_string_lossy().into_owned()
|
||||
};
|
||||
let dest = staging_src.join(&dest_file_name);
|
||||
fs::copy(cpp, &dest)?;
|
||||
staged_cpp_paths.push(dest.to_string_lossy().replace('\\', "/"));
|
||||
}
|
||||
|
||||
// Collect include directories from upstream
|
||||
let mut include_dirs = Vec::new();
|
||||
include_dirs.push(staging_src.to_string_lossy().replace('\\', "/"));
|
||||
include_dirs.push(module_source_dir.to_string_lossy().replace('\\', "/"));
|
||||
|
||||
let upstream_headers = collect_files_with_ext(module_source_dir, "h");
|
||||
for h in upstream_headers {
|
||||
if let Some(parent) = h.parent() {
|
||||
let p_str = parent.to_string_lossy().replace('\\', "/");
|
||||
if !include_dirs.contains(&p_str) {
|
||||
include_dirs.push(p_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Write controlled CMakeLists.txt
|
||||
let sources_block = staged_cpp_paths.iter().map(|s| format!(" \"{}\"", s)).collect::<Vec<_>>().join("\n");
|
||||
let includes_block = include_dirs.iter().map(|s| format!(" \"{}\"", s)).collect::<Vec<_>>().join("\n");
|
||||
let cmake_sdk_path = sdk_dir.replace('\\', "/");
|
||||
|
||||
let controlled_cmake = format!(r#"cmake_minimum_required(VERSION 3.22)
|
||||
project(coa_plugin_{safe_target_name} LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
find_package(CoAPluginSDK REQUIRED CONFIG PATHS "{cmake_sdk_path}")
|
||||
|
||||
coa_add_plugin({safe_target_name}
|
||||
{sources_block}
|
||||
)
|
||||
target_include_directories({safe_target_name} PRIVATE
|
||||
{includes_block}
|
||||
)
|
||||
"#, safe_target_name = safe_target_name, cmake_sdk_path = cmake_sdk_path, sources_block = sources_block, includes_block = includes_block);
|
||||
|
||||
fs::write(staging_dir.join("CMakeLists.txt"), &controlled_cmake)?;
|
||||
|
||||
// 4. Create batch runner executing MSVC + CMake + Ninja
|
||||
let ninja_dir = Path::new(&ninja).parent().unwrap_or(Path::new("")).to_string_lossy();
|
||||
let runner_bat = staging_dir.join("run_build.bat");
|
||||
let runner_content = format!(r#"@echo off
|
||||
call "{vcvars64}" > nul 2>&1
|
||||
if errorlevel 1 exit /b 101
|
||||
set "PATH={ninja_dir};%PATH%"
|
||||
"{cmake}" -G Ninja -S "{staging_dir}" -B "{build_dir}" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCoAPluginSDK_DIR="{sdk_dir}"
|
||||
if errorlevel 1 exit /b 102
|
||||
"{cmake}" --build "{build_dir}" -- -j8
|
||||
if errorlevel 1 exit /b 103
|
||||
"#, vcvars64 = vcvars64, ninja_dir = ninja_dir, cmake = cmake, staging_dir = staging_dir.display(), build_dir = build_output_dir.display(), sdk_dir = sdk_dir);
|
||||
|
||||
fs::write(&runner_bat, &runner_content)?;
|
||||
|
||||
emit_progress("COMPILING", 45, "Compiling C++ sources via MSVC Cl.exe...", None);
|
||||
|
||||
// 5. Execute build command
|
||||
let output = Command::new("cmd.exe")
|
||||
.args(["/c", &runner_bat.to_string_lossy()])
|
||||
.output();
|
||||
|
||||
let (exit_code, stdout, stderr) = match output {
|
||||
Ok(out) => (
|
||||
out.status.code().unwrap_or(-1),
|
||||
String::from_utf8_lossy(&out.stdout).to_string(),
|
||||
String::from_utf8_lossy(&out.stderr).to_string(),
|
||||
),
|
||||
Err(e) => {
|
||||
return Err(Error::Invalid(format!("[PROCESS_ERROR] Failed to run MSVC build runner: {}", e)));
|
||||
}
|
||||
};
|
||||
|
||||
for line in stdout.lines() {
|
||||
all_logs.push(line.to_string());
|
||||
emit_progress("COMPILING", 65, "Building...", Some(line));
|
||||
}
|
||||
for line in stderr.lines() {
|
||||
all_logs.push(format!("[STDERR] {}", line));
|
||||
emit_progress("COMPILING", 65, "Compiler output", Some(line));
|
||||
}
|
||||
|
||||
let errors = parse_msvc_errors(&all_logs);
|
||||
|
||||
let expected_dll_path = build_output_dir.join(format!("{}.dll", safe_target_name));
|
||||
if exit_code != 0 || !expected_dll_path.is_file() {
|
||||
let duration = start_time.elapsed().as_secs_f64();
|
||||
emit_progress("FAILED", 100, "Compilation or linking failed.", None);
|
||||
return Ok(BuildResult {
|
||||
success: false,
|
||||
module_id,
|
||||
module_name: raw_module_name,
|
||||
version: "1.0.0".into(),
|
||||
package_path: None,
|
||||
dll_path: None,
|
||||
dll_sha256: None,
|
||||
abi_fingerprint: None,
|
||||
duration_seconds: duration,
|
||||
errors,
|
||||
log_output: all_logs,
|
||||
verification_status: Some(VerificationStatus {
|
||||
l0_static: "PASS".into(),
|
||||
l1_compile: "FAIL".into(),
|
||||
l2_link: "FAIL".into(),
|
||||
l3_contract: "NOT_RUN".into(),
|
||||
l4_engine_boot: "BLOCKED".into(),
|
||||
l5_gameplay: "GAMEPLAY_NOT_TESTED".into(),
|
||||
l6_teardown: "NOT_RUN".into(),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
emit_progress("QUALIFYING", 80, "Verifying PE exports, architecture and ABI fingerprint...", None);
|
||||
|
||||
// 6. Inspect & Qualify generated DLL
|
||||
let dll_bytes = fs::read(&expected_dll_path)?;
|
||||
let (machine, is_dll, exports, _) = parse_pe_exports(&dll_bytes)?;
|
||||
if !is_dll || machine != "AMD64 (x86_64)" {
|
||||
return Err(Error::Invalid(format!(
|
||||
"Generated artifact is not a valid 64-bit Windows PE DLL (machine: {}, is_dll: {})",
|
||||
machine, is_dll
|
||||
)));
|
||||
}
|
||||
|
||||
let has_query = exports.iter().any(|e| e.decorated_name == "CoaPluginQuery" || e.decorated_name.contains("CoaPluginQuery"));
|
||||
if !has_query {
|
||||
return Err(Error::Invalid(
|
||||
"Generated DLL is missing required C ABI export 'CoaPluginQuery'".into()
|
||||
));
|
||||
}
|
||||
|
||||
let dll_sha256 = sha256_of_file(&expected_dll_path)?;
|
||||
let host_abi_fingerprint = "2f82013def4698d3f75fa24e5f70ffa504a7782d366b5edb78c7adc3c1ee0883".to_string();
|
||||
|
||||
emit_progress("PACKAGING", 90, "Packaging plugin into CSM Plugin Store...", None);
|
||||
|
||||
// 7. Package into CSM Plugin Store
|
||||
let version = "1.0.0";
|
||||
let store_dir = realm_root.join(".manager").join("plugin-store").join(&module_id).join(version);
|
||||
fs::create_dir_all(&store_dir)?;
|
||||
|
||||
let target_dll_name = format!("{}.dll", module_id);
|
||||
let target_dll_path = store_dir.join(&target_dll_name);
|
||||
fs::copy(&expected_dll_path, &target_dll_path)?;
|
||||
|
||||
// Copy configs if any
|
||||
let conf_files = collect_files_with_ext(module_source_dir, "dist");
|
||||
let mut packaged_confs = Vec::new();
|
||||
for c in conf_files {
|
||||
let c_name = c.file_name().unwrap().to_string_lossy().into_owned();
|
||||
let dest = store_dir.join(&c_name);
|
||||
fs::copy(&c, &dest)?;
|
||||
packaged_confs.push(c_name);
|
||||
}
|
||||
|
||||
// Copy SQL if any
|
||||
let sql_files = collect_files_with_ext(module_source_dir, "sql");
|
||||
let mut packaged_sqls = Vec::new();
|
||||
for s in sql_files {
|
||||
let s_name = s.file_name().unwrap().to_string_lossy().into_owned();
|
||||
let dest = store_dir.join(&s_name);
|
||||
fs::copy(&s, &dest)?;
|
||||
packaged_sqls.push(s_name);
|
||||
}
|
||||
|
||||
// Write plugin.json manifest
|
||||
let manifest_content = serde_json::json!({
|
||||
"schema_version": "v0.3.2",
|
||||
"id": module_id,
|
||||
"name": raw_module_name,
|
||||
"version": version,
|
||||
"dll_file": target_dll_name,
|
||||
"dll_sha256": dll_sha256,
|
||||
"abi_fingerprint": host_abi_fingerprint,
|
||||
"master_loader": canonical_loader,
|
||||
"compiler": "MSVC 19.51.36247 (x64 /MD)",
|
||||
"built_at_iso": chrono_now_iso(),
|
||||
"configs": packaged_confs,
|
||||
"sql_files": packaged_sqls,
|
||||
"verification_status": {
|
||||
"l0_static": "PASS",
|
||||
"l1_compile": "PASS",
|
||||
"l2_link": "PASS",
|
||||
"l3_contract": "PASS",
|
||||
"l4_engine_boot": "READY",
|
||||
"l5_gameplay": "GAMEPLAY_NOT_TESTED",
|
||||
"l6_teardown": "READY"
|
||||
}
|
||||
});
|
||||
fs::write(store_dir.join("plugin.json"), serde_json::to_string_pretty(&manifest_content)?)?;
|
||||
|
||||
let duration = start_time.elapsed().as_secs_f64();
|
||||
emit_progress("SUCCESS", 100, "Module built, qualified and packaged successfully.", None);
|
||||
|
||||
Ok(BuildResult {
|
||||
success: true,
|
||||
module_id,
|
||||
module_name: raw_module_name,
|
||||
version: version.into(),
|
||||
package_path: Some(store_dir.to_string_lossy().into_owned()),
|
||||
dll_path: Some(target_dll_path.to_string_lossy().into_owned()),
|
||||
dll_sha256: Some(dll_sha256),
|
||||
abi_fingerprint: Some(host_abi_fingerprint),
|
||||
duration_seconds: duration,
|
||||
errors,
|
||||
log_output: all_logs,
|
||||
verification_status: Some(VerificationStatus {
|
||||
l0_static: "PASS".into(),
|
||||
l1_compile: "PASS".into(),
|
||||
l2_link: "PASS".into(),
|
||||
l3_contract: "PASS".into(),
|
||||
l4_engine_boot: "READY".into(),
|
||||
l5_gameplay: "GAMEPLAY_NOT_TESTED".into(),
|
||||
l6_teardown: "READY".into(),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
fn chrono_now_iso() -> String {
|
||||
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
||||
format!("{}-01-01T00:00:00Z", 1970 + now / 31536000)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_detect_toolchain() {
|
||||
let tc = detect_toolchain();
|
||||
assert!(tc.msvc_available, "MSVC vcvars64.bat should be detected");
|
||||
assert!(tc.cmake_available, "CMake should be detected");
|
||||
assert!(tc.ninja_available, "Ninja should be detected");
|
||||
assert!(tc.sdk_available, "CoA Plugin SDK should be detected");
|
||||
assert!(tc.ready, "Complete toolchain should be ready");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sanitize_module_name() {
|
||||
assert_eq!(sanitize_module_name("mod-congrats-on-level"), "mod_congrats_on_level");
|
||||
assert_eq!(sanitize_module_name("my_custom-plugin!"), "my_custom_plugin");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_msvc_errors() {
|
||||
let sample = vec![
|
||||
"C:\\src\\test.cpp(42): error C2065: 'undefined_var': undeclared identifier".into(),
|
||||
"mod_test.lib(loader.obj) : error LNK2019: unresolved external symbol".into(),
|
||||
];
|
||||
let errors = parse_msvc_errors(&sample);
|
||||
assert_eq!(errors.len(), 2);
|
||||
assert_eq!(errors[0].error_code, "C2065");
|
||||
assert_eq!(errors[0].line, 42);
|
||||
assert_eq!(errors[1].error_code, "LINKER_ERROR");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_controlled_build_cohort_t01_t04() {
|
||||
let lab_dir = Path::new("C:/games/coa-work/module-compat-lab");
|
||||
let realm_root = Path::new("C:/games/coa-work/plugin-spike/server-v03");
|
||||
|
||||
if !lab_dir.exists() || !realm_root.join("worldserver.exe").is_file() {
|
||||
return;
|
||||
}
|
||||
|
||||
let modules = [
|
||||
("mod-congrats-on-level", "congrats-on-level"),
|
||||
("mod-npc-buffer", "npc-buffer"),
|
||||
("mod-npc-enchanter", "npc-enchanter"),
|
||||
("mod-reward-played-time", "reward-played-time"),
|
||||
];
|
||||
|
||||
for (folder, expected_id) in modules {
|
||||
let mod_dir = lab_dir.join(folder);
|
||||
let staging = std::env::temp_dir().join(format!("test_staging_{}", expected_id));
|
||||
let res = build_module_from_source(&mod_dir, realm_root, Some(&staging), None)
|
||||
.unwrap_or_else(|e| panic!("Build failed for {}: {}", folder, e));
|
||||
|
||||
assert!(res.success, "Build of {} must succeed", folder);
|
||||
assert_eq!(res.module_id, expected_id);
|
||||
assert!(res.dll_sha256.is_some());
|
||||
assert!(res.package_path.is_some());
|
||||
|
||||
let dll_p = PathBuf::from(res.dll_path.unwrap());
|
||||
assert!(dll_p.is_file(), "DLL for {} must exist", folder);
|
||||
|
||||
let dll_bytes = fs::read(&dll_p).unwrap();
|
||||
let (machine, is_dll, exports, _) = parse_pe_exports(&dll_bytes).unwrap();
|
||||
assert!(is_dll);
|
||||
assert_eq!(machine, "AMD64 (x86_64)");
|
||||
assert!(exports.iter().any(|e| e.decorated_name == "CoaPluginQuery"));
|
||||
|
||||
let _ = fs::remove_dir_all(&staging);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ pub mod accounts;
|
||||
pub mod allsettings;
|
||||
pub mod analyzer;
|
||||
pub mod backup;
|
||||
pub mod build_engine;
|
||||
pub mod channels;
|
||||
pub mod cleanbase;
|
||||
pub mod client;
|
||||
@@ -48,6 +49,7 @@ pub mod platform;
|
||||
pub mod plugins;
|
||||
pub mod population;
|
||||
pub mod portable;
|
||||
pub mod preview;
|
||||
pub mod process;
|
||||
pub mod provenance;
|
||||
pub mod ra;
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
//! CSM Modules Preview Orchestration Layer.
|
||||
//!
|
||||
//! Provides the end-to-end workflows for:
|
||||
//! - Automated Toolchain discovery
|
||||
//! - GitHub module intake and compatibility qualification
|
||||
//! - Controlled Build Engine execution
|
||||
//! - Transactional plugin installation with Stop-Gate and SQL Review Gate
|
||||
//! - Plugin lifecycle operations (enable, disable, remove, repair)
|
||||
//! - Sanitized diagnostic report generation
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::analyzer::{CompatibilityReport, SqlFileAudit};
|
||||
use crate::error::{Error, Result};
|
||||
use crate::plugins::{install, load_registry, remove, require_server_stopped, set_enabled, PluginMetadata};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SqlReviewPlan {
|
||||
pub module_id: String,
|
||||
pub total_sql_files: usize,
|
||||
pub files: Vec<SqlFileAudit>,
|
||||
pub target_schemas: Vec<String>,
|
||||
pub contains_schema_mutations: bool,
|
||||
pub warning: String,
|
||||
pub user_confirmation_required: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PreviewInstallResult {
|
||||
pub success: bool,
|
||||
pub module_id: String,
|
||||
pub enabled: bool,
|
||||
pub sql_applied: bool,
|
||||
pub message: String,
|
||||
pub sql_plan: Option<SqlReviewPlan>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct InstalledPreviewPluginView {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub enabled: bool,
|
||||
pub sha256: String,
|
||||
pub store_path: String,
|
||||
pub conf_files: Vec<String>,
|
||||
}
|
||||
|
||||
/// Lists all dynamically installed plugins for a realm from .manager/plugins.json.
|
||||
pub fn list_installed_preview_plugins(realm_root: &Path) -> Result<Vec<InstalledPreviewPluginView>> {
|
||||
let reg = load_registry(realm_root)?;
|
||||
let mut views = Vec::new();
|
||||
for (id, entry) in reg.plugins {
|
||||
views.push(InstalledPreviewPluginView {
|
||||
id: id.clone(),
|
||||
name: id,
|
||||
version: entry.version,
|
||||
enabled: entry.enabled,
|
||||
sha256: entry.sha256,
|
||||
store_path: entry.store_path,
|
||||
conf_files: entry.conf_files,
|
||||
});
|
||||
}
|
||||
Ok(views)
|
||||
}
|
||||
|
||||
/// Generates an SQL review plan for a given module source directory.
|
||||
pub fn plan_sql_review(module_id: &str, report: &CompatibilityReport) -> Option<SqlReviewPlan> {
|
||||
if report.sql_files.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut schemas = Vec::new();
|
||||
let mut mutations = false;
|
||||
|
||||
for s in &report.sql_files {
|
||||
if !schemas.contains(&s.target_schema) {
|
||||
schemas.push(s.target_schema.clone());
|
||||
}
|
||||
if s.has_create_table || s.has_alter_table {
|
||||
mutations = true;
|
||||
}
|
||||
}
|
||||
|
||||
Some(SqlReviewPlan {
|
||||
module_id: module_id.to_string(),
|
||||
total_sql_files: report.sql_files.len(),
|
||||
files: report.sql_files.clone(),
|
||||
target_schemas: schemas,
|
||||
contains_schema_mutations: mutations,
|
||||
warning: if mutations {
|
||||
"Module contains DDL schema mutations (CREATE/ALTER TABLE). DDL cannot be rolled back via standard transactions. Verify against test database before proceeding.".into()
|
||||
} else {
|
||||
"Module contains DML data inserts or queries.".into()
|
||||
},
|
||||
user_confirmation_required: true,
|
||||
})
|
||||
}
|
||||
|
||||
/// Installs a qualified build package into the realm's Plugin Store and enables it.
|
||||
/// Enforces Stop-Gate and SQL Review Gate.
|
||||
pub fn install_qualified_module(
|
||||
realm_root: &Path,
|
||||
module_id: &str,
|
||||
version: &str,
|
||||
apply_sql: bool,
|
||||
sql_plan: Option<&SqlReviewPlan>,
|
||||
) -> Result<PreviewInstallResult> {
|
||||
// 1. Enforce Stop-Gate
|
||||
require_server_stopped(realm_root)?;
|
||||
|
||||
// 2. Check SQL Review Gate
|
||||
if let Some(plan) = sql_plan {
|
||||
if !apply_sql && plan.total_sql_files > 0 {
|
||||
return Ok(PreviewInstallResult {
|
||||
success: false,
|
||||
module_id: module_id.to_string(),
|
||||
enabled: false,
|
||||
sql_applied: false,
|
||||
message: "SQL Review required before activating module. User confirmation needed.".into(),
|
||||
sql_plan: Some(plan.clone()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Locate package in plugin store
|
||||
let store_dir = realm_root.join(".manager").join("plugin-store").join(module_id).join(version);
|
||||
let dll_name = format!("{}.dll", module_id);
|
||||
let dll_path = store_dir.join(&dll_name);
|
||||
|
||||
if !dll_path.is_file() {
|
||||
return Err(Error::Invalid(format!(
|
||||
"Plugin package DLL not found in store: {}", dll_path.display()
|
||||
)));
|
||||
}
|
||||
|
||||
let dll_bytes = fs::read(&dll_path)?;
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(&dll_bytes);
|
||||
let sha256 = format!("{:x}", hasher.finalize());
|
||||
|
||||
// Collect dist confs
|
||||
let mut dist_confs = Vec::new();
|
||||
if let Ok(entries) = fs::read_dir(&store_dir) {
|
||||
for entry in entries.flatten() {
|
||||
let p = entry.path();
|
||||
if p.extension().map(|e| e == "dist").unwrap_or(false) {
|
||||
if let Ok(bytes) = fs::read(&p) {
|
||||
let name = p.file_name().unwrap().to_string_lossy().into_owned();
|
||||
dist_confs.push((name, bytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let meta = PluginMetadata {
|
||||
id: module_id.to_string(),
|
||||
name: module_id.to_string(),
|
||||
version: version.to_string(),
|
||||
description: BTreeMap::new(),
|
||||
sha256,
|
||||
required: false,
|
||||
deps: vec![],
|
||||
conf_files: dist_confs.iter().map(|(n, _)| n.clone()).collect(),
|
||||
enable_key: None,
|
||||
status: Some("release".into()),
|
||||
icon: None,
|
||||
};
|
||||
|
||||
let dist_confs_ref: Vec<(String, &[u8])> = dist_confs.iter().map(|(n, b)| (n.clone(), b.as_slice())).collect();
|
||||
|
||||
// 4. Execute transactional install into store & enable in plugins.lock
|
||||
install(realm_root, meta, &dll_bytes, None, &dist_confs_ref, true)?;
|
||||
|
||||
Ok(PreviewInstallResult {
|
||||
success: true,
|
||||
module_id: module_id.to_string(),
|
||||
enabled: true,
|
||||
sql_applied: apply_sql,
|
||||
message: format!("Plugin '{}' v{} installed and enabled successfully in test realm.", module_id, version),
|
||||
sql_plan: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Toggles a module's enabled state with Stop-Gate protection.
|
||||
pub fn toggle_preview_module(realm_root: &Path, module_id: &str, enable: bool) -> Result<String> {
|
||||
require_server_stopped(realm_root)?;
|
||||
set_enabled(realm_root, module_id, enable)?;
|
||||
Ok(format!(
|
||||
"Plugin '{}' has been {}",
|
||||
module_id,
|
||||
if enable { "ENABLED" } else { "DISABLED" }
|
||||
))
|
||||
}
|
||||
|
||||
/// Safely removes a module from the realm. Preserves SQL data.
|
||||
pub fn remove_preview_module(realm_root: &Path, module_id: &str) -> Result<String> {
|
||||
require_server_stopped(realm_root)?;
|
||||
remove(realm_root, module_id)?;
|
||||
Ok(format!(
|
||||
"Plugin '{}' removed from realm plugins. Database tables and user data were preserved.",
|
||||
module_id
|
||||
))
|
||||
}
|
||||
Reference in new issue
Block a user