src/main.rs

Tue, 08 Dec 2020 23:44:49 -0700

author
Glitchvid <Glitchvid@glitchvid.com>
date
Tue, 08 Dec 2020 23:44:49 -0700
changeset 0
79c63b4440ee
child 2
474f76259cf0
permissions
-rw-r--r--

Initial

0
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
1 use std::env;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
2 use std::convert::TryFrom;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
3 use regex::Regex;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
4
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
5 const REGEX_STEAMID2: &str = r"^STEAM_([0-5]):([01]):(\d+$)";
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
6 const REGEX_STEAMID3: &str = r"^\[(.):([01]):(\d+)\]$";
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
7
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
8 /* Valve SteamID Format:
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
9 * A SteamID is just a packed 64-bit unsigned integer!
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
10 *
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
11 * It consists of five parts, from least to most significant bit:
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
12 * 1. Authentication Server - 1 bit (1)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
13 * 2. Account Number - 31 bits (32)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
14 * 3. Instance - 20 bits (52)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
15 * 4. Account Type - 4 bits (56)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
16 * 5. Universe - 8 bits (64)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
17 *
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
18 * This can be visualized like so:
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
19 * 1. _______________________________________________________________X
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
20 * 2. ________________________________XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX_
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
21 * 3. ____________XXXXXXXXXXXXXXXXXXXX________________________________
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
22 * 4. ________XXXX____________________________________________________
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
23 * 5. XXXXXXXX________________________________________________________
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
24 *
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
25 * There are multiple ways to express a SteamID, some are lossy.
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
26 * A. steamID64 - (1)+(2)+(3)+(4)+(5)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
27 * B. steamID2 - STEAM_(5):(1):(2)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
28 * C. steamID3 - [(4):(5):(1)+(2)]
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
29 */
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
30
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
31 /*
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
32 * Instance
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
33 * The Instance field nominally holds what 'instance' the steamID is, however
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
34 * when specifying a chatroom, the last 8 bits define the "type" of chatroom.
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
35 * This can be visualized like so:
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
36 * ____________ZZZZZZZZXXXXXXXXXXXX
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
37 */
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
38
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
39 fn account_type_to_char(account_type: u8, instance: Option<u32>) -> char {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
40 match account_type {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
41 0 => 'I', // Invalid
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
42 1 => 'U', // Individual (Profiles)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
43 2 => 'M', // Multiseat
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
44 3 => 'G', // GameServer
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
45 4 => 'A', // AnonGameServer
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
46 5 => 'P', // Pending
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
47 6 => 'C', // ContentServer
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
48 7 => 'g', // Clan (Groups)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
49 8 => match instance.unwrap_or(0) >> 12 & 255 { // EChatSteamIDInstanceFlags (Shifted 12 right)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
50 1 => 'T', // MatchMaking Lobby
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
51 2 => 'L', // Lobby
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
52 4 => 'c', // Clan Chat
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
53 _ => 'c', // Clan Chat (Default)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
54 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
55 //9 => '',// ConsoleUser
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
56 10 => 'a', // AnonUser
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
57 _ => 'I', // Invalid (Default)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
58 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
59 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
60
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
61 #[derive(Debug)]
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
62 enum SteamIDAccountType {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
63 Invalid,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
64 Individual,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
65 Multiseat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
66 GameServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
67 AnonGameServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
68 Pending,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
69 ContentServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
70 Clan,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
71 Chat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
72 ConsoleUser,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
73 AnonUser,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
74 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
75
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
76 impl SteamIDAccountType {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
77 fn to_char(self, instance: Option<u32>) -> char {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
78 match self {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
79 SteamIDAccountType::Invalid => 'I',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
80 SteamIDAccountType::Individual => 'U',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
81 SteamIDAccountType::Multiseat => 'M',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
82 SteamIDAccountType::GameServer => 'G',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
83 SteamIDAccountType::AnonGameServer => 'A',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
84 SteamIDAccountType::Pending => 'P',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
85 SteamIDAccountType::ContentServer => 'C',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
86 SteamIDAccountType::Clan => 'g',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
87 SteamIDAccountType::Chat =>
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
88 match instance.unwrap_or(0) >> 12 & 255 { // EChatSteamIDInstanceFlags (Shifted 12 right)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
89 1 => 'T', // MatchMaking Lobby
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
90 2 => 'L', // Lobby
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
91 4 => 'c', // Clan Chat
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
92 _ => 'c', // Clan Chat (Default)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
93 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
94 SteamIDAccountType::ConsoleUser => 'I',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
95 SteamIDAccountType::AnonUser => 'a',
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
96 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
97 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
98 fn to_int(self) -> u32 {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
99 match self {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
100 SteamIDAccountType::Invalid => 0,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
101 SteamIDAccountType::Individual => 1,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
102 SteamIDAccountType::Multiseat => 2,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
103 SteamIDAccountType::GameServer => 3,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
104 SteamIDAccountType::AnonGameServer => 4,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
105 SteamIDAccountType::Pending => 5,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
106 SteamIDAccountType::ContentServer => 6,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
107 SteamIDAccountType::Clan => 7,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
108 SteamIDAccountType::Chat => 8,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
109 SteamIDAccountType::ConsoleUser => 9,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
110 SteamIDAccountType::AnonUser => 10,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
111 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
112 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
113 fn from_char(account_type: char) -> SteamIDAccountType {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
114 match account_type {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
115 'I' => SteamIDAccountType::Invalid,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
116 'U' => SteamIDAccountType::Individual,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
117 'M' => SteamIDAccountType::Multiseat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
118 'G' => SteamIDAccountType::GameServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
119 'A' => SteamIDAccountType::AnonGameServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
120 'P' => SteamIDAccountType::Pending,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
121 'C' => SteamIDAccountType::ContentServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
122 'g' => SteamIDAccountType::Clan,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
123 'c' => SteamIDAccountType::Chat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
124 'T' => SteamIDAccountType::Chat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
125 'L' => SteamIDAccountType::Chat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
126 'a' => SteamIDAccountType::AnonUser,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
127 _ => SteamIDAccountType::Invalid,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
128 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
129 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
130 fn from_int(account_type: u8) -> SteamIDAccountType {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
131 match account_type {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
132 0 => SteamIDAccountType::Invalid,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
133 1 => SteamIDAccountType::Individual,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
134 2 => SteamIDAccountType::Multiseat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
135 3 => SteamIDAccountType::GameServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
136 4 => SteamIDAccountType::AnonGameServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
137 5 => SteamIDAccountType::Pending,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
138 6 => SteamIDAccountType::ContentServer,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
139 7 => SteamIDAccountType::Clan,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
140 8 => SteamIDAccountType::Chat,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
141 9 => SteamIDAccountType::ConsoleUser,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
142 10 => SteamIDAccountType::AnonUser,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
143 _ => SteamIDAccountType::Invalid,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
144 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
145 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
146 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
147
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
148 #[derive(Debug, PartialEq)]
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
149 enum SteamIDType{
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
150 SteamID64,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
151 SteamID2,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
152 SteamID3,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
153 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
154
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
155 fn string_to_steamid_type(steamid: &str) -> Result<SteamIDType, &str> {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
156 if steamid.parse::<u64>().is_ok() {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
157 return Ok(SteamIDType::SteamID64)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
158 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
159 let steamid2 = Regex::new(REGEX_STEAMID2).unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
160 if steamid2.is_match(steamid) {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
161 return Ok(SteamIDType::SteamID2)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
162 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
163
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
164 let steamid3 = Regex::new(REGEX_STEAMID3).unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
165 if steamid3.is_match(steamid) {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
166 return Ok(SteamIDType::SteamID3)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
167 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
168 Err("Unable to parse to any SteamID Format.")
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
169 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
170
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
171 fn steamid2_to_steamid64( steamid2: &str) -> u64 {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
172 let regex = Regex::new(REGEX_STEAMID2).unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
173 let captures = regex.captures(steamid2).unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
174
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
175 let universe = captures.get(1).unwrap().as_str().parse::<u64>().unwrap_or(1);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
176 let auth_server = captures.get(2).unwrap().as_str().parse::<u64>().unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
177 let account_id = captures.get(3).unwrap().as_str().parse::<u64>().unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
178
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
179 let steam64 = (universe << 56) | auth_server | (account_id << 1) | 76561197960265728;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
180 steam64
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
181 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
182
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
183 fn steamid3_to_steamid64( steamid3: &str) -> u64 {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
184 let regex = Regex::new(REGEX_STEAMID3).unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
185 let captures = regex.captures(steamid3).unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
186
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
187 let account_type = captures.get(1).unwrap().as_str().parse::<char>().unwrap_or('I');
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
188 let account_type = u64::from(SteamIDAccountType::to_int(SteamIDAccountType::from_char(account_type)));
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
189
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
190 let universe = captures.get(2).unwrap().as_str().parse::<u64>().unwrap_or(1);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
191 let account_id = captures.get(3).unwrap().as_str().parse::<u64>().unwrap();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
192
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
193 let steam64 = (account_type << 52) | (universe << 56) | account_id ;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
194 steam64
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
195 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
196
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
197 fn string_to_steamid64( input: &str) -> Result<u64, &str> {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
198 let steam_type = string_to_steamid_type(input);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
199 match steam_type {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
200 Ok(steam_type) => {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
201 match steam_type {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
202 SteamIDType::SteamID64 => return Ok(input.parse::<u64>().unwrap()),
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
203 SteamIDType::SteamID2 => return Ok(steamid2_to_steamid64(input)),
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
204 SteamIDType::SteamID3 => return Ok(steamid3_to_steamid64(input)),
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
205 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
206 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
207 Err(steam_type) => return Err(steam_type)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
208 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
209 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
210
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
211 struct SteamID {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
212 account_id: u32,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
213 account_instance: u32,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
214 account_type: u8,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
215 account_universe: u8,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
216 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
217
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
218 impl SteamID {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
219 fn new() -> SteamID {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
220 SteamID {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
221 account_id: 0,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
222 account_instance: 1,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
223 account_type: 1,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
224 account_universe: 1,
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
225 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
226 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
227 fn set_steamid64(&mut self, steamid_64: u64) {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
228 self.account_id = u32::try_from(steamid_64 & 4294967295).unwrap_or(1);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
229 self.account_instance = u32::try_from(steamid_64 >> 32 & 1048575).unwrap_or(1);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
230 self.account_type = u8::try_from(steamid_64 >> 52 & 15).unwrap_or(1);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
231 self.account_universe = u8::try_from(steamid_64 >> 56 & 15).unwrap_or(1);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
232 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
233 fn get_steamid64(&self) -> u64 {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
234 u64::from(self.account_id) |
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
235 u64::from(self.account_instance) << 32 |
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
236 u64::from(self.account_type) << 52 |
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
237 u64::from(self.account_universe) << 56
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
238 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
239 fn get_steamid2(&self) -> String {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
240 let authserver: u32 = self.account_id & 1; // Ideally we'd cast this to a bool and convert that to a 0 or 1 later.
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
241 let accountid: u32 = (self.account_id >> 1) & 2147483647;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
242 format!("STEAM_{}:{}:{}", self.account_universe, authserver, accountid)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
243 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
244 fn get_steamid3(&self) -> String {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
245 let type_char: char = account_type_to_char(self.account_type, Some(self.account_instance));
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
246 format!("[{}:{}:{}]", type_char, self.account_universe, self.account_id)
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
247 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
248 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
249
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
250
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
251
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
252 fn main() {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
253 // Gather our CLI arguments
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
254 let args: Vec<String> = env::args().collect();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
255
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
256 //println!("Args: {:?}", args);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
257 //println!("Len: {}", args.len());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
258
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
259 // Dumb check, make sure they even tried providing a SteamID
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
260 if args.len() < 2 {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
261 println!("No IDs provided!");
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
262 std::process::exit(-1);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
263 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
264
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
265 // Process all of our passed strings
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
266 for i in 1..args.len() {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
267 let input = &args[i];
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
268 let steam_type = string_to_steamid_type(input);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
269 match steam_type {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
270 Ok(steam_type) => {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
271 println!("Interpreting as {:?}", steam_type );
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
272 match steam_type {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
273 SteamIDType::SteamID64 => {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
274 let mut steamid_object = SteamID::new();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
275 steamid_object.set_steamid64(input.parse::<u64>().expect("SteamID64 Not a Number!"));
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
276 println!("steamID64:\t{}", steamid_object.get_steamid64());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
277 println!("steamID: \t{}", steamid_object.get_steamid2());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
278 println!("steamID3: \t{}", steamid_object.get_steamid3());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
279 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
280 SteamIDType::SteamID2 => {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
281 let mut steamid_object = SteamID::new();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
282 steamid_object.set_steamid64(steamid2_to_steamid64(input));
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
283 println!("steamID64:\t{}", steamid_object.get_steamid64());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
284 println!("steamID: \t{}", steamid_object.get_steamid2());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
285 println!("steamID3: \t{}", steamid_object.get_steamid3());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
286 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
287 SteamIDType::SteamID3 => {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
288 let mut steamid_object = SteamID::new();
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
289 steamid_object.set_steamid64(steamid3_to_steamid64(input));
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
290 println!("steamID64:\t{}", steamid_object.get_steamid64());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
291 println!("steamID: \t{}", steamid_object.get_steamid2());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
292 println!("steamID3: \t{}", steamid_object.get_steamid3());
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
293 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
294 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
295 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
296 Err(_steam_type) => {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
297 println!("Unable to interpret {}", input);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
298 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
299 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
300 println!("");
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
301 }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
302
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
303 // println!("Generating Alias SteamID64s...");
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
304 // thread::sleep(time::Duration::from_secs(1));
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
305 // for n in 1..1048575 {
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
306 // let instance: u64 = n << 32;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
307 // let newsteam64 = (steamid64 & 18442240478377148415) | instance;
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
308 // println!("http://steamcommunity.com/profiles/{}", newsteam64);
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
309 // }
79c63b4440ee Initial
Glitchvid <Glitchvid@glitchvid.com>
parents:
diff changeset
310 }

mercurial