-
Notifications
You must be signed in to change notification settings - Fork 0
/
SteamGameServerList.cs
133 lines (111 loc) · 4.41 KB
/
SteamGameServerList.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
using System;
using Steamworks;
using UnityEngine;
namespace SharkkitDedicated
{
public class SteamGameServerList
{
public static CSteamID ServerId => SteamGameServer.GetSteamID();
private Callback<SteamServersConnected_t> _connectedCallback;
private Callback<SteamServersDisconnected_t> disconnectedCallback;
private Callback<SteamServerConnectFailure_t> connectFailureCallback;
private Callback<ValidateAuthTicketResponse_t> authTicketResponse;
private Callback<GSPolicyResponse_t> policyResponse;
public bool ConnectedToSteam { get; private set; }
public void Init()
{
if (_connectedCallback != null)
{
return;
}
_connectedCallback = Callback<SteamServersConnected_t>.CreateGameServer(t =>
{
Debug.Log($"Successfully connected to Steam as {ServerId}");
ConnectedToSteam = true;
UpdateGameInformation();
});
disconnectedCallback = Callback<SteamServersDisconnected_t>.CreateGameServer(t =>
{
Debug.Log($"Disconnected from Steam: {t.m_eResult}");
ConnectedToSteam = false;
});
connectFailureCallback = Callback<SteamServerConnectFailure_t>.CreateGameServer(t =>
{
Debug.Log(
$"Failure when trying to connect to Steam: {t.m_eResult}, still trying: {t.m_bStillRetrying}");
});
authTicketResponse = Callback<ValidateAuthTicketResponse_t>.CreateGameServer(t =>
{
Debug.Log($"Auth response! {t.m_eAuthSessionResponse}, id {t.m_SteamID}, owner {t.m_OwnerSteamID}");
});
policyResponse = Callback<GSPolicyResponse_t>.CreateGameServer(t =>
{
// VAC: t.m_bSecure
});
}
public bool RegisterServer(ushort gamePort = 1337, ushort queryPort = 1336, string gsUserToken = null)
{
const int appId = 648800;
Environment.SetEnvironmentVariable("SteamAppId", appId.ToString());
Environment.SetEnvironmentVariable("SteamGameId", appId.ToString());
if (!GameServer.Init(0 /* ANY */, 8765 /* outgoing port */, gamePort, queryPort,
EServerMode.eServerModeAuthenticationAndSecure, "1.0.0.0"))
{
return false;
}
SteamGameServer.SetModDir("spacewar"); // TODO: RaftDedicated? SharkkitDedicated?
SteamGameServer.SetProduct("SharkkitDedicated");
SteamGameServer.SetGameDescription("Sharkkit Dedicated");
if (gsUserToken == null)
{
SteamGameServer.LogOnAnonymous();
}
else
{
SteamGameServer.LogOn(gsUserToken);
}
SteamGameServer.EnableHeartbeats(true);
return true;
}
public EBeginAuthSessionResult BeginAuthentication(byte[] authTicket, CSteamID steamId)
{
return SteamGameServer.BeginAuthSession(authTicket, authTicket.Length, steamId);
}
public void EndAuthentication(CSteamID steamID)
{
SteamGameServer.EndAuthSession(steamID);
}
public void Tick()
{
// TODO: Register callbacks for auth....
if (_connectedCallback == null)
{
return; // Never initialized
}
GameServer.RunCallbacks();
if (ConnectedToSteam)
{
UpdateGameInformation();
}
}
public void UpdateGameInformation()
{
// TODO: First called when steam callback is there? What happens if we call that too early?
SteamGameServer.SetMaxPlayerCount(5);
SteamGameServer.SetPasswordProtected(false);
SteamGameServer.SetServerName("Raft Test Dedicated Server");
SteamGameServer.SetDedicatedServer(true);
SteamGameServer.SetMapName("Pacific Ocean");
SteamGameServer.SetGameTags("sharkkit");
}
public void Shutdown()
{
if (_connectedCallback == null)
{
return; // Never initialized
}
SteamGameServer.LogOff();
GameServer.Shutdown();
}
}
}