Skip to content

Commit aad7ff2

Browse files
committed
Add Steam ID type
1 parent f512483 commit aad7ff2

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Configuration/SteamId.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using JetBrains.Annotations;
3+
4+
namespace NFive.SDK.Core.Configuration
5+
{
6+
[PublicAPI]
7+
public class SteamId
8+
{
9+
protected readonly long Value;
10+
11+
public SteamId(long id64)
12+
{
13+
this.Value = id64;
14+
}
15+
16+
public override string ToString() => this.Value.ToString();
17+
18+
public override int GetHashCode() => this.Value.GetHashCode();
19+
20+
public override bool Equals(object obj) => Equals(obj as SteamId);
21+
22+
public bool Equals(SteamId other) => other != null && this.Value == other.Value;
23+
24+
public static implicit operator long(SteamId id) => id.Value;
25+
26+
public static explicit operator SteamId(long l) => new SteamId(l);
27+
28+
public static SteamId FromSteamId32(string id32)
29+
{
30+
if (id32.StartsWith("[") && id32.EndsWith("]")) id32 = id32.TrimStart('[').TrimEnd(']');
31+
if (!id32.StartsWith("U:1:", StringComparison.InvariantCultureIgnoreCase)) throw new ArgumentException("Input not in valid SteamID32 format", nameof(id32));
32+
33+
var steam32 = Convert.ToInt64(id32.Substring(4));
34+
if (steam32 < 1L || steam32 > 9999999999L) throw new ArgumentException("Input not in valid SteamID32 format", nameof(id32));
35+
36+
return new SteamId(steam32 + 76561197960265728L);
37+
}
38+
39+
public static SteamId FromSteamId2(string id2)
40+
{
41+
if (!id2.StartsWith("STEAM_0:", StringComparison.InvariantCultureIgnoreCase)) throw new ArgumentException("Input not in valid SteamID2 format", nameof(id2));
42+
if (!id2.Substring(8).StartsWith("0") && !id2.Substring(8).StartsWith("1")) throw new ArgumentException("Input not in valid SteamID2 format", nameof(id2));
43+
44+
var id = Convert.ToInt64(id2.Substring(10));
45+
if (id < 1L || id > 9999999999L) throw new ArgumentException("Input not in valid SteamID2 format", nameof(id2));
46+
47+
return new SteamId(76561197960265728L + id * 2L + Convert.ToInt64(id2.Substring(8, 1)));
48+
}
49+
}
50+
}

NFive.SDK.Core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
</ItemGroup>
4848
<ItemGroup>
4949
<Compile Include="Configuration\LocaleConfiguration.cs" />
50+
<Compile Include="Configuration\SteamId.cs" />
5051
<Compile Include="Controllers\ControllerConfiguration.cs" />
5152
<Compile Include="Controllers\IControllerConfiguration.cs" />
5253
<Compile Include="Diagnostics\ILogger.cs" />

0 commit comments

Comments
 (0)