X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=KancolleSniffer%2FConfig.cs;h=6636163140959cb4f2bcd24b957da3bee79269f8;hb=fdba424819a4bc0c4104b16d842ef714b448c052;hp=ea0b7250c9db2db5ef30d39b185fb1ddca8ae00e;hpb=f92aa6a4b3c0af47ad6db7bebada6c9fc5e5d275;p=kancollesniffer%2FKancolleSniffer.git diff --git a/KancolleSniffer/Config.cs b/KancolleSniffer/Config.cs index ea0b725..6636163 100644 --- a/KancolleSniffer/Config.cs +++ b/KancolleSniffer/Config.cs @@ -1,30 +1,29 @@ -// Copyright (C) 2014 Kazuhiro Fujieda +// Copyright (C) 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.Drawing; using System.IO; -using System.Windows.Forms; -using Codeplex.Data; +using System.Linq; +using System.Xml.Serialization; namespace KancolleSniffer { public class ProxyConfig { + public const int DefaultListenPort = 8080; public bool Auto { get; set; } public int Listen { get; set; } public bool UseUpstream { get; set; } @@ -33,7 +32,7 @@ namespace KancolleSniffer public ProxyConfig() { Auto = true; - Listen = 8080; + Listen = DefaultListenPort; UseUpstream = false; UpstreamPort = 8888; } @@ -41,87 +40,306 @@ namespace KancolleSniffer public class ShipListConfig { + public bool Visible { get; set; } public Point Location { get; set; } public Size Size { get; set; } + public string Mode { get; set; } public bool ShipType { get; set; } + public bool ShowHpInPercent { get; set; } + public ListForm.SortOrder SortOrder { get; set; } = ListForm.SortOrder.ExpToNext; + public List> ShipGroup { get; set; } public ShipListConfig() { Location = new Point(int.MinValue, int.MinValue); + ShipGroup = new List>(); } } - public class Config + public class LogConfig + { + public bool On { get; set; } + public string OutputDir { get; set; } + public int MaterialLogInterval { get; set; } + + public LogConfig() + { + On = true; + OutputDir = ""; + MaterialLogInterval = 10; + } + } + + public class PushbulletConfig { - private readonly string _configFileName = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "config.json"); + public bool On { get; set; } + public string Token { get; set; } = ""; + } + + public class PushoverConfig + { + public bool On { get; set; } + public string ApiKey { get; set; } = ""; + public string UserKey { get; set; } = ""; + } + + public class SoundConfig + { + public int Volume { get; set; } = 100; + + public string[] Files { get; set; } = + { + "ensei.mp3", + "nyuukyo.mp3", + "kenzou.mp3", + "kanmusu.mp3", + "soubi.mp3", + "taiha.mp3", + "20min.mp3", + "syuuri.mp3", + "syuuri2.mp3", + "hirou.mp3" + }; + + public string this[string name] + { + get => Files[Config.NotificationIndex[name]]; + set => Files[Config.NotificationIndex[name]] = value; + } + } + + [Flags] + public enum NotificationType + { + FlashWindow = 1, + ShowBaloonTip = 1 << 1, + PlaySound = 1 << 2, + All = (1 << 3) - 1, + Pushbullet = 1 << 3, + Push = 1 << 4, + Repeat = 1 << 5, + Cont = 1 << 6, + Preliminary = 1 << 7 + } + + + public class NotificationSpec + { + public string Name { get; set; } + public NotificationType Flags { get; set; } + public int RepeatInterval { get; set; } + public int PreliminaryPeriod { get; set; } + } + + public class NotificationConfig + { + public NotificationType[] Settings = + Config.NotificationNames.Select(x => NotificationType.All).ToArray(); + + public int[] RepeatIntervals = + Config.NotificationNames.Select(x => 0).ToArray(); + + public int[] PreliminaryPeriods = + Config.NotificationNames.Select(x => 0).ToArray(); + + public NotificationSpec this[string name] + { + get => new NotificationSpec + { + Name = name, + Flags = Settings[Config.NotificationIndex[name]], + RepeatInterval = RepeatIntervals[Config.NotificationIndex[name]], + PreliminaryPeriod = PreliminaryPeriods[Config.NotificationIndex[name]] + }; + set + { + Settings[Config.NotificationIndex[name]] = value.Flags; + RepeatIntervals[Config.NotificationIndex[name]] = value.RepeatInterval; + PreliminaryPeriods[Config.NotificationIndex[name]] = value.PreliminaryPeriod; + } + } + } + public class LocationPerMachine + { + public string MachineName { get; set; } public Point Location { get; set; } + public int Zoom { get; set; } = 100; + public Point ListLocation { get; set; } + public Size ListSize { get; set; } + } + + [Flags] + public enum Spoiler + { + ResultRank = 1, + AirBattleResult = 1 << 1, + BattleResult = 1 << 2, + All = (1 << 3) - 1 + } + + [Flags] + public enum TimerKind + { + Mission = 1, + NDock = 1 << 1 + } + + public class Config + { + private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar); + private readonly string _configFileName; + + public Point Location { get; set; } = new Point(int.MinValue, int.MinValue); public bool TopMost { get; set; } public bool HideOnMinimized { get; set; } - public bool FlashWindow { get; set; } - public bool ShowBaloonTip { get; set; } - public bool PlaySound { get; set; } - public int MarginShips { get; set; } - public int MarginEquips { get; set; } + public bool ExitSilently { get; set; } + public int Zoom { get; set; } = 100; + public bool SaveLocationPerMachine { get; set; } + public List LocationList { get; set; } = new List(); + public bool ShowHpInPercent { get; set; } + public TimerKind ShowEndTime { get; set; } + public bool FlashWindow { get; set; } = true; + public bool ShowBaloonTip { get; set; } = true; + public bool PlaySound { get; set; } = true; + public NotificationType NotificationFlags { get; set; } = NotificationType.All; + public NotificationConfig Notifications { get; set; } = new NotificationConfig(); + public int MarginShips { get; set; } = 4; + public int MarginEquips { get; set; } = 10; public List NotifyConditions { get; set; } public List ResetHours { get; set; } - public int SoundVolume { get; set; } - public string MissionSoundFile { get; set; } - public string NDockSoundFile { get; set; } - public string KDockSoundFile { get; set; } - public string MaxShipsSoundFile { get; set; } - public string MaxEquipsSoundFile { get; set; } - public string DamagedShipSoundFile { get; set; } - public string Akashi20MinSoundFile { get; set; } - public string AkashiProgressSoundFile { get; set; } - public string ConditionSoundFile { get; set; } - public bool Logging { get; set; } - public string LogFile { get; set; } - public ProxyConfig Proxy { get; set; } - public ShipListConfig ShipList { get; set; } + public bool AlwaysShowResultRank { get; set; } + public Spoiler Spoilers { get; set; } + public bool UsePresetAkashi { get; set; } + public SoundConfig Sounds { get; set; } = new SoundConfig(); + public bool DebugLogging { get; set; } + public string DebugLogFile { get; set; } = "log.txt"; + public ProxyConfig Proxy { get; set; } = new ProxyConfig(); + public ShipListConfig ShipList { get; set; } = new ShipListConfig(); + public LogConfig Log { get; set; } = new LogConfig(); + public PushbulletConfig Pushbullet { get; set; } = new PushbulletConfig(); + public PushoverConfig Pushover { get; set; } = new PushoverConfig(); + + public static readonly string[] NotificationNames = + { + "遠征終了", "入渠終了", "建造完了", "艦娘数超過", "装備数超過", + "大破警告", "泊地修理20分経過", "泊地修理進行", "泊地修理完了", "疲労回復" + }; + + public static readonly Dictionary NotificationIndex = + NotificationNames.Select((name, i) => new {name, i}).ToDictionary(entry => entry.name, entry => entry.i); public Config() { - Location = new Point(int.MinValue, int.MinValue); - FlashWindow = ShowBaloonTip = PlaySound = true; - MarginShips = 4; - MarginEquips = 10; - NotifyConditions = new List(new[]{40, 49}); - ResetHours = new List(); - SoundVolume = 100; - var dir = Path.GetDirectoryName(Application.ExecutablePath); -// ReSharper disable AssignNullToNotNullAttribute - MissionSoundFile = Path.Combine(dir, "ensei.mp3"); - NDockSoundFile = Path.Combine(dir, "nyuukyo.mp3"); - KDockSoundFile = Path.Combine(dir, "kenzou.mp3"); - MaxShipsSoundFile = Path.Combine(dir, "kanmusu.mp3"); - MaxEquipsSoundFile = Path.Combine(dir, "soubi.mp3"); - DamagedShipSoundFile = Path.Combine(dir, "taiha.mp3"); - Akashi20MinSoundFile = Path.Combine(dir, "20min.mp3"); - AkashiProgressSoundFile = Path.Combine(dir, "syuuri.mp3"); - ConditionSoundFile = Path.Combine(dir, "hirou.mp3"); - LogFile = Path.Combine(dir, "log.txt"); -// ReSharper restore AssignNullToNotNullAttribute - Proxy = new ProxyConfig(); - ShipList = new ShipListConfig(); + _configFileName = Path.Combine(_baseDir, "config.xml"); + ConvertPath(PrependBaseDir); + } + + public void InitializeValues() + { + NotifyConditions = new List(new[] {40, 49}); + ResetHours = new List(new[] {2}); } public void Load() { try { - var config = (Config)DynamicJson.Parse(File.ReadAllText(_configFileName)); + var serializer = new XmlSerializer(typeof(Config)); + Config config; + using (var file = File.OpenText(_configFileName)) + config = (Config)serializer.Deserialize(file); foreach (var property in GetType().GetProperties()) property.SetValue(this, property.GetValue(config, null), null); + var ns = Notifications.Settings; + for (var i = 0; i < ns.Length; i++) + { + if ((ns[i] & NotificationType.Pushbullet) != 0) + ns[i] = ns[i] ^ NotificationType.Pushbullet | NotificationType.Push; + } + ComposeNotificationFlags(); + if (AlwaysShowResultRank) + { + Spoilers = Spoiler.All; + AlwaysShowResultRank = false; + } + if (SaveLocationPerMachine) + { + foreach (var l in LocationList) + { + if (l.MachineName != Environment.MachineName) + continue; + Location = l.Location; + Zoom = l.Zoom; + ShipList.Location = l.ListLocation; + ShipList.Size = l.ListSize; + } + } } catch (FileNotFoundException) { + InitializeValues(); + Save(); } + ConvertPath(PrependBaseDir); + } + + private void ComposeNotificationFlags() + { + NotificationFlags = (NotificationFlags & ~NotificationType.All) | + (FlashWindow ? NotificationType.FlashWindow : 0) | + (ShowBaloonTip ? NotificationType.ShowBaloonTip : 0) | + (PlaySound ? NotificationType.PlaySound : 0); } public void Save() { - File.WriteAllText(_configFileName, DynamicJson.Serialize(this)); + if (SaveLocationPerMachine) + { + LocationList = LocationList.Where(l => l.MachineName != Environment.MachineName).ToList(); + LocationList.Add(new LocationPerMachine + { + MachineName = Environment.MachineName, + Location = Location, + Zoom = Zoom, + ListLocation = ShipList.Location, + ListSize = ShipList.Size + }); + } + else + { + LocationList = new List(); + } + DecomposeNotificationFlags(); + ConvertPath(StripBaseDir); + var serializer = new XmlSerializer(typeof(Config)); + using (var file = File.CreateText(_configFileName)) + serializer.Serialize(file, this); + } + + private void DecomposeNotificationFlags() + { + FlashWindow = (NotificationFlags & NotificationType.FlashWindow) != 0; + ShowBaloonTip = (NotificationFlags & NotificationType.ShowBaloonTip) != 0; + PlaySound = (NotificationFlags & NotificationType.PlaySound) != 0; + } + + private void ConvertPath(Func func) + { + DebugLogFile = func(DebugLogFile); + Log.OutputDir = func(Log.OutputDir); + for (var i = 0; i < Sounds.Files.Length; i++) + Sounds.Files[i] = func(Sounds.Files[i]); } + + private string StripBaseDir(string path) + { + if (!path.StartsWith(_baseDir)) + return path; + path = path.Substring(_baseDir.Length); + return path.TrimStart(Path.DirectorySeparatorChar); + } + + private string PrependBaseDir(string path) => Path.IsPathRooted(path) ? path : Path.Combine(_baseDir, path); } } \ No newline at end of file