From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- .../Impostor.Patcher.Shared/Configuration.cs | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs (limited to 'Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs') 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 _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(); + } + + public IReadOnlyList 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 -- cgit v1.1-26-g67d0