about summary refs log tree commit diff stats
path: root/AnodyneArchipelago/Menu/ArchipelagoSettings.cs
blob: eff3ddfcc4d477ec688d6cd63e70d5ac7588ed71 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
using AnodyneSharp.Registry;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;

namespace AnodyneArchipelago.Menu
{
    internal class ConnectionDetails : IEquatable<ConnectionDetails>
    {
        public string ApServer;
        public string ApSlot;
        public string ApPassword;

        public override bool Equals(object obj)
        {
            return this.Equals(obj as ConnectionDetails);
        }

        public bool Equals(ConnectionDetails other)
        {
            return (ApServer == other.ApServer) && (ApSlot == other.ApSlot) && (ApPassword == other.ApPassword);
        }

        public override int GetHashCode()
        {
            return (ApServer, ApSlot, ApPassword).GetHashCode();
        }
    }

    internal class ArchipelagoSettings
    {
        public static JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
        {
            IncludeFields = true
        };

        public List<ConnectionDetails> ConnectionDetails = new();

        public static string GetFilePath() => string.Format("{0}Saves/ap_settings.dat", GameConstants.SavePath);

        public static ArchipelagoSettings Load()
        {
            try
            {
                string s = File.ReadAllText(GetFilePath());
                return JsonSerializer.Deserialize<ArchipelagoSettings>(s, serializerOptions);
            } catch (Exception)
            {
                return null;
            }
        }

        public void Save()
        {
            File.WriteAllText(GetFilePath(), JsonSerializer.Serialize(this, serializerOptions));
        }

        public void AddConnection(ConnectionDetails connectionDetails)
        {
            if (ConnectionDetails.Contains(connectionDetails))
            {
                ConnectionDetails.Remove(connectionDetails);
            }

            ConnectionDetails.Insert(0, connectionDetails);

            if (ConnectionDetails.Count > 9)
            {
                ConnectionDetails.RemoveAt(ConnectionDetails.Count - 1);
            }
        }
    }
}