using System; using System.Diagnostics; using System.IO; using Netch.Models; using Netch.Utils; namespace Netch.Controllers { public abstract class Controller { /// /// 控制器名 /// /// 未赋值会在 赋值为 /// public string AkaName; /// /// 其他需要文件 /// protected string[] ExtFiles; /// /// 进程实例 /// public Process Instance; /// /// 主程序名(不含扩展名) /// public string MainFile; /// /// 运行检查, 由 赋值 /// public bool Ready; /// /// 当前状态 /// protected State State = State.Waiting; public abstract void Stop(); /// /// 停止 /// protected void StopInstance() { try { if (Instance == null || Instance.HasExited) return; Instance.Kill(); Instance.WaitForExit(); } catch (Exception e) { Logging.Error($"停止 {MainFile}.exe 错误:\n" + e); } } /// /// 杀残留进程,清除日志,检查文件 /// /// protected void InitCheck() { if (string.IsNullOrEmpty(AkaName)) AkaName = MainFile; var result = false; // 杀残留 MainController.KillProcessByName(MainFile); // 清日志 try { if (File.Exists($"logging\\{AkaName}.log")) File.Delete($"logging\\{AkaName}.log"); } catch (Exception) { // ignored } // 检查文件 var mainResult = true; var extResult = true; if (!string.IsNullOrEmpty(MainFile) && !File.Exists($"bin\\{MainFile}.exe")) { mainResult = false; Logging.Error($"主程序 bin\\{MainFile}.exe 不存在"); } if (ExtFiles == null) extResult = true; else foreach (var f in ExtFiles) if (!File.Exists($"bin\\{f}")) { extResult = false; Logging.Error($"附加文件 bin\\{f} 不存在"); } result = extResult && mainResult; if (!result) Logging.Error(AkaName + " 未就绪"); Ready = result; } /// /// 写日志 /// /// /// 是否为空 protected bool WriteLog(DataReceivedEventArgs std) { if (string.IsNullOrWhiteSpace(std.Data)) return false; try { File.AppendAllText($"logging\\{AkaName}.log", $@"{std.Data}{Global.EOF}"); } catch (Exception e) { Logging.Error($"写入{AkaName}日志错误:\n" + e); } return true; } public static Process GetProcess(string path = null, bool redirectStd = true) { var p = new Process { StartInfo = { Arguments = "", WorkingDirectory = $"{Global.NetchDir}\\bin", CreateNoWindow = true, RedirectStandardError = redirectStd, RedirectStandardInput = redirectStd, RedirectStandardOutput = redirectStd, UseShellExecute = false }, EnableRaisingEvents = true }; if (path != null) p.StartInfo.FileName = Path.GetFullPath(path); return p; } } }