コード例 #1
0
        public void Dispose()
        {
            State = TVTCommentState.Disposing;
            if (!quickDispose)
            {
                try
                {
                    //相手からCloseを要求を受けていないなら
                    if (!isClosing)
                    {
                        closingResetEvent.Reset();
                        //CloseIPCMessageを相手に送る
                        ipcModule.Send(new IPC.IPCMessage.CloseIPCMessage()).Wait();
                        //相手からCloseIPCMessageが来るまで待つ、1秒以内に来なかったら無視して進める
                        closingResetEvent.Wait(1000);
                    }
                }
                catch { }
            }

            foreach (var chatService in ChatServices)
            {
                chatService.Dispose();
            }

            //メール欄例保存
            this.Settings.ChatPostMailTextExamples = this.ChatPostMailTextExamples.ToArray();

            //各種SubModule破棄
            CommandModule?.Dispose();
            ChatCollectServiceCreationPresetModule?.Dispose();
            DefaultChatCollectServiceModule?.Dispose();
            ChatModule?.Dispose();
            ChatTrendServiceModule?.Dispose();
            ChatCollectServiceModule?.Dispose();
            if (ipcModule != null)
            {
                ipcModule.Disposed        -= ipcManager_Disposed;
                ipcModule.MessageReceived -= ipcManager_MessageReceived;
                ipcModule.Dispose();
            }

            // 設定保存。asyncだがawaitせずに例外は無視。
            this.settingReaderWriter.Write(this.Settings).Wait(5000);

            State = TVTCommentState.Disposed;
        }
コード例 #2
0
        private async Task initialize()
        {
            if (State != TVTCommentState.NotInitialized)
            {
                throw new InvalidOperationException("This object is already initialized");
            }

            // プラグインの無効化→有効化を短時間で行うと
            // 設定ファイルがアクセス中でIOExceptionが飛ぶので時間を空けて試す
            for (int i = 1; ; ++i)
            {
                try
                {
                    this.Settings = await this.settingReaderWriter.Read();

                    break;
                }
                catch (FormatException)
                {
                    this.Settings = new TVTCommentSettings();
                    break;
                }
                catch (IOException)
                {
                    const int retryCount = 6;
                    if (i >= retryCount)
                    {
                        throw;
                    }
                }
                await Task.Delay(500);
            }

            string baseDir = Path.GetDirectoryName(getExePath());

            this.channelDatabase = new ChannelDatabase(Path.Combine(baseDir, "channels.txt"));
            this.ChatServices    = new ReadOnlyCollection <ChatService.IChatService>(new ChatService.IChatService[] {
                new ChatService.NiconicoChatService(
                    Settings.Niconico, this.channelDatabase, Path.Combine(baseDir, "niconicojikkyouids.txt"), Path.Combine(baseDir, "niconicoliveids.txt")
                    ),
                new ChatService.NichanChatService(Settings.Nichan, this.channelDatabase, Path.Combine(baseDir, "2chthreads.txt")),
                new ChatService.FileChatService()
            });

            var chatCollectServiceEntryIds = this.ChatServices.SelectMany(x => x.ChatCollectServiceEntries).Select(x => x.Id);

            System.Diagnostics.Debug.Assert(
                chatCollectServiceEntryIds.Distinct().Count() == chatCollectServiceEntryIds.Count(),
                "IDs of ChatCollectServiceEntries are not unique"
                );

            //Viewerとの接続
            string[] commandLine = Environment.GetCommandLineArgs();
            if (commandLine.Length == 3)
            {
                ipcModule = new IPCModule(commandLine[1], commandLine[2], SynchronizationContext.Current);
            }
            else
            {
                ipcModule = new IPCModule("TVTComment_Up", "TVTComment_Down", SynchronizationContext.Current);
            }

            ipcModule.Disposed -= ipcManager_Disposed;
            ipcModule.Disposed += ipcManager_Disposed;
            try
            {
                await ipcModule.Connect();
            }
            catch (IPCModule.ConnectException) { return; }
            ipcModule.MessageReceived += ipcManager_MessageReceived;

            //各種SubModule作成
            ChannelInformationModule = new ChannelInformationModule(ipcModule);
            ChatCollectServiceModule = new ChatCollectServiceModule(ChannelInformationModule);
            ChatTrendServiceModule   = new ChatTrendServiceModule(SynchronizationContext.Current);
            ChatModule = new ChatModule(
                this.Settings, ChatServices, ChatCollectServiceModule, ipcModule, ChannelInformationModule
                );
            DefaultChatCollectServiceModule = new DefaultChatCollectServiceModule(
                this.Settings, ChannelInformationModule, ChatCollectServiceModule, ChatServices.SelectMany(x => x.ChatCollectServiceEntries)
                );
            CommandModule = new CommandModule(
                ipcModule, SynchronizationContext.Current
                );
            ChatCollectServiceCreationPresetModule = new ChatCollectServiceCreationPresetModule(
                this.Settings, ChatServices.SelectMany(x => x.ChatCollectServiceEntries)
                );

            //コメント透過度設定処理
            ChatOpacity = new ObservableValue <byte>(this.Settings.ChatOpacity);
            ChatOpacity.Subscribe(async opacity =>
            {
                this.Settings.ChatOpacity = opacity;
                await ipcModule.Send(new IPC.IPCMessage.SetChatOpacityIPCMessage {
                    Opacity = opacity
                });
            });

            //メール欄例設定
            var chatPostMailTextExamples = this.Settings.ChatPostMailTextExamples;

            ChatPostMailTextExamples.AddRange(chatPostMailTextExamples);

            ipcModule.StartReceiving();
            State = TVTCommentState.Working;
        }