Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72e3c9f

Browse files
committed
BouyomiPluginをリファクタリング
1 parent f8970d5 commit 72e3c9f

File tree

3 files changed

+153
-16
lines changed

3 files changed

+153
-16
lines changed

‎BouyomiPlugin/IpcTalker.cs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using FNF.Utility;
2+
using System;
3+
using System.Runtime.Remoting;
4+
using System.Runtime.Remoting.Contexts;
5+
using System.Runtime.Remoting.Messaging;
6+
7+
namespace BouyomiPlugin
8+
{
9+
class IpcTalker : ITalker
10+
{
11+
public void Dispose()
12+
{
13+
_bouyomiChanClient.Dispose();
14+
}
15+
16+
public void TalkText(string text)
17+
{
18+
_bouyomiChanClient.AddTalkTask2(text);
19+
}
20+
21+
public void TalkText(string text, Int16 voiceSpeed, Int16 voiceTone, Int16 voiceVolume, VoiceType voiceType)
22+
{
23+
try
24+
{
25+
_bouyomiChanClient.AddTalkTask2(
26+
text,
27+
voiceSpeed,
28+
voiceTone,
29+
voiceVolume,
30+
voiceType
31+
);
32+
}
33+
catch (RemotingException ex)
34+
{
35+
throw new TalkException("", ex);
36+
}
37+
}
38+
private readonly BouyomiChanClient _bouyomiChanClient = new FNF.Utility.BouyomiChanClient();
39+
}
40+
}

‎BouyomiPlugin/TcpTalker.cs‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Sockets;
4+
using System.Text;
5+
6+
namespace BouyomiPlugin
7+
{
8+
class TcpTalker : ITalker
9+
{
10+
private bool _disposedValue;
11+
12+
const Int16 COMMAND = 0x0001;
13+
const byte CHARCODE_UTF8 = 0;
14+
15+
public string IpAddr { get; set; }
16+
public int Port { get; set; }
17+
18+
public void TalkText(string text)
19+
{
20+
//-1を指定すると棒読みちゃん画面上の設定を使用する
21+
TalkText(text, -1, -1, -1, FNF.Utility.VoiceType.Default);
22+
}
23+
public void TalkText(string text, Int16 voiceSpeed, Int16 voiceTone, Int16 voiceVolume, FNF.Utility.VoiceType voiceTypeIndex)
24+
{
25+
var ipAddr = "127.0.0.1";
26+
var port = 50001;
27+
var messageBytes = Encoding.UTF8.GetBytes(text);
28+
29+
try
30+
{
31+
using (var tc = new TcpClient(ipAddr, port))
32+
using (NetworkStream ns = tc.GetStream())
33+
using (var bw = new BinaryWriter(ns))
34+
{
35+
bw.Write(COMMAND); //コマンド( 0:メッセージ読み上げ)
36+
bw.Write((Int16)voiceSpeed); //速度 (-1:棒読みちゃん画面上の設定)
37+
bw.Write((Int16)voiceTone); //音程 (-1:棒読みちゃん画面上の設定)
38+
bw.Write((Int16)voiceVolume); //音量 (-1:棒読みちゃん画面上の設定)
39+
bw.Write((Int16)voiceTypeIndex); //声質 ( 0:棒読みちゃん画面上の設定、1:女性1、2:女性2、3:男性1、4:男性2、5:中性、6:ロボット、7:機械1、8:機械2、10001〜:SAPI5)
40+
bw.Write(CHARCODE_UTF8); //文字列のbyte配列の文字コード(0:UTF-8, 1:Unicode, 2:Shift-JIS)
41+
bw.Write((Int32)messageBytes.Length); //文字列のbyte配列の長さ
42+
bw.Write(messageBytes); //文字列のbyte配列
43+
}
44+
}
45+
catch (Exception ex)
46+
{
47+
throw new TalkException("", ex);
48+
}
49+
}
50+
51+
protected virtual void Dispose(bool disposing)
52+
{
53+
if (!_disposedValue)
54+
{
55+
if (disposing)
56+
{
57+
// TODO: dispose managed state (managed objects)
58+
}
59+
60+
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
61+
// TODO: set large fields to null
62+
_disposedValue = true;
63+
}
64+
}
65+
66+
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
67+
// ~TcpTalker()
68+
// {
69+
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
70+
// Dispose(disposing: false);
71+
// }
72+
73+
public void Dispose()
74+
{
75+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
76+
Dispose(disposing: true);
77+
GC.SuppressFinalize(this);
78+
}
79+
public TcpTalker(string ipAddr, int port)
80+
{
81+
IpAddr = ipAddr;
82+
Port = port;
83+
}
84+
}
85+
}

‎BouyomiPlugin/main.cs‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
using System.Collections.Generic;
1515
using System.ComponentModel.Composition;
1616
using System.Diagnostics;
17-
using System.Runtime.Remoting.Contexts;
18-
using System.Runtime.Remoting.Messaging;
1917
using TwicasSitePlugin;
2018
using TwitchSitePlugin;
2119
using WhowatchSitePlugin;
@@ -40,7 +38,7 @@ public static string ToTextWithImageAlt(this IEnumerable<IMessagePart> parts)
4038
{
4139
s += image.Alt;
4240
}
43-
else if(part is IMessageRemoteSvg remoteSvg)
41+
else if(part is IMessageRemoteSvg remoteSvg)
4442
{
4543
s += remoteSvg.Alt;
4644
}
@@ -53,10 +51,28 @@ public static string ToTextWithImageAlt(this IEnumerable<IMessagePart> parts)
5351
return s;
5452
}
5553
}
54+
interface ITalker : IDisposable
55+
{
56+
void TalkText(string text);
57+
58+
void TalkText(string text, Int16 voiceSpeed, Int16 voiceTone, Int16 voiceVolume, FNF.Utility.VoiceType voiceType);
59+
}
60+
61+
[Serializable]
62+
public class TalkException : Exception
63+
{
64+
public TalkException() { }
65+
public TalkException(string message) : base(message) { }
66+
public TalkException(string message, Exception inner) : base(message, inner) { }
67+
protected TalkException(
68+
System.Runtime.Serialization.SerializationInfo info,
69+
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
70+
}
5671
[Export(typeof(IPlugin))]
5772
public class BouyomiPlugin : IPlugin, IDisposable
5873
{
59-
private readonly FNF.Utility.BouyomiChanClient _bouyomiChanClient;
74+
//private readonly FNF.Utility.BouyomiChanClient _bouyomiChanClient;
75+
private readonly ITalker _talker;
6076
private Options _options;
6177
Process _bouyomiChanProcess;
6278
public string Name => "棒読みちゃん連携";
@@ -695,7 +711,7 @@ public void OnMessageReceived(ISiteMessage message, IMessageMetadata messageMeta
695711
}
696712
TalkText(dataToRead);
697713
}
698-
catch (System.Runtime.Remoting.RemotingException)
714+
catch (TalkException)
699715
{
700716
//多分棒読みちゃんが起動していない。
701717
if (_bouyomiChanProcess == null && System.IO.File.Exists(_options.BouyomiChanPath))
@@ -720,21 +736,17 @@ public void OnMessageReceived(ISiteMessage message, IMessageMetadata messageMeta
720736
}
721737
}
722738

723-
private int TalkText(string text)
739+
private void TalkText(string text)
724740
{
725741
if (_options.IsVoiceTypeSpecfied)
726742
{
727-
return _bouyomiChanClient.AddTalkTask2(
728-
text,
729-
_options.VoiceSpeed,
730-
_options.VoiceTone,
731-
_options.VoiceVolume,
732-
(FNF.Utility.VoiceType)Enum.ToObject(typeof(FNF.Utility.VoiceType), _options.VoiceTypeIndex)
733-
);
743+
_talker.TalkText(text, (Int16)_options.VoiceSpeed,
744+
(Int16)_options.VoiceTone,
745+
(Int16)_options.VoiceVolume, (FNF.Utility.VoiceType)Enum.ToObject(typeof(FNF.Utility.VoiceType), _options.VoiceTypeIndex));
734746
}
735747
else
736748
{
737-
return_bouyomiChanClient.AddTalkTask2(text);
749+
_talker.TalkText(text);
738750
}
739751
}
740752

@@ -763,7 +775,7 @@ public void ShowSettingView()
763775
}
764776
public BouyomiPlugin()
765777
{
766-
_bouyomiChanClient = new FNF.Utility.BouyomiChanClient();
778+
_talker = new IpcTalker();
767779
_options = new Options();
768780
}
769781

@@ -777,7 +789,7 @@ protected virtual void Dispose(bool disposing)
777789
if (disposing)
778790
{
779791
}
780-
_bouyomiChanClient.Dispose();
792+
_talker.Dispose();
781793
if (_bouyomiChanProcess != null)
782794
{
783795
_bouyomiChanProcess.Close();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /