private async void Button_Click(object sender, RoutedEventArgs e) {// 打开设置窗 var result = await SendDanmakuMain.api.doAuth(SendDanmakuMain.self); switch (result) { case AuthorizationResult.Success: SendDanmakuMain.log("插件已经被允许使用B站账号!"); break; case AuthorizationResult.Failure: SendDanmakuMain.log("授权失败,用户拒绝"); break; case AuthorizationResult.Disabled: SendDanmakuMain.log("权限被禁用,请到“登录中心”插件启用授权"); break; case AuthorizationResult.Timeout: SendDanmakuMain.log("授权失败,用户未操作超时"); break; case AuthorizationResult.Illegal: case AuthorizationResult.Duplicate: default: break; } }
/// <summary> /// 窗口加载完成事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SendTool_Loaded(object sender, RoutedEventArgs e) { try { DeveloperMode = Convert.ToBoolean(WinAPI.ProfileReadValue("SendDanmaku", "DeveloperMode", PluginPath + "Config.ini")); SendTool.Opacity = Convert.ToDouble(WinAPI.ProfileReadValue("SendDanmaku", "Opacity", PluginPath + "Config.ini")); SendTool.Topmost = Convert.ToBoolean(WinAPI.ProfileReadValue("SendDanmaku", "Topmost", PluginPath + "Config.ini")); string[] sArray = Regex.Split(WinAPI.ProfileReadValue("SendDanmaku", "Hotkey", PluginPath + "Config.ini"), " ", RegexOptions.IgnoreCase); RegHotKey(new WindowInteropHelper(this).Handle, 37844, Convert.ToUInt16(sArray[0]), Convert.ToUInt16(sArray[1])); sArray = Regex.Split(WinAPI.ProfileReadValue("SendDanmaku", "Size", PluginPath + "Config.ini"), " ", RegexOptions.IgnoreCase); SendTool.Width = Convert.ToDouble(sArray[0]); SendTool.Height = Convert.ToDouble(sArray[1]); text_count_max.Text = Convert.ToString(WinAPI.ProfileReadValue("SendDanmaku", "Countmax", PluginPath + "Config.ini")); input.MaxLength = Convert.ToInt16(text_count_max.Text); } catch (Exception) { //配置文件读取失败或不存在 try { if (false == System.IO.Directory.Exists(PluginPath)) { //创建文件夹 Directory.CreateDirectory(PluginPath); //创建配置文件 StreamWriter t = new StreamWriter(PluginPath + "Config.ini", false, System.Text.Encoding.ASCII); t.Close(); } else { //创建配置文件 StreamWriter t = new StreamWriter(PluginPath + "Config.ini", false, System.Text.Encoding.ASCII); t.Close(); } if (Initialize()) { SendDanmakuMain.log("初始化完成"); } else { MessageBox.Show("配置文件初始化完成失败,请检查!", "开发者模式", MessageBoxButton.OK, MessageBoxImage.Error); return; } } catch (Exception ex) { MessageBox.Show("配置文件创建失败,请检查!\n" + ex.ToString(), "开发者模式", MessageBoxButton.OK, MessageBoxImage.Error); return; } } }
private async void input_TextChanged(object sender, TextChangedEventArgs e) { try { if (input.Text.Contains("\n")) { string text = input.Text.Replace("\r", string.Empty).Replace("\n", string.Empty); input.Text = string.Empty; history = 0; add2List(text); string result = null; try { if (SendDanmakuMain.self.RoomId.HasValue) { result = await SendDanmakuAsync(SendDanmakuMain.self.RoomId.Value, text, LoginCenterAPI.getCookies()); //string.Join("; ", LoginCenterAPI.getCookies().GetCookies(new Uri("http://live.bilibili.com/")).OfType<Cookie>().Select(p => $"{p.Name}={p.Value}")) //result = await SendDanmakuMain.api.send(SendDanmakuMain.self.RoomId.Value, text); } else { SendDanmakuMain.log("还未连接直播间!"); return; } } catch (PluginNotAuthorizedException) { SendDanmakuMain.log("插件未被授权使用B站账号"); return; } if (result == null) { SendDanmakuMain.log("网络错误,请重试"); } else { var j = JObject.Parse(result); string msg = (j["msg"] ?? j["message"]).ToString(); if (msg != string.Empty) { SendDanmakuMain.log("服务器返回:" + msg); } } } } finally {// 统计弹幕字数 text_count.Text = input.Text.Length.ToString(); } }
private async void input_TextChanged(object sender, TextChangedEventArgs e) { try { if (input.Text.Contains("\n")) { string text = input.Text.Replace("\r", string.Empty).Replace("\n", string.Empty); input.Text = string.Empty; history = 0; add2List(text); string result = null; try { if (SendDanmakuMain.self.RoomId.HasValue) { result = await SendDanmakuMain.api.send(SendDanmakuMain.self.RoomId.Value, text); } else { SendDanmakuMain.log("还未连接直播间!"); return; } } catch (PluginNotAuthorizedException) { SendDanmakuMain.log("插件未被授权使用B站账号"); return; } if (result == null) { SendDanmakuMain.log("网络错误,请重试"); } else { var j = JObject.Parse(result); if (j["msg"].ToString() != string.Empty) { SendDanmakuMain.log("服务器返回:" + j["msg"].ToString()); } } } } finally {// 统计弹幕字数 text_count.Text = input.Text.Length.ToString(); } }
public SendDanmakuMain() { if (self == null) { self = this; } else { throw new InvalidOperationException(); } this.PluginName = "弹幕发送"; this.PluginDesc = "使用弹幕姬快速发送弹幕"; this.PluginAuth = "宅急送队长"; this.PluginCont = "私信15253直播间主播或弹幕姬群内私聊"; this.PluginVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(3); VersionChecker.Check(this); }
/// <summary> /// 开发者模式入口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (!DeveloperMode) { ClickCount++; if (ClickCount == 5) { ClickCount = 0; DeveloperMode = true; try { WinAPI.ProfileWriteValue("SendDanmaku", "DeveloperMode", DeveloperMode.ToString(), PluginPath + "Config.ini"); SendDanmakuMain.log("您已进入开发者模式!"); help_Text.Text = "您已进入开发者模式!"; MessageBox.Show("\n辅助键:\nAlt=1\nCtrl=2\nCtrl+Alt=3\nShift=4\nShift+Alt=5\nShift+Alt+Ctrl=7\nWindows键=8\n\n主键:\nSpace=32\nLeft=37\nUp=38\nRight=39\nDown=40\nA=65\nB=66\nC=67\nD=68\nE=69\nF=70\nG=71\nH=72\nI=73\nJ=74\nK=75\nL=76\nM=77\nN=78\nO=79\nP=80\nQ=81\nR=82\nS=83\nT=84\nU=85\nV=86\nW=87\nX=88\nY=89\nZ=90\nF1=112\nF2=113\nF3=114\nF4=115\nF5=116\nF6=117\nF7=118\nF8=119\nF9=120\nF10=121\nF11=122\nF12=123\n", "开发者模式", MessageBoxButton.OK, MessageBoxImage.Asterisk); } catch (Exception ex) { MessageBox.Show("配置文件写入失败,请检查!\n" + ex.ToString(), "开发者模式", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else if (ClickCount < 5) { SendDanmakuMain.log("再点击 " + (5 - ClickCount) + " 次将会进入开发者模式"); help_Text.Text = "再点击 " + (5 - ClickCount) + " 次将会进入开发者模式"; return; } else if (ClickCount > 5) { SendDanmakuMain.log("您已进入开发者模式!"); help_Text.Text = "您已进入开发者模式!"; return; } } else { SendDanmakuMain.log("您已处于开发者模式!"); help_Text.Text = "您已处于开发者模式!"; return; } return; }
/// <summary> /// 注册热键 /// </summary> /// <param name="hwnd">窗口句柄</param> /// <param name="hotKey_id">热键ID</param> /// <param name="keyModifiers">组合键</param> /// <param name="key">热键</param> public static void RegHotKey(IntPtr hwnd, int hotKeyId, uint KeyModifiers, int key) { if (!RegisterHotKey(hwnd, hotKeyId, KeyModifiers, key)) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode == 1409) { MessageBox.Show("热键被占用 !"); } else { MessageBox.Show("热键注册失败!错误代码:" + errorCode); } } else { SendDanmakuMain.log("热键注册成功"); } }
/// <summary> /// 申请授权 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Button_Click(object sender, RoutedEventArgs e) { var result = await SendDanmakuMain.api.doAuth(SendDanmakuMain.self); switch (result) { case AuthorizationResult.Success: SendDanmakuMain.log("插件已经被允许使用B站账号!"); input.IsEnabled = true; SendDanmakuMain.Toolwindows.GridA.Children.Remove(Button); if (!DeveloperMode) { help_Text.Text = "[回车键发送弹幕] 按“Ctrl+Alt+W”隐藏/唤出发送窗"; } else { help_Text.Text = "[回车键发送弹幕] 请使用您自定义的键位隐藏/唤出发送窗"; } break; case AuthorizationResult.Failure: SendDanmakuMain.log("授权失败,用户拒绝"); help_Text.Text = "授权失败,用户拒绝"; break; case AuthorizationResult.Disabled: SendDanmakuMain.log("权限被禁用,请到“登录中心”插件启用授权"); help_Text.Text = "权限被禁用,请到“登录中心”插件启用授权"; break; case AuthorizationResult.Timeout: SendDanmakuMain.log("授权失败,用户未操作超时"); help_Text.Text = "授权失败,用户未操作超时"; break; case AuthorizationResult.Illegal: case AuthorizationResult.Duplicate: default: break; } }
private bool Initialize() { try { WinAPI.ProfileWriteValue("SendDanmaku", "DeveloperMode", DeveloperMode.ToString(), PluginPath + "Config.ini"); WinAPI.ProfileWriteValue("SendDanmaku", "Opacity", SendTool.Opacity.ToString(), PluginPath + "Config.ini"); WinAPI.ProfileWriteValue("SendDanmaku", "Topmost", SendTool.Topmost.ToString(), PluginPath + "Config.ini"); WinAPI.ProfileWriteValue("SendDanmaku", "Hotkey", 3 + " " + 87, PluginPath + "Config.ini"); WinAPI.ProfileWriteValue("SendDanmaku", "Size", 768 + " " + 32, PluginPath + "Config.ini"); WinAPI.ProfileWriteValue("SendDanmaku", "Countmax", "30", PluginPath + "Config.ini"); //开始插件初始化 DeveloperMode = Convert.ToBoolean(WinAPI.ProfileReadValue("SendDanmaku", "DeveloperMode", PluginPath + "Config.ini")); SendTool.Opacity = Convert.ToDouble(WinAPI.ProfileReadValue("SendDanmaku", "Opacity", PluginPath + "Config.ini")); SendDanmakuMain.log("已调整透明度为" + SendTool.Opacity.ToString()); SendTool.Topmost = Convert.ToBoolean(WinAPI.ProfileReadValue("SendDanmaku", "Topmost", PluginPath + "Config.ini")); SendDanmakuMain.log("已调整窗口置顶为" + SendTool.Topmost.ToString()); string[] sArray = Regex.Split(WinAPI.ProfileReadValue("SendDanmaku", "Hotkey", PluginPath + "Config.ini"), " ", RegexOptions.IgnoreCase); UnregisterHotKey(new WindowInteropHelper(this).Handle, 37844); RegHotKey(new WindowInteropHelper(this).Handle, 37844, Convert.ToUInt16(sArray[0]), Convert.ToUInt16(sArray[1])); SendDanmakuMain.log("已调整热键为 " + sArray[0] + " " + sArray[1]); sArray = Regex.Split(WinAPI.ProfileReadValue("SendDanmaku", "Size", PluginPath + "Config.ini"), " ", RegexOptions.IgnoreCase); SendTool.Width = Convert.ToDouble(sArray[0]); SendTool.Height = Convert.ToDouble(sArray[1]); SendDanmakuMain.log("已调整窗口大小为" + sArray[0] + "*" + sArray[1]); text_count_max.Text = Convert.ToString(WinAPI.ProfileReadValue("SendDanmaku", "Countmax", PluginPath + "Config.ini")); input.MaxLength = Convert.ToInt16(text_count_max.Text); SendDanmakuMain.log("已设置弹幕字数上限为" + text_count_max.Text); } catch (Exception) { return(false); } return(true); }
/// <summary> /// 用户输入监听 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void input_TextChanged(object sender, TextChangedEventArgs e) { try { if (input.Text != string.Empty) { if (input.Text.Substring(input.Text.Length - 1, 1) == "\n") { if (input.Text.Remove(1, input.Text.Length - 1) == "/") { string[] sArray = Regex.Split(input.Text.Remove(0, 1).Replace("\r", string.Empty).Replace("\n", string.Empty), " ", RegexOptions.IgnoreCase); switch (DeveloperModeSet(sArray)) { case 0: help_Text.Text = "指令 " + sArray[1] + " 执行成功!"; return; case 1: help_Text.Text = "指令 " + sArray[1] + " 的执行已被取消!"; return; case 2: help_Text.Text = "指令 " + sArray[1] + " 的执行出现错误!"; return; case 3: help_Text.Text = "指令 " + sArray[1] + " 的执行出现未知错误!"; return; default: help_Text.Text = "指令 " + sArray[1] + " 的未能被识别!将作为弹幕进行发送。"; break; } } help_Text.Text = "发送中..."; string text = input.Text.Replace("\r", string.Empty).Replace("\n", string.Empty); input.Text = string.Empty; history = 0; add2List(text); string result = null; try { if (SendDanmakuMain.self.RoomId.HasValue) { result = await SendDanmakuAsync(SendDanmakuMain.self.RoomId.Value, text, LoginCenterAPI.getCookies()); //string.Join("; ", LoginCenterAPI.getCookies().GetCookies(new Uri("http://live.bilibili.com/")).OfType<Cookie>().Select(p => $"{p.Name}={p.Value}")) //result = await SendDanmakuMain.api.send(SendDanmakuMain.self.RoomId.Value, text); } else { SendDanmakuMain.log("还未连接直播间!"); help_Text.Text = "还未连接至直播间!"; return; } } catch (PluginNotAuthorizedException) { SendDanmakuMain.log("插件未被授权使用B站账号"); help_Text.Text = "插件未被授权使用B站账号"; return; } if (result == null) { SendDanmakuMain.log("网络错误,请重试"); help_Text.Text = "网络错误,请重试"; return; } else { var j = JObject.Parse(result); string msg = (j["msg"] ?? j["message"]).ToString(); if (msg != string.Empty) { SendDanmakuMain.log("服务器返回:" + msg); help_Text.Text = "错误:" + msg; return; } } help_Text.Text = "已发送!"; } } } finally {// 统计弹幕字数 text_count.Text = input.Text.Length.ToString(); if (input.Text.Length >= input.MaxLength) { text_count.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } else { text_count.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0)); } } }
/// <summary> /// 开发者模式主程序 /// </summary> /// <param name="data">指令和参数</param> /// <returns></returns> private int DeveloperModeSet(string[] data) { if (DeveloperMode) { switch (data[0].ToUpper()) { case "SET": switch (data[1].ToUpper()) { case "OPACITY": try { SendTool.Opacity = Convert.ToDouble(data[2]); SendDanmakuMain.log("已调整透明度为" + SendTool.Opacity); WinAPI.ProfileWriteValue("SendDanmaku", "Opacity", SendTool.Opacity.ToString(), PluginPath + "Config.ini"); break; } catch (Exception) { return(2); } case "TOPMOST": try { SendTool.Topmost = Convert.ToBoolean(data[2]); SendDanmakuMain.log("已调整窗口置顶为" + SendTool.Topmost.ToString()); WinAPI.ProfileWriteValue("SendDanmaku", "Topmost", SendTool.Topmost.ToString(), PluginPath + "Config.ini"); break; } catch (Exception) { return(2); } case "HOTKEY": try { //注销热键 UnregisterHotKey(new WindowInteropHelper(this).Handle, 37844); //注册热键 RegHotKey(new WindowInteropHelper(this).Handle, 37844, Convert.ToUInt16(data[2]), Convert.ToUInt16(data[3])); SendDanmakuMain.log("已调整热键为" + data[2] + data[3]); MessageBox.Show("已调整热键为" + data[2] + data[3], "开发者模式", MessageBoxButton.OK, MessageBoxImage.Asterisk); WinAPI.ProfileWriteValue("SendDanmaku", "Hotkey", data[2] + " " + data[3], PluginPath + "Config.ini"); break; } catch (Exception) { return(2); } case "SIZE": try { if ((Convert.ToDouble(data[2]) <= 300) || (Convert.ToDouble(data[3]) <= 30)) { switch (MessageBox.Show("注意,如果你设置的窗口大小过小可能会影响正常使用。\n是否确认修改?", "开发者模式", MessageBoxButton.YesNo, MessageBoxImage.Asterisk)) { case MessageBoxResult.Yes: SendTool.Width = Convert.ToDouble(data[2]); SendTool.Height = Convert.ToDouble(data[3]); SendDanmakuMain.log("已调整窗口大小为 " + data[2] + " * " + data[3]); WinAPI.ProfileWriteValue("SendDanmaku", "Size", data[2] + " " + data[3], PluginPath + "Config.ini"); break; case MessageBoxResult.No: return(1); } } else { SendTool.Width = Convert.ToDouble(data[2]); SendTool.Height = Convert.ToDouble(data[3]); SendDanmakuMain.log("已调整窗口大小为 " + data[2] + " * " + data[3]); WinAPI.ProfileWriteValue("SendDanmaku", "Size", data[2] + " " + data[3], PluginPath + "Config.ini"); break; } return(2); } catch (Exception) { return(2); } case "COUNTMAX": try { if (!(Convert.ToInt16(data[2]) <= 20)) { input.MaxLength = Convert.ToInt16(data[2]); text_count_max.Text = input.MaxLength.ToString(); SendDanmakuMain.log("已弹幕字数上限为 " + data[2]); WinAPI.ProfileWriteValue("SendDanmaku", "Countmax", data[2], PluginPath + "Config.ini"); break; } else if (Convert.ToInt16(data[2]) <= 10) { MessageBox.Show("注意,字数上限过低!", "开发者模式", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(1); } else { switch (MessageBox.Show("注意,字数上限小于或等于20可能会导致您无法修改该数值。\n是否确认修改?", "开发者模式", MessageBoxButton.YesNo, MessageBoxImage.Asterisk)) { case MessageBoxResult.Yes: input.MaxLength = Convert.ToInt16(data[2]); text_count_max.Text = input.MaxLength.ToString(); SendDanmakuMain.log("已弹幕字数上限为 " + data[2]); WinAPI.ProfileWriteValue("SendDanmaku", "Countmax", data[2], PluginPath + "Config.ini"); break; case MessageBoxResult.No: return(1); } } break; } catch (Exception) { return(2); } default: MessageBox.Show("没有这个指令!", "开发者模式", MessageBoxButton.OK, MessageBoxImage.Asterisk); return(1); } break; case "RESET": case "INITIALIZE": switch (MessageBox.Show("注意,该操作将会初始化配置文件。\n是否确认初始化?", "开发者模式", MessageBoxButton.YesNo, MessageBoxImage.Exclamation)) { case MessageBoxResult.Yes: if (Initialize()) { SendDanmakuMain.log("初始化完成"); break; } else { MessageBox.Show("配置文件初始化完成失败,请检查!", "开发者模式", MessageBoxButton.OK, MessageBoxImage.Error); return(2); } case MessageBoxResult.No: return(1); } break; default: MessageBox.Show("缺少参数!", "开发者模式", MessageBoxButton.OK, MessageBoxImage.Asterisk); return(1); } input.Text = string.Empty; return(0); } return(3); }