summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.WinForms/Forms/FrmMain.cs
blob: 5c06669e0c3a44c323be46c281370d07e6158e23 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Impostor.Patcher.Shared;
using Impostor.Patcher.Shared.Events;

namespace Impostor.Patcher.WinForms.Forms
{
    public partial class FrmMain : Form
    {
        private readonly Configuration _config;
        private readonly AmongUsModifier _modifier;

        public FrmMain()
        {
            InitializeComponent();

            AcceptButton = buttonLaunch;

            _config = new Configuration();
            _modifier = new AmongUsModifier();
            _modifier.Error += ModifierOnError;
            _modifier.Saved += ModifierOnSaved;
        }

        private void ModifierOnError(object sender, ErrorEventArgs e)
        {
            MessageBox.Show(e.Message, "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);

            comboIp.Text = string.Empty;
            comboIp.Focus();

            comboIp.Enabled = true;
            buttonLaunch.Enabled = true;
        }

        private void ModifierOnSaved(object sender, SavedEventArgs e)
        {
            MessageBox.Show("The IP Address was saved, please (re)start Among Us.", "Success",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            var ipText = e.Port == AmongUsModifier.DefaultPort
                ? e.IpAddress
                : $"{e.IpAddress}:{e.Port}";

            comboIp.Text = ipText;
            comboIp.Enabled = true;
            buttonLaunch.Enabled = true;

            _config.AddIp(ipText);
            _config.Save();

            RefreshComboIps();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            _config.Load();

            RefreshComboIps();

            if (_modifier.TryLoadIp(out var ipAddress))
            {
                comboIp.Text = ipAddress;
            }
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            comboIp.Focus();
        }

        private void textIp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }

            e.Handled = true;

            buttonLaunch_Click(this, EventArgs.Empty);
        }

        private async void buttonLaunch_Click(object sender, EventArgs e)
        {
            comboIp.Enabled = false;
            buttonLaunch.Enabled = false;

            await _modifier.SaveIpAsync(comboIp.Text);
        }

        private void lblUrl_Click(object sender, EventArgs e)
        {
            Process.Start("https://github.com/AeonLucid/Impostor");
        }

        private void RefreshComboIps()
        {
            comboIp.Items.Clear();

            if (_config.RecentIps.Count > 0)
            {
                foreach (var ip in _config.RecentIps)
                {
                    comboIp.Items.Add(ip);
                }
            }
        }
    }
}