1 // Copyright (C) 2013, 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\r
3 // Licensed under the Apache License, Version 2.0 (the "License");
\r
4 // you may not use this file except in compliance with the License.
\r
5 // You may obtain a copy of the License at
\r
7 // http://www.apache.org/licenses/LICENSE-2.0
\r
9 // Unless required by applicable law or agreed to in writing, software
\r
10 // distributed under the License is distributed on an "AS IS" BASIS,
\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 // See the License for the specific language governing permissions and
\r
13 // limitations under the License.
\r
16 using System.Collections.Generic;
\r
17 using System.Diagnostics;
\r
18 using System.Drawing;
\r
19 using System.Globalization;
\r
22 using System.Net.Sockets;
\r
23 using System.Runtime.InteropServices;
\r
25 using System.Text.RegularExpressions;
\r
26 using System.Threading;
\r
27 using System.Threading.Tasks;
\r
28 using System.Windows.Forms;
\r
29 using Microsoft.CSharp.RuntimeBinder;
\r
30 using Microsoft.Win32;
\r
31 using static System.Math;
\r
32 using Timer = System.Windows.Forms.Timer;
\r
34 namespace KancolleSniffer
\r
36 public partial class MainForm : Form
\r
38 private readonly Sniffer _sniffer = new Sniffer();
\r
39 private readonly Config _config = new Config();
\r
40 private readonly ConfigDialog _configDialog;
\r
41 private int _currentFleet;
\r
42 private bool _combinedFleet;
\r
43 private readonly Label[] _labelCheckFleets;
\r
44 private readonly ShipLabels _shipLabels;
\r
45 private readonly ListForm _listForm;
\r
46 private readonly NoticeQueue _noticeQueue;
\r
47 private bool _started;
\r
48 private string _debugLogFile;
\r
49 private IEnumerator<string> _playLog;
\r
50 private LogServer _logServer;
\r
51 private int _prevProxyPort;
\r
52 private readonly SystemProxy _systemProxy = new SystemProxy();
\r
53 private readonly ErrorDialog _errorDialog = new ErrorDialog();
\r
54 private bool _missionFinishTimeMode;
\r
55 private bool _ndockFinishTimeMode;
\r
56 private readonly KancolleDb _kancolleDb = new KancolleDb();
\r
60 InitializeComponent();
\r
61 HttpProxy.AfterSessionComplete += HttpProxy_AfterSessionComplete;
\r
62 _configDialog = new ConfigDialog(_config, this);
\r
63 _labelCheckFleets = new[] {labelCheckFleet1, labelCheckFleet2, labelCheckFleet3, labelCheckFleet4};
\r
65 // この時点でAutoScaleDimensions == CurrentAutoScaleDimensionsなので、
\r
66 // MainForm.Designer.csのAutoScaleDimensionsの6f,12fを使う。
\r
67 ShipLabel.ScaleFactor = new SizeF(CurrentAutoScaleDimensions.Width / 6f,
\r
68 CurrentAutoScaleDimensions.Height / 12f);
\r
71 _shipLabels = new ShipLabels();
\r
72 _shipLabels.CreateAkashiTimers(panelShipInfo);
\r
73 _shipLabels.CreateShipLabels(panelShipInfo, ShowShipOnShipList);
\r
74 _shipLabels.CreateCombinedShipLabels(panelCombinedFleet, ShowShipOnShipList);
\r
75 _shipLabels.CreateRepairList(panelRepairList, panelRepairList_Click);
\r
76 _shipLabels.CreateNDockLabels(panelDock, labelNDock_Click);
\r
77 labelPresetAkashiTimer.BackColor = ShipLabels.ColumnColors[1];
\r
78 _listForm = new ListForm(_sniffer, _config) {Owner = this};
\r
79 _noticeQueue = new NoticeQueue(Ring);
\r
82 _sniffer.LoadState();
\r
85 private void HttpProxy_AfterSessionComplete(HttpProxy.Session session)
\r
87 Invoke(new Action<HttpProxy.Session>(ProcessRequest), session);
\r
90 private void ProcessRequest(HttpProxy.Session session)
\r
92 var url = session.Request.PathAndQuery;
\r
93 if (!url.Contains("kcsapi/"))
\r
95 var request = session.Request.BodyAsString;
\r
96 var response = session.Response.BodyAsString;
\r
97 if (response == null || !response.StartsWith("svdata="))
\r
99 WriteDebugLog(url, request, response);
\r
102 if (_config.KancolleDb.On)
\r
103 _kancolleDb.Send(url, request, response);
\r
104 response = UnescapeString(response.Remove(0, "svdata=".Length));
\r
105 WriteDebugLog(url, request, response);
\r
108 UpdateInfo(_sniffer.Sniff(url, request, JsonParser.Parse(response)));
\r
110 catch (RuntimeBinderException e)
\r
112 if (_errorDialog.ShowDialog(this,
\r
113 "このバージョンは現在の艦これに対応していません。\r\n新しいバージョンを利用してください。", e.ToString()) == DialogResult.Abort)
\r
114 Application.Exit();
\r
116 catch (LogIOException e)
\r
118 if (_errorDialog.ShowDialog(this, e.Message, e.InnerException.ToString()) == DialogResult.Abort)
\r
119 Application.Exit();
\r
121 catch (Exception e)
\r
123 if (_errorDialog.ShowDialog(this, "エラーが発生しました。", e.ToString()) == DialogResult.Abort)
\r
124 Application.Exit();
\r
128 private void WriteDebugLog(string url, string request, string response)
\r
130 if (_debugLogFile != null)
\r
132 File.AppendAllText(_debugLogFile,
\r
133 $"url: {url}\nrequest: {request}\nresponse: {response ?? "(null)"}\n");
\r
137 private string UnescapeString(string s)
\r
141 var rx = new Regex(@"\\[uU]([0-9A-Fa-f]{4})");
\r
142 return rx.Replace(s,
\r
143 match => ((char)int.Parse(match.Value.Substring(2), NumberStyles.HexNumber)).ToString());
\r
145 catch (ArgumentException)
\r
151 private void UpdateInfo(Sniffer.Update update)
\r
153 if (update == Sniffer.Update.Start)
\r
155 labelLogin.Visible = false;
\r
156 labelGuide.Visible = false;
\r
162 if ((update & Sniffer.Update.Item) != 0)
\r
164 if ((update & Sniffer.Update.Timer) != 0)
\r
166 if ((update & Sniffer.Update.NDock) != 0)
\r
167 UpdateNDocLabels();
\r
168 if ((update & Sniffer.Update.Mission) != 0)
\r
169 UpdateMissionLabels();
\r
170 if ((update & Sniffer.Update.QuestList) != 0)
\r
172 if ((update & Sniffer.Update.Ship) != 0)
\r
174 if ((update & Sniffer.Update.Battle) != 0)
\r
175 UpdateBattleInfo();
\r
178 private void MainForm_Load(object sender, EventArgs e)
\r
181 if (_config.HideOnMinimized && WindowState == FormWindowState.Minimized)
\r
182 ShowInTaskbar = false;
\r
184 ApplyDebugLogSetting();
\r
186 ApplyProxySetting();
\r
187 SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
\r
188 if (_config.KancolleDb.On)
\r
189 _kancolleDb.Start(_config.KancolleDb.Token);
\r
192 private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
\r
194 if (e.Mode != PowerModes.Resume || !_config.Proxy.Auto)
\r
198 for (var i = 0; i < 5; Thread.Sleep(10000), i++)
\r
199 SystemProxy.Refresh();
\r
203 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
\r
206 _config.Location = (WindowState == FormWindowState.Normal ? Bounds : RestoreBounds).Location;
\r
208 Task.Run(() => ShutdownProxy());
\r
209 _logServer?.Stop();
\r
210 if (_config.Proxy.Auto)
\r
211 _systemProxy.RestoreSettings();
\r
212 SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
\r
213 _kancolleDb.Stop();
\r
216 private void ShutdownProxy()
\r
218 HttpProxy.Shutdown();
\r
221 private void MainForm_Resize(object sender, EventArgs e)
\r
223 ShowInTaskbar = !(_config.HideOnMinimized && WindowState == FormWindowState.Minimized);
\r
226 private void notifyIconMain_MouseDoubleClick(object sender, MouseEventArgs e)
\r
228 NotifyIconOpenToolStripMenuItem_Click(sender, e);
\r
231 private void NotifyIconOpenToolStripMenuItem_Click(object sender, EventArgs e)
\r
233 ShowInTaskbar = true;
\r
234 WindowState = FormWindowState.Normal;
\r
235 TopMost = _config.TopMost; // 最前面に表示されなくなることがあるのを回避する
\r
239 private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
\r
244 private void ConfigToolStripMenuItem_Click(object sender, EventArgs e)
\r
246 if (_configDialog.ShowDialog(this) == DialogResult.OK)
\r
250 private void PerformZoom()
\r
252 if (_config.Zoom == 100)
\r
254 var prev = CurrentAutoScaleDimensions;
\r
255 foreach (var control in new Control[] {this, _listForm, labelLogin, labelGuide})
\r
256 control.Font = new Font(control.Font.FontFamily, control.Font.Size * _config.Zoom / 100);
\r
257 ShipLabel.LatinFont = new Font("Tahoma", 8f * _config.Zoom / 100);
\r
258 var cur = CurrentAutoScaleDimensions;
\r
259 ShipLabel.ScaleFactor = new SizeF(ShipLabel.ScaleFactor.Width * cur.Width / prev.Width,
\r
260 ShipLabel.ScaleFactor.Height * cur.Height / prev.Height);
\r
263 private void RestoreLocation()
\r
265 if (_config.Location.X == int.MinValue)
\r
267 var newBounds = Bounds;
\r
268 newBounds.Location = _config.Location;
\r
269 if (IsVisibleOnAnyScreen(newBounds))
\r
270 Location = _config.Location;
\r
273 private void ApplyConfig()
\r
275 _listForm.TopMost = TopMost = _config.TopMost;
\r
276 _sniffer.Item.MarginShips = _config.MarginShips;
\r
277 _sniffer.Item.MarginEquips = _config.MarginEquips;
\r
278 _sniffer.Achievement.ResetHours = _config.ResetHours;
\r
279 labelAkashiRepair.Visible = labelAkashiRepairTimer.Visible =
\r
280 labelPresetAkashiTimer.Visible = _config.UsePresetAkashi;
\r
281 if (_config.KancolleDb.On)
\r
282 _kancolleDb.Start(_config.KancolleDb.Token);
\r
285 public void ApplyDebugLogSetting()
\r
287 _debugLogFile = _config.DebugLogging ? _config.DebugLogFile : null;
\r
290 public bool ApplyProxySetting()
\r
292 if (!_config.Proxy.Auto)
\r
293 _systemProxy.RestoreSettings();
\r
294 if (_config.Proxy.UseUpstream)
\r
296 HttpProxy.UpstreamProxyHost = "127.0.0.1";
\r
297 HttpProxy.UpstreamProxyPort = _config.Proxy.UpstreamPort;
\r
299 HttpProxy.IsEnableUpstreamProxy = _config.Proxy.UseUpstream;
\r
301 if (!HttpProxy.IsInListening || _config.Proxy.Listen != _prevProxyPort)
\r
304 result = StartProxy();
\r
306 if (_config.Proxy.Auto && result)
\r
308 _systemProxy.SetAutoProxyUrl($"http://localhost:{_config.Log.Listen}/proxy.pac?port={_config.Proxy.Listen}");
\r
310 _prevProxyPort = _config.Proxy.Listen;
\r
314 private bool StartProxy()
\r
318 HttpProxy.Startup(_config.Proxy.Listen, false, false);
\r
320 catch (SocketException e)
\r
322 if (e.SocketErrorCode != SocketError.AddressAlreadyInUse)
\r
324 if (WarnConflictPortNumber("プロキシサーバー", _config.Proxy.Listen, _config.Proxy.Auto) == DialogResult.No ||
\r
325 !_config.Proxy.Auto)
\r
327 _systemProxy.RestoreSettings();
\r
330 HttpProxy.Startup(0, false, false);
\r
331 _config.Proxy.Listen = HttpProxy.LocalPort;
\r
336 private DialogResult WarnConflictPortNumber(string name, int port, bool auto)
\r
338 var msg = $"{name}のポート番号{port}は他のアプリケーションが使用中です。";
\r
339 var cap = "ポート番号の衝突";
\r
341 ? MessageBox.Show(this, msg + "自動的に別の番号を割り当てますか?", cap,
\r
342 MessageBoxButtons.YesNo, MessageBoxIcon.Question)
\r
343 : MessageBox.Show(this, msg + "設定ダイアログでポート番号を変更してください。", cap,
\r
344 MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
\r
347 public bool ApplyLogSetting()
\r
350 if (_config.Proxy.Auto)
\r
351 _config.Log.ServerOn = true;
\r
352 if (_config.Log.ServerOn)
\r
354 result = StartLogServer();
\r
358 _logServer?.Stop();
\r
361 _sniffer.EnableLog(_config.Log.On ? LogType.All : LogType.None);
\r
362 _sniffer.MaterialLogInterval = _config.Log.MaterialLogInterval;
\r
363 _sniffer.LogOutputDir = _config.Log.OutputDir;
\r
367 private bool StartLogServer()
\r
369 var port = _config.Log.Listen;
\r
370 if (_logServer?.Port == port)
\r
372 _logServer?.Stop();
\r
376 _logServer = new LogServer(port);
\r
377 _logServer.Start();
\r
379 catch (SocketException e) when (e.SocketErrorCode == SocketError.AddressAlreadyInUse)
\r
381 if (WarnConflictPortNumber("閲覧サーバー", port, true) == DialogResult.No)
\r
383 _logServer = new LogServer(0);
\r
384 _logServer.Start();
\r
385 _config.Log.Listen = _logServer.Port;
\r
387 _logServer.OutputDir = _config.Log.OutputDir;
\r
391 public static bool IsVisibleOnAnyScreen(Rectangle rect)
\r
393 return Screen.AllScreens.Any(screen => screen.WorkingArea.IntersectsWith(rect));
\r
396 private void timerMain_Tick(object sender, EventArgs e)
\r
404 catch (Exception ex)
\r
406 if (_errorDialog.ShowDialog(this, "エラーが発生しました。", ex.ToString()) == DialogResult.Abort)
\r
407 Application.Exit();
\r
410 if (_playLog == null || _configDialog.Visible)
\r
412 labelPlayLog.Visible = false;
\r
418 public void SetPlayLog(string file)
\r
420 _playLog = File.ReadLines(file).GetEnumerator();
\r
423 private void PlayLog()
\r
425 var lines = new List<string>();
\r
426 foreach (var s in new[] {"url: ", "request: ", "response: "})
\r
428 if (!_playLog.MoveNext() || !_playLog.Current.StartsWith(s))
\r
430 labelPlayLog.Visible = false;
\r
433 lines.Add(_playLog.Current.Substring(s.Length));
\r
435 labelPlayLog.Visible = !labelPlayLog.Visible;
\r
436 var json = JsonParser.Parse(lines[2]);
\r
437 UpdateInfo(_sniffer.Sniff(lines[0], lines[1], json));
\r
440 private void ShowShipOnShipList(object sender, EventArgs ev)
\r
442 if (!_listForm.Visible)
\r
444 var idx = (int)((Control)sender).Tag;
\r
445 var statuses = _sniffer.GetShipStatuses(_currentFleet);
\r
446 if (statuses.Length <= idx)
\r
448 _listForm.ShowShip(statuses[idx].Id);
\r
451 private void UpdateItemInfo()
\r
453 UpdateNumOfShips();
\r
454 UpdateNumOfEquips();
\r
455 labelNumOfBuckets.Text = _sniffer.Material.MaterialHistory[(int)Material.Bucket].Now.ToString("D");
\r
456 UpdateBucketHistory();
\r
457 var ac = _sniffer.Achievement.Value;
\r
460 labelAchievement.Text = ac >= 1000 ? ((int)ac).ToString("D") : ac.ToString("F1");
\r
461 toolTipAchievement.SetToolTip(labelAchievement,
\r
462 "今月 " + _sniffer.Achievement.ValueOfMonth.ToString("F1") + "\n" +
\r
463 "EO " + _sniffer.ExMap.Achievement);
\r
464 UpdateMaterialHistry();
\r
465 if (_listForm.Visible)
\r
466 _listForm.UpdateList();
\r
469 private void UpdateNumOfShips()
\r
471 var item = _sniffer.Item;
\r
472 labelNumOfShips.Text = $"{item.NowShips:D}/{item.MaxShips:D}";
\r
473 labelNumOfShips.ForeColor = item.TooManyShips ? Color.Red : Color.Black;
\r
474 if (item.RingShips)
\r
476 var message = $"残り{_sniffer.Item.MaxShips - _sniffer.Item.NowShips:D}隻";
\r
477 _noticeQueue.Enqueue("艦娘が多すぎます", message, _config.Sounds["艦娘数超過"]);
\r
478 item.RingShips = false;
\r
482 private void UpdateNumOfEquips()
\r
484 var item = _sniffer.Item;
\r
485 labelNumOfEquips.Text = $"{item.NowEquips:D}/{item.MaxEquips:D}";
\r
486 labelNumOfEquips.ForeColor = item.TooManyEquips ? Color.Red : Color.Black;
\r
487 if (item.RingEquips)
\r
489 var message = $"残り{_sniffer.Item.MaxEquips - _sniffer.Item.NowEquips:D}個";
\r
490 _noticeQueue.Enqueue("装備が多すぎます", message, _config.Sounds["装備数超過"]);
\r
491 item.RingEquips = false;
\r
495 private void UpdateBucketHistory()
\r
497 var count = _sniffer.Material.MaterialHistory[(int)Material.Bucket];
\r
498 var day = CutOverflow(count.Now - count.BegOfDay, 999);
\r
499 var week = CutOverflow(count.Now - count.BegOfWeek, 999);
\r
500 labelBucketHistory.Text = $"{day:+#;-#;±0} 今日\n{week:+#;-#;±0} 今週";
\r
503 private void UpdateMaterialHistry()
\r
505 var labels = new[] {labelFuelHistory, labelBulletHistory, labelSteelHistory, labelBouxiteHistory};
\r
506 var text = new[] {"燃料", "弾薬", "鋼材", "ボーキ"};
\r
507 for (var i = 0; i < labels.Length; i++)
\r
509 var count = _sniffer.Material.MaterialHistory[i];
\r
510 var port = CutOverflow(count.Now - _sniffer.Material.PrevPort[i], 99999);
\r
511 var day = CutOverflow(count.Now - count.BegOfDay, 99999);
\r
512 var week = CutOverflow(count.Now - count.BegOfWeek, 99999);
\r
513 labels[i].Text = $"{text[i]}\n{port:+#;-#;±0}\n{day:+#;-#;±0}\n{week:+#;-#;±0}";
\r
517 private int CutOverflow(int value, int limit)
\r
521 if (value < -limit)
\r
526 private void UpdateShipInfo()
\r
528 UpdatePanelShipInfo();
\r
529 NotifyDamagedShip();
\r
530 UpdateChargeInfo();
\r
531 UpdateRepairList();
\r
532 if (_listForm.Visible)
\r
533 _listForm.UpdateList();
\r
536 private void UpdatePanelShipInfo()
\r
538 var statuses = _sniffer.GetShipStatuses(_currentFleet);
\r
539 _shipLabels.SetShipLabels(statuses);
\r
540 if (_sniffer.CombinedFleetType == 0)
\r
541 _combinedFleet = false;
\r
542 labelFleet1.Text = _combinedFleet ? "連合" : "第一";
\r
543 panelCombinedFleet.Visible = _combinedFleet;
\r
544 if (_combinedFleet)
\r
545 _shipLabels.SetCombinedShipLabels(_sniffer.GetShipStatuses(0), _sniffer.GetShipStatuses(1));
\r
546 UpdateAkashiTimer();
\r
547 UpdateFighterPower();
\r
549 UpdateCondTimers();
\r
552 private void NotifyDamagedShip()
\r
554 if (_sniffer.BadlyDamagedShips.Any())
\r
555 _noticeQueue.Enqueue("大破した艦娘がいます", string.Join(" ", _sniffer.BadlyDamagedShips),
\r
556 _config.Sounds["大破警告"]);
\r
559 private void NotifyAkashiTimer()
\r
561 var akashi = _sniffer.AkashiTimer;
\r
562 var msgs = akashi.GetNotice();
\r
563 if (msgs.Length == 0)
\r
565 if (!akashi.CheckReparing() && !(akashi.CheckPresetReparing() && _config.UsePresetAkashi))
\r
567 if (msgs[0].Proceeded == "20分経過しました。")
\r
569 _noticeQueue.Enqueue("泊地修理", msgs[0].Proceeded, _config.Sounds["泊地修理20分経過"]);
\r
570 msgs[0].Proceeded = "";
\r
571 // 修理完了がいるかもしれないので続ける
\r
573 var fn = new[] {"第一艦隊", "第二艦隊", "第三艦隊", "第四艦隊"};
\r
574 for (var i = 0; i < fn.Length; i++)
\r
576 if (msgs[i].Proceeded != "")
\r
577 _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理進行:" + msgs[i].Proceeded, _config.Sounds["泊地修理進行"]);
\r
578 if (msgs[i].Completed != "")
\r
579 _noticeQueue.Enqueue("泊地修理 " + fn[i], "修理完了:" + msgs[i].Completed, _config.Sounds["泊地修理完了"]);
\r
583 public void UpdateFighterPower()
\r
585 var fp = _sniffer.GetFighterPower(_currentFleet);
\r
586 labelFighterPower.Text = fp[0].ToString("D");
\r
587 var cr = _sniffer.GetContactTriggerRate(_currentFleet) * 100;
\r
588 var text = "制空: " + (fp[0] == fp[1] ? $"{fp[0]}" : $"{fp[0]}~{fp[1]}") +
\r
590 toolTipFighterPower.SetToolTip(labelFighterPower, text);
\r
591 toolTipFighterPower.SetToolTip(labelFighterPowerCaption, text);
\r
594 private void UpdateLoS()
\r
596 labelLoS.Text = (Floor(_sniffer.GetFleetLineOfSights(_currentFleet) * 10) / 10.0).ToString("F1");
\r
599 private void UpdateBattleInfo()
\r
601 labelFormation.Text = "";
\r
602 labelEnemyFighterPower.Text = "";
\r
603 labelFighterPower.ForeColor = DefaultForeColor;
\r
604 labelResultRank.Text = "判定";
\r
605 panelBattleInfo.Visible = _sniffer.Battle.InBattle;
\r
606 if (!_sniffer.Battle.InBattle)
\r
608 panelBattleInfo.BringToFront();
\r
609 var battle = _sniffer.Battle;
\r
610 var color = new[] {DefaultForeColor, DefaultForeColor, Color.Blue, Color.Green, Color.Orange, Color.Red};
\r
611 labelFormation.Text = battle.Formation;
\r
612 labelEnemyFighterPower.Text = battle.EnemyFighterPower;
\r
613 labelFighterPower.ForeColor = color[battle.AirControlLevel + 1];
\r
614 if (_config.AlwaysShowResultRank)
\r
618 private void ShowResultRank()
\r
620 var result = new[] {"完全S", "勝利S", "勝利A", "勝利B", "敗北C", "敗北D", "敗北E"};
\r
621 labelResultRank.Text = result[(int)_sniffer.Battle.ResultRank];
\r
624 private void labelResultRank_Click(object sender, EventArgs e)
\r
629 private void UpdateChargeInfo()
\r
631 var fuelSq = new[] {labelFuelSq1, labelFuelSq2, labelFuelSq3, labelFuelSq4};
\r
632 var bullSq = new[] {labelBullSq1, labelBullSq2, labelBullSq3, labelBullSq4};
\r
634 for (var i = 0; i < fuelSq.Length; i++)
\r
636 var stat = _sniffer.ChargeStatuses[i];
\r
637 fuelSq[i].ImageIndex = stat.Fuel;
\r
638 bullSq[i].ImageIndex = stat.Bull;
\r
642 private void UpdateNDocLabels()
\r
644 _shipLabels.SetNDockLabels(_sniffer.NDock);
\r
648 private void labelNDock_Click(object sender, EventArgs e)
\r
650 _ndockFinishTimeMode = !_ndockFinishTimeMode;
\r
654 private void UpdateMissionLabels()
\r
656 foreach (var entry in
\r
657 new[] {labelMissionName1, labelMissionName2, labelMissionName3}.Zip(_sniffer.Missions,
\r
658 (label, mission) => new {label, mission.Name}))
\r
659 entry.label.Text = entry.Name;
\r
662 private void labelMission_Click(object sender, EventArgs e)
\r
664 _missionFinishTimeMode = !_missionFinishTimeMode;
\r
668 private void UpdateTimers()
\r
670 foreach (var entry in
\r
671 new[] {labelMission1, labelMission2, labelMission3}.Zip(_sniffer.Missions,
\r
672 (label, mission) => new {label, mission.Name, mission.Timer}))
\r
674 entry.Timer.Update();
\r
675 SetTimerColor(entry.label, entry.Timer);
\r
676 entry.label.Text = entry.Timer.ToString(_missionFinishTimeMode);
\r
677 if (!entry.Timer.NeedRing)
\r
679 _noticeQueue.Enqueue("遠征が終わりました", entry.Name, _config.Sounds["遠征終了"]);
\r
680 entry.Timer.NeedRing = false;
\r
682 for (var i = 0; i < _sniffer.NDock.Length; i++)
\r
684 var entry = _sniffer.NDock[i];
\r
685 entry.Timer.Update();
\r
686 _shipLabels.SetNDockTimer(i, entry.Timer, _ndockFinishTimeMode);
\r
687 if (!entry.Timer.NeedRing)
\r
689 _noticeQueue.Enqueue("入渠が終わりました", entry.Name, _config.Sounds["入渠終了"]);
\r
690 entry.Timer.NeedRing = false;
\r
692 var kdock = new[] {labelConstruct1, labelConstruct2, labelConstruct3, labelConstruct4};
\r
693 for (var i = 0; i < kdock.Length; i++)
\r
695 var timer = _sniffer.KDock[i];
\r
697 SetTimerColor(kdock[i], timer);
\r
699 kdock[i].Text = timer.EndTime == DateTime.MinValue ? "" : timer.Rest.ToString(@"hh\:mm\:ss");
\r
700 if (!timer.NeedRing)
\r
702 _noticeQueue.Enqueue("建造が終わりました", $"第{i + 1:D}ドック", _config.Sounds["建造完了"]);
\r
703 timer.NeedRing = false;
\r
705 UpdateCondTimers();
\r
706 UpdateAkashiTimer();
\r
709 private void SetTimerColor(Label label, RingTimer timer)
\r
711 label.ForeColor = timer.IsFinished ? Color.Red : Color.Black;
\r
714 private void UpdateCondTimers()
\r
717 if (_combinedFleet)
\r
719 var timer1 = _sniffer.GetConditionTimer(0);
\r
720 var timer2 = _sniffer.GetConditionTimer(1);
\r
721 timer = timer2 > timer1 ? timer2 : timer1;
\r
725 timer = _sniffer.GetConditionTimer(_currentFleet);
\r
727 var now = DateTime.Now;
\r
728 if (timer == DateTime.MinValue)
\r
730 labelCondTimerTitle.Text = "";
\r
731 labelCondTimer.Text = "";
\r
734 var span = TimeSpan.FromSeconds(Ceiling((timer - now).TotalSeconds));
\r
735 if (span >= TimeSpan.FromMinutes(9))
\r
737 labelCondTimerTitle.Text = "cond40まで";
\r
738 labelCondTimer.Text = (span - TimeSpan.FromMinutes(9)).ToString(@"mm\:ss");
\r
742 labelCondTimerTitle.Text = "cond49まで";
\r
743 labelCondTimer.Text = (span >= TimeSpan.Zero ? span : TimeSpan.Zero).ToString(@"mm\:ss");
\r
745 var notice = _sniffer.GetConditionNotice();
\r
746 if (notice == null)
\r
748 var fn = new[] {"第一艦隊", "第二艦隊", "第三艦隊", "第四艦隊"};
\r
749 for (var i = 0; i < fn.Length; i++)
\r
751 if (!_config.NotifyConditions.Contains(notice[i]))
\r
753 _noticeQueue.Enqueue("疲労が回復しました", fn[i] + " cond" + notice[i].ToString("D"), _config.Sounds["疲労回復"]);
\r
757 private void UpdateAkashiTimer()
\r
759 if (_config.UsePresetAkashi)
\r
760 UpdatePresetAkashiTimer();
\r
761 _shipLabels.SetAkashiTimer(_sniffer.GetShipStatuses(_currentFleet),
\r
762 _sniffer.AkashiTimer.GetTimers(_currentFleet));
\r
763 NotifyAkashiTimer();
\r
766 private void UpdatePresetAkashiTimer()
\r
768 var akashi = _sniffer.AkashiTimer;
\r
769 var span = akashi.PresetDeckTimer;
\r
770 var color = span == TimeSpan.Zero && akashi.CheckPresetReparing() ? Color.Red : DefaultForeColor;
\r
771 var text = span == TimeSpan.MinValue ? "" : span.ToString(@"mm\:ss");
\r
772 labelAkashiRepairTimer.ForeColor = color;
\r
773 labelAkashiRepairTimer.Text = text;
\r
774 if (akashi.CheckPresetReparing() && !akashi.CheckReparing(_currentFleet))
\r
776 labelPresetAkashiTimer.ForeColor = color;
\r
777 labelPresetAkashiTimer.Text = text;
\r
781 labelPresetAkashiTimer.ForeColor = DefaultForeColor;
\r
782 labelPresetAkashiTimer.Text = "";
\r
786 private void UpdateRepairList()
\r
788 _shipLabels.SetRepairList(_sniffer.RepairList);
\r
791 private void UpdateQuestList()
\r
793 var category = new[]
\r
795 labelQuestColor1, labelQuestColor2, labelQuestColor3, labelQuestColor4, labelQuestColor5,
\r
798 var name = new[] {labelQuest1, labelQuest2, labelQuest3, labelQuest4, labelQuest5, labelQuest6};
\r
799 var progress = new[]
\r
800 {labelProgress1, labelProgress2, labelProgress3, labelProgress4, labelProgress5, labelProgress6};
\r
801 var quests = _sniffer.Quests;
\r
802 for (var i = 0; i < name.Length; i++)
\r
804 if (i < quests.Length)
\r
806 category[i].BackColor = quests[i].Color;
\r
807 name[i].Text = quests[i].Name;
\r
808 progress[i].Text = $"{quests[i].Progress:D}%";
\r
812 category[i].BackColor = DefaultBackColor;
\r
813 name[i].Text = progress[i].Text = "";
\r
818 private class NoticeQueue
\r
820 private readonly Action<string, string, string> _ring;
\r
821 private readonly Queue<Tuple<string, string, string>> _queue = new Queue<Tuple<string, string, string>>();
\r
822 private readonly Timer _timer = new Timer {Interval = 2000};
\r
824 public NoticeQueue(Action<string, string, string> ring)
\r
827 _timer.Tick += TimerOnTick;
\r
830 private void TimerOnTick(object obj, EventArgs e)
\r
832 if (_queue.Count == 0)
\r
837 var notice = _queue.Dequeue();
\r
838 _ring(notice.Item1, notice.Item2, notice.Item3);
\r
841 public void Enqueue(string title, string message, string soundFile)
\r
843 if (_timer.Enabled)
\r
845 _queue.Enqueue(new Tuple<string, string, string>(title, message, soundFile));
\r
849 _ring(title, message, soundFile);
\r
855 private void Ring(string baloonTitle, string baloonMessage, string soundFile)
\r
857 if (_config.FlashWindow)
\r
858 Win32API.FlashWindow(Handle);
\r
859 if (_config.ShowBaloonTip)
\r
860 notifyIconMain.ShowBalloonTip(20000, baloonTitle, baloonMessage, ToolTipIcon.Info);
\r
861 if (_config.PlaySound)
\r
862 PlaySound(soundFile, _config.Sounds.Volume);
\r
865 [DllImport("winmm.dll")]
\r
866 private static extern int mciSendString(String command,
\r
867 StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
\r
869 // ReSharper disable InconsistentNaming
\r
870 private const int MM_MCINOTIFY = 0x3B9;
\r
871 private const int MCI_NOTIFY_SUCCESSFUL = 1;
\r
872 // ReSharper restore InconsistentNaming
\r
874 public void PlaySound(string file, int volume)
\r
876 if (!File.Exists(file))
\r
878 mciSendString("close sound", null, 0, IntPtr.Zero);
\r
879 if (mciSendString("open \"" + file + "\" type mpegvideo alias sound", null, 0, IntPtr.Zero) != 0)
\r
881 mciSendString("setaudio sound volume to " + volume * 10, null, 0, IntPtr.Zero);
\r
882 mciSendString("play sound notify", null, 0, Handle);
\r
885 protected override void WndProc(ref Message m)
\r
887 if (m.Msg == MM_MCINOTIFY && (int)m.WParam == MCI_NOTIFY_SUCCESSFUL)
\r
888 mciSendString("close sound", null, 0, IntPtr.Zero);
\r
889 base.WndProc(ref m);
\r
892 private void SetupFleetClick()
\r
896 new[] {labelFleet1, labelFleet2, labelFleet3, labelFleet4},
\r
897 new[] {labelFuelSq1, labelFuelSq2, labelFuelSq3, labelFuelSq4},
\r
898 new[] {labelBullSq1, labelBullSq2, labelBullSq3, labelBullSq4}
\r
900 foreach (var a in labels)
\r
902 for (var fleet = 0; fleet < labels[0].Length; fleet++)
\r
904 a[fleet].Tag = fleet;
\r
905 a[fleet].Click += labelFleet_Click;
\r
910 private void labelFleet_Click(object sender, EventArgs e)
\r
914 var fleet = (int)((Label)sender).Tag;
\r
915 if (_currentFleet == fleet)
\r
919 _combinedFleet = _sniffer.CombinedFleetType > 0 && !_combinedFleet;
\r
920 UpdatePanelShipInfo();
\r
923 _combinedFleet = false;
\r
924 _currentFleet = fleet;
\r
925 foreach (var label in _labelCheckFleets)
\r
926 label.Visible = false;
\r
927 _labelCheckFleets[fleet].Visible = true;
\r
928 UpdatePanelShipInfo();
\r
931 private void labelFleet1_MouseHover(object sender, EventArgs e)
\r
933 labelFleet1.Text = _currentFleet == 0 && _sniffer.CombinedFleetType > 0 && !_combinedFleet ? "連合" : "第一";
\r
936 private void labelFleet1_MouseLeave(object sender, EventArgs e)
\r
938 labelFleet1.Text = _combinedFleet ? "連合" : "第一";
\r
941 private void labelBucketHistoryButton_Click(object sender, EventArgs e)
\r
943 if (labelBucketHistory.Visible)
\r
945 labelBucketHistory.Visible = false;
\r
946 labelBucketHistoryButton.BackColor = DefaultBackColor;
\r
950 labelBucketHistory.Visible = true;
\r
951 labelBucketHistory.BringToFront();
\r
952 labelBucketHistoryButton.BackColor = SystemColors.ActiveCaption;
\r
956 private void labelBucketHistory_Click(object sender, EventArgs e)
\r
958 labelBucketHistory.Visible = false;
\r
959 labelBucketHistoryButton.BackColor = DefaultBackColor;
\r
962 private void labelMaterialHistoryButton_Click(object sender, EventArgs e)
\r
964 if (panelMaterialHistory.Visible)
\r
966 panelMaterialHistory.Visible = false;
\r
967 labelMaterialHistoryButton.BackColor = DefaultBackColor;
\r
971 panelMaterialHistory.Visible = true;
\r
972 panelMaterialHistory.BringToFront();
\r
973 labelMaterialHistoryButton.BackColor = SystemColors.ActiveCaption;
\r
977 private void panelMaterialHistory_Click(object sender, EventArgs e)
\r
979 panelMaterialHistory.Visible = false;
\r
980 labelMaterialHistoryButton.BackColor = DefaultBackColor;
\r
983 public void ResetAchievemnt()
\r
985 _sniffer.Achievement.Reset();
\r
989 private void labelRepairListButton_Click(object sender, EventArgs e)
\r
991 if (panelRepairList.Visible)
\r
993 panelRepairList.Visible = false;
\r
994 labelRepairListButton.BackColor = DefaultBackColor;
\r
998 panelRepairList.Visible = true;
\r
999 panelRepairList.BringToFront();
\r
1000 labelRepairListButton.BackColor = SystemColors.ActiveCaption;
\r
1004 private void panelRepairList_Click(object sender, EventArgs e)
\r
1006 panelRepairList.Visible = false;
\r
1007 labelRepairListButton.BackColor = DefaultBackColor;
\r
1010 private void ShipListToolStripMenuItem_Click(object sender, EventArgs e)
\r
1012 _listForm.UpdateList();
\r
1014 if (_listForm.WindowState == FormWindowState.Minimized)
\r
1015 _listForm.WindowState = FormWindowState.Normal;
\r
1016 _listForm.Activate();
\r
1019 private void LogToolStripMenuItem_Click(object sender, EventArgs e)
\r
1021 Process.Start("http://localhost:" + _config.Log.Listen + "/");
\r
1024 private void CaptureToolStripMenuItem_Click(object sender, EventArgs e)
\r
1028 var proc = new ProcessStartInfo("BurageSnap.exe") {WorkingDirectory = "Capture"};
\r
1029 Process.Start(proc);
\r
1031 catch (FileNotFoundException)
\r