Welcome to mirror list, hosted at ThFree Co, Russian Federation.

steamworks_matchmaking.cpp « Steam « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58d5e869d151189cf2b00491b799cb05c3af0e89 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//-----------------------------------------------------------------------------
//           Name: steamworks_matchmaking.cpp
//      Developer: Wolfire Games LLC
//    Description: Steamworks wrapper for Overgrowth, to simplify use and
//                 minimize state.
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "steamworks_matchmaking.h"

#include <Online/online.h>
#include <Internal/config.h>
#include <Logging/logdata.h>
#include <Steam/steamworks_util.h>

#if ENABLE_STEAMWORKS
#include <isteamnetworkingutils.h>
#endif

#include <algorithm>


static const char* LEVEL_PATH = "levelpath";

#if ENABLE_STEAMWORKS
const char *SteamworksMatchmaking::StatusToString(ESteamNetworkingConnectionState state) {
    switch (state) {
    case k_ESteamNetworkingConnectionState_None:
        return "None";
        break;

    case k_ESteamNetworkingConnectionState_Connecting:
        return "Connecting";
        break;

    case k_ESteamNetworkingConnectionState_FindingRoute:
        return "FindingRoute";
        break;

    case k_ESteamNetworkingConnectionState_Connected:
        return "Connected";
        break;

    case k_ESteamNetworkingConnectionState_ClosedByPeer:
        return "ClosedByPeer";
        break;

    case k_ESteamNetworkingConnectionState_ProblemDetectedLocally:
        return "ProblemDetectedLocally";
        break;

    default:
        return "impossible internal state";
        break;

    }

    return "unknown";
}


SteamworksMatchmaking::SteamworksMatchmaking() {
    // initializing relay access on startup helps to start multiplayer faster
    // but if the game is closed before it has finished initializing
    // it will crash inside steam libraries
    // so we don't do it
    // FIXME: enable if steam fixes their lib
#if 0
    LOGI << "Initializing steam relay network access ..." << std::endl;
    SteamNetworkingUtils()->InitRelayNetworkAccess();
#endif  // 0
}


SteamworksMatchmaking::~SteamworksMatchmaking() {
    // if we free these it can cause crashes on exit on linux
    // apparently steam api doesn't properly wait for its internal threads
    // to shut down

    if (!lobbies.empty()) {
        LOGW << lobbies.size() << " lobbies remain at shutdown" << std::endl;
#if 0
        for (unsigned int i = 0; i < lobbies.size(); i++) {
            SteamMatchmaking()->LeaveLobby(lobbies[i]);
        }
#endif  // 0

        lobbies.clear();
    }
}


HSteamListenSocket SteamworksMatchmaking::ActivateGameOverlayInviteDialog() {
    LOGI << "SteamworksMatchmaking::ActivateGameOverlayInviteDialog()" << std::endl;

    SteamMatchmaking()->CreateLobby(k_ELobbyTypePrivate, 10);

    ISteamNetworkingSockets *isns = SteamNetworkingSockets();
    assert(isns);

    HSteamListenSocket listen = isns->CreateListenSocketP2P(0, 0, nullptr);
 
    return listen;
}

void SteamworksMatchmaking::OpenInviteDialog()
{
	if (lobbies.begin() != lobbies.end()) {
		if (SteamMatchmaking != nullptr) {
			ISteamFriends * friends = SteamFriends();
			if (friends) {
				friends->ActivateGameOverlayInviteDialog(*lobbies.begin());
			}
		 }
	}

}


void SteamworksMatchmaking::JoinLobby(const std::string &lobbyIDStr) {
    LOGI << "SteamworksMatchmaking::JoinLobby " << lobbyIDStr << std::endl;

    char *end   = NULL;
    // We presume uint64 to be the same as the unsigned long returned by strtoul
    uint64 l    = strtoul(lobbyIDStr.c_str(), &end, 10);

    CSteamID lobbyID(l);

    LOGI << "parsed lobby id: \"" << lobbyID.ConvertToUint64() << "\"" << std::endl;

    SteamMatchmaking()->JoinLobby(lobbyID);
}


void SteamworksMatchmaking::LeaveLobby(CSteamID lobbyID) {
    SteamMatchmaking()->LeaveLobby(lobbyID);
    auto it = std::find(lobbies.begin(), lobbies.end(), lobbyID);
    if (it != lobbies.end()) {
        lobbies.erase(it);
    }
}


void SteamworksMatchmaking::OnLobbyEnter(LobbyEnter_t *data) {
    Online* online = Online::Instance();

    assert(data);

    LOGI << "OnLobbyEnter " << data->m_ulSteamIDLobby << std::endl;

    lobbies.insert(data->m_ulSteamIDLobby);

    if (online->IsHosting()) {
        // we are the host
        // set lobby metadata
        SteamMatchmaking()->SetLobbyData(data->m_ulSteamIDLobby, LEVEL_PATH, "");

        // activate overlay
        SteamFriends()->ActivateGameOverlayInviteDialog(data->m_ulSteamIDLobby);
    } else {
        // we are client
        // get lobby metadata 

        // Start multiplayer with host
        CSteamID lobbyHost = SteamMatchmaking()->GetLobbyOwner(data->m_ulSteamIDLobby);
        ISteamNetworkingSockets *isns = SteamNetworkingSockets();
        assert(isns);

        SteamNetworkingIdentity id;
        id.SetSteamID(lobbyHost);

        HSteamNetConnection connection = isns->ConnectP2P(id, 0, 0, nullptr);
        std::vector<char> temp(1024, 0);
        int ret = isns->GetDetailedConnectionStatus(connection, &temp[0], temp.size());
        if (ret >= 0) {
            LOGI << "connection status: " << &temp[0] << std::endl;
        }
        else {
            LOGE << "GetDetailedConnectionStatus failed" << std::endl;
        }
        steamPendingConnections.insert(connection);

        // Leave lobby - maybe not?
        LeaveLobby(data->m_ulSteamIDLobby);

		if (!online->IsActive())
			online->StartMultiplayer(MultiplayerMode::Client);
    }
}


void SteamworksMatchmaking::OnLobbyDataUpdate(LobbyDataUpdate_t *data) {
    assert(data);

    LOGI << "OnLobbyDataUpdate " << data->m_ulSteamIDLobby << std::endl;

	if (data->m_bSuccess) {
		LOGI << "Transition was a success" << std::endl;
	}
 }


void SteamworksMatchmaking::OnLobbyJoinRequested(GameLobbyJoinRequested_t *data) {
    assert(data);

    LOGI << "OnLobbyJoinRequested" << std::endl;

    SteamMatchmaking()->JoinLobby(data->m_steamIDLobby);
}


void SteamworksMatchmaking::OnLobbyChatUpdate(LobbyChatUpdate_t *data) {
    Online* online = Online::Instance();
    assert(data);

    LOGI << "OnLobbyChatUpdate " << data->m_ulSteamIDLobby << std::endl;
    if (data->m_rgfChatMemberStateChange & k_EChatMemberStateChangeEntered) {
        LOGI << "User " << data->m_ulSteamIDUserChanged << " joined lobby" << std::endl;
        if (online->IsHosting()) {
            // Friend accepted invite
            // Leave lobby
            // For single player mode host should maybe stay in lobby and only clients leave as they enter game
            //LeaveLobby(data->m_ulSteamIDLobby);
            // Steam doesn't offer way to close overlay from app, user needs to do that
        }
    } else {
        LOGI << "User " << data->m_ulSteamIDUserChanged << " left lobby" << std::endl;
    }
}


void SteamworksMatchmaking::OnConnectionChange(SteamNetConnectionStatusChangedCallback_t *data) {

	ISteamNetworkingSockets *isns = SteamNetworkingSockets();

    assert(data);
    LOGI << "SteamworksMatchmaking::OnConnectionChange " << data->m_hConn << " old:" << StatusToString(data->m_eOldState) <<  "  new: " << StatusToString(data->m_info.m_eState) << std::endl;



	/*/
	switch (data->m_info.m_eState) {
		case k_ESteamNetworkingConnectionState_Connecting: {
			//EResult result = isns->AcceptConnection(data->m_hConn);
			break;
		}

		default: {

		}
	}
	*/


    auto it = steamPendingConnections.find(data->m_hConn);
    if (it != steamPendingConnections.end()) {
        // This is a Steam connection
        LOGI << "Steam connection" << std::endl;
        steamPendingConnections.erase(it);
    }
}


std::string SteamworksMatchmaking::GetSteamNickname(const CSteamID &friendID) {
    std::string name = "<unknown>";

    ISteamFriends *friends = SteamFriends();
    assert(friends);

    if (friendID.IsValid()) {
        // query for friend nickname, might not exist
        const char *nickname = friends->GetPlayerNickname(friendID);
        if (nickname) {
            name = nickname;
        } else {
            // TODO: might not exist immediately when joining lobby
            //  need to wait for PersonaStateChange_t
            // when not exists guaranteed to be non-NULL but possibly empty
            name = friends->GetFriendPersonaName(friendID);
        }
    }

    return name;
}
#endif