Develop and Download Open Source Software

Browse Subversion Repository

Contents of /TextReaderServices.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations) (download)
Thu Apr 21 13:23:22 2011 UTC (12 years, 10 months ago) by aqua877
File size: 3451 byte(s)


1 using System.Collections.Generic;
2 using System.IO;
3 using System;
4 using System.Linq;
5 using System.Diagnostics;
6 using System.Text.RegularExpressions;
7
8 namespace Aqua877.WinApp.IronLivetube
9 {
10 public enum MatchTarget
11 {
12 ToText,
13 ExceptText,
14 All
15 }
16
17 public class Replacement
18 {
19 public string Before { get; set; }
20 public string After { get; set; }
21 public bool AddedByUser { get; set; }
22 public MatchTarget ReplacementTarget { get; set; }
23 public bool IsApplyToListView { get; set; }
24 public bool IsUseRegex { get; set; }
25 }
26
27 public class TextReaderServices
28 {
29 public bool IsEnable = false;
30 private bool FirstPush = true;
31 private Queue<string> TextQueue = new Queue<string>();
32 private IDisposable ReadTimer = null;
33 private object SyncObj = new object();
34
35 public void Read(IEnumerable<LivetubeCommentData> text)
36 {
37 if (this.FirstPush && !GlobalValues.Setting.IsReadCommentAtFirst)
38 {
39 //このあたりをObservable.Throttle使って書きなおしたほうが重くならずに済むかも
40 this.FirstPush = false;
41 return;
42 }
43 text.OrderBy(d => d.Number)
44 .ForEach(s => this.Read(s));
45 }
46
47 public void Read(LivetubeCommentData comment)
48 {
49 if (!GlobalValues.Setting.IsEnableReading)
50 {
51 return;
52 }
53
54 lock (this.SyncObj)
55 {
56
57 //softalkに読ませるためのテキスト作成
58 string readText = GlobalValues.Setting.ReadingTemplate;
59
60 readText = readText
61 .Replace("{ResID}", comment.Number.ToString())
62 .Replace("{NanashiID}", comment.ID)
63 .Replace("{PostText}", comment.Text);
64
65 if (comment.Name != "")
66 {
67 readText = Regex.Replace(readText, "{Writer\\[(.*?)\\]}", comment.Name + "$1");
68 }
69 else
70 {
71 readText = Regex.Replace(readText, "{Writer\\[(.*?)\\]}", "");
72 }
73
74 if (Regex.IsMatch(readText, "{PostDate\\[(.*?)\\]}"))
75 {
76 string format = Regex.Replace(readText, "{PostDate\\[(.*?)\\]}", "$1");
77 readText = Regex.Replace(readText, "{PostDate\\[(.*?)\\]}", comment.PostedDate.ToString(format));
78 }
79 this.TextQueue.Enqueue(readText);
80 }
81 }
82
83 public void Start()
84 {
85 this.FirstPush = true;
86 this.IsEnable = true;
87 this.ReadTimer = Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(3))
88 .Subscribe(_ => this.Elapsed());
89 }
90
91 public void Stop()
92 {
93 this.IsEnable = false;
94 this.ReadTimer.Dispose();
95 this.TextQueue.Clear();
96 }
97
98 public void Restart()
99 {
100 this.IsEnable = true;
101 this.ReadTimer = Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(3))
102 .Subscribe(_ => this.Elapsed());
103 }
104
105 public void Suspend()
106 {
107 this.IsEnable = false;
108 this.ReadTimer.Dispose();
109 }
110
111 private void Elapsed()
112 {
113 if(!IsEnable)
114 {
115 return;
116 }
117
118 lock (this.SyncObj)
119 {
120 if (this.TextQueue.Count == 0)
121 {
122 return;
123 }
124
125 //読み込み待機数の数によって速度を設定
126 var readSpeed = 50 + (int)(100 * this.TextQueue.Count * 0.5);
127
128 if (readSpeed > 300)
129 {
130 readSpeed += 300 - readSpeed;
131 }
132
133 string readText = String.Format("/T:0 /S:{0} /W:{1}", readSpeed, this.TextQueue.Dequeue());
134
135 try
136 {
137 Process.Start(GlobalValues.Setting.SoftalkPath, readText);
138 }
139 catch
140 {
141 #warning TODO : エラーロギング
142 this.Stop();
143 }
144 }
145 }
146 }
147 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26