X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=KancolleSniffer%2FMainForm.cs;h=214d64e5b545f88cf93e59a81c453db82c363924;hb=484250b68f1620edc8331032939b69bf889e6f44;hp=3d4b45b1663ecf77e1541b52a5a372588231a4c4;hpb=864c2d0eca7c4dda6fd845b3d840b9a2de23b88e;p=kancollesniffer%2FKancolleSniffer.git diff --git a/KancolleSniffer/MainForm.cs b/KancolleSniffer/MainForm.cs index 3d4b45b..214d64e 100644 --- a/KancolleSniffer/MainForm.cs +++ b/KancolleSniffer/MainForm.cs @@ -1,105 +1,151 @@ -// Copyright (C) 2013, 2014, 2015 Kazuhiro Fujieda +// Copyright (C) 2013, 2014, 2015 Kazuhiro Fujieda // -// This program is part of KancolleSniffer. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at // -// KancolleSniffer is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. +// http://www.apache.org/licenses/LICENSE-2.0 // -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, see . +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; +using System.Globalization; using System.IO; using System.Linq; +using System.Net.Sockets; +using System.Runtime.InteropServices; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; -using Codeplex.Data; -using Fiddler; +using Microsoft.CSharp.RuntimeBinder; +using Microsoft.Win32; +using static System.Math; +using Timer = System.Windows.Forms.Timer; namespace KancolleSniffer { public partial class MainForm : Form { private readonly Sniffer _sniffer = new Sniffer(); - private readonly dynamic _wmp = Activator.CreateInstance(Type.GetTypeFromProgID("WMPlayer.OCX.7")); private readonly Config _config = new Config(); private readonly ConfigDialog _configDialog; private int _currentFleet; + private bool _combinedFleet; private readonly Label[] _labelCheckFleets; private readonly ShipLabels _shipLabels; - private readonly ShipListForm _shipListForm; + private readonly ListForm _listForm; private readonly NoticeQueue _noticeQueue; private bool _started; private string _debugLogFile; private IEnumerator _playLog; private LogServer _logServer; - private readonly ProxyConfig _prevProxy = new ProxyConfig(); + private int _prevProxyPort; + private readonly SystemProxy _systemProxy = new SystemProxy(); + private readonly ErrorDialog _errorDialog = new ErrorDialog(); + private bool _missionFinishTimeMode; + private bool _ndockFinishTimeMode; + private readonly KancolleDb _kancolleDb = new KancolleDb(); public MainForm() { InitializeComponent(); - FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest; - FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete; - _wmp.PlayStateChange += new EventHandler(_wmp_PlayStateChange); + HttpProxy.AfterSessionComplete += HttpProxy_AfterSessionComplete; _configDialog = new ConfigDialog(_config, this); _labelCheckFleets = new[] {labelCheckFleet1, labelCheckFleet2, labelCheckFleet3, labelCheckFleet4}; // この時点でAutoScaleDimensions == CurrentAutoScaleDimensionsなので、 // MainForm.Designer.csのAutoScaleDimensionsの6f,12fを使う。 - ShipLabel.ScaleFactor = new SizeF(CurrentAutoScaleDimensions.Width / 6f, CurrentAutoScaleDimensions.Height / 12f); + ShipLabel.ScaleFactor = new SizeF(CurrentAutoScaleDimensions.Width / 6f, + CurrentAutoScaleDimensions.Height / 12f); SetupFleetClick(); _shipLabels = new ShipLabels(); _shipLabels.CreateAkashiTimers(panelShipInfo); - _shipLabels.CreateLabels(panelShipInfo, ShowShipOnShipList); - _shipLabels.CreateDamagedShipList(panelDamagedShipList); - _shipLabels.CreateNDockLabels(panelDock); - _shipListForm = new ShipListForm(_sniffer, _config) {Owner = this}; + _shipLabels.CreateShipLabels(panelShipInfo, ShowShipOnShipList); + _shipLabels.CreateCombinedShipLabels(panelCombinedFleet, ShowShipOnShipList); + _shipLabels.CreateRepairList(panelRepairList, panelRepairList_Click); + _shipLabels.CreateNDockLabels(panelDock, labelNDock_Click); + labelPresetAkashiTimer.BackColor = ShipLabels.ColumnColors[1]; + _listForm = new ListForm(_sniffer, _config) {Owner = this}; _noticeQueue = new NoticeQueue(Ring); + _config.Load(); + PerformZoom(); + _sniffer.LoadState(); } - private void FiddlerApplication_BeforeRequest(Session oSession) + private void HttpProxy_AfterSessionComplete(HttpProxy.Session session) { - var path = oSession.PathAndQuery; - var proxy = _config.Proxy; - if (proxy.UseUpstream && (path.StartsWith("/kcsapi/api_") || - // この二つはMyFleetGirlsに必要 - path.StartsWith("/kcs/resources/") || path.StartsWith("/kcs/sound/"))) - oSession["x-overrideGateway"] = string.Format("localhost:{0:D}", proxy.UpstreamPort); // 上流プロキシを設定する - if (!path.StartsWith("/kcsapi/api_")) // 艦これのAPI以外は無視する - oSession.Ignore(); + Invoke(new Action(ProcessRequest), session); } - private void FiddlerApplication_AfterSessionComplete(Session oSession) + private void ProcessRequest(HttpProxy.Session session) { - if (!oSession.bHasResponse || !oSession.uriContains("/kcsapi/api_")) + var url = session.Request.PathAndQuery; + if (!url.Contains("kcsapi/")) + return; + var request = session.Request.BodyAsString; + var response = session.Response.BodyAsString; + if (response == null || !response.StartsWith("svdata=")) + { + WriteDebugLog(url, request, response); return; - Invoke(new Action(ProcessRequest), oSession); + } + if (_config.KancolleDb.On) + _kancolleDb.Send(url, request, response); + response = UnescapeString(response.Remove(0, "svdata=".Length)); + WriteDebugLog(url, request, response); + try + { + UpdateInfo(_sniffer.Sniff(url, request, JsonParser.Parse(response))); + } + catch (RuntimeBinderException e) + { + if (_errorDialog.ShowDialog(this, + "このバージョンは現在の艦これに対応していません。\r\n新しいバージョンを利用してください。", e.ToString()) == DialogResult.Abort) + Application.Exit(); + } + catch (LogIOException e) + { + if (_errorDialog.ShowDialog(this, e.Message, e.InnerException.ToString()) == DialogResult.Abort) + Application.Exit(); + } + catch (Exception e) + { + if (_errorDialog.ShowDialog(this, "エラーが発生しました。", e.ToString()) == DialogResult.Abort) + Application.Exit(); + } } - private void ProcessRequest(Session session) + private void WriteDebugLog(string url, string request, string response) { - var response = session.GetResponseBodyAsString(); - if (!response.StartsWith("svdata=")) - return; - response = response.Remove(0, "svdata=".Length); - var json = DynamicJson.Parse(response); - var request = session.GetRequestBodyAsString(); if (_debugLogFile != null) { File.AppendAllText(_debugLogFile, - string.Format("url: {0}\nrequest: {1}\nresponse: {2}\n", session.url, request, json.ToString())); + $"url: {url}\nrequest: {request}\nresponse: {response ?? "(null)"}\n"); + } + } + + private string UnescapeString(string s) + { + try + { + var rx = new Regex(@"\\[uU]([0-9A-Fa-f]{4})"); + return rx.Replace(s, + match => ((char)int.Parse(match.Value.Substring(2), NumberStyles.HexNumber)).ToString()); + } + catch (ArgumentException) + { + return s; } - UpdateInfo(_sniffer.Sniff(session.url, request, json)); } private void UpdateInfo(Sniffer.Update update) @@ -107,6 +153,7 @@ namespace KancolleSniffer if (update == Sniffer.Update.Start) { labelLogin.Visible = false; + labelGuide.Visible = false; _started = true; return; } @@ -130,23 +177,27 @@ namespace KancolleSniffer private void MainForm_Load(object sender, EventArgs e) { - _config.Load(); RestoreLocation(); + if (_config.HideOnMinimized && WindowState == FormWindowState.Minimized) + ShowInTaskbar = false; ApplyConfig(); ApplyDebugLogSetting(); ApplyLogSetting(); - _sniffer.LoadState(); - StartProxy(); + ApplyProxySetting(); + SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; + if (_config.KancolleDb.On) + _kancolleDb.Start(_config.KancolleDb.Token); } - private void StartProxy() + private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) { - if (_config.Proxy.Auto) - FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.RegisterAsSystemProxy); - else - FiddlerApplication.Startup(_config.Proxy.Listen, FiddlerCoreStartupFlags.None); - _prevProxy.Auto = _config.Proxy.Auto; - _prevProxy.Listen = _config.Proxy.Listen; + if (e.Mode != PowerModes.Resume || !_config.Proxy.Auto) + return; + Task.Run(() => + { + for (var i = 0; i < 5; Thread.Sleep(10000), i++) + SystemProxy.Refresh(); + }); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) @@ -154,21 +205,22 @@ namespace KancolleSniffer e.Cancel = false; _config.Location = (WindowState == FormWindowState.Normal ? Bounds : RestoreBounds).Location; _config.Save(); - _sniffer.SaveState(); - ShutdownProxy(); - if (_logServer != null) - _logServer.Stop(); + Task.Run(() => ShutdownProxy()); + _logServer?.Stop(); + if (_config.Proxy.Auto) + _systemProxy.RestoreSettings(); + SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; + _kancolleDb.Stop(); } private void ShutdownProxy() { - FiddlerApplication.Shutdown(); + HttpProxy.Shutdown(); } private void MainForm_Resize(object sender, EventArgs e) { - if (_config.HideOnMinimized && WindowState == FormWindowState.Minimized) - ShowInTaskbar = false; + ShowInTaskbar = !(_config.HideOnMinimized && WindowState == FormWindowState.Minimized); } private void notifyIconMain_MouseDoubleClick(object sender, MouseEventArgs e) @@ -180,6 +232,7 @@ namespace KancolleSniffer { ShowInTaskbar = true; WindowState = FormWindowState.Normal; + TopMost = _config.TopMost; // 最前面に表示されなくなることがあるのを回避する Activate(); } @@ -194,6 +247,19 @@ namespace KancolleSniffer ApplyConfig(); } + private void PerformZoom() + { + if (_config.Zoom == 100) + return; + var prev = CurrentAutoScaleDimensions; + foreach (var control in new Control[] {this, _listForm, labelLogin, labelGuide}) + control.Font = new Font(control.Font.FontFamily, control.Font.Size * _config.Zoom / 100); + ShipLabel.LatinFont = new Font("Tahoma", 8f * _config.Zoom / 100); + var cur = CurrentAutoScaleDimensions; + ShipLabel.ScaleFactor = new SizeF(ShipLabel.ScaleFactor.Width * cur.Width / prev.Width, + ShipLabel.ScaleFactor.Height * cur.Height / prev.Height); + } + private void RestoreLocation() { if (_config.Location.X == int.MinValue) @@ -206,10 +272,14 @@ namespace KancolleSniffer private void ApplyConfig() { - _shipListForm.TopMost = TopMost = _config.TopMost; + _listForm.TopMost = TopMost = _config.TopMost; _sniffer.Item.MarginShips = _config.MarginShips; _sniffer.Item.MarginEquips = _config.MarginEquips; _sniffer.Achievement.ResetHours = _config.ResetHours; + labelAkashiRepair.Visible = labelAkashiRepairTimer.Visible = + labelPresetAkashiTimer.Visible = _config.UsePresetAkashi; + if (_config.KancolleDb.On) + _kancolleDb.Start(_config.KancolleDb.Token); } public void ApplyDebugLogSetting() @@ -217,32 +287,105 @@ namespace KancolleSniffer _debugLogFile = _config.DebugLogging ? _config.DebugLogFile : null; } - public void ApplyProxySetting() + public bool ApplyProxySetting() { - if (_config.Proxy.Auto == _prevProxy.Auto && _config.Proxy.Listen == _prevProxy.Listen) - return; - ShutdownProxy(); - StartProxy(); + if (!_config.Proxy.Auto) + _systemProxy.RestoreSettings(); + if (_config.Proxy.UseUpstream) + { + HttpProxy.UpstreamProxyHost = "127.0.0.1"; + HttpProxy.UpstreamProxyPort = _config.Proxy.UpstreamPort; + } + HttpProxy.IsEnableUpstreamProxy = _config.Proxy.UseUpstream; + var result = true; + if (!HttpProxy.IsInListening || _config.Proxy.Listen != _prevProxyPort) + { + ShutdownProxy(); + result = StartProxy(); + } + if (_config.Proxy.Auto && result) + { + _systemProxy.SetAutoProxyUrl(HttpProxy.LocalPort == 8080 + ? ProxyConfig.AutoConfigUrl + : ProxyConfig.AutoConfigUrlWithPort + HttpProxy.LocalPort); + } + _prevProxyPort = _config.Proxy.Listen; + return result; } - public void ApplyLogSetting() + private bool StartProxy() { - if (_logServer != null && (!_config.Log.ServerOn || _config.Log.Listen != _logServer.Port)) + try { - _logServer.Stop(); - _logServer = null; + HttpProxy.Startup(_config.Proxy.Listen, false, false); } - if (_logServer == null && _config.Log.ServerOn) + catch (SocketException e) { - _logServer = new LogServer(_config.Log.Listen); - _logServer.Start(); + if (e.SocketErrorCode != SocketError.AddressAlreadyInUse) + throw; + if (WarnConflictPortNumber("プロキシサーバー", _config.Proxy.Listen, _config.Proxy.Auto) == DialogResult.No || + !_config.Proxy.Auto) + { + _systemProxy.RestoreSettings(); + return false; + } + HttpProxy.Startup(0, false, false); + _config.Proxy.Listen = HttpProxy.LocalPort; + } + return true; + } + private DialogResult WarnConflictPortNumber(string name, int port, bool auto) + { + var msg = $"{name}のポート番号{port}は他のアプリケーションが使用中です。"; + var cap = "ポート番号の衝突"; + return auto + ? MessageBox.Show(this, msg + "自動的に別の番号を割り当てますか?", cap, + MessageBoxButtons.YesNo, MessageBoxIcon.Question) + : MessageBox.Show(this, msg + "設定ダイアログでポート番号を変更してください。", cap, + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + } + + public bool ApplyLogSetting() + { + var result = true; + if (_config.Log.ServerOn) + { + result = StartLogServer(); + } + else + { + _logServer?.Stop(); + _logServer = null; } - if (_logServer != null) - _logServer.OutputDir = _config.Log.OutputDir; _sniffer.EnableLog(_config.Log.On ? LogType.All : LogType.None); _sniffer.MaterialLogInterval = _config.Log.MaterialLogInterval; _sniffer.LogOutputDir = _config.Log.OutputDir; + return result; + } + + private bool StartLogServer() + { + var port = _config.Log.Listen; + if (_logServer?.Port == port) + return true; + _logServer?.Stop(); + _logServer = null; + try + { + _logServer = new LogServer(port); + _logServer.Start(); + } + catch (SocketException e) when (e.SocketErrorCode == SocketError.AddressAlreadyInUse) + { + if (WarnConflictPortNumber("閲覧サーバー", port, true) == DialogResult.No) + return false; + _logServer = new LogServer(0); + _logServer.Start(); + _config.Log.Listen = _logServer.Port; + } + _logServer.OutputDir = _config.Log.OutputDir; + return true; } public static bool IsVisibleOnAnyScreen(Rectangle rect) @@ -253,7 +396,17 @@ namespace KancolleSniffer private void timerMain_Tick(object sender, EventArgs e) { if (_started) - UpdateTimers(); + { + try + { + UpdateTimers(); + } + catch (Exception ex) + { + if (_errorDialog.ShowDialog(this, "エラーが発生しました。", ex.ToString()) == DialogResult.Abort) + Application.Exit(); + } + } if (_playLog == null || _configDialog.Visible) { labelPlayLog.Visible = false; @@ -277,46 +430,51 @@ namespace KancolleSniffer labelPlayLog.Visible = false; return; } - lines.Add(_playLog.Current.Substring(s.Count())); + lines.Add(_playLog.Current.Substring(s.Length)); } labelPlayLog.Visible = !labelPlayLog.Visible; - var json = DynamicJson.Parse(lines[2]); + var json = JsonParser.Parse(lines[2]); UpdateInfo(_sniffer.Sniff(lines[0], lines[1], json)); } private void ShowShipOnShipList(object sender, EventArgs ev) { - if (!_shipListForm.Visible) + if (!_listForm.Visible) return; var idx = (int)((Control)sender).Tag; var statuses = _sniffer.GetShipStatuses(_currentFleet); if (statuses.Length <= idx) return; - _shipListForm.ShowShip(statuses[idx].Id); + _listForm.ShowShip(statuses[idx].Id); } private void UpdateItemInfo() { UpdateNumOfShips(); UpdateNumOfEquips(); - labelNumOfBuckets.Text = _sniffer.Item.MaterialHistory[(int)Material.Bucket].Now.ToString("D"); + labelNumOfBuckets.Text = _sniffer.Material.MaterialHistory[(int)Material.Bucket].Now.ToString("D"); UpdateBucketHistory(); var ac = _sniffer.Achievement.Value; if (ac >= 10000) ac = 9999; - labelAchievement.Text = ac >= 1000 ? ac.ToString("D") : ac.ToString("F1"); + labelAchievement.Text = ac >= 1000 ? ((int)ac).ToString("D") : ac.ToString("F1"); + toolTipAchievement.SetToolTip(labelAchievement, + "今月 " + _sniffer.Achievement.ValueOfMonth.ToString("F1") + "\n" + + "EO " + _sniffer.ExMap.Achievement); UpdateMaterialHistry(); + if (_listForm.Visible) + _listForm.UpdateList(); } private void UpdateNumOfShips() { var item = _sniffer.Item; - labelNumOfShips.Text = string.Format("{0:D}/{1:D}", item.NowShips, item.MaxShips); + labelNumOfShips.Text = $"{item.NowShips:D}/{item.MaxShips:D}"; labelNumOfShips.ForeColor = item.TooManyShips ? Color.Red : Color.Black; if (item.RingShips) { - var message = string.Format("残り{0:D}隻", _sniffer.Item.MaxShips - _sniffer.Item.NowShips); - _noticeQueue.Enqueue("艦娘が多すぎます", message, _config.MaxShipsSoundFile); + var message = $"残り{_sniffer.Item.MaxShips - _sniffer.Item.NowShips:D}隻"; + _noticeQueue.Enqueue("艦娘が多すぎます", message, _config.Sounds["艦娘数超過"]); item.RingShips = false; } } @@ -324,26 +482,26 @@ namespace KancolleSniffer private void UpdateNumOfEquips() { var item = _sniffer.Item; - labelNumOfEquips.Text = string.Format("{0:D}/{1:D}", item.NowEquips, item.MaxEquips); + labelNumOfEquips.Text = $"{item.NowEquips:D}/{item.MaxEquips:D}"; labelNumOfEquips.ForeColor = item.TooManyEquips ? Color.Red : Color.Black; if (item.RingEquips) { - var message = string.Format("残り{0:D}個", _sniffer.Item.MaxEquips - _sniffer.Item.NowEquips); - _noticeQueue.Enqueue("装備が多すぎます", message, _config.MaxEquipsSoundFile); + var message = $"残り{_sniffer.Item.MaxEquips - _sniffer.Item.NowEquips:D}個"; + _noticeQueue.Enqueue("装備が多すぎます", message, _config.Sounds["装備数超過"]); item.RingEquips = false; } } private void UpdateBucketHistory() { - var count = _sniffer.Item.MaterialHistory[(int)Material.Bucket]; + var count = _sniffer.Material.MaterialHistory[(int)Material.Bucket]; var day = count.Now - count.BegOfDay; var week = count.Now - count.BegOfWeek; if (day >= 1000) day = 999; if (week >= 1000) week = 999; - labelBucketHistory.Text = string.Format("{0:+#;-#;±0} 今日\n{1:+#;-#;±0} 今週", day, week); + labelBucketHistory.Text = $"{day:+#;-#;±0} 今日\n{week:+#;-#;±0} 今週"; } private void UpdateMaterialHistry() @@ -352,14 +510,17 @@ namespace KancolleSniffer var text = new[] {"燃料", "弾薬", "鋼材", "ボーキ"}; for (var i = 0; i < labels.Length; i++) { - var count = _sniffer.Item.MaterialHistory[i]; + var count = _sniffer.Material.MaterialHistory[i]; + var port = count.Now - _sniffer.Material.PrevPort[i]; + if (port >= 100000) + port = 99999; var day = count.Now - count.BegOfDay; if (day >= 100000) day = 99999; var week = count.Now - count.BegOfWeek; if (week >= 100000) week = 99999; - labels[i].Text = string.Format("{0}\n{1:+#;-#;±0}\n{2:+#;-#;±0}", text[i], day, week); + labels[i].Text = $"{text[i]}\n{port:+#;-#;±0}\n{day:+#;-#;±0}\n{week:+#;-#;±0}"; } } @@ -368,68 +529,102 @@ namespace KancolleSniffer UpdatePanelShipInfo(); NotifyDamagedShip(); UpdateChargeInfo(); - UpdateDamagedShipList(); - if (_shipListForm.Visible) - _shipListForm.UpdateList(); + UpdateRepairList(); + if (_listForm.Visible) + _listForm.UpdateList(); } private void UpdatePanelShipInfo() { var statuses = _sniffer.GetShipStatuses(_currentFleet); - _shipLabels.SetShipInfo(statuses); - labelAirSuperiority.Text = _sniffer.GetAirSuperiority(_currentFleet).ToString("D"); + _shipLabels.SetShipLabels(statuses); + if (_sniffer.CombinedFleetType == 0) + _combinedFleet = false; + labelFleet1.Text = _combinedFleet ? "連合" : "第一"; + panelCombinedFleet.Visible = _combinedFleet; + if (_combinedFleet) + _shipLabels.SetCombinedShipLabels(_sniffer.GetShipStatuses(0), _sniffer.GetShipStatuses(1)); UpdateAkashiTimer(); + UpdateFighterPower(); UpdateLoS(); UpdateCondTimers(); } private void NotifyDamagedShip() { - if (_sniffer.Battle.HasDamagedShip) - _noticeQueue.Enqueue("大破した艦娘がいます", string.Join(" ", _sniffer.Battle.DamagedShipNames), - _config.DamagedShipSoundFile); + if (_sniffer.BadlyDamagedShips.Any()) + _noticeQueue.Enqueue("大破した艦娘がいます", string.Join(" ", _sniffer.BadlyDamagedShips), + _config.Sounds["大破警告"]); } private void NotifyAkashiTimer() { - var msgs = _sniffer.GetAkashiTimerNotice(); + var akashi = _sniffer.AkashiTimer; + var msgs = akashi.GetNotice(); if (msgs.Length == 0) return; - if (msgs[0] == "20分経過しました。") - { - _noticeQueue.Enqueue("泊地修理", msgs[0], _config.Akashi20MinSoundFile); + if (!akashi.CheckReparing() && !(akashi.CheckPresetReparing() && _config.UsePresetAkashi)) return; + if (msgs[0].Proceeded == "20分経過しました。") + { + _noticeQueue.Enqueue("泊地修理", msgs[0].Proceeded, _config.Sounds["泊地修理20分経過"]); + msgs[0].Proceeded = ""; + // 修理完了がいるかもしれないので続ける } var fn = new[] {"第一艦隊", "第二艦隊", "第三艦隊", "第四艦隊"}; for (var i = 0; i < fn.Length; i++) { - if (msgs[i] == "") - continue; - _noticeQueue.Enqueue("泊地修理 " + fn[i], msgs[i], _config.AkashiProgressSoundFile); + if (msgs[i].Proceeded != "") + _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理進行:" + msgs[i].Proceeded, _config.Sounds["泊地修理進行"]); + if (msgs[i].Completed != "") + _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理完了:" + msgs[i].Completed, _config.Sounds["泊地修理完了"]); } } + public void UpdateFighterPower() + { + var fp = _sniffer.GetFighterPower(_currentFleet); + labelFighterPower.Text = fp[0].ToString("D"); + var cr = _sniffer.GetContactTriggerRate(_currentFleet) * 100; + var text = "制空: " + (fp[0] == fp[1] ? $"{fp[0]}" : $"{fp[0]}~{fp[1]}") + + $" 触接: {cr:f1}"; + toolTipFighterPower.SetToolTip(labelFighterPower, text); + toolTipFighterPower.SetToolTip(labelFighterPowerCaption, text); + } + private void UpdateLoS() { - labelLoS.Text = _sniffer.GetFleetLineOfSights(_currentFleet).ToString("F1"); + labelLoS.Text = (Floor(_sniffer.GetFleetLineOfSights(_currentFleet) * 10) / 10.0).ToString("F1"); } private void UpdateBattleInfo() { labelFormation.Text = ""; - labelEnemyAirSuperiority.Text = ""; + labelEnemyFighterPower.Text = ""; + labelFighterPower.ForeColor = DefaultForeColor; + labelResultRank.Text = "判定"; panelBattleInfo.Visible = _sniffer.Battle.InBattle; if (!_sniffer.Battle.InBattle) return; panelBattleInfo.BringToFront(); - var t = new Timer {Interval = 2000}; // 艦隊が表示されるまで遅延させる - t.Tick += (sender, args) => - { - labelFormation.Text = _sniffer.Battle.Formation; - labelEnemyAirSuperiority.Text = _sniffer.Battle.EnemyAirSuperiority.ToString("D"); - t.Stop(); - }; - t.Start(); + var battle = _sniffer.Battle; + var color = new[] {DefaultForeColor, DefaultForeColor, Color.Blue, Color.Green, Color.Orange, Color.Red}; + labelFormation.Text = battle.Formation; + labelEnemyFighterPower.Text = battle.EnemyFighterPower; + labelFighterPower.ForeColor = color[battle.AirControlLevel + 1]; + if (_config.AlwaysShowResultRank) + ShowResultRank(); + } + + private void ShowResultRank() + { + var result = new[] {"完全S", "勝利S", "勝利A", "勝利B", "敗北C", "敗北D", "敗北E"}; + labelResultRank.Text = result[(int)_sniffer.Battle.ResultRank]; + } + + private void labelResultRank_Click(object sender, EventArgs e) + { + ShowResultRank(); } private void UpdateChargeInfo() @@ -450,6 +645,13 @@ namespace KancolleSniffer _shipLabels.SetNDockLabels(_sniffer.NDock); } + + private void labelNDock_Click(object sender, EventArgs e) + { + _ndockFinishTimeMode = !_ndockFinishTimeMode; + UpdateTimers(); + } + private void UpdateMissionLabels() { foreach (var entry in @@ -458,6 +660,12 @@ namespace KancolleSniffer entry.label.Text = entry.Name; } + private void labelMission_Click(object sender, EventArgs e) + { + _missionFinishTimeMode = !_missionFinishTimeMode; + UpdateTimers(); + } + private void UpdateTimers() { foreach (var entry in @@ -466,21 +674,20 @@ namespace KancolleSniffer { entry.Timer.Update(); SetTimerColor(entry.label, entry.Timer); - var rest = entry.Timer.Rest; - entry.label.Text = rest.Days == 0 ? rest.ToString(@"hh\:mm\:ss") : rest.ToString(@"d\.hh\:mm"); + entry.label.Text = entry.Timer.ToString(_missionFinishTimeMode); if (!entry.Timer.NeedRing) continue; - _noticeQueue.Enqueue("遠征が終わりました", entry.Name, _config.MissionSoundFile); + _noticeQueue.Enqueue("遠征が終わりました", entry.Name, _config.Sounds["遠征終了"]); entry.Timer.NeedRing = false; } for (var i = 0; i < _sniffer.NDock.Length; i++) { var entry = _sniffer.NDock[i]; entry.Timer.Update(); - _shipLabels.SetNDockTimer(i, entry.Timer); + _shipLabels.SetNDockTimer(i, entry.Timer, _ndockFinishTimeMode); if (!entry.Timer.NeedRing) continue; - _noticeQueue.Enqueue("入渠が終わりました", entry.Name, _config.NDockSoundFile); + _noticeQueue.Enqueue("入渠が終わりました", entry.Name, _config.Sounds["入渠終了"]); entry.Timer.NeedRing = false; } var kdock = new[] {labelConstruct1, labelConstruct2, labelConstruct3, labelConstruct4}; @@ -489,10 +696,11 @@ namespace KancolleSniffer var timer = _sniffer.KDock[i]; timer.Update(); SetTimerColor(kdock[i], timer); - kdock[i].Text = timer.Rest.ToString(@"hh\:mm\:ss"); + + kdock[i].Text = timer.EndTime == DateTime.MinValue ? "" : timer.Rest.ToString(@"hh\:mm\:ss"); if (!timer.NeedRing) continue; - _noticeQueue.Enqueue("建造が終わりました", string.Format("第{0:D}ドック", i + 1), _config.KDockSoundFile); + _noticeQueue.Enqueue("建造が終わりました", $"第{i + 1:D}ドック", _config.Sounds["建造完了"]); timer.NeedRing = false; } UpdateCondTimers(); @@ -506,7 +714,17 @@ namespace KancolleSniffer private void UpdateCondTimers() { - var timer = _sniffer.GetConditionTimer(_currentFleet); + DateTime timer; + if (_combinedFleet) + { + var timer1 = _sniffer.GetConditionTimer(0); + var timer2 = _sniffer.GetConditionTimer(1); + timer = timer2 > timer1 ? timer2 : timer1; + } + else + { + timer = _sniffer.GetConditionTimer(_currentFleet); + } var now = DateTime.Now; if (timer == DateTime.MinValue) { @@ -514,7 +732,7 @@ namespace KancolleSniffer labelCondTimer.Text = ""; return; } - var span = TimeSpan.FromSeconds(Math.Ceiling((timer - now).TotalSeconds)); + var span = TimeSpan.FromSeconds(Ceiling((timer - now).TotalSeconds)); if (span >= TimeSpan.FromMinutes(9)) { labelCondTimerTitle.Text = "cond40まで"; @@ -533,36 +751,66 @@ namespace KancolleSniffer { if (!_config.NotifyConditions.Contains(notice[i])) return; - _noticeQueue.Enqueue("疲労が回復しました", fn[i] + " cond" + notice[i].ToString("D"), _config.ConditionSoundFile); + _noticeQueue.Enqueue("疲労が回復しました", fn[i] + " cond" + notice[i].ToString("D"), _config.Sounds["疲労回復"]); } } private void UpdateAkashiTimer() { + if (_config.UsePresetAkashi) + UpdatePresetAkashiTimer(); _shipLabels.SetAkashiTimer(_sniffer.GetShipStatuses(_currentFleet), - _sniffer.GetAkashiTimers(_currentFleet)); + _sniffer.AkashiTimer.GetTimers(_currentFleet)); NotifyAkashiTimer(); } - private void UpdateDamagedShipList() + private void UpdatePresetAkashiTimer() { - _shipLabels.SetDamagedShipList(_sniffer.DamagedShipList); + var akashi = _sniffer.AkashiTimer; + var span = akashi.PresetDeckTimer; + var color = span == TimeSpan.Zero && akashi.CheckPresetReparing() ? Color.Red : DefaultForeColor; + var text = span == TimeSpan.MinValue ? "" : span.ToString(@"mm\:ss"); + labelAkashiRepairTimer.ForeColor = color; + labelAkashiRepairTimer.Text = text; + if (akashi.CheckPresetReparing() && !akashi.CheckReparing(_currentFleet)) + { + labelPresetAkashiTimer.ForeColor = color; + labelPresetAkashiTimer.Text = text; + } + else + { + labelPresetAkashiTimer.ForeColor = DefaultForeColor; + labelPresetAkashiTimer.Text = ""; + } } - private void UpdateQuestList() + private void UpdateRepairList() { - var name = new[] {labelQuest1, labelQuest2, labelQuest3, labelQuest4, labelQuest5}; - var progress = new[] {labelProgress1, labelProgress2, labelProgress3, labelProgress4, labelProgress5}; + _shipLabels.SetRepairList(_sniffer.RepairList); + } + private void UpdateQuestList() + { + var category = new[] + { + labelQuestColor1, labelQuestColor2, labelQuestColor3, labelQuestColor4, labelQuestColor5, + labelQuestColor6 + }; + var name = new[] {labelQuest1, labelQuest2, labelQuest3, labelQuest4, labelQuest5, labelQuest6}; + var progress = new[] + {labelProgress1, labelProgress2, labelProgress3, labelProgress4, labelProgress5, labelProgress6}; + var quests = _sniffer.Quests; for (var i = 0; i < name.Length; i++) { - if (i < _sniffer.Quests.Length) + if (i < quests.Length) { - name[i].Text = _sniffer.Quests[i].Name; - progress[i].Text = string.Format("{0:D}%", _sniffer.Quests[i].Progress); + category[i].BackColor = quests[i].Color; + name[i].Text = quests[i].Name; + progress[i].Text = $"{quests[i].Progress:D}%"; } else { + category[i].BackColor = DefaultBackColor; name[i].Text = progress[i].Text = ""; } } @@ -612,24 +860,34 @@ namespace KancolleSniffer if (_config.ShowBaloonTip) notifyIconMain.ShowBalloonTip(20000, baloonTitle, baloonMessage, ToolTipIcon.Info); if (_config.PlaySound) - PlaySound(soundFile, _config.SoundVolume); + PlaySound(soundFile, _config.Sounds.Volume); } + [DllImport("winmm.dll")] + private static extern int mciSendString(String command, + StringBuilder buffer, int bufferSize, IntPtr hwndCallback); + +// ReSharper disable InconsistentNaming + private const int MM_MCINOTIFY = 0x3B9; + private const int MCI_NOTIFY_SUCCESSFUL = 1; +// ReSharper restore InconsistentNaming + public void PlaySound(string file, int volume) { if (!File.Exists(file)) return; - _wmp.settings.volume = volume + 1; - _wmp.settings.volume = volume - 1; - _wmp.settings.volume = volume; - _wmp.URL = file; - _wmp.controls.play(); + mciSendString("close sound", null, 0, IntPtr.Zero); + if (mciSendString("open \"" + file + "\" type mpegvideo alias sound", null, 0, IntPtr.Zero) != 0) + return; + mciSendString("setaudio sound volume to " + volume * 10, null, 0, IntPtr.Zero); + mciSendString("play sound notify", null, 0, Handle); } - private void _wmp_PlayStateChange(object sender, EventArgs e) + protected override void WndProc(ref Message m) { - if (_wmp.playState == 8) // MediaEnded - _wmp.URL = ""; // 再生したファイルが差し替えできなくなるのを防ぐ。 + if (m.Msg == MM_MCINOTIFY && (int)m.WParam == MCI_NOTIFY_SUCCESSFUL) + mciSendString("close sound", null, 0, IntPtr.Zero); + base.WndProc(ref m); } private void SetupFleetClick() @@ -652,18 +910,35 @@ namespace KancolleSniffer private void labelFleet_Click(object sender, EventArgs e) { + if (!_started) + return; var fleet = (int)((Label)sender).Tag; if (_currentFleet == fleet) + { + if (fleet > 0) + return; + _combinedFleet = _sniffer.CombinedFleetType > 0 && !_combinedFleet; + UpdatePanelShipInfo(); return; + } + _combinedFleet = false; _currentFleet = fleet; foreach (var label in _labelCheckFleets) label.Visible = false; _labelCheckFleets[fleet].Visible = true; - if (!_started) - return; UpdatePanelShipInfo(); } + private void labelFleet1_MouseHover(object sender, EventArgs e) + { + labelFleet1.Text = _currentFleet == 0 && _sniffer.CombinedFleetType > 0 && !_combinedFleet ? "連合" : "第一"; + } + + private void labelFleet1_MouseLeave(object sender, EventArgs e) + { + labelFleet1.Text = _combinedFleet ? "連合" : "第一"; + } + private void labelBucketHistoryButton_Click(object sender, EventArgs e) { if (labelBucketHistory.Visible) @@ -679,6 +954,12 @@ namespace KancolleSniffer } } + private void labelBucketHistory_Click(object sender, EventArgs e) + { + labelBucketHistory.Visible = false; + labelBucketHistoryButton.BackColor = DefaultBackColor; + } + private void labelMaterialHistoryButton_Click(object sender, EventArgs e) { if (panelMaterialHistory.Visible) @@ -694,39 +975,63 @@ namespace KancolleSniffer } } + private void panelMaterialHistory_Click(object sender, EventArgs e) + { + panelMaterialHistory.Visible = false; + labelMaterialHistoryButton.BackColor = DefaultBackColor; + } + public void ResetAchievemnt() { _sniffer.Achievement.Reset(); UpdateItemInfo(); } - private void labelDamgedShipListButton_Click(object sender, EventArgs e) + private void labelRepairListButton_Click(object sender, EventArgs e) { - if (panelDamagedShipList.Visible) + if (panelRepairList.Visible) { - panelDamagedShipList.Visible = false; - labelDamgedShipListButton.BackColor = DefaultBackColor; + panelRepairList.Visible = false; + labelRepairListButton.BackColor = DefaultBackColor; } else { - panelDamagedShipList.Visible = true; - panelDamagedShipList.BringToFront(); - labelDamgedShipListButton.BackColor = SystemColors.ActiveCaption; + panelRepairList.Visible = true; + panelRepairList.BringToFront(); + labelRepairListButton.BackColor = SystemColors.ActiveCaption; } } + private void panelRepairList_Click(object sender, EventArgs e) + { + panelRepairList.Visible = false; + labelRepairListButton.BackColor = DefaultBackColor; + } + private void ShipListToolStripMenuItem_Click(object sender, EventArgs e) { - _shipListForm.UpdateList(); - _shipListForm.Show(); - if (_shipListForm.WindowState == FormWindowState.Minimized) - _shipListForm.WindowState = FormWindowState.Normal; - _shipListForm.Activate(); + _listForm.UpdateList(); + _listForm.Show(); + if (_listForm.WindowState == FormWindowState.Minimized) + _listForm.WindowState = FormWindowState.Normal; + _listForm.Activate(); } private void LogToolStripMenuItem_Click(object sender, EventArgs e) { Process.Start("http://localhost:" + _config.Log.Listen + "/"); } + + private void CaptureToolStripMenuItem_Click(object sender, EventArgs e) + { + try + { + var proc = new ProcessStartInfo("BurageSnap.exe") {WorkingDirectory = "Capture"}; + Process.Start(proc); + } + catch (FileNotFoundException) + { + } + } } } \ No newline at end of file