Skip to content

Commit defc57c

Browse files
committed
✨ SteamIdConvert
1 parent 087bd18 commit defc57c

File tree

3 files changed

+152
-6
lines changed

3 files changed

+152
-6
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
namespace BD.SteamClient.Helpers;
2+
3+
public class SteamIdConvert
4+
{
5+
// Usage:
6+
// - SteamIdConvert sid = new SteamIdConvert("STEAM_0:0:52161201");
7+
// string Steam64 = sid.Id64;
8+
9+
#pragma warning disable SA1132 // Do not combine fields
10+
private string? Id = "STEAM_0:", Id3 = "U:1:", Id32, Id64;
11+
12+
private string? _input;
13+
private byte _inputType;
14+
15+
private static readonly char[] Sid3Strings = { 'U', 'I', 'M', 'G', 'A', 'P', 'C', 'g', 'T', 'L', 'C', 'a' };
16+
private const byte SteamId = 1, SteamId3 = 2, SteamId32 = 3, SteamId64 = 4;
17+
public const long UndefinedId = 76561197960265728;
18+
#pragma warning restore SA1132 // Do not combine fields
19+
20+
public SteamIdConvert(string anySteamId)
21+
{
22+
try
23+
{
24+
GetIdType(anySteamId);
25+
ConvertAll();
26+
}
27+
catch (Exception ex)
28+
{
29+
Log.Error(nameof(SteamIdConvert), ex, "Convert Error");
30+
}
31+
}
32+
33+
private void GetIdType(string sInput)
34+
{
35+
_input = sInput;
36+
if (_input[0] == 'S')
37+
{
38+
_inputType = 1; // SteamID
39+
}
40+
else if (Sid3Strings.Contains(_input[0]))
41+
{
42+
_inputType = 2; // SteamID3
43+
}
44+
else if (char.IsNumber(_input[0]))
45+
{
46+
_inputType = _input.Length switch
47+
{
48+
< 17 => 3,
49+
17 => 4,
50+
_ => _inputType
51+
};
52+
}
53+
else
54+
{
55+
throw new Exception("Input SteamID was not recognised!");
56+
}
57+
}
58+
59+
private static string GetOddity(string input)
60+
{
61+
return (int.Parse(input) % 2).ToString();
62+
}
63+
64+
private static string FloorDivide(string sIn, double divIn)
65+
{
66+
return Math.Floor(int.Parse(sIn) / divIn).ToString(CultureInfo.InvariantCulture);
67+
}
68+
69+
private void CalcSteamId()
70+
{
71+
if (_inputType == SteamId)
72+
{
73+
Id = _input;
74+
}
75+
else
76+
{
77+
var s = _inputType switch
78+
{
79+
SteamId3 => _input[4..],
80+
SteamId32 => _input,
81+
SteamId64 => CalcSteamId32(),
82+
_ => ""
83+
};
84+
85+
Id += GetOddity(s) + ":" + FloorDivide(s, 2);
86+
}
87+
}
88+
89+
private void CalcSteamId3()
90+
{
91+
if (_inputType == SteamId3)
92+
Id3 = _input;
93+
else
94+
Id3 += CalcSteamId32();
95+
Id3 = $"[{Id3}]";
96+
}
97+
98+
private string CalcSteamId32()
99+
{
100+
if (_inputType == SteamId32)
101+
{
102+
Id32 = _input;
103+
}
104+
else
105+
{
106+
Id32 = _inputType switch
107+
{
108+
SteamId => (int.Parse(_input[10..]) * 2 + int.Parse($"{_input[8]}")).ToString(),
109+
SteamId3 => _input[4..],
110+
SteamId64 => (long.Parse(_input) - UndefinedId).ToString(),
111+
_ => Id32
112+
};
113+
}
114+
115+
return Id32;
116+
}
117+
118+
private void CalcSteamId64()
119+
{
120+
if (_inputType == SteamId64)
121+
Id64 = _input;
122+
else
123+
Id64 = _inputType switch
124+
{
125+
SteamId => (int.Parse(_input[10..]) * 2 + int.Parse($"{_input[8]}") + UndefinedId).ToString(),
126+
SteamId3 => (int.Parse(_input[4..]) + UndefinedId).ToString(),
127+
SteamId32 => (int.Parse(_input) + UndefinedId).ToString(),
128+
_ => Id64
129+
};
130+
}
131+
132+
public void ConvertAll()
133+
{
134+
CalcSteamId();
135+
CalcSteamId3();
136+
_ = CalcSteamId32();
137+
CalcSteamId64();
138+
}
139+
}

src/BD.SteamClient/Services.Implementation/SteamServiceImpl.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public List<SteamUser> GetRememberUserList()
255255
return users;
256256
}
257257

258-
public bool UpdateAuthorizedDeviceList(IEnumerable<AuthorizedDevice> model)
258+
public bool UpdateAuthorizedDeviceList(IEnumerable<AuthorizedDevice> items)
259259
{
260260
var authorizeds = new List<AuthorizedDevice>();
261261
try
@@ -264,7 +264,7 @@ public bool UpdateAuthorizedDeviceList(IEnumerable<AuthorizedDevice> model)
264264
{
265265
var v = VdfHelper.Read(ConfigVdfPath);
266266
var lists = new KVCollectionValue();
267-
foreach (var item in model.OrderBy(x => x.Index))
267+
foreach (var item in items.OrderBy(x => x.Index))
268268
{
269269
KVObject itemTemp = new KVObject(item.SteamId3_Int.ToString(), new KVObject[]
270270
{
@@ -426,7 +426,7 @@ public void DeleteLocalUserData(SteamUser user, bool isDeleteUserData = false)
426426
}
427427
}
428428

429-
public void UpdateLocalUserData(IEnumerable<SteamUser> users)
429+
public void UpdateLocalUserData(params SteamUser[] users)
430430
{
431431
if (string.IsNullOrWhiteSpace(UserVdfPath) || !File.Exists(UserVdfPath))
432432
{
@@ -446,7 +446,7 @@ public void UpdateLocalUserData(IEnumerable<SteamUser> users)
446446
if (itemUser == null)
447447
{
448448
item["MostRecent"] = 0;
449-
break;
449+
continue;
450450
}
451451
item["MostRecent"] = Convert.ToInt16(itemUser.MostRecent);
452452
item["WantsOfflineMode"] = Convert.ToInt16(itemUser.WantsOfflineMode);

src/BD.SteamClient/Services/ISteamService.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public partial interface ISteamService
7777
/// <returns></returns>
7878
List<SteamUser> GetRememberUserList();
7979

80-
bool UpdateAuthorizedDeviceList(IEnumerable<AuthorizedDevice> list);
80+
bool UpdateAuthorizedDeviceList(IEnumerable<AuthorizedDevice> model);
8181

8282
bool RemoveAuthorizedDeviceList(AuthorizedDevice list);
8383

@@ -93,9 +93,16 @@ public partial interface ISteamService
9393
/// <param name="userName"></param>
9494
void SetCurrentUser(string userName);
9595

96+
/// <summary>
97+
/// Sets whether the user is invisible or not
98+
/// </summary>
99+
/// <param name="steamId32">SteamID of user to update</param>
100+
/// <param name="ePersonaState">Persona state enum for user (0-7)</param>
101+
void SetPersonaState(string steamId32, PersonaState ePersonaState);
102+
96103
void DeleteLocalUserData(SteamUser user, bool isDeleteUserData = false);
97104

98-
void UpdateLocalUserData(IEnumerable<SteamUser> users);
105+
void UpdateLocalUserData(params SteamUser[] users);
99106

100107
void WatchLocalUserDataChange(Action changedAction);
101108

0 commit comments

Comments
 (0)