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
This commit is contained in:
M4RCK5 2024-09-13 15:12:27 -03:00 committed by GitHub
parent cd8fcaf61f
commit 367234ae64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -31,9 +31,18 @@
#ifdef _WIN32 #ifdef _WIN32
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#else
#endif #endif
void title() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
std::cout << "\n----Lobby Connect----\n\n";
}
int main() { int main() {
std::string appid_str(std::to_string(LOBBY_CONNECT_APPID)); std::string appid_str(std::to_string(LOBBY_CONNECT_APPID));
set_env_variable("SteamAppId", appid_str); set_env_variable("SteamAppId", appid_str);
@ -45,28 +54,20 @@ int main() {
//Set appid to: LOBBY_CONNECT_APPID //Set appid to: LOBBY_CONNECT_APPID
SteamAPI_RestartAppIfNecessary(LOBBY_CONNECT_APPID); SteamAPI_RestartAppIfNecessary(LOBBY_CONNECT_APPID);
std::cout << "This is a program to find lobbies and run the game with lobby connect parameters" << std::endl;
std::cout << "Api initialized, "; refresh:
top: title();
std::cout << "waiting a few seconds for connections:" << std::endl; std::cout << "Please wait...\n";
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
SteamAPI_RunCallbacks(); SteamAPI_RunCallbacks();
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
} }
int friend_count = SteamFriends()->GetFriendCount(k_EFriendFlagAll); int friend_count = SteamFriends()->GetFriendCount(k_EFriendFlagAll);
std::cout << "People on the network: " << friend_count << std::endl;
for (int i = 0; i < friend_count; ++i) { title();
CSteamID id = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagAll);
const char *name = SteamFriends()->GetFriendPersonaName(id);
FriendGameInfo_t friend_info = {};
SteamFriends()->GetFriendGamePlayed(id, &friend_info);
std::cout << name << " is playing: " << friend_info.m_gameID.AppID() << std::endl;
}
std::cout << std::endl << "--------------Menu-------------" << std::endl << "\tappid\tname\tcommand line" << std::endl;
std::vector<std::pair<std::string, uint32>> arguments; std::vector<std::pair<std::string, uint32>> arguments;
for (int i = 0; i < friend_count; ++i) { for (int i = 0; i < friend_count; ++i) {
CSteamID id = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagAll); CSteamID id = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagAll);
@ -75,100 +76,109 @@ top:
FriendGameInfo_t friend_info = {}; FriendGameInfo_t friend_info = {};
SteamFriends()->GetFriendGamePlayed(id, &friend_info); SteamFriends()->GetFriendGamePlayed(id, &friend_info);
auto appid = friend_info.m_gameID.AppID(); auto appid = friend_info.m_gameID.AppID();
if (strlen(connect) > 0) { if (strlen(connect) > 0) {
std::cout << arguments.size() << "\t" << appid << "\t" << name << "\t" << connect << std::endl; std::cout << arguments.size() << " - " << name << " is playing " << appid << " (" << connect << ").\n";
arguments.emplace_back(connect, appid); arguments.emplace_back(connect, appid);
} else { } else {
if (friend_info.m_steamIDLobby != k_steamIDNil) { if (friend_info.m_steamIDLobby != k_steamIDNil) {
std::string connect = "+connect_lobby " + std::to_string(friend_info.m_steamIDLobby.ConvertToUint64()); std::string connect = "+connect_lobby " + std::to_string(friend_info.m_steamIDLobby.ConvertToUint64());
std::cout << arguments.size() << "\t" << appid << "\t" << name << "\t" << connect << std::endl; std::cout << arguments.size() << " - " << name << " is playing " << appid << " (" << connect << ").\n";
arguments.emplace_back(connect, appid); arguments.emplace_back(connect, appid);
} }
} }
} }
std::cout << arguments.size() << ": Retry." << std::endl; std::cout << arguments.size() << " - Refresh.\n\n";
std::cout << std::endl << "Enter the number corresponding to your choice then press Enter." << std::endl; std::cout << "Choose an option: ";
unsigned int choice; unsigned int choice;
std::cin >> choice; std::cin >> choice;
if (choice >= arguments.size()) goto top; if (std::cin.fail() || choice >= arguments.size()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
goto refresh;
}
title();
auto connect = arguments[choice].first; auto connect = arguments[choice].first;
#ifdef _WIN32
auto appid = arguments[choice].second; #ifdef _WIN32
std::cout << "starting the game with: " << connect << std::endl; auto appid = arguments[choice].second;
std::cout << "Starting the game with: " << connect << "\n";
char szBaseDirectory[MAX_PATH] = "";
GetModuleFileNameA(0, szBaseDirectory, MAX_PATH); char szBaseDirectory[MAX_PATH] = "";
if (auto bs = strrchr(szBaseDirectory, '\\')) { GetModuleFileNameA(0, szBaseDirectory, MAX_PATH);
*bs = '\0'; if (auto bs = strrchr(szBaseDirectory, '\\')) {
} *bs = '\0';
auto lobbyFile = std::string(szBaseDirectory) + "\\lobby_connect_" + std::to_string(appid) + ".txt"; }
auto lobbyFile = std::string(szBaseDirectory) + "\\lobby_connect_" + std::to_string(appid) + ".txt";
auto readLobbyFile = [&lobbyFile]() {
std::string data; auto readLobbyFile = [&lobbyFile]() {
std::ifstream ifs(std::filesystem::u8path(lobbyFile)); std::string data;
if (ifs.is_open()) std::ifstream ifs(std::filesystem::u8path(lobbyFile));
std::getline(ifs, data); if (ifs.is_open())
return data; std::getline(ifs, data);
}; return data;
};
auto writeLobbyFile = [&lobbyFile](const std::string& data) {
std::ofstream ofs(std::filesystem::u8path(lobbyFile)); auto writeLobbyFile = [&lobbyFile](const std::string& data) {
ofs << data << std::endl; std::ofstream ofs(std::filesystem::u8path(lobbyFile));
}; ofs << data << "\n";
};
auto fileExists = [](const std::string& filename) {
std::ifstream ifs(std::filesystem::u8path(filename)); auto fileExists = [](const std::string& filename) {
return ifs.is_open(); std::ifstream ifs(std::filesystem::u8path(filename));
}; return ifs.is_open();
};
auto joinLobby = [&connect](std::string filename) {
filename = "\"" + filename + "\" " + connect; auto joinLobby = [&connect](std::string filename) {
std::cout << filename << std::endl; filename = "\"" + filename + "\" " + connect;
std::cout << filename << std::endl;
STARTUPINFOA lpStartupInfo;
PROCESS_INFORMATION lpProcessInfo; STARTUPINFOA lpStartupInfo;
PROCESS_INFORMATION lpProcessInfo;
ZeroMemory( &lpStartupInfo, sizeof( lpStartupInfo ) );
lpStartupInfo.cb = sizeof( lpStartupInfo ); ZeroMemory( &lpStartupInfo, sizeof( lpStartupInfo ) );
ZeroMemory( &lpProcessInfo, sizeof( lpProcessInfo ) ); lpStartupInfo.cb = sizeof( lpStartupInfo );
ZeroMemory( &lpProcessInfo, sizeof( lpProcessInfo ) );
auto success = !!CreateProcessA( NULL,
const_cast<char *>(filename.c_str()), NULL, NULL, auto success = !!CreateProcessA( NULL,
NULL, NULL, NULL, NULL, const_cast<char *>(filename.c_str()), NULL, NULL,
&lpStartupInfo, NULL, NULL, NULL, NULL,
&lpProcessInfo &lpStartupInfo,
); &lpProcessInfo
);
CloseHandle(lpProcessInfo.hThread);
CloseHandle(lpProcessInfo.hProcess); CloseHandle(lpProcessInfo.hThread);
return success; CloseHandle(lpProcessInfo.hProcess);
}; return success;
};
std::string filename = readLobbyFile();
if (filename.empty() || !fileExists(filename) || !joinLobby(filename)) { std::string filename = readLobbyFile();
std::cout << "Please select the game exe" << std::endl; if (filename.empty() || !fileExists(filename) || !joinLobby(filename)) {
std::cout << "Please select the game executable.\n";
OPENFILENAMEA ofn;
char szFileName[MAX_PATH] = ""; OPENFILENAMEA ofn;
ZeroMemory(&ofn, sizeof(ofn)); char szFileName[MAX_PATH] = "";
ofn.lStructSize = sizeof(ofn); ZeroMemory(&ofn, sizeof(ofn));
ofn.hwndOwner = 0; ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = "Exe Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0"; ofn.hwndOwner = 0;
ofn.lpstrFile = szFileName; ofn.lpstrFilter = "Exe Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0";
ofn.nMaxFile = MAX_PATH; ofn.lpstrFile = szFileName;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "exe"; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileNameA(&ofn) && joinLobby(szFileName)) { ofn.lpstrDefExt = "exe";
writeLobbyFile(szFileName); if(GetOpenFileNameA(&ofn) && joinLobby(szFileName)) {
} writeLobbyFile(szFileName);
} }
#else }
std::cout << "Please launch the game with these arguments: " << connect << std::endl; #else
#endif std::cout << "Please launch the game with these arguments: " << connect << "\n\n";
#endif
return 0; return 0;
} }