refactor steam_client a little + create setters/getters instead of directly modifying fields

This commit is contained in:
otavepto 2024-06-01 14:21:10 +03:00
parent 5da9e44344
commit ba1de0165d
5 changed files with 48 additions and 18 deletions

View File

@ -157,7 +157,7 @@ static void load_old_steam_interfaces()
STEAMAPI_API HSteamUser SteamAPI_GetHSteamUser()
{
PRINT_DEBUG_ENTRY();
if (!get_steam_client()->user_logged_in) return 0;
if (!get_steam_client()->IsUserLogIn()) return 0;
return CLIENT_HSTEAMUSER;
}
@ -187,7 +187,7 @@ void destroy_client()
std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (steamclient_instance) {
delete steamclient_instance;
steamclient_instance = NULL;
steamclient_instance = nullptr;
}
}
@ -257,13 +257,17 @@ static void *create_client_interface(const char *ver)
STEAMAPI_API void * S_CALLTYPE SteamInternal_CreateInterface( const char *ver )
{
PRINT_DEBUG("%s", ver);
if (!get_steam_client()->user_logged_in && !get_steam_client()->IsServerInit()) return NULL;
if (!get_steam_client()->IsUserLogIn() && !get_steam_client()->IsServerInit()) return NULL;
return create_client_interface(ver);
}
static uintp global_counter;
struct ContextInitData { void (*pFn)(void* pCtx); uintp counter; CSteamAPIContext ctx; };
static uintp global_counter{};
struct ContextInitData {
void (*pFn)(void* pCtx) = nullptr;
uintp counter{};
CSteamAPIContext ctx{};
};
STEAMAPI_API void * S_CALLTYPE SteamInternal_ContextInit( void *pContextInitData )
{
@ -513,7 +517,7 @@ STEAMAPI_API void S_CALLTYPE SteamAPI_UnregisterCallback( class CCallbackBase *p
// Internal functions used by the utility CCallResult objects to receive async call results
STEAMAPI_API void S_CALLTYPE SteamAPI_RegisterCallResult( class CCallbackBase *pCallback, SteamAPICall_t hAPICall )
{
PRINT_DEBUG_ENTRY();
PRINT_DEBUG("%llu %p", hAPICall, pCallback);
if (!hAPICall)
return;
@ -633,7 +637,7 @@ STEAMAPI_API ISteamClient *SteamClient()
PRINT_DEBUG("old");
// call this first since it loads old interfaces
Steam_Client* client = get_steam_client();
if (!client->user_logged_in) return NULL;
if (!client->IsUserLogIn()) return NULL;
return (ISteamClient *)SteamInternal_CreateInterface(old_client);
}
@ -830,7 +834,7 @@ STEAMAPI_API void * S_CALLTYPE SteamGameServerInternal_CreateInterface( const ch
return SteamInternal_CreateInterface(ver);
}
static HSteamPipe server_steam_pipe;
static HSteamPipe server_steam_pipe = 0;
STEAMAPI_API HSteamPipe S_CALLTYPE SteamGameServer_GetHSteamPipe()
{
PRINT_DEBUG_ENTRY();
@ -840,7 +844,7 @@ STEAMAPI_API HSteamPipe S_CALLTYPE SteamGameServer_GetHSteamPipe()
STEAMAPI_API HSteamUser S_CALLTYPE SteamGameServer_GetHSteamUser()
{
PRINT_DEBUG_ENTRY();
if (!get_steam_client()->server_init) return 0;
if (!get_steam_client()->IsServerInit()) return 0;
return SERVER_HSTEAMUSER;
}

View File

@ -81,6 +81,13 @@ public ISteamClient019,
public ISteamClient020,
public ISteamClient
{
private:
bool user_logged_in = false;
bool server_init = false;
std::atomic_bool cb_run_active = false;
std::atomic<unsigned long long> last_cb_run{};
public:
Networking *network{};
SteamCallResults *callback_results_server{}, *callback_results_client{};
@ -139,15 +146,11 @@ public:
Steam_Overlay* steam_overlay{};
bool user_logged_in = false;
bool server_init = false;
bool steamclient_server_inited = false;
bool gameserver_has_ipv6_functions{};
std::thread background_keepalive{};
std::atomic<unsigned long long> last_cb_run{};
std::atomic_bool cb_run_active = false;
unsigned steam_pipe_counter = 1;
std::map<HSteamPipe, enum Steam_Pipe> steam_pipes{};
@ -316,6 +319,10 @@ public:
bool IsUserLogIn();
void DestroyAllInterfaces();
bool runcallbacks_active() const;
unsigned long long get_last_runcallbacks_time() const;
void set_last_runcallbacks_time(unsigned long long time_ms);
};
#endif // __INCLUDED_STEAM_CLIENT_H__

View File

@ -297,10 +297,13 @@ void Steam_Client::setAppID(uint32 appid)
HSteamPipe Steam_Client::CreateSteamPipe()
{
PRINT_DEBUG_ENTRY();
HSteamPipe pipe = steam_pipe_counter++;
PRINT_DEBUG(" pipe handle %i", pipe);
if (!steam_pipe_counter) ++steam_pipe_counter;
HSteamPipe pipe = steam_pipe_counter;
++steam_pipe_counter;
PRINT_DEBUG(" returned pipe handle %i", pipe);
steam_pipes[pipe] = Steam_Pipe::NO_USER;
return pipe;
}
@ -330,7 +333,6 @@ HSteamUser Steam_Client::ConnectToGlobalUser( HSteamPipe hSteamPipe )
userLogIn();
if (!settings_client->disable_overlay) steam_overlay->SetupOverlay();
// games like appid 1740720 and 2379780 do not call SteamAPI_RunCallbacks() or SteamAPI_ManualDispatch_RunFrame() or Steam_BGetCallback()
// hence all run_callbacks() will never run, which might break the assumption that these callbacks are always run
@ -341,6 +343,7 @@ HSteamUser Steam_Client::ConnectToGlobalUser( HSteamPipe hSteamPipe )
PRINT_DEBUG("spawned background thread *********");
}
steam_overlay->SetupOverlay();
steam_pipes[hSteamPipe] = Steam_Pipe::CLIENT;
return CLIENT_HSTEAMUSER;
}
@ -435,11 +438,11 @@ bool Steam_Client::BShutdownIfAllPipesClosed()
steam_controller->Shutdown();
if(!settings_client->disable_overlay) steam_overlay->UnSetupOverlay();
if (joinable) {
background_keepalive.join();
}
steam_overlay->UnSetupOverlay();
PRINT_DEBUG("all pipes closed");
return true;
@ -979,3 +982,18 @@ void Steam_Client::DestroyAllInterfaces()
{
PRINT_DEBUG_TODO();
}
bool Steam_Client::runcallbacks_active() const
{
return cb_run_active;
}
unsigned long long Steam_Client::get_last_runcallbacks_time() const
{
return last_cb_run;
}
void Steam_Client::set_last_runcallbacks_time(unsigned long long time_ms)
{
last_cb_run = time_ms;
}

View File

@ -1232,7 +1232,7 @@ SteamAPICall_t Steam_User_Stats::RequestUserStats( CSteamID steamIDUser )
// requests stat information for a user, usable after a successful call to RequestUserStats()
bool Steam_User_Stats::GetUserStat( CSteamID steamIDUser, const char *pchName, int32 *pData )
{
PRINT_DEBUG("%s %llu", pchName, steamIDUser.ConvertToUint64());
PRINT_DEBUG("'%s' %llu", pchName, steamIDUser.ConvertToUint64());
std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (!pchName) return false;

View File

@ -1750,6 +1750,7 @@ void Steam_Overlay::UnSetupOverlay()
{
PRINT_DEBUG_ENTRY();
std::lock_guard<std::recursive_mutex> lock(overlay_mutex);
if (settings->disable_overlay) return;
bool already_called = true;
if (setup_overlay_called.compare_exchange_weak(already_called, false)) {