using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace Facepunch.Steamworks { public partial class Server : BaseSteamworks { /// /// Current user's Username /// public string Username { get; private set; } /// /// Current user's SteamId /// public ulong SteamId { get; private set; } internal override bool IsGameServer { get { return true; } } public Server( uint appId, uint IpAddress, ushort GamePort, ushort QueryPort, bool Secure, string VersionString ) { Valve.Interop.NativeEntrypoints.Extended.SteamInternal_GameServer_Init( IpAddress, 0, GamePort, QueryPort, Secure ? 3 : 2, VersionString ); native = new Interop.NativeInterface(); // // Get other interfaces // if ( !native.InitServer() ) { native.Dispose(); native = null; return; } // // Set up warning hook callback // SteamAPIWarningMessageHook ptr = InternalOnWarning; var d = Marshal.GetFunctionPointerForDelegate( ptr ); var rr = GCHandle.Alloc( d ); native.utils.SetWarningMessageHook( d ); // // Setup interfaces that client and server both have // SetupCommonInterfaces(); // // Cache common, unchanging info // AppId = appId; // // Initial settings // native.gameServer.EnableHeartbeats( true ); MaxPlayers = 32; BotCount = 0; MapName = "unset"; // // Run update, first call does some initialization // Update(); } /// /// Initialize a server - query port will use the same as GamePort (MASTERSERVERUPDATERPORT_USEGAMESOCKETSHARE) /// public Server( uint appId, uint IpAddress, ushort GamePort, bool Secure, string VersionString ) : this( appId, IpAddress, GamePort, 0xFFFF, Secure, VersionString ) { } [UnmanagedFunctionPointer( CallingConvention.Cdecl )] public delegate void SteamAPIWarningMessageHook( int nSeverity, string pchDebugText ); private void InternalOnWarning( int nSeverity, string text ) { if ( OnMessage != null ) { OnMessage( ( MessageType)nSeverity, text.ToString() ); } Console.WriteLine( "STEAM: {0}", text ); } /// /// Should be called at least once every frame /// public override void Update() { if ( !IsValid ) return; Valve.Interop.NativeEntrypoints.Extended.SteamGameServer_RunCallbacks(); base.Update(); } /// /// Gets or sets the current MaxPlayers. /// This doesn't enforce any kind of limit, it just updates the master server. /// public int MaxPlayers { get { return _maxplayers; } set { if ( _maxplayers == value ) return; native.gameServer.SetMaxPlayerCount( value ); _maxplayers = value; } } private int _maxplayers = 0; /// /// Gets or sets the current BotCount. /// This doesn't enforce any kind of limit, it just updates the master server. /// public int BotCount { get { return _botcount; } set { if ( _botcount == value ) return; native.gameServer.SetBotPlayerCount( value ); _botcount = value; } } private int _botcount = 0; /// /// Gets or sets the current Map Name. /// public string MapName { get { return _mapname; } set { if ( _mapname == value ) return; native.gameServer.SetMapName( value ); _mapname = value; } } private string _mapname; /// /// Gets or sets the current ModDir /// public string ModDir { get { return _modDir; } set { if ( _modDir == value ) return; native.gameServer.SetModDir( value ); _modDir = value; } } private string _modDir = ""; /// /// Gets or sets the current Product /// public string Product { get { return _product; } set { if ( _product == value ) return; native.gameServer.SetProduct( value ); _product = value; } } private string _product = ""; /// /// Gets or sets the current Product /// public string GameDescription { get { return _gameDescription; } set { if ( _gameDescription == value ) return; native.gameServer.SetGameDescription( value ); _gameDescription = value; } } private string _gameDescription = ""; /// /// Gets or sets the current ServerName /// public string ServerName { get { return _serverName; } set { if ( _serverName == value ) return; native.gameServer.SetServerName( value ); _serverName = value; } } private string _serverName = ""; /// /// Gets or sets the current Passworded /// public bool Passworded { get { return _passworded; } set { if ( _passworded == value ) return; native.gameServer.SetPasswordProtected( value ); _passworded = value; } } private bool _passworded; /// /// Gets or sets the current GameTags /// public string GameTags { get { return _gametags; } set { if ( _gametags == value ) return; native.gameServer.SetGameTags( value ); _gametags = value; } } private string _gametags = ""; /// /// Log onto Steam anonymously /// public void LogOnAnonymous() { native.gameServer.LogOnAnonymous(); } Dictionary KeyValue = new Dictionary(); /// /// Sets a Key Value /// public void SetKey( string Key, string Value ) { if ( KeyValue.ContainsKey( Key ) ) { if ( KeyValue[Key] == Value ) return; KeyValue[Key] = Value; } else { KeyValue.Add( Key, Value ); } native.gameServer.SetKeyValue( Key, Value ); } public void UpdatePlayer( ulong steamid, string name, int score ) { native.gameServer.BUpdateUserData( steamid, name, (uint) score ); } public bool LoggedOn { get { return native.gameServer.BLoggedOn(); } } } }