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.ComponentModel;
\r
18 using System.Diagnostics;
\r
19 using System.Drawing;
\r
20 using System.Globalization;
\r
24 using System.Runtime.InteropServices;
\r
26 using System.Text.RegularExpressions;
\r
27 using System.Threading.Tasks;
\r
28 using System.Windows.Forms;
\r
29 using Microsoft.CSharp.RuntimeBinder;
\r
30 using static System.Math;
\r
32 namespace KancolleSniffer
\r
34 public partial class MainForm : Form
\r
36 private readonly Sniffer _sniffer = new Sniffer();
\r
37 private readonly Config _config = new Config();
\r
38 private readonly ConfigDialog _configDialog;
\r
39 private readonly ProxyManager _proxyManager;
\r
40 private readonly ResizableToolTip _toolTip = new ResizableToolTip {ShowAlways = true};
\r
41 private readonly ResizableToolTip _toolTipQuest = new ResizableToolTip {ShowAlways = true, AutoPopDelay = 10000};
\r
42 private readonly ResizableToolTip _tooltipCopy = new ResizableToolTip {AutomaticDelay = 0};
\r
43 private int _currentFleet;
\r
44 private bool _combinedFleet;
\r
45 private readonly Label[] _labelCheckFleets;
\r
46 private readonly MainFormLabels _mainLabels;
\r
47 private readonly ListForm _listForm;
\r
48 private readonly NotificationManager _notificationManager;
\r
49 private bool _started;
\r
50 private bool _timerEnabled;
\r
51 private string _debugLogFile;
\r
52 private IEnumerator<string> _playLog;
\r
53 private DateTime _prev, _now;
\r
54 private bool _inSortie;
\r
56 private readonly ErrorDialog _errorDialog = new ErrorDialog();
\r
57 private readonly ErrorLog _errorLog;
\r
61 InitializeComponent();
\r
62 HttpProxy.AfterSessionComplete += HttpProxy_AfterSessionComplete;
\r
63 _configDialog = new ConfigDialog(_config, this);
\r
64 _labelCheckFleets = new[] {labelCheckFleet1, labelCheckFleet2, labelCheckFleet3, labelCheckFleet4};
\r
66 // この時点でAutoScaleDimensions == CurrentAutoScaleDimensionsなので、
\r
67 // MainForm.Designer.csのAutoScaleDimensionsの6f,12fを使う。
\r
68 ShipLabel.ScaleFactor = new SizeF(CurrentAutoScaleDimensions.Width / 6f,
\r
69 CurrentAutoScaleDimensions.Height / 12f);
\r
72 _mainLabels = new MainFormLabels();
\r
73 _mainLabels.CreateAkashiTimers(panelShipInfo);
\r
74 _mainLabels.CreateShipLabels(panelShipInfo, ShowShipOnShipList);
\r
75 _mainLabels.CreateAkashiTimers7(panel7Ships);
\r
76 _mainLabels.CreateShipLabels7(panel7Ships, ShowShipOnShipList);
\r
77 _mainLabels.CreateCombinedShipLabels(panelCombinedFleet, ShowShipOnShipList);
\r
78 _mainLabels.CreateNDockLabels(panelDock, labelNDock_Click);
\r
79 panelRepairList.CreateLabels(panelRepairList_Click);
\r
80 labelPresetAkashiTimer.BackColor = ShipLabel.ColumnColors[1];
\r
81 _listForm = new ListForm(_sniffer, _config) {Owner = this};
\r
82 _notificationManager = new NotificationManager(Alarm);
\r
84 _proxyManager = new ProxyManager(_config, this);
\r
85 _errorLog = new ErrorLog(_sniffer);
\r
86 _proxyManager.UpdatePacFile();
\r
88 _mainLabels.AdjustAkashiTimers();
\r
90 _sniffer.RepeatingTimerController = new RepeatingTimerController(_notificationManager, _config);
\r
94 /// パネルのz-orderがくるうのを避ける
\r
95 /// https://stackoverflow.com/a/5777090/1429506
\r
97 private void MainForm_Shown(object sender, EventArgs e)
\r
99 // ReSharper disable once NotAccessedVariable
\r
101 foreach (var panel in new[] {panelShipInfo, panel7Ships, panelCombinedFleet})
\r
102 // ReSharper disable once RedundantAssignment
\r
103 handle = panel.Handle;
\r
106 private readonly FileSystemWatcher _watcher = new FileSystemWatcher
\r
108 Path = AppDomain.CurrentDomain.BaseDirectory,
\r
109 NotifyFilter = NotifyFilters.LastWrite
\r
112 private readonly Timer _watcherTimer = new Timer {Interval = 1000};
\r
114 private void LoadData()
\r
117 _sniffer.LoadState();
\r
118 DataLoader.LoadTpSpec();
\r
119 _watcher.SynchronizingObject = this;
\r
120 _watcherTimer.Tick += (sender, ev) =>
\r
122 _watcherTimer.Stop();
\r
126 _sniffer.LoadState();
\r
129 DataLoader.LoadTpSpec();
\r
133 _watcher.Changed += (sender, ev) =>
\r
136 _watcherTimer.Stop();
\r
137 _watcherTimer.Start();
\r
139 _watcher.EnableRaisingEvents = true;
\r
142 private class RepeatingTimerController : Sniffer.IRepeatingTimerController
\r
144 private readonly NotificationManager _manager;
\r
145 private readonly Config _config;
\r
147 public RepeatingTimerController(NotificationManager manager, Config config)
\r
149 _manager = manager;
\r
153 public void Stop(string key)
\r
155 _manager.StopRepeat(key,
\r
156 (key == "入渠終了" || key == "遠征終了") &&
\r
157 (_config.Notifications[key].Flags & NotificationType.Cont) != 0);
\r
160 public void Stop(string key, int fleet) => _manager.StopRepeat(key, fleet);
\r
162 public void Suspend(string exception = null) => _manager.SuspendRepeat(exception);
\r
164 public void Resume() => _manager.ResumeRepeat();
\r
167 public class ConfigFileException : Exception
\r
169 public ConfigFileException(string message, Exception innerException) : base(message, innerException)
\r
174 private void HttpProxy_AfterSessionComplete(HttpProxy.Session session)
\r
176 BeginInvoke(new Action<HttpProxy.Session>(ProcessRequest), session);
\r
179 private void ProcessRequest(HttpProxy.Session session)
\r
181 var url = session.Request.PathAndQuery;
\r
182 if (!url.Contains("kcsapi/"))
\r
184 var request = session.Request.BodyAsString;
\r
185 var response = session.Response.BodyAsString;
\r
186 if (response == null || !response.StartsWith("svdata="))
\r
188 WriteDebugLog(url, request, response);
\r
191 response = UnescapeString(response.Remove(0, "svdata=".Length));
\r
192 WriteDebugLog(url, request, response);
\r
193 ProcessRequestMain(url, request, response);
\r
196 private void ProcessRequestMain(string url, string request, string response)
\r
200 UpdateInfo(_sniffer.Sniff(url, request, JsonParser.Parse(response)));
\r
201 _errorLog.CheckBattleApi(url, request, response);
\r
204 catch (RuntimeBinderException e)
\r
206 if (_errorDialog.ShowDialog(this,
\r
207 "艦これに仕様変更があったか、受信内容が壊れています。",
\r
208 _errorLog.GenerateErrorLog(url, request, response, e.ToString())) == DialogResult.Abort)
\r
211 catch (LogIOException e)
\r
213 // ReSharper disable once PossibleNullReferenceException
\r
214 if (_errorDialog.ShowDialog(this, e.Message, e.InnerException.ToString()) == DialogResult.Abort)
\r
217 catch (BattleResultError)
\r
219 if (_errorDialog.ShowDialog(this, "戦闘結果の計算に誤りがあります。",
\r
220 _errorLog.GenerateBattleErrorLog()) == DialogResult.Abort)
\r
223 catch (Exception e)
\r
225 if (_errorDialog.ShowDialog(this, "エラーが発生しました。",
\r
226 _errorLog.GenerateErrorLog(url, request, response, e.ToString())) == DialogResult.Abort)
\r
231 private void Exit()
\r
233 _proxyManager.Shutdown();
\r
234 Environment.Exit(1);
\r
237 private void WriteDebugLog(string url, string request, string response)
\r
239 if (_debugLogFile != null)
\r
241 File.AppendAllText(_debugLogFile,
\r
242 $"date: {DateTime.Now:g}\nurl: {url}\nrequest: {request}\nresponse: {response ?? "(null)"}\n");
\r
246 private string UnescapeString(string s)
\r
250 var rx = new Regex(@"\\[uU]([0-9A-Fa-f]{4})");
\r
251 return rx.Replace(s,
\r
252 match => ((char)int.Parse(match.Value.Substring(2), NumberStyles.HexNumber)).ToString());
\r
254 catch (ArgumentException)
\r
260 private void UpdateInfo(Sniffer.Update update)
\r
262 if (update == Sniffer.Update.Start)
\r
264 labelLogin.Visible = false;
\r
265 linkLabelGuide.Visible = false;
\r
271 if (_now == DateTime.MinValue)
\r
272 _now = DateTime.Now;
\r
273 if ((update & Sniffer.Update.Item) != 0)
\r
275 if ((update & Sniffer.Update.Timer) != 0)
\r
277 if ((update & Sniffer.Update.NDock) != 0)
\r
278 UpdateNDocLabels();
\r
279 if ((update & Sniffer.Update.Mission) != 0)
\r
280 UpdateMissionLabels();
\r
281 if ((update & Sniffer.Update.QuestList) != 0)
\r
283 if ((update & Sniffer.Update.Ship) != 0)
\r
285 if ((update & Sniffer.Update.Battle) != 0)
\r
286 UpdateBattleInfo();
\r
287 if ((update & Sniffer.Update.Cell) != 0)
\r
291 private void MainForm_Load(object sender, EventArgs e)
\r
294 if (_config.HideOnMinimized && WindowState == FormWindowState.Minimized)
\r
295 ShowInTaskbar = false;
\r
296 if (_config.ShowHpInPercent)
\r
297 _mainLabels.ToggleHpPercent();
\r
298 if (_config.ShipList.Visible)
\r
301 ApplyDebugLogSetting();
\r
303 ApplyProxySetting();
\r
304 CheckVersionUp((current, latest) =>
\r
306 if (latest == current)
\r
308 linkLabelGuide.Text = $"バージョン{latest}があります。";
\r
309 linkLabelGuide.LinkArea = new LinkArea(0, linkLabelGuide.Text.Length);
\r
310 linkLabelGuide.Click += (obj, ev) =>
\r
312 Process.Start("https://ja.osdn.net/rel/kancollesniffer/" + latest);
\r
317 public async void CheckVersionUp(Action<string, string> action)
\r
319 var current = string.Join(".", Application.ProductVersion.Split('.').Take(2));
\r
322 var latest = (await new WebClient().DownloadStringTaskAsync("http://kancollesniffer.osdn.jp/version"))
\r
326 action(current, latest);
\r
328 catch (InvalidOperationException)
\r
332 catch (WebException)
\r
337 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
\r
339 if (!_config.ExitSilently)
\r
341 using (var dialog = new ConfirmDialog())
\r
343 if (dialog.ShowDialog(this) != DialogResult.Yes)
\r
351 _sniffer.FlashLog();
\r
352 _config.Location = (WindowState == FormWindowState.Normal ? Bounds : RestoreBounds).Location;
\r
353 _config.ShowHpInPercent = _mainLabels.ShowHpInPercent;
\r
354 _config.ShipList.Visible = _listForm.Visible && _listForm.WindowState == FormWindowState.Normal;
\r
356 _sniffer.SaveState();
\r
357 _proxyManager.Shutdown();
\r
360 private void MainForm_Resize(object sender, EventArgs e)
\r
362 ShowInTaskbar = !(_config.HideOnMinimized && WindowState == FormWindowState.Minimized);
\r
365 private void notifyIconMain_MouseDoubleClick(object sender, MouseEventArgs e)
\r
367 NotifyIconOpenToolStripMenuItem_Click(sender, e);
\r
370 private void NotifyIconOpenToolStripMenuItem_Click(object sender, EventArgs e)
\r
372 ShowInTaskbar = true;
\r
373 WindowState = FormWindowState.Normal;
\r
374 TopMost = _config.TopMost; // 最前面に表示されなくなることがあるのを回避する
\r
378 private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
\r
383 private void ConfigToolStripMenuItem_Click(object sender, EventArgs e)
\r
385 if (_configDialog.ShowDialog(this) == DialogResult.OK)
\r
389 StopRepeatingTimer(_configDialog.RepeatSettingsChanged);
\r
393 private void StopRepeatingTimer(IEnumerable<string> names)
\r
395 foreach (var name in names)
\r
396 _notificationManager.StopRepeat(name);
\r
399 private void PerformZoom()
\r
401 if (_config.Zoom == 100)
\r
403 var prev = CurrentAutoScaleDimensions;
\r
404 foreach (var control in new Control[]
\r
406 this, _listForm, labelLogin, linkLabelGuide,
\r
407 _configDialog, _configDialog.NotificationConfigDialog,
\r
408 contextMenuStripMain, _errorDialog
\r
411 control.Font = new Font(control.Font.FontFamily, control.Font.Size * _config.Zoom / 100);
\r
413 foreach (var toolTip in new[]{_toolTip, _toolTipQuest, _tooltipCopy})
\r
415 toolTip.Font = new Font(toolTip.Font.FontFamily, toolTip.Font.Size * _config.Zoom / 100);
\r
417 ShipLabel.LatinFont = new Font("Tahoma", 8f * _config.Zoom / 100);
\r
418 var cur = CurrentAutoScaleDimensions;
\r
419 ShipLabel.ScaleFactor = new SizeF(ShipLabel.ScaleFactor.Width * cur.Width / prev.Width,
\r
420 ShipLabel.ScaleFactor.Height * cur.Height / prev.Height);
\r
423 private void RestoreLocation()
\r
425 if (_config.Location.X == int.MinValue)
\r
427 if (IsTitleBarOnAnyScreen(_config.Location))
\r
428 Location = _config.Location;
\r
431 private void ApplyConfig()
\r
433 _listForm.TopMost = TopMost = _config.TopMost;
\r
434 _sniffer.Item.MarginShips = _config.MarginShips;
\r
435 UpdateNumOfShips();
\r
436 _sniffer.Item.MarginEquips = _config.MarginEquips;
\r
437 UpdateNumOfEquips();
\r
438 _sniffer.Achievement.ResetHours = _config.ResetHours;
\r
439 labelAkashiRepair.Visible = labelAkashiRepairTimer.Visible =
\r
440 labelPresetAkashiTimer.Visible = _config.UsePresetAkashi;
\r
443 public void ApplyDebugLogSetting()
\r
445 _debugLogFile = _config.DebugLogging ? _config.DebugLogFile : null;
\r
448 public bool ApplyProxySetting()
\r
450 return _proxyManager.ApplyConfig();
\r
453 public void ApplyLogSetting()
\r
455 LogServer.OutputDir = _config.Log.OutputDir;
\r
456 LogServer.MaterialHistory = _sniffer.Material.MaterialHistory;
\r
457 _sniffer.EnableLog(_config.Log.On ? LogType.All : LogType.None);
\r
458 _sniffer.MaterialLogInterval = _config.Log.MaterialLogInterval;
\r
459 _sniffer.LogOutputDir = _config.Log.OutputDir;
\r
462 public static bool IsTitleBarOnAnyScreen(Point location)
\r
464 var rect = new Rectangle(
\r
465 new Point(location.X + SystemInformation.IconSize.Width + SystemInformation.HorizontalFocusThickness,
\r
466 location.Y + SystemInformation.CaptionHeight), new Size(60, 1));
\r
467 return Screen.AllScreens.Any(screen => screen.WorkingArea.Contains(rect));
\r
470 private void timerMain_Tick(object sender, EventArgs e)
\r
476 _now = DateTime.Now;
\r
481 catch (Exception ex)
\r
483 if (_errorDialog.ShowDialog(this, "エラーが発生しました。", ex.ToString()) == DialogResult.Abort)
\r
487 if (_playLog == null || _configDialog.Visible)
\r
489 labelPlayLog.Visible = false;
\r
495 public void SetPlayLog(string file)
\r
497 _playLog = File.ReadLines(file).GetEnumerator();
\r
500 private void PlayLog()
\r
502 var lines = new List<string>();
\r
503 foreach (var s in new[] {"url: ", "request: ", "response: "})
\r
507 if (!_playLog.MoveNext() || _playLog.Current == null)
\r
509 labelPlayLog.Visible = false;
\r
512 } while (!_playLog.Current.StartsWith(s));
\r
513 lines.Add(_playLog.Current.Substring(s.Length));
\r
515 labelPlayLog.Visible = !labelPlayLog.Visible;
\r
516 ProcessRequestMain(lines[0], lines[1], lines[2]);
\r
519 private void ShowShipOnShipList(object sender, EventArgs ev)
\r
521 if (!_listForm.Visible)
\r
523 var idx = (int)((Control)sender).Tag;
\r
524 var ships = _sniffer.Fleets[_currentFleet].Ships;
\r
525 if (ships.Length <= idx)
\r
527 _listForm.ShowShip(ships[idx].Id);
\r
530 private void UpdateItemInfo()
\r
532 UpdateNumOfShips();
\r
533 UpdateNumOfEquips();
\r
534 _notificationManager.Flash();
\r
535 labelNumOfBuckets.Text = _sniffer.Material.MaterialHistory[(int)Material.Bucket].Now.ToString("D");
\r
536 UpdateBucketHistory();
\r
537 var ac = _sniffer.Achievement.Value;
\r
540 labelAchievement.Text = ac >= 1000 ? ((int)ac).ToString("D") : ac.ToString("F1");
\r
541 _toolTip.SetToolTip(labelAchievement,
\r
542 "今月 " + _sniffer.Achievement.ValueOfMonth.ToString("F1") + "\n" +
\r
543 "EO " + _sniffer.ExMap.Achievement);
\r
544 UpdateMaterialHistry();
\r
545 if (_listForm.Visible)
\r
546 _listForm.UpdateList();
\r
549 private void UpdateNumOfShips()
\r
551 var item = _sniffer.Item;
\r
552 labelNumOfShips.Text = $"{item.NowShips:D}/{item.MaxShips:D}";
\r
553 labelNumOfShips.ForeColor = item.TooManyShips ? CUDColor.Red : Color.Black;
\r
554 if (item.AlarmShips)
\r
556 var message = $"残り{_sniffer.Item.MaxShips - _sniffer.Item.NowShips:D}隻";
\r
557 _notificationManager.Enqueue("艦娘数超過", message);
\r
558 item.AlarmShips = false;
\r
562 private void UpdateNumOfEquips()
\r
564 var item = _sniffer.Item;
\r
565 labelNumOfEquips.Text = $"{item.NowEquips:D}/{item.MaxEquips:D}";
\r
566 labelNumOfEquips.ForeColor = item.TooManyEquips ? CUDColor.Red : Color.Black;
\r
567 if (item.AlarmEquips)
\r
569 var message = $"残り{_sniffer.Item.MaxEquips - _sniffer.Item.NowEquips:D}個";
\r
570 _notificationManager.Enqueue("装備数超過", message);
\r
571 item.AlarmEquips = false;
\r
575 private void UpdateBucketHistory()
\r
577 var count = _sniffer.Material.MaterialHistory[(int)Material.Bucket];
\r
578 var day = CutOverflow(count.Now - count.BegOfDay, 999);
\r
579 var week = CutOverflow(count.Now - count.BegOfWeek, 999);
\r
580 labelBucketHistory.Text = $"{day:+#;-#;±0} 今日\n{week:+#;-#;±0} 今週";
\r
583 private void UpdateMaterialHistry()
\r
585 var labels = new[] {labelFuelHistory, labelBulletHistory, labelSteelHistory, labelBouxiteHistory};
\r
586 var text = new[] {"燃料", "弾薬", "鋼材", "ボーキ"};
\r
587 for (var i = 0; i < labels.Length; i++)
\r
589 var count = _sniffer.Material.MaterialHistory[i];
\r
590 var port = CutOverflow(count.Now - _sniffer.Material.PrevPort[i], 99999);
\r
591 var day = CutOverflow(count.Now - count.BegOfDay, 99999);
\r
592 var week = CutOverflow(count.Now - count.BegOfWeek, 99999);
\r
593 labels[i].Text = $"{text[i]}\n{port:+#;-#;±0}\n{day:+#;-#;±0}\n{week:+#;-#;±0}";
\r
597 private int CutOverflow(int value, int limit)
\r
601 if (value < -limit)
\r
606 private void UpdateShipInfo()
\r
609 UpdatePanelShipInfo();
\r
610 NotifyDamagedShip();
\r
611 UpdateChargeInfo();
\r
612 UpdateRepairList();
\r
613 if (_listForm.Visible)
\r
614 _listForm.UpdateList();
\r
617 private void SetCurrentFleet()
\r
619 var states = _sniffer.Fleets.Select(fleet => fleet.State).ToArray();
\r
620 var inSortie = states.Any(state => state >= FleetState.Sortie);
\r
621 if (_inSortie || !inSortie)
\r
623 _inSortie = inSortie;
\r
627 if (states[0] == FleetState.Sortie && states[1] == FleetState.Sortie)
\r
629 _combinedFleet = true;
\r
634 _combinedFleet = false;
\r
635 _currentFleet = Array.FindIndex(states, state => state >= FleetState.Sortie);
\r
639 private void UpdatePanelShipInfo()
\r
641 var fleets = _sniffer.Fleets;
\r
642 var ships = fleets[_currentFleet].Ships;
\r
643 panel7Ships.Visible = ships.Length == 7;
\r
644 _mainLabels.SetShipLabels(ships);
\r
645 if (!_sniffer.IsCombinedFleet)
\r
646 _combinedFleet = false;
\r
647 labelFleet1.Text = _combinedFleet ? "連合" : "第一";
\r
648 panelCombinedFleet.Visible = _combinedFleet;
\r
649 if (_combinedFleet)
\r
650 _mainLabels.SetCombinedShipLabels(fleets[0].Ships, fleets[1].Ships);
\r
651 for (var i = 0; i < _labelCheckFleets.Length; i++)
\r
652 _labelCheckFleets[i].Visible = _currentFleet == i;
\r
653 UpdateAkashiTimer();
\r
654 var battle = _sniffer.Battle;
\r
655 UpdateFighterPower(_combinedFleet && (battle.BattleState == BattleState.None || battle.EnemyIsCombined));
\r
657 UpdateCondTimers();
\r
660 private void NotifyDamagedShip()
\r
662 if (!_sniffer.BadlyDamagedShips.Any())
\r
664 SetNotification("大破警告", string.Join(" ", _sniffer.BadlyDamagedShips));
\r
665 _notificationManager.Flash();
\r
668 public void UpdateFighterPower(bool combined)
\r
670 var fleets = _sniffer.Fleets;
\r
672 ? fleets[0].FighterPower.Zip(fleets[1].FighterPower, (a, b) => a + b).ToArray()
\r
673 : fleets[_currentFleet].FighterPower;
\r
674 labelFighterPower.Text = fp[0].ToString("D");
\r
676 ? fleets[0].ContactTriggerRate + fleets[1].ContactTriggerRate
\r
677 : fleets[_currentFleet].ContactTriggerRate;
\r
678 var text = "制空: " + (fp[0] == fp[1] ? $"{fp[0]}" : $"{fp[0]}~{fp[1]}") +
\r
679 $" 触接: {cr * 100:f1}";
\r
680 _toolTip.SetToolTip(labelFighterPower, text);
\r
681 _toolTip.SetToolTip(labelFighterPowerCaption, text);
\r
684 private void UpdateLoS()
\r
686 var fleet = _sniffer.Fleets[_currentFleet];
\r
687 labelLoS.Text = RoundDown(fleet.GetLineOfSights(1)).ToString("F1");
\r
688 var text = $"係数3: {RoundDown(fleet.GetLineOfSights(3)):F1}\r\n" +
\r
689 $"係数4: {RoundDown(fleet.GetLineOfSights(4)):F1}";
\r
690 _toolTip.SetToolTip(labelLoS, text);
\r
691 _toolTip.SetToolTip(labelLoSCaption, text);
\r
694 private double RoundDown(double number)
\r
696 return Floor(number * 10) / 10.0;
\r
699 private void UpdateBattleInfo()
\r
702 _listForm.UpdateBattleResult();
\r
703 if (_sniffer.Battle.BattleState == BattleState.None)
\r
705 panelBattleInfo.BringToFront();
\r
706 var battle = _sniffer.Battle;
\r
707 labelFormation.Text = new[] {"同航戦", "反航戦", "T字有利", "T字不利"}[battle.Formation[2] - 1];
\r
708 UpdateBattleFighterPower();
\r
709 if ((_config.Spoilers & Spoiler.ResultRank) != 0)
\r
711 if (_sniffer.Battle.BattleState == BattleState.Day)
\r
713 _listForm.UpdateAirBattleResult();
\r
714 _listForm.UpdateCellInfo();
\r
718 private void UpdateCellInfo()
\r
720 _listForm.UpdateCellInfo();
\r
723 private void ResetBattleInfo()
\r
725 labelFormation.Text = "";
\r
726 labelEnemyFighterPower.Text = "";
\r
727 labelFighterPower.ForeColor = DefaultForeColor;
\r
728 labelResultRank.Text = "判定";
\r
729 panelBattleInfo.Visible = _sniffer.Battle.BattleState != BattleState.None;
\r
732 private void UpdateBattleFighterPower()
\r
734 var battle = _sniffer.Battle;
\r
735 var power = battle.EnemyFighterPower;
\r
736 labelEnemyFighterPower.Text = power.AirCombat + power.UnknownMark;
\r
737 if (power.AirCombat != power.Interception)
\r
739 var text = "防空: " + power.Interception + power.UnknownMark;
\r
740 _toolTip.SetToolTip(labelEnemyFighterPower, text);
\r
741 _toolTip.SetToolTip(labelEnemyFighterPowerCaption, text);
\r
743 UpdateFighterPower(_sniffer.IsCombinedFleet && battle.EnemyIsCombined);
\r
744 labelFighterPower.ForeColor = new[]
\r
745 {DefaultForeColor, DefaultForeColor, CUDColor.Blue, CUDColor.Green, CUDColor.Orange, CUDColor.Red}[
\r
746 battle.AirControlLevel + 1];
\r
749 private void ShowResultRank()
\r
751 var result = new[] {"完全S", "勝利S", "勝利A", "勝利B", "敗北C", "敗北D", "敗北E"};
\r
752 labelResultRank.Text = result[(int)_sniffer.Battle.ResultRank];
\r
755 private void labelResultRank_Click(object sender, EventArgs e)
\r
760 private void UpdateChargeInfo()
\r
762 var fuelSq = new[] {labelFuelSq1, labelFuelSq2, labelFuelSq3, labelFuelSq4};
\r
763 var bullSq = new[] {labelBullSq1, labelBullSq2, labelBullSq3, labelBullSq4};
\r
765 for (var i = 0; i < fuelSq.Length; i++)
\r
767 var stat = _sniffer.Fleets[i].ChargeStatus;
\r
768 fuelSq[i].ImageIndex = stat.Fuel;
\r
769 bullSq[i].ImageIndex = stat.Bull;
\r
773 private void UpdateNDocLabels()
\r
775 _mainLabels.SetNDockLabels(_sniffer.NDock);
\r
779 private void labelNDock_Click(object sender, EventArgs e)
\r
781 _config.ShowEndTime ^= TimerKind.NDock;
\r
785 private void UpdateMissionLabels()
\r
787 foreach (var entry in
\r
788 new[] {labelMissionName1, labelMissionName2, labelMissionName3}.Zip(_sniffer.Missions,
\r
789 (label, mission) => new {label, mission.Name}))
\r
790 entry.label.Text = entry.Name;
\r
793 private void labelMission_Click(object sender, EventArgs e)
\r
795 _config.ShowEndTime ^= TimerKind.Mission;
\r
799 private void UpdateTimers()
\r
801 var mission = new[] {labelMission1, labelMission2, labelMission3};
\r
802 for (var i = 0; i < mission.Length; i++)
\r
804 var entry = _sniffer.Missions[i];
\r
805 SetTimerColor(mission[i], entry.Timer, _now);
\r
806 mission[i].Text = entry.Timer.ToString(_now, (_config.ShowEndTime & TimerKind.Mission) != 0);
\r
808 for (var i = 0; i < _sniffer.NDock.Length; i++)
\r
810 var entry = _sniffer.NDock[i];
\r
811 _mainLabels.SetNDockTimer(i, entry.Timer, _now, (_config.ShowEndTime & TimerKind.NDock) != 0);
\r
813 var kdock = new[] {labelConstruct1, labelConstruct2, labelConstruct3, labelConstruct4};
\r
814 for (var i = 0; i < kdock.Length; i++)
\r
816 var timer = _sniffer.KDock[i];
\r
817 SetTimerColor(kdock[i], timer, _now);
\r
818 kdock[i].Text = timer.ToString(_now);
\r
820 UpdateCondTimers();
\r
821 UpdateAkashiTimer();
\r
822 _timerEnabled = true;
\r
825 private void NotifyTimers()
\r
827 for (var i = 0; i < _sniffer.Missions.Length; i++)
\r
829 var entry = _sniffer.Missions[i];
\r
830 CheckAlarm("遠征終了", entry.Timer, i + 1, entry.Name);
\r
832 for (var i = 0; i < _sniffer.NDock.Length; i++)
\r
834 var entry = _sniffer.NDock[i];
\r
835 CheckAlarm("入渠終了", entry.Timer, i, entry.Name);
\r
837 for (var i = 0; i < _sniffer.KDock.Length; i++)
\r
839 var timer = _sniffer.KDock[i];
\r
840 CheckAlarm("建造完了", timer, 0, $"第{i + 1:D}ドック");
\r
842 NotifyCondTimers();
\r
843 NotifyAkashiTimer();
\r
844 _notificationManager.Flash();
\r
847 private void CheckAlarm(string key, AlarmTimer timer, int fleet, string subject)
\r
849 if (timer.CheckAlarm(_prev, _now))
\r
851 SetNotification(key, fleet, subject);
\r
854 var pre = TimeSpan.FromSeconds(_config.Notifications[key].PreliminaryPeriod);
\r
855 if (pre == TimeSpan.Zero)
\r
857 if (timer.CheckAlarm(_prev + pre, _now + pre))
\r
858 SetPreNotification(key, fleet, subject);
\r
861 private void SetTimerColor(Label label, AlarmTimer timer, DateTime now)
\r
863 label.ForeColor = timer.IsFinished(now) ? CUDColor.Red : Color.Black;
\r
866 private void UpdateCondTimers()
\r
869 if (_combinedFleet)
\r
871 var timer1 = _sniffer.GetConditionTimer(0);
\r
872 var timer2 = _sniffer.GetConditionTimer(1);
\r
873 timer = timer2 > timer1 ? timer2 : timer1;
\r
877 timer = _sniffer.GetConditionTimer(_currentFleet);
\r
879 if (timer == DateTime.MinValue)
\r
881 labelCondTimerTitle.Text = "";
\r
882 labelCondTimer.Text = "";
\r
885 var span = TimeSpan.FromSeconds(Ceiling((timer - _now).TotalSeconds));
\r
886 if (span >= TimeSpan.FromMinutes(9))
\r
888 labelCondTimerTitle.Text = "cond40まで";
\r
889 labelCondTimer.Text = (span - TimeSpan.FromMinutes(9)).ToString(@"mm\:ss");
\r
890 labelCondTimer.ForeColor = DefaultForeColor;
\r
894 labelCondTimerTitle.Text = "cond49まで";
\r
895 labelCondTimer.Text = (span >= TimeSpan.Zero ? span : TimeSpan.Zero).ToString(@"mm\:ss");
\r
896 labelCondTimer.ForeColor = span <= TimeSpan.Zero ? CUDColor.Red : DefaultForeColor;
\r
900 private void NotifyCondTimers()
\r
902 var notice = _sniffer.GetConditionNotice(_prev, _now);
\r
903 var pre = TimeSpan.FromSeconds(_config.Notifications["疲労回復"].PreliminaryPeriod);
\r
904 var preNotice = pre == TimeSpan.Zero
\r
905 ? new int[ShipInfo.FleetCount]
\r
906 : _sniffer.GetConditionNotice(_prev + pre, _now + pre);
\r
907 for (var i = 0; i < ShipInfo.FleetCount; i++)
\r
909 if (_config.NotifyConditions.Contains(notice[i]))
\r
911 SetNotification("疲労回復" + notice[i], i, "cond" + notice[i]);
\r
913 else if (_config.NotifyConditions.Contains(preNotice[i]))
\r
915 SetPreNotification("疲労回復" + preNotice[i], i, "cond" + notice[i]);
\r
920 private void UpdateAkashiTimer()
\r
922 if (_config.UsePresetAkashi)
\r
923 UpdatePresetAkashiTimer();
\r
924 _mainLabels.SetAkashiTimer(_sniffer.Fleets[_currentFleet].Ships,
\r
925 _sniffer.AkashiTimer.GetTimers(_currentFleet));
\r
928 private void UpdatePresetAkashiTimer()
\r
930 var akashi = _sniffer.AkashiTimer;
\r
931 var span = akashi.PresetDeckTimer;
\r
932 var color = span == TimeSpan.Zero && akashi.CheckPresetRepairing() ? CUDColor.Red : DefaultForeColor;
\r
933 var text = span == TimeSpan.MinValue ? "" : span.ToString(@"mm\:ss");
\r
934 labelAkashiRepairTimer.ForeColor = color;
\r
935 labelAkashiRepairTimer.Text = text;
\r
936 if (akashi.CheckPresetRepairing() && !akashi.CheckRepairing(_currentFleet))
\r
938 labelPresetAkashiTimer.ForeColor = color;
\r
939 labelPresetAkashiTimer.Text = text;
\r
943 labelPresetAkashiTimer.ForeColor = DefaultForeColor;
\r
944 labelPresetAkashiTimer.Text = "";
\r
948 private void NotifyAkashiTimer()
\r
950 var akashi = _sniffer.AkashiTimer;
\r
951 var msgs = akashi.GetNotice(_prev, _now);
\r
952 if (msgs.Length == 0)
\r
954 _notificationManager.StopRepeat("泊地修理");
\r
957 if (!akashi.CheckRepairing() && !(akashi.CheckPresetRepairing() && _config.UsePresetAkashi))
\r
959 _notificationManager.StopRepeat("泊地修理");
\r
962 var skipPreliminary = false;
\r
963 if (msgs[0].Proceeded == "20分経過しました。")
\r
965 SetNotification("泊地修理20分経過", msgs[0].Proceeded);
\r
966 msgs[0].Proceeded = "";
\r
967 skipPreliminary = true;
\r
968 // 修理完了がいるかもしれないので続ける
\r
970 for (var i = 0; i < ShipInfo.FleetCount; i++)
\r
972 if (msgs[i].Proceeded != "")
\r
973 SetNotification("泊地修理進行", i, msgs[i].Proceeded);
\r
974 if (msgs[i].Completed != "")
\r
975 SetNotification("泊地修理完了", i, msgs[i].Completed);
\r
977 var pre = TimeSpan.FromSeconds(_config.Notifications["泊地修理20分経過"].PreliminaryPeriod);
\r
978 if (skipPreliminary || pre == TimeSpan.Zero)
\r
980 if ((msgs = akashi.GetNotice(_prev + pre, _now + pre))[0].Proceeded == "20分経過しました。")
\r
981 SetPreNotification("泊地修理20分経過", 0, msgs[0].Proceeded);
\r
984 private void SetNotification(string key, string subject)
\r
986 SetNotification(key, 0, subject);
\r
989 private void SetNotification(string key, int fleet, string subject)
\r
991 var spec = _config.Notifications[_notificationManager.KeyToName(key)];
\r
992 _notificationManager.Enqueue(key, fleet, subject,
\r
993 (spec.Flags & _config.NotificationFlags & NotificationType.Repeat) == 0 ? 0 : spec.RepeatInterval);
\r
996 private void SetPreNotification(string key, int fleet, string subject)
\r
998 var spec = _config.Notifications[_notificationManager.KeyToName(key)];
\r
999 if ((spec.Flags & NotificationType.Preliminary) != 0)
\r
1000 _notificationManager.Enqueue(key, fleet, subject, 0, true);
\r
1003 private void UpdateRepairList()
\r
1005 panelRepairList.SetRepairList(_sniffer.RepairList);
\r
1008 private void UpdateQuestList()
\r
1010 var category = new[]
\r
1012 labelQuestColor1, labelQuestColor2, labelQuestColor3, labelQuestColor4, labelQuestColor5,
\r
1015 var name = new[] {labelQuest1, labelQuest2, labelQuest3, labelQuest4, labelQuest5, labelQuest6};
\r
1018 labelQuestCount1, labelQuestCount2, labelQuestCount3, labelQuestCount4, labelQuestCount5,
\r
1021 var progress = new[]
\r
1022 {labelProgress1, labelProgress2, labelProgress3, labelProgress4, labelProgress5, labelProgress6};
\r
1023 var quests = _sniffer.Quests;
\r
1024 for (var i = 0; i < name.Length; i++)
\r
1026 if (i < quests.Length)
\r
1028 category[i].BackColor = quests[i].Color;
\r
1029 name[i].Text = quests[i].Name;
\r
1030 progress[i].Text = $"{quests[i].Progress:D}%";
\r
1031 _toolTipQuest.SetToolTip(name[i], quests[i].ToToolTip());
\r
1032 var c = quests[i].Count;
\r
1035 count[i].Text = "";
\r
1036 count[i].ForeColor = Color.Black;
\r
1037 _toolTip.SetToolTip(count[i], "");
\r
1040 count[i].Text = " " + c;
\r
1041 count[i].ForeColor = c.Cleared ? CUDColor.Green : Color.Black;
\r
1042 _toolTip.SetToolTip(count[i], c.ToToolTip());
\r
1046 category[i].BackColor = DefaultBackColor;
\r
1047 name[i].Text = count[i].Text = progress[i].Text = "";
\r
1048 _toolTipQuest.SetToolTip(name[i], "");
\r
1049 _toolTip.SetToolTip(count[i], "");
\r
1054 private void Alarm(string balloonTitle, string balloonMessage, string name)
\r
1056 var flags = _config.Notifications[name].Flags;
\r
1057 var effective = _config.NotificationFlags & _config.Notifications[name].Flags;
\r
1058 if ((effective & NotificationType.FlashWindow) != 0)
\r
1059 Win32API.FlashWindow(Handle);
\r
1060 if ((effective & NotificationType.ShowBaloonTip) != 0)
\r
1061 notifyIconMain.ShowBalloonTip(20000, balloonTitle, balloonMessage, ToolTipIcon.Info);
\r
1062 if ((effective & NotificationType.PlaySound) != 0)
\r
1063 PlaySound(_config.Sounds[name], _config.Sounds.Volume);
\r
1064 if (_config.Pushbullet.On && (flags & NotificationType.Push) != 0)
\r
1068 PushNotification.PushToPushbullet(_config.Pushbullet.Token, balloonTitle, balloonMessage);
\r
1071 if (_config.Pushover.On && (flags & NotificationType.Push) != 0)
\r
1075 PushNotification.PushToPushover(_config.Pushover.ApiKey, _config.Pushover.UserKey,
\r
1076 balloonTitle, balloonMessage);
\r
1081 [DllImport("winmm.dll")]
\r
1082 private static extern int mciSendString(String command,
\r
1083 StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
\r
1085 // ReSharper disable InconsistentNaming
\r
1086 private const int MM_MCINOTIFY = 0x3B9;
\r
1088 private const int MCI_NOTIFY_SUCCESSFUL = 1;
\r
1089 // ReSharper restore InconsistentNaming
\r
1091 public void PlaySound(string file, int volume)
\r
1093 if (!File.Exists(file))
\r
1095 mciSendString("close sound", null, 0, IntPtr.Zero);
\r
1096 if (mciSendString("open \"" + file + "\" type mpegvideo alias sound", null, 0, IntPtr.Zero) != 0)
\r
1098 mciSendString("setaudio sound volume to " + volume * 10, null, 0, IntPtr.Zero);
\r
1099 mciSendString("play sound notify", null, 0, Handle);
\r
1102 protected override void WndProc(ref Message m)
\r
1104 if (m.Msg == MM_MCINOTIFY && (int)m.WParam == MCI_NOTIFY_SUCCESSFUL)
\r
1105 mciSendString("close sound", null, 0, IntPtr.Zero);
\r
1106 base.WndProc(ref m);
\r
1109 private void SetupFleetClick()
\r
1111 var labels = new[]
\r
1113 new[] {labelFleet1, labelFleet2, labelFleet3, labelFleet4},
\r
1114 new[] {labelFuelSq1, labelFuelSq2, labelFuelSq3, labelFuelSq4},
\r
1115 new[] {labelBullSq1, labelBullSq2, labelBullSq3, labelBullSq4}
\r
1117 foreach (var a in labels)
\r
1119 for (var fleet = 0; fleet < labels[0].Length; fleet++)
\r
1121 a[fleet].Tag = fleet;
\r
1122 a[fleet].Click += labelFleet_Click;
\r
1123 a[fleet].DoubleClick += labelFleet_DoubleClick;
\r
1128 private void labelFleet_Click(object sender, EventArgs e)
\r
1132 var fleet = (int)((Label)sender).Tag;
\r
1133 if (_currentFleet == fleet)
\r
1137 _combinedFleet = _sniffer.IsCombinedFleet && !_combinedFleet;
\r
1138 UpdatePanelShipInfo();
\r
1141 _combinedFleet = false;
\r
1142 _currentFleet = fleet;
\r
1143 UpdatePanelShipInfo();
\r
1146 private void labelFleet1_MouseHover(object sender, EventArgs e)
\r
1148 labelFleet1.Text = _currentFleet == 0 && _sniffer.IsCombinedFleet && !_combinedFleet ? "連合" : "第一";
\r
1151 private void labelFleet1_MouseLeave(object sender, EventArgs e)
\r
1153 labelFleet1.Text = _combinedFleet ? "連合" : "第一";
\r
1156 private void labelFleet_DoubleClick(object sender, EventArgs e)
\r
1160 var fleet = (int)((Label)sender).Tag;
\r
1161 var text = TextGenerator.GenerateFleetData(_sniffer, fleet);
\r
1162 if (string.IsNullOrEmpty(text))
\r
1164 Clipboard.SetText(text);
\r
1165 _tooltipCopy.Active = true;
\r
1166 _tooltipCopy.Show("コピーしました。", (Label)sender);
\r
1167 Task.Run(async () =>
\r
1169 await Task.Delay(1000);
\r
1170 _tooltipCopy.Active = false;
\r
1174 private readonly Color _activeButtonColor = Color.FromArgb(152, 179, 208);
\r
1176 private void labelBucketHistoryButton_Click(object sender, EventArgs e)
\r
1178 if (labelBucketHistory.Visible)
\r
1180 labelBucketHistory.Visible = false;
\r
1181 labelBucketHistoryButton.BackColor = DefaultBackColor;
\r
1185 labelBucketHistory.Visible = true;
\r
1186 labelBucketHistory.BringToFront();
\r
1187 labelBucketHistoryButton.BackColor = _activeButtonColor;
\r
1191 private void labelBucketHistory_Click(object sender, EventArgs e)
\r
1193 labelBucketHistory.Visible = false;
\r
1194 labelBucketHistoryButton.BackColor = DefaultBackColor;
\r
1197 private void labelMaterialHistoryButton_Click(object sender, EventArgs e)
\r
1199 if (panelMaterialHistory.Visible)
\r
1201 panelMaterialHistory.Visible = false;
\r
1202 labelMaterialHistoryButton.BackColor = DefaultBackColor;
\r
1206 panelMaterialHistory.Visible = true;
\r
1207 panelMaterialHistory.BringToFront();
\r
1208 labelMaterialHistoryButton.BackColor = _activeButtonColor;
\r
1212 private void panelMaterialHistory_Click(object sender, EventArgs e)
\r
1214 panelMaterialHistory.Visible = false;
\r
1215 labelMaterialHistoryButton.BackColor = DefaultBackColor;
\r
1218 public void ResetAchievemnt()
\r
1220 _sniffer.Achievement.Reset();
\r
1224 private void labelRepairListButton_Click(object sender, EventArgs e)
\r
1226 if (panelRepairList.Visible)
\r
1228 panelRepairList.Visible = false;
\r
1229 labelRepairListButton.BackColor = DefaultBackColor;
\r
1233 panelRepairList.Visible = true;
\r
1234 panelRepairList.BringToFront();
\r
1235 labelRepairListButton.BackColor = _activeButtonColor;
\r
1239 private void panelRepairList_Click(object sender, EventArgs e)
\r
1241 panelRepairList.Visible = false;
\r
1242 labelRepairListButton.BackColor = DefaultBackColor;
\r
1245 private void ShipListToolStripMenuItem_Click(object sender, EventArgs e)
\r
1247 _listForm.UpdateList();
\r
1249 if (_listForm.WindowState == FormWindowState.Minimized)
\r
1250 _listForm.WindowState = FormWindowState.Normal;
\r
1251 _listForm.Activate();
\r
1254 private void LogToolStripMenuItem_Click(object sender, EventArgs e)
\r
1256 Process.Start("http://localhost:" + _config.Proxy.Listen + "/");
\r
1259 private void labelClearQuest_Click(object sender, EventArgs e)
\r
1261 _sniffer.ClearQuests();
\r
1262 UpdateQuestList();
\r
1265 private void labelClearQuest_MouseDown(object sender, MouseEventArgs e)
\r
1267 labelClearQuest.BackColor = _activeButtonColor;
\r
1270 private void labelClearQuest_MouseUp(object sender, MouseEventArgs e)
\r
1272 labelClearQuest.BackColor = DefaultBackColor;
\r
1275 private void labelQuest_DoubleClick(object sender, EventArgs e)
\r
1277 var label = (Label)sender;
\r
1278 if (string.IsNullOrEmpty(label.Text))
\r
1280 Clipboard.SetText(label.Text);
\r
1281 _tooltipCopy.Active = true;
\r
1282 _tooltipCopy.Show("コピーしました。", label);
\r
1283 Task.Run(async () =>
\r
1285 await Task.Delay(1000);
\r
1286 _tooltipCopy.Active = false;
\r
1290 private void CaptureToolStripMenuItem_Click(object sender, EventArgs e)
\r
1294 var proc = new ProcessStartInfo("BurageSnap.exe") {WorkingDirectory = "Capture"};
\r
1295 Process.Start(proc);
\r
1297 catch (FileNotFoundException)
\r
1300 catch (Win32Exception)
\r