Skip to content

Commit 367234a

Browse files
authored
Lobby Connect Improvements
-Removed active user list to prioritize multiplayer lobbies -Fixed infinite loop when the user inputs letters instead of numbers -Clear output when refreshing lobby list
1 parent cd8fcaf commit 367234a

1 file changed

Lines changed: 112 additions & 102 deletions

File tree

‎tools/lobby_connect/lobby_connect.cpp‎

Lines changed: 112 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,18 @@
3131
#ifdef _WIN32
3232
#define WIN32_LEAN_AND_MEAN
3333
#include <windows.h>
34-
#else
35-
3634
#endif
35+
36+
void title() {
37+
#ifdef _WIN32
38+
system("cls");
39+
#else
40+
system("clear");
41+
#endif
42+
43+
std::cout << "\n----Lobby Connect----\n\n";
44+
}
45+
3746
int main() {
3847
std::string appid_str(std::to_string(LOBBY_CONNECT_APPID));
3948
set_env_variable("SteamAppId", appid_str);
@@ -45,28 +54,20 @@ int main() {
4554

4655
//Set appid to: LOBBY_CONNECT_APPID
4756
SteamAPI_RestartAppIfNecessary(LOBBY_CONNECT_APPID);
48-
std::cout << "This is a program to find lobbies and run the game with lobby connect parameters" << std::endl;
49-
std::cout << "Api initialized, ";
50-
top:
51-
std::cout << "waiting a few seconds for connections:" << std::endl;
57+
58+
refresh:
59+
title();
60+
std::cout << "Please wait...\n";
61+
5262
for (int i = 0; i < 10; ++i) {
5363
SteamAPI_RunCallbacks();
5464
std::this_thread::sleep_for(std::chrono::milliseconds(200));
5565
}
56-
66+
5767
int friend_count = SteamFriends()->GetFriendCount(k_EFriendFlagAll);
58-
std::cout << "People on the network: " << friend_count << std::endl;
59-
for (int i = 0; i < friend_count; ++i) {
60-
CSteamID id = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagAll);
61-
const char *name = SteamFriends()->GetFriendPersonaName(id);
62-
63-
FriendGameInfo_t friend_info = {};
64-
SteamFriends()->GetFriendGamePlayed(id, &friend_info);
65-
std::cout << name << " is playing: " << friend_info.m_gameID.AppID() << std::endl;
66-
}
67-
68-
std::cout << std::endl << "--------------Menu-------------" << std::endl << "\tappid\tname\tcommand line" << std::endl;
69-
68+
69+
title();
70+
7071
std::vector<std::pair<std::string, uint32>> arguments;
7172
for (int i = 0; i < friend_count; ++i) {
7273
CSteamID id = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagAll);
@@ -75,100 +76,109 @@ int main() {
7576
FriendGameInfo_t friend_info = {};
7677
SteamFriends()->GetFriendGamePlayed(id, &friend_info);
7778
auto appid = friend_info.m_gameID.AppID();
78-
79+
7980
if (strlen(connect) > 0) {
80-
std::cout << arguments.size() << "\t" << appid << "\t" << name << "\t" << connect << std::endl;
81+
std::cout << arguments.size() << " - " << name << " is playing " << appid << " (" << connect << ").\n";
8182
arguments.emplace_back(connect, appid);
8283
} else {
8384
if (friend_info.m_steamIDLobby != k_steamIDNil) {
8485
std::string connect = "+connect_lobby " + std::to_string(friend_info.m_steamIDLobby.ConvertToUint64());
85-
std::cout << arguments.size() << "\t" << appid << "\t" << name << "\t" << connect << std::endl;
86+
std::cout << arguments.size() << " - " << name << " is playing " << appid << " (" << connect << ").\n";
8687
arguments.emplace_back(connect, appid);
8788
}
8889
}
8990
}
90-
91-
std::cout << arguments.size() << ": Retry." << std::endl;
92-
std::cout << std::endl << "Enter the number corresponding to your choice then press Enter." << std::endl;
91+
92+
std::cout << arguments.size() << " - Refresh.\n\n";
93+
std::cout << "Choose an option: ";
9394
unsigned int choice;
9495
std::cin >> choice;
95-
96-
if (choice >= arguments.size()) goto top;
97-
96+
97+
if (std::cin.fail() || choice >= arguments.size()) {
98+
std::cin.clear();
99+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
100+
goto refresh;
101+
}
102+
103+
title();
104+
98105
auto connect = arguments[choice].first;
99-
#ifdef _WIN32
100-
auto appid = arguments[choice].second;
101-
std::cout << "starting the game with: " << connect << std::endl;
102-
103-
char szBaseDirectory[MAX_PATH] = "";
104-
GetModuleFileNameA(0, szBaseDirectory, MAX_PATH);
105-
if (auto bs = strrchr(szBaseDirectory, '\\')) {
106-
*bs = '\0';
107-
}
108-
auto lobbyFile = std::string(szBaseDirectory) + "\\lobby_connect_" + std::to_string(appid) + ".txt";
109-
110-
auto readLobbyFile = [&lobbyFile]() {
111-
std::string data;
112-
std::ifstream ifs(std::filesystem::u8path(lobbyFile));
113-
if (ifs.is_open())
114-
std::getline(ifs, data);
115-
return data;
116-
};
117-
118-
auto writeLobbyFile = [&lobbyFile](const std::string& data) {
119-
std::ofstream ofs(std::filesystem::u8path(lobbyFile));
120-
ofs << data << std::endl;
121-
};
122-
123-
auto fileExists = [](const std::string& filename) {
124-
std::ifstream ifs(std::filesystem::u8path(filename));
125-
return ifs.is_open();
126-
};
127-
128-
auto joinLobby = [&connect](std::string filename) {
129-
filename = "\"" + filename + "\" " + connect;
130-
std::cout << filename << std::endl;
131-
132-
STARTUPINFOA lpStartupInfo;
133-
PROCESS_INFORMATION lpProcessInfo;
134-
135-
ZeroMemory( &lpStartupInfo, sizeof( lpStartupInfo ) );
136-
lpStartupInfo.cb = sizeof( lpStartupInfo );
137-
ZeroMemory( &lpProcessInfo, sizeof( lpProcessInfo ) );
138-
139-
auto success = !!CreateProcessA( NULL,
140-
const_cast<char *>(filename.c_str()), NULL, NULL,
141-
NULL, NULL, NULL, NULL,
142-
&lpStartupInfo,
143-
&lpProcessInfo
144-
);
145-
146-
CloseHandle(lpProcessInfo.hThread);
147-
CloseHandle(lpProcessInfo.hProcess);
148-
return success;
149-
};
150-
151-
std::string filename = readLobbyFile();
152-
if (filename.empty() || !fileExists(filename) || !joinLobby(filename)) {
153-
std::cout << "Please select the game exe" << std::endl;
154-
155-
OPENFILENAMEA ofn;
156-
char szFileName[MAX_PATH] = "";
157-
ZeroMemory(&ofn, sizeof(ofn));
158-
ofn.lStructSize = sizeof(ofn);
159-
ofn.hwndOwner = 0;
160-
ofn.lpstrFilter = "Exe Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0";
161-
ofn.lpstrFile = szFileName;
162-
ofn.nMaxFile = MAX_PATH;
163-
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
164-
ofn.lpstrDefExt = "exe";
165-
if(GetOpenFileNameA(&ofn) && joinLobby(szFileName)) {
166-
writeLobbyFile(szFileName);
167-
}
168-
}
169-
#else
170-
std::cout << "Please launch the game with these arguments: " << connect << std::endl;
171-
#endif
172-
106+
107+
#ifdef _WIN32
108+
auto appid = arguments[choice].second;
109+
std::cout << "Starting the game with: " << connect << "\n";
110+
111+
char szBaseDirectory[MAX_PATH] = "";
112+
GetModuleFileNameA(0, szBaseDirectory, MAX_PATH);
113+
if (auto bs = strrchr(szBaseDirectory, '\\')) {
114+
*bs = '\0';
115+
}
116+
auto lobbyFile = std::string(szBaseDirectory) + "\\lobby_connect_" + std::to_string(appid) + ".txt";
117+
118+
auto readLobbyFile = [&lobbyFile]() {
119+
std::string data;
120+
std::ifstream ifs(std::filesystem::u8path(lobbyFile));
121+
if (ifs.is_open())
122+
std::getline(ifs, data);
123+
return data;
124+
};
125+
126+
auto writeLobbyFile = [&lobbyFile](const std::string& data) {
127+
std::ofstream ofs(std::filesystem::u8path(lobbyFile));
128+
ofs << data << "\n";
129+
};
130+
131+
auto fileExists = [](const std::string& filename) {
132+
std::ifstream ifs(std::filesystem::u8path(filename));
133+
return ifs.is_open();
134+
};
135+
136+
auto joinLobby = [&connect](std::string filename) {
137+
filename = "\"" + filename + "\" " + connect;
138+
std::cout << filename << std::endl;
139+
140+
STARTUPINFOA lpStartupInfo;
141+
PROCESS_INFORMATION lpProcessInfo;
142+
143+
ZeroMemory( &lpStartupInfo, sizeof( lpStartupInfo ) );
144+
lpStartupInfo.cb = sizeof( lpStartupInfo );
145+
ZeroMemory( &lpProcessInfo, sizeof( lpProcessInfo ) );
146+
147+
auto success = !!CreateProcessA( NULL,
148+
const_cast<char *>(filename.c_str()), NULL, NULL,
149+
NULL, NULL, NULL, NULL,
150+
&lpStartupInfo,
151+
&lpProcessInfo
152+
);
153+
154+
CloseHandle(lpProcessInfo.hThread);
155+
CloseHandle(lpProcessInfo.hProcess);
156+
return success;
157+
};
158+
159+
std::string filename = readLobbyFile();
160+
if (filename.empty() || !fileExists(filename) || !joinLobby(filename)) {
161+
std::cout << "Please select the game executable.\n";
162+
163+
OPENFILENAMEA ofn;
164+
char szFileName[MAX_PATH] = "";
165+
ZeroMemory(&ofn, sizeof(ofn));
166+
ofn.lStructSize = sizeof(ofn);
167+
ofn.hwndOwner = 0;
168+
ofn.lpstrFilter = "Exe Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0";
169+
ofn.lpstrFile = szFileName;
170+
ofn.nMaxFile = MAX_PATH;
171+
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
172+
ofn.lpstrDefExt = "exe";
173+
if(GetOpenFileNameA(&ofn) && joinLobby(szFileName)) {
174+
writeLobbyFile(szFileName);
175+
}
176+
}
177+
#else
178+
std::cout << "Please launch the game with these arguments: " << connect << "\n\n";
179+
#endif
180+
173181
return 0;
174182
}
183+
184+

0 commit comments

Comments
 (0)