This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SteamManager.cs
143 lines (124 loc) · 4.04 KB
/
SteamManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using FishNet;
using Steamworks;
using System;
using System.Net;
using UnityEngine;
public class SteamManager : MonoBehaviour
{
#region Serialized.
[Header("Steam Settings")]
/// <summary>
/// Steam application Id.
/// </summary>
[Tooltip("Steam application Id.")]
[SerializeField]
private uint _steamAppID = 480;
/// <summary>
/// SSteam Mod Directory.
/// </summary>
[Tooltip("Steam Mod Directory.")]
[SerializeField]
private string _modDir = string.Empty;
/// <summary>
/// Steam Game Description.
/// </summary>
[Tooltip("Steam Game Description.")]
[SerializeField]
private string _gameDesc = string.Empty;
/// <summary>
/// Steam version.
/// </summary>
[Tooltip("Steam version.")]
[SerializeField]
private string _version = string.Empty;
[Header("Server Settings")]
/// <summary>
/// Servername.
/// </summary>
[Tooltip("Server Name")]
[SerializeField]
private string _serverName = string.Empty;
/// <summary>
/// Server Password.
/// </summary>
[Tooltip("Server Password.")]
[SerializeField]
private string _password = string.Empty;
/// <summary>
/// Steam Server Query Port.
/// </summary>
[Tooltip("Server Query Port.")]
[SerializeField]
private ushort _queryPort = 27016;
/// <summary>
/// Server VAC Secure.
/// </summary>
[Tooltip("Server VAC Secure.")]
[SerializeField]
private bool _vac = true;
/// <summary>
/// Server as Dedicated Server.
/// </summary>
[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.LocalConnectionStates.Started)
{
var serverInit = new SteamServerInit(_modDir, _gameDesc)
{
IpAddress = IPAddress.Parse(InstanceFinder.TransportManager.Transport.GetServerBindAddress()),
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");
}
}