diff --git a/Core/Config.cs b/Core/Config.cs index 493faba0..ee49944c 100644 --- a/Core/Config.cs +++ b/Core/Config.cs @@ -1622,6 +1622,50 @@ public bool _PlayWindowDanmaSwitch } } + private static string Linux_Only_ShellSwitch = "false"; + /// + /// 是否开启Linux下的Shell脚本支持 + /// 默认值:false + /// + public bool _Linux_Only_ShellSwitch + { + get + { + return bool.Parse(Linux_Only_ShellSwitch); + } + set + { + if (value.ToString() != Linux_Only_ShellSwitch) + { + Linux_Only_ShellSwitch = value.ToString(); + OnPropertyChanged(); + ModifyConfig(value); + } + } + } + + private static string Linux_Only_ShellCommand = string.Empty; + /// + /// Linux下将执行的Shell命令模版 + /// 默认值:空字符串(string.Empty) + /// + public string _Linux_Only_ShellCommand + { + get + { + return Linux_Only_ShellCommand; + } + set + { + if (value.ToString() != Linux_Only_ShellCommand) + { + Linux_Only_ShellCommand = value.ToString(); + OnPropertyChanged(); + ModifyConfig(value); + } + } + } + } #endregion } diff --git a/Core/RuntimeObject/DetectRoom.cs b/Core/RuntimeObject/DetectRoom.cs index 46812e95..a562f80f 100644 --- a/Core/RuntimeObject/DetectRoom.cs +++ b/Core/RuntimeObject/DetectRoom.cs @@ -90,6 +90,7 @@ internal static async void DetectRoom_LiveStart(Object? sender, (RoomCardClass C bool Reconnection = false; do { + //如果是启动后第一次房间状态查询就触发下载,那就当作重连处理 if(LiveInvoke.IsFirst) { Reconnection = true; @@ -100,8 +101,14 @@ internal static async void DetectRoom_LiveStart(Object? sender, (RoomCardClass C Reconnection = true; } //如果检测到还在开播,且用户没有取消,那么就再来一次 - while ((RoomInfo.GetLiveStatus(roomCard.RoomId) && !roomCard.DownInfo.Unmark) && roomCard.DownInfo.Status!= RoomCardClass.DownloadStatus.Special); - + while ((RoomInfo.GetLiveStatus(roomCard.RoomId) && !roomCard.DownInfo.Unmark) && roomCard.DownInfo.Status != RoomCardClass.DownloadStatus.Special); + + //执行shell + if(OperatingSystem.IsLinux() && Config.Core_RunConfig._Linux_Only_ShellSwitch) + { + Tools.Shell.Run(Tools.KeyCharacterReplacement.ReplaceKeyword(Config.Core_RunConfig._Linux_Only_ShellCommand, DateTime.Now, roomCard.UID)); + } + //在这一步之前应该处理完所有本次录制任务的工作,执行完成后,清空本次除了录制的文件以外的所有记录 Basics.DownloadCompletedReset(ref roomCard); diff --git a/Core/Tools/KeyCharacterReplacement.cs b/Core/Tools/KeyCharacterReplacement.cs index 7222aee4..247ca767 100644 --- a/Core/Tools/KeyCharacterReplacement.cs +++ b/Core/Tools/KeyCharacterReplacement.cs @@ -46,6 +46,9 @@ public static string ReplaceKeyword(string Text, DateTime dateTime = default,lon Title = new() { Value = "【自我介绍】大家好!我叫绊(kizuna)爱(ai)" }, }; } + string WorkPath = ReplaceKeyword($"{Config.Core_RunConfig._DefaultLiverFolderName}/{Core.Config.Core_RunConfig._DefaultDataFolderName}", DateTime.Now, uid); + WorkPath = Path.GetFullPath(WorkPath); + Text = Text .Replace("{ROOMID}", roomCardClass.RoomId.ToString()) .Replace("{YYYY}", dateTime.ToString("yyyy")) @@ -65,12 +68,55 @@ public static string ReplaceKeyword(string Text, DateTime dateTime = default,lon .Replace("{ss}", dateTime.ToString("ss")) .Replace("{fff}", dateTime.ToString("fff")) .Replace("{NAME}", roomCardClass.Name) + .Replace("{Name}", roomCardClass.Name) .Replace("{DATE}", dateTime.ToString("yyyy_MM_dd")) + .Replace("{Date}", dateTime.ToString("yyyy_MM_dd")) .Replace("{TIME}", dateTime.ToString("HH_mm_ss")) - .Replace("{TITLE}", roomCardClass.Title.Value); + .Replace("{Time}", dateTime.ToString("HH_mm_ss")) + .Replace("{TITLE}", roomCardClass.Title.Value) + .Replace("{Title}", roomCardClass.Title.Value) + .Replace("{R}",new Random().Next(1000,9999).ToString()) + .Replace("{CWD}",WorkPath); + + Replace_Shell_Keyword(roomCardClass, ref Text); + return Text; } + /// + /// 替换{Shell}关键字 + /// + /// 待替换的字符 + /// 需要替换的房间卡片 + /// + public static void Replace_Shell_Keyword(RoomCardClass roomCard, ref string text) + { + var fileTypes = new Dictionary> + { + {"{AfterRepairFiles}", roomCard.DownInfo.DownloadFileList.VideoFile}, + {"{DanmakuFiles}", roomCard.DownInfo.DownloadFileList.DanmuFile}, + {"{SCFiles}", roomCard.DownInfo.DownloadFileList.SCFile}, + {"{GuardFiles}", roomCard.DownInfo.DownloadFileList.GuardFile}, + {"{GiftFiles}", roomCard.DownInfo.DownloadFileList.GiftFile} + }; + + List SumFiles = new List(); + + foreach (var fileType in fileTypes) + { + var files = fileType.Value.Where(File.Exists).Select(f => new FileInfo(f).FullName); + SumFiles.AddRange(files); + text = text.Replace(fileType.Key, string.Join(",", files)); + } + + string sumFilesString = string.Join(",", SumFiles); + text.Replace("{Files}", sumFilesString); + } + + + + + /// /// 随机字符串 /// diff --git a/Core/Tools/Shell.cs b/Core/Tools/Shell.cs new file mode 100644 index 00000000..c23f323e --- /dev/null +++ b/Core/Tools/Shell.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; + +namespace Core.Tools +{ + internal class Shell + { + public static string Run(string Command) + { + if (Config.Core_RunConfig._Linux_Only_ShellSwitch) + { + Process process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "/bin/bash", + Arguments = $"-c \"{Command}\"", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + } + }; + process.Start(); + string result = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return result; + } + else + { + return "收到下载调度器提交的Shell执行请求,但是检测到Config.Core_RunConfig._Linux_Only_ShellSwitch为关闭状态,拒绝执行"; + } + } + } +}