using System; using System.Collections.Generic; using System.Diagnostics; namespace AxCopilot.Services; public static class RuntimeDetector { private static Dictionary? _cache; private static DateTime _cacheTime; private static readonly TimeSpan CacheTtl = TimeSpan.FromMinutes(5.0); public static bool IsAvailable(string runtime) { if (string.IsNullOrWhiteSpace(runtime)) { return true; } runtime = runtime.Trim().ToLowerInvariant(); if (_cache != null && DateTime.UtcNow - _cacheTime < CacheTtl) { if (_cache.TryGetValue(runtime, out var value)) { return value; } } else { _cache = new Dictionary(StringComparer.OrdinalIgnoreCase); _cacheTime = DateTime.UtcNow; } bool flag = DetectRuntime(runtime); _cache[runtime] = flag; return flag; } public static void ClearCache() { _cache = null; } private static bool DetectRuntime(string runtime) { string value = RunQuick("where.exe", runtime); if (string.IsNullOrEmpty(value)) { return false; } if (1 == 0) { } string text = ((!(runtime == "java")) ? "--version" : "-version"); if (1 == 0) { } string args = text; string value2 = RunQuick(runtime, args); return !string.IsNullOrEmpty(value2); } private static string? RunQuick(string exe, string args) { try { ProcessStartInfo startInfo = new ProcessStartInfo(exe, args) { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }; using Process process = Process.Start(startInfo); if (process == null) { return null; } string text = process.StandardOutput.ReadToEnd(); string text2 = process.StandardError.ReadToEnd(); process.WaitForExit(5000); string text3 = (string.IsNullOrWhiteSpace(text) ? text2 : text); return string.IsNullOrWhiteSpace(text3) ? null : text3.Trim(); } catch { return null; } } }