diff options
author | chai <chaifix@163.com> | 2020-12-30 20:59:04 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-12-30 20:59:04 +0800 |
commit | e9ea621b93fbb58d9edfca8375918791637bbd52 (patch) | |
tree | 19ce3b1c1f2d51eda6878c9d0f2c9edc27f13650 /Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs |
+init
Diffstat (limited to 'Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs')
-rw-r--r-- | Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs b/Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs new file mode 100644 index 0000000..b256156 --- /dev/null +++ b/Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Impostor.Patcher.Shared +{ + public class Configuration + { + private const string FileRecentIps = @"recent_ips.txt"; + private const int MaxRecentIps = 5; + + private readonly string _baseDir; + private readonly string _recentIpsPath; + private readonly List<string> _recentIps; + + public Configuration() + { + var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)); + + _baseDir = Path.Combine(appData, "Impostor"); + _recentIpsPath = Path.Combine(_baseDir, FileRecentIps); + _recentIps = new List<string>(); + } + + public IReadOnlyList<string> RecentIps => _recentIps; + + public void Load() + { + if (File.Exists(_recentIpsPath)) + { + _recentIps.AddRange(File.ReadAllLines(_recentIpsPath)); + } + } + + public void Save() + { + Directory.CreateDirectory(_baseDir); + + if (!Directory.Exists(_baseDir)) + { + return; + } + + if (_recentIps.Count > 0) + { + File.WriteAllLines(_recentIpsPath, _recentIps); + } + } + + public void AddIp(string ip) + { + if (_recentIps.Contains(ip)) + { + _recentIps.Remove(ip); + } + + _recentIps.Insert(0, ip); + + if (_recentIps.Count > MaxRecentIps) + { + _recentIps.RemoveAt(MaxRecentIps); + } + } + } +}
\ No newline at end of file |