Skip to content

Commit

Permalink
Merge branch 'FixSteamIdOnWindowsServer' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Dec 8, 2023
2 parents 1cc9555 + a537be8 commit 98b2b01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
18 changes: 12 additions & 6 deletions managed/CounterStrikeSharp.API.Tests/SteamIDTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace CounterStrikeSharp.API.Tests;
public class SteamIdTests
{
[Theory]
[InlineData(76561197960524373ul, "STEAM_0:1:129322", "[U:1:258645]", 258645, SteamAccountType.Individual, SteamAccountInstance.Desktop, SteamAccountUniverse.Public, true)]
[InlineData(0ul, "STEAM_0:0:9185091437874642944", "[I:0:18370182875749285888]", 0, SteamAccountType.Invalid, SteamAccountInstance.All, SteamAccountUniverse.Unspecified, false)]
[InlineData(76561197960265728ul, "STEAM_0:0:0", "[U:1:0]", 0, SteamAccountType.Individual, SteamAccountInstance.Desktop, SteamAccountUniverse.Public, false)]
[InlineData(103582791429521412ul, "STEAM_0:0:13510796734627842", "[g:1:27021593469255684]", 4, SteamAccountType.Clan, SteamAccountInstance.All, SteamAccountUniverse.Public, true)]
public void ValidateSteamId(ulong steamId64, string steamId2, string steamId3, int steamId32, SteamAccountType accountType, SteamAccountInstance accountInstance, SteamAccountUniverse accountUniverse, bool valid)
[InlineData(76561197960524373ul, 76561197960524373ul, "STEAM_0:1:129322", "[U:1:258645]", 258645, SteamAccountType.Individual, SteamAccountInstance.Desktop, SteamAccountUniverse.Public, true)]
[InlineData(258645, 76561197960524373ul, "STEAM_0:1:129322", "[U:1:258645]", 258645, SteamAccountType.Individual, SteamAccountInstance.Desktop, SteamAccountUniverse.Public, true)]
[InlineData(76561197960265728ul, 76561197960265728ul, "STEAM_0:0:0", "[U:1:0]", 0, SteamAccountType.Individual, SteamAccountInstance.Desktop, SteamAccountUniverse.Public, false)]
[InlineData(103582791429521412ul, 103582791429521412ul, "STEAM_0:0:13510796734627842", "[g:1:27021593469255684]", 4, SteamAccountType.Clan, SteamAccountInstance.All, SteamAccountUniverse.Public, true)]
public void ValidateSteamId(ulong parseValue, ulong steamId64, string steamId2, string steamId3, int steamId32, SteamAccountType accountType, SteamAccountInstance accountInstance, SteamAccountUniverse accountUniverse, bool valid)
{
var steamId = new SteamID(steamId64);
var steamId = new SteamID(parseValue);

Assert.Equal(steamId64, steamId.SteamId64);
Assert.Equal(steamId2, steamId.SteamId2);
Expand Down Expand Up @@ -43,4 +43,10 @@ public void CanUseValueEquality()
Assert.True(steamId1 == steamId2);
Assert.True(steamId1 != steamId3);
}

[Fact]
public void ThrowsOutOfRangeException()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new SteamID(0));
}
}
49 changes: 27 additions & 22 deletions managed/CounterStrikeSharp.API/Modules/Entities/SteamID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ public class SteamID : IEquatable<SteamID>
const long Base = 76561197960265728;
public ulong SteamId64 { get; set; }

public SteamID(ulong id) => SteamId64 = id;
public SteamID(string id) => SteamId64 = id.StartsWith("[") ? ParseId3(id) : ParseId(id);
public SteamID(ulong id)
{
if (id <= 0) throw new ArgumentOutOfRangeException(nameof(id));
SteamId64 = id >= Base ? id : id + Base;
}

public SteamID(string id) : this(id.StartsWith('[') ? ParseId3(id) : ParseId(id)) { }

public static explicit operator SteamID(ulong u) => new(u);
public static explicit operator SteamID(string s) => new(s);

ulong ParseId(string id)
static ulong ParseId(string id)
{
var parts = id.Split(':');
if (parts.Length != 3 || !ulong.TryParse(parts[2], out var num)) throw new FormatException();
return Base + num * 2 + (parts[1] == "1" ? 1UL : 0);
return Base + (num * 2) + (parts[1] == "1" ? 1UL : 0);
}

ulong ParseId3(string id)
static ulong ParseId3(string id)
{
var parts = id.Replace("[", "").Replace("]", "").Split(':');
if (parts.Length != 3 || !ulong.TryParse(parts[2], out var num)) throw new FormatException();
Expand All @@ -39,34 +44,34 @@ public string SteamId3
get => $"[{EnumUtils.GetEnumMemberAttributeValue(AccountType)}:{(int)AccountUniverse}:{SteamId64 - Base}]";
set => SteamId64 = ParseId3(value);
}

public int SteamId32
{
get => (int)(SteamId64 - Base);
set => SteamId64 = (ulong)value + Base;
}

public int AccountId => (int)((SteamId64 >> 0) & 0xFFFFFFFF);
public int AccountId => (int)(SteamId64 & 0xFFFFFFFF);

public SteamAccountInstance AccountInstance =>
public SteamAccountInstance AccountInstance =>
(SteamAccountInstance)((SteamId64 >> 32) & 0xFFFFF);
public SteamAccountType AccountType =>

public SteamAccountType AccountType =>
(SteamAccountType)((SteamId64 >> 52) & 0xF);

public SteamAccountUniverse AccountUniverse =>
public SteamAccountUniverse AccountUniverse =>
(SteamAccountUniverse)((SteamId64 >> 56) & 0xF);

public bool IsValid()
{
if (AccountUniverse == SteamAccountUniverse.Unspecified
|| AccountType == SteamAccountType.Invalid
if (AccountUniverse == SteamAccountUniverse.Unspecified
|| AccountType == SteamAccountType.Invalid
|| AccountInstance == SteamAccountInstance.Invalid)
return false;
if (AccountType == SteamAccountType.Individual
if (AccountType == SteamAccountType.Individual
&& (AccountId == 0 || AccountInstance != SteamAccountInstance.Desktop))
return false;
if (AccountType == SteamAccountType.Clan
if (AccountType == SteamAccountType.Clan
&& (AccountId == 0 || AccountInstance != SteamAccountInstance.All))
return false;
if (AccountType == SteamAccountType.GameServer && AccountId == 0)
Expand All @@ -78,12 +83,12 @@ public bool IsValid()

public Uri ToCommunityUrl()
{
string url = string.Empty;
if (AccountType == SteamAccountType.Individual)
url = "https://steamcommunity.com/profiles/" + SteamId64;
if (AccountType == SteamAccountType.Clan)
url = "https://steamcommunity.com/gid/" + SteamId64;
return new Uri(url);
return AccountType switch
{
SteamAccountType.Individual => new Uri("https://steamcommunity.com/profiles/" + SteamId64),
SteamAccountType.Clan => new Uri("https://steamcommunity.com/gid/" + SteamId64),
_ => new Uri(string.Empty),
};
}

public bool Equals(SteamID? other)
Expand All @@ -96,7 +101,7 @@ public override bool Equals(object? obj)
if (obj?.GetType() != this.GetType()) return false;
return Equals((SteamID)obj);
}

public static bool TryParse(string s, out SteamID? steamId)
{
try
Expand Down

0 comments on commit 98b2b01

Please sign in to comment.