Skip to content

Commit

Permalink
Add Friend.Nickname to get Steam nicknames for players
Browse files Browse the repository at this point in the history
Expose FriendGameInfo.GameID so you can see what appID friends are playing
  • Loading branch information
Rohansi committed Dec 10, 2024
1 parent ee6572a commit 520ac1c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 19 deletions.
23 changes: 21 additions & 2 deletions Facepunch.Steamworks/Structs/Friend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override string ToString()
/// <summary>
/// Return true if this user is playing the game we're running
/// </summary>
public bool IsPlayingThisGame => GameInfo?.GameID == SteamClient.AppId;
public bool IsPlayingThisGame => GameInfo?.GameID is { Type: GameIdType.App } && GameInfo.Value.GameID.AppId == SteamClient.AppId;

/// <summary>
/// Returns true if this friend is online
Expand Down Expand Up @@ -75,7 +75,26 @@ public async Task RequestInfoAsync()

public Relationship Relationship => SteamFriends.Internal.GetFriendRelationship( Id );
public FriendState State => SteamFriends.Internal.GetFriendPersonaState( Id );

/// <summary>
/// Returns the player's current Steam name.
/// <remarks>
/// Steam returns nicknames here if "Append nicknames to friends' names" is disabled in the Steam client.
/// </remarks>
/// </summary>
public string Name => SteamFriends.Internal.GetFriendPersonaName( Id );

/// <summary>
/// Returns the nickname that was set for this Steam player, if any.
/// <remarks>
/// Steam will never return nicknames if "Append nicknames to friends' names" is disabled in the Steam client.
/// </remarks>
/// </summary>
public string Nickname => SteamFriends.Internal.GetPlayerNickname( Id );

/// <summary>
/// Returns the player's Steam name history.
/// </summary>
public IEnumerable<string> NameHistory
{
get
Expand Down Expand Up @@ -114,10 +133,10 @@ public bool IsIn( SteamId group_or_room )

public struct FriendGameInfo
{
internal ulong GameID; // m_gameID class CGameID
internal uint GameIP; // m_unGameIP uint32
internal ulong SteamIDLobby; // m_steamIDLobby class CSteamID

public GameId GameID;
public int ConnectionPort;
public int QueryPort;

Expand Down
75 changes: 58 additions & 17 deletions Facepunch.Steamworks/Structs/GameId.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;


namespace Steamworks.Data
{
public struct GameId
public enum GameIdType : byte
{
App = 0,
GameMod = 1,
Shortcut = 2,
P2P = 3,
}

public struct GameId : IEquatable<GameId>
{
// TODO - Be able to access these vars

/*
enum EGameIDType
{
k_EGameIDTypeApp = 0,
k_EGameIDTypeGameMod = 1,
k_EGameIDTypeShortcut = 2,
k_EGameIDTypeP2P = 3,
};
# ifdef VALVE_BIG_ENDIAN
unsigned int m_nModID : 32;
unsigned int m_nType : 8;
Expand All @@ -30,8 +23,31 @@ enum EGameIDType
unsigned int m_nModID : 32;
#endif
*/

// 0xAAAAAAAA_BBCCCCCC
// A = m_nModID
// B = m_nType
// C = m_nAppID
public ulong Value;

public GameIdType Type
{
get => (GameIdType)(byte)( Value >> 24 );
set => Value = ( Value & 0xFFFFFFFF_00FFFFFF ) | ( (ulong)(byte)value << 24 );
}

public uint AppId
{
get => (uint)( Value & 0x00000000_00FFFFFF );
set => Value = ( Value & 0xFFFFFFFF_FF000000 ) | (value & 0x00000000_00FFFFFF);
}

public uint ModId
{
get => (uint)( Value >> 32 );
set => Value = ( Value & 0x00000000_FFFFFFFF ) | ( (ulong)value << 32 );
}

public static implicit operator GameId( ulong value )
{
return new GameId { Value = value };
Expand All @@ -41,5 +57,30 @@ public static implicit operator ulong( GameId value )
{
return value.Value;
}

public bool Equals(GameId other)
{
return Value == other.Value;
}

public override bool Equals(object obj)
{
return obj is GameId other && Equals(other);
}

public override int GetHashCode()
{
return Value.GetHashCode();
}

public static bool operator ==(GameId left, GameId right)
{
return left.Equals(right);
}

public static bool operator !=(GameId left, GameId right)
{
return !left.Equals(right);
}
}
}
}

0 comments on commit 520ac1c

Please sign in to comment.