using FishNet; using FishNet.Transporting; using Steamworks; using System; using System.Net; using UnityEngine; public class SteamManager : MonoBehaviour { #region Serialized. [Header("Steam Settings")] /// /// Steam application Id. /// [Tooltip("Steam application Id.")] [SerializeField] private uint _steamAppID = 480; /// /// SSteam Mod Directory. /// [Tooltip("Steam Mod Directory.")] [SerializeField] private string _modDir = string.Empty; /// /// Steam Game Description. /// [Tooltip("Steam Game Description.")] [SerializeField] private string _gameDesc = string.Empty; /// /// Steam version. /// [Tooltip("Steam version.")] [SerializeField] private string _version = string.Empty; [Header("Server Settings")] /// /// Servername. /// [Tooltip("Server Name")] [SerializeField] private string _serverName = string.Empty; /// /// Server Password. /// [Tooltip("Server Password.")] [SerializeField] private string _password = string.Empty; /// /// Steam Server Query Port. /// [Tooltip("Server Query Port.")] [SerializeField] private ushort _queryPort = 27016; /// /// Server VAC Secure. /// [Tooltip("Server VAC Secure.")] [SerializeField] private bool _vac = true; /// /// Server as Dedicated Server. /// [Tooltip("Server as Dedicated Server.")] [SerializeField] private bool _ds = true; #endregion void Start() { #if UNITY_SERVER InstanceFinder.ServerManager.StartConnection(); InstanceFinder.ServerManager.OnServerConnectionState += OnServerConnectionState; #endif } private void OnServerConnectionState(FishNet.Transporting.ServerConnectionStateArgs state) { if (state.ConnectionState == FishNet.Transporting.LocalConnectionState.Started) { var serverInit = new SteamServerInit(_modDir, _gameDesc) { IpAddress = IPAddress.Parse(InstanceFinder.TransportManager.Transport.GetServerBindAddress(IPAddressType.IPv4)), GamePort = InstanceFinder.TransportManager.Transport.GetPort(), QueryPort = _queryPort, Secure = _vac, DedicatedServer = _ds, VersionString = _version, }; serverInit.WithRandomSteamPort(); try { SteamServer.Init(1280590, serverInit, true); SteamServer.ServerName = _serverName; SteamServer.MaxPlayers = InstanceFinder.TransportManager.Transport.GetMaximumClients(); SteamServer.Passworded = !string.IsNullOrEmpty(_password); SteamServer.DedicatedServer = _ds; SteamServer.AutomaticHeartbeats = true; SteamServer.LogOnAnonymous(); SteamServer.OnSteamServersConnected += OnSteamServersConnected; SteamServer.OnSteamServersDisconnected += OnSteamServersDisconnected; SteamServer.OnSteamServerConnectFailure += OnSteamServerConnectFailure; if (!SteamServer.IsValid) { Debug.LogWarning("Couldn't initialize server"); return; } } catch (Exception ex) { Debug.LogError("Couldn't initialize Steam server (" + ex.Message + ")"); Application.Quit(); } } } private void OnSteamServersConnected() { Debug.Log("Dedicated Server connected to Steam successfully"); } private void OnSteamServerConnectFailure(Result result, bool stilltrying) { Debug.Log("Dedicated Server failed to connect to Steam"); } private void OnSteamServersDisconnected(Result result) { Debug.Log("Dedicated Server got logged out of Steam"); } }