Skip to content

Commit

Permalink
增加shell功能
Browse files Browse the repository at this point in the history
  • Loading branch information
CHKZL committed Jun 22, 2024
1 parent ac30114 commit 0435efc
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 3 deletions.
44 changes: 44 additions & 0 deletions Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,50 @@ public bool _PlayWindowDanmaSwitch
}
}

private static string Linux_Only_ShellSwitch = "false";
/// <summary>
/// 是否开启Linux下的Shell脚本支持
/// 默认值:false
/// </summary>
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;
/// <summary>
/// Linux下将执行的Shell命令模版
/// 默认值:空字符串(string.Empty)
/// </summary>
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
}
Expand Down
11 changes: 9 additions & 2 deletions Core/RuntimeObject/DetectRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ internal static async void DetectRoom_LiveStart(Object? sender, (RoomCardClass C
bool Reconnection = false;
do
{
//如果是启动后第一次房间状态查询就触发下载,那就当作重连处理
if(LiveInvoke.IsFirst)
{
Reconnection = true;
Expand All @@ -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);
Expand Down
48 changes: 47 additions & 1 deletion Core/Tools/KeyCharacterReplacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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;
}

/// <summary>
/// 替换{Shell}关键字
/// </summary>
/// <param name="text">待替换的字符</param>
/// <param name="roomCard">需要替换的房间卡片</param>
/// <returns></returns>
public static void Replace_Shell_Keyword(RoomCardClass roomCard, ref string text)
{
var fileTypes = new Dictionary<string, List<string>>
{
{"{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<string> SumFiles = new List<string>();

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);
}





/// <summary>
/// 随机字符串
/// </summary>
Expand Down
39 changes: 39 additions & 0 deletions Core/Tools/Shell.cs
Original file line number Diff line number Diff line change
@@ -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为关闭状态,拒绝执行";
}
}
}
}

0 comments on commit 0435efc

Please sign in to comment.