mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-04-24 16:29:13 -04:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
102 lines
2.5 KiB
C#
102 lines
2.5 KiB
C#
using NativeEngine;
|
|
using Sandbox.Network;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Sandbox;
|
|
|
|
/// <summary>
|
|
/// Rich Presence System for Steam
|
|
/// </summary>
|
|
internal sealed class SteamRichPresenceSystem : IRichPresenceSystem
|
|
{
|
|
Dictionary<string, string> _values = new();
|
|
|
|
void SetValue( string key, string value )
|
|
{
|
|
if ( _values.TryGetValue( key, out var oldValue ) && oldValue == value )
|
|
return;
|
|
|
|
Steam.SteamFriends().SetRichPresence( key, value );
|
|
|
|
_values[key] = value;
|
|
}
|
|
|
|
void Clear()
|
|
{
|
|
Steam.SteamFriends().ClearRichPresence();
|
|
_values.Clear();
|
|
}
|
|
|
|
void IRichPresenceSystem.Poll()
|
|
{
|
|
if ( PartyRoom.Current is PartyRoom party )
|
|
{
|
|
SetValue( "party_id", $"{party.Id}" );
|
|
}
|
|
else
|
|
{
|
|
SetValue( "party_id", null );
|
|
}
|
|
|
|
SetValue( "in_editor", Application.IsEditor ? "1" : "0" );
|
|
|
|
if ( IGameInstance.Current is null )
|
|
{
|
|
SetValue( "steam_display", "#StatusMenu" );
|
|
SetValue( "gametitle", null );
|
|
SetValue( "steam_player_group", null );
|
|
SetValue( "steam_player_group_size", null );
|
|
SetValue( "lobby", null );
|
|
SetValue( "connect", null );
|
|
return;
|
|
}
|
|
|
|
if ( Networking.IsActive )
|
|
{
|
|
var gameLobby = Networking.System.Sockets
|
|
.OfType<SteamLobbySocket>()
|
|
.FirstOrDefault();
|
|
|
|
if ( gameLobby is not null )
|
|
{
|
|
SetValue( "connect", $"+connect {gameLobby.LobbySteamId}" );
|
|
SetValue( "steam_player_group", $"{gameLobby.LobbySteamId}" );
|
|
SetValue( "steam_player_group_size", $"{gameLobby.LobbyMemberCount}" );
|
|
}
|
|
else
|
|
{
|
|
var connection = Networking.System.Connection;
|
|
var playerCount = Connection.All.Count( c => c.State == Connection.ChannelState.Connected );
|
|
|
|
if ( connection is SteamNetwork.IpConnection or SteamNetwork.IdConnection )
|
|
{
|
|
SetValue( "connect", $"+connect {connection.Address}" );
|
|
SetValue( "steam_player_group", $"{connection.Address}" );
|
|
SetValue( "steam_player_group_size", $"{playerCount}" );
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SetValue( "connect", null );
|
|
SetValue( "steam_player_group", null );
|
|
SetValue( "steam_player_group_size", null );
|
|
}
|
|
|
|
if ( IGameInstance.Current.Package is Package gamePackage )
|
|
{
|
|
SetValue( "steam_display", "#StatusGame" );
|
|
SetValue( "gametitle", gamePackage.Title );
|
|
SetValue( "map", "" );
|
|
SetValue( "gamename", Application.GameIdent );
|
|
}
|
|
else
|
|
{
|
|
SetValue( "steam_display", "#StatusGame" );
|
|
SetValue( "gametitle", "Secret Game" );
|
|
SetValue( "map", "" );
|
|
SetValue( "gamename", Application.GameIdent );
|
|
}
|
|
}
|
|
}
|