summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Shared/Configuration.cs
blob: b256156cd2e63ef52206ed9ccee1767d63960a6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
            }
        }
    }
}