-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAlmeticaClient.cs
93 lines (81 loc) · 3.2 KB
/
AlmeticaClient.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Google.Protobuf;
using Newtonsoft.Json;
using static AlmeticaLauncher.ServerList.Types;
namespace AlmeticaLauncher
{
internal class AlmeticaClient
{
private readonly Uri _serverBasePath;
public AlmeticaClient(Uri serverBasePath)
{
_serverBasePath = serverBasePath;
}
Uri ServerListUri => new Uri(_serverBasePath, "server/list");
Uri AuthUri => new Uri(_serverBasePath, "auth");
public byte[] GetTicket(string accountName, string password)
{
return GetTicketAsync(accountName, password).GetAwaiter().GetResult();
}
public ServerList GetServerList()
{
return GetServerListAsync().GetAwaiter().GetResult();
}
private async Task<byte[]> GetTicketAsync(string accountName, string password)
{
using var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"accountname", accountName},
{"password", password}
});
using var client = new HttpClient();
var response = await client.PostAsync(AuthUri, content);
var authenticationResponse = await response.Content.ReadAsStringAsync();
var auth = JsonConvert.DeserializeObject<ResponseAuth>(authenticationResponse);
return Convert.FromBase64String(auth.Ticket);
}
private async Task<ServerList> GetServerListAsync()
{
using var client = new HttpClient();
var response = await client.GetAsync(ServerListUri);
var serverListResponse = await response.Content.ReadAsStringAsync();
var serverList = JsonConvert.DeserializeObject<ResponseServerList>(serverListResponse);
var gameCompatibleServerList = serverList.Servers.Select(x => new Server
{
Id = x.Id,
Category = ByteString.CopyFrom(Encoding.Unicode.GetBytes(x.Category)),
Rawname = ByteString.CopyFrom(Encoding.Unicode.GetBytes(x.Rawname)),
Name = ByteString.CopyFrom(Encoding.Unicode.GetBytes(x.Name)),
Crowdness = ByteString.CopyFrom(Encoding.Unicode.GetBytes(x.Crowdness)),
Open = ByteString.CopyFrom(Encoding.Unicode.GetBytes(x.Open)),
Ip = IpV4ToInt(x.Ip),
Port = x.Port,
Lang = x.Lang,
Popup = ByteString.CopyFrom(Encoding.Unicode.GetBytes(x.Popup))
}).ToList();
return new ServerList
{
Servers = {gameCompatibleServerList},
LastPlayedId = 1,
Unknwn = 0
};
}
private int IpV4ToInt(string s)
{
var address = IPAddress.Parse(s);
var ipv4 = address.MapToIPv4();
var addressBytes = ipv4.GetAddressBytes();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(addressBytes);
}
return BitConverter.ToInt32(addressBytes, 0);
}
}
}