summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Patcher/Impostor.Patcher.Cli/Program.cs
blob: 76653a1b5fa99fdfba94d691b7fdde9d9e9c82d1 (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
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading.Tasks;
using Impostor.Patcher.Shared;
using Impostor.Patcher.Shared.Events;

namespace Impostor.Patcher.Cli
{
    internal static class Program
    {
        private static readonly AmongUsModifier Modifier = new AmongUsModifier();

        internal static Task<int> Main(string[] args)
        {
            var rootCommand = new RootCommand
            {
                new Option<string>(
                    "--address",
                    "IP Address of the server, will prompt if not specified"
                ),
                new Option<string>(
                    "--name",
                    () => AmongUsModifier.DefaultRegionName,
                    "Name for server region"
                )
            };

            rootCommand.Handler = CommandHandler.Create<string, string>((address, name) =>
            {
                Modifier.RegionName = name;
                Modifier.Error += ModifierOnError;
                Modifier.Saved += ModifierOnSaved;

                Console.WriteLine("Welcome to Impostor");

                if (Modifier.TryLoadRegionInfo(out var regionInfo))
                {
                    Console.WriteLine($"Currently selected region: {regionInfo.Name} ({regionInfo.Ping}, {regionInfo.Servers.Count} server(s))");
                }

                if (address != null)
                {
                    return Modifier.SaveIpAsync(address);
                }

                return PromptAsync();
            });

            return rootCommand.InvokeAsync(args);
        }

        private static void ModifierOnSaved(object sender, SavedEventArgs e)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("The IP Address was saved, please (re)start Among Us.");
            Console.ResetColor();
        }

        private static void WriteError(string message)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(message);
            Console.ResetColor();
        }

        private static void ModifierOnError(object sender, ErrorEventArgs e)
        {
            WriteError(e.Message);
        }

        private static async Task PromptAsync()
        {
            Console.WriteLine("Please enter in the IP Address of the server you would like to use for Among Us");
            Console.WriteLine("If you want to stop playing on the server, simply select another region");

            while (true)
            {
                Console.Write("> ");

                if (await Modifier.SaveIpAsync(Console.ReadLine()))
                {
                    return;
                }
            }
        }
    }
}