r5sdk/r5dev/squirrel/sqinit.cpp

342 lines
13 KiB
C++
Raw Normal View History

Code base refactor + major performance and readability improvement. Read description for details. * Codebase restructured to SourceSDK codebase style and .cpp/.h assertion paths in the game executable. * Document most functions with valve style 'Purpose' blocks. * Rename variables to match the rest of the codebase and Valve's naming convention. * Dedicated DLL and the SDKLauncher now share the same codebase as the DevSDK. * Obtain globals or pointers directly instead of waiting for runtime initialized data. * Dynamically search for all functions and globals (this doesn't count for dedicated yet!). * Initialize most in-SDK variables. * Move certain prints and other utilities under ConVars to reduce verbosity and increase performance. * Print all pattern scan results through a virtual function to make it easier to add and debug new patterns in the future. * Type global var pointers appropriately if class or type is known and implemented. * Forward declare 'CClient' class to avoid having 2 'g_pClient' copies. * Add IDA's pseudo definitions for easier prototyping with decompiled assembly code. * RPAK decompress Command callback implementation. * Load decompressed RPaks from 'paks\Win32\' overriding the ones in 'paks\Win64\' (the decompress callback will automatically fix the header and write it to 'paks\Win32\'). * VPK decompress Command callback implementation. * Move CRC32 ands Adler32 to implementation files. * Server will print out more details about the connecting client. * Upgrade ImGui lib to v1.86. * Don't compile id3dx.h for dedicated. * Don't compile id3dx.cpp for dedicated * Implement DevMsg print function allowing to print information to the in-game VGUI/RUI console overlay, ImGui console overlay and the external windows console * Fixed bug where the Error function would not properly terminate the process when an error is called. This caused access violations for critical/non-recoverable errors. * Fixed bug where the game would crash if the console or server browser was enabled while the game was still starting up. * Several bug fixes for the dedicated server (warning: dedicated is still considered work-in-progress!).
2021-12-25 22:36:38 +01:00
//=============================================================================//
//
// Purpose: Expose native code to VScript API
//
//-----------------------------------------------------------------------------
//
// Create functions here under the target VM namespace. If the function has to
// be registered for 2 or more VM's, put them under the 'SHARED' namespace.
2022-03-28 12:02:11 +02:00
// Ifdef them out for 'DEDICATED' / 'CLIENT_DLL' if the target VM's do not
// include 'SERVER' / 'CLIENT'.
Code base refactor + major performance and readability improvement. Read description for details. * Codebase restructured to SourceSDK codebase style and .cpp/.h assertion paths in the game executable. * Document most functions with valve style 'Purpose' blocks. * Rename variables to match the rest of the codebase and Valve's naming convention. * Dedicated DLL and the SDKLauncher now share the same codebase as the DevSDK. * Obtain globals or pointers directly instead of waiting for runtime initialized data. * Dynamically search for all functions and globals (this doesn't count for dedicated yet!). * Initialize most in-SDK variables. * Move certain prints and other utilities under ConVars to reduce verbosity and increase performance. * Print all pattern scan results through a virtual function to make it easier to add and debug new patterns in the future. * Type global var pointers appropriately if class or type is known and implemented. * Forward declare 'CClient' class to avoid having 2 'g_pClient' copies. * Add IDA's pseudo definitions for easier prototyping with decompiled assembly code. * RPAK decompress Command callback implementation. * Load decompressed RPaks from 'paks\Win32\' overriding the ones in 'paks\Win64\' (the decompress callback will automatically fix the header and write it to 'paks\Win32\'). * VPK decompress Command callback implementation. * Move CRC32 ands Adler32 to implementation files. * Server will print out more details about the connecting client. * Upgrade ImGui lib to v1.86. * Don't compile id3dx.h for dedicated. * Don't compile id3dx.cpp for dedicated * Implement DevMsg print function allowing to print information to the in-game VGUI/RUI console overlay, ImGui console overlay and the external windows console * Fixed bug where the Error function would not properly terminate the process when an error is called. This caused access violations for critical/non-recoverable errors. * Fixed bug where the game would crash if the console or server browser was enabled while the game was still starting up. * Several bug fixes for the dedicated server (warning: dedicated is still considered work-in-progress!).
2021-12-25 22:36:38 +01:00
//
//=============================================================================//
#include "core/stdafx.h"
2022-05-29 02:08:07 +02:00
#include "vpc/keyvalues.h"
#ifndef CLIENT_DLL
#include "engine/server/server.h"
#endif // CLIENT_DLL
#include "engine/cmodel_bsp.h"
#include "engine/host_state.h"
2022-03-29 22:27:56 +02:00
#include "squirrel/sqtype.h"
#include "squirrel/sqapi.h"
#include "squirrel/sqinit.h"
2022-07-01 10:29:27 +02:00
#include "networksystem/pylon.h"
#ifndef DEDICATED
#include "gameui/IBrowser.h" // TODO: create dedicated class for exposing server utils to ImGui and UI VM.
#endif // !DEDICATED
namespace VSquirrel
{
namespace SHARED
{
//-----------------------------------------------------------------------------
// Purpose: SDK test and example body
//-----------------------------------------------------------------------------
SQRESULT SDKNativeTest(HSQUIRRELVM v)
{
// Function code goes here.
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: expose SDK version to the VScript API
//-----------------------------------------------------------------------------
SQRESULT GetSDKVersion(HSQUIRRELVM v)
{
sq_pushstring(v, SDK_VERSION, -1);
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: return all available maps
//-----------------------------------------------------------------------------
SQRESULT GetAvailableMaps(HSQUIRRELVM v)
{
if (g_vAllMaps.empty())
return SQ_OK;
sq_newarray(v, 0);
for (auto& it : g_vAllMaps)
{
sq_pushstring(v, it.c_str(), -1);
sq_arrayappend(v, -2);
}
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: return all available playlists
//-----------------------------------------------------------------------------
SQRESULT GetAvailablePlaylists(HSQUIRRELVM v)
{
if (g_vAllPlaylists.empty())
return SQ_OK;
sq_newarray(v, 0);
for (auto& it : g_vAllPlaylists)
{
sq_pushstring(v, it.c_str(), -1);
sq_arrayappend(v, -2);
}
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: shutdown local game (host only)
//-----------------------------------------------------------------------------
SQRESULT ShutdownHostGame(HSQUIRRELVM v)
{
if (g_pHostState->m_bActiveGame)
g_pHostState->m_iNextState = HostStates_t::HS_GAME_SHUTDOWN;
return SQ_OK;
}
}
2022-03-28 12:02:11 +02:00
#ifndef CLIENT_DLL
namespace SERVER
{
//-----------------------------------------------------------------------------
// Purpose: gets the number of real players on this server
//-----------------------------------------------------------------------------
SQRESULT GetNumHumanPlayers(HSQUIRRELVM v)
{
sq_pushinteger(v, g_pServer->GetNumHumanPlayers());
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: gets the number of fake players on this server
//-----------------------------------------------------------------------------
SQRESULT GetNumFakeClients(HSQUIRRELVM v)
{
sq_pushinteger(v, g_pServer->GetNumFakeClients());
return SQ_OK;
}
}
2022-03-28 12:02:11 +02:00
#endif // !CLIENT_DLL
#ifndef DEDICATED
namespace CLIENT
{
}
namespace UI
{
//-----------------------------------------------------------------------------
2022-01-16 12:40:25 +01:00
// Purpose: get server's current name from serverlist index
//-----------------------------------------------------------------------------
SQRESULT GetServerName(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
SQInteger iServerIndex = sq_getinteger(v, 1);
string svServerName = g_pBrowser->m_vServerList[iServerIndex].m_svHostName;
2022-03-29 22:27:56 +02:00
sq_pushstring(v, svServerName.c_str(), -1);
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: get server's current playlist via serverlist index
//-----------------------------------------------------------------------------
SQRESULT GetServerPlaylist(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
SQInteger iServerIndex = sq_getinteger(v, 1);
string svServerPlaylist = g_pBrowser->m_vServerList[iServerIndex].m_svPlaylist;
2022-03-29 22:27:56 +02:00
sq_pushstring(v, svServerPlaylist.c_str(), -1);
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: get server's current map via serverlist index
//-----------------------------------------------------------------------------
SQRESULT GetServerMap(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
SQInteger iServerIndex = sq_getinteger(v, 1);
string svServerMapName = g_pBrowser->m_vServerList[iServerIndex].m_svMapName;
2022-03-29 22:27:56 +02:00
sq_pushstring(v, svServerMapName.c_str(), -1);
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: get current server count from pylon
//-----------------------------------------------------------------------------
SQRESULT GetServerCount(HSQUIRRELVM v)
{
g_pBrowser->GetServerList(); // Refresh svListing list.
sq_pushinteger(v, g_pBrowser->m_vServerList.size());
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: get promo data for serverbrowser panels
//-----------------------------------------------------------------------------
SQRESULT GetPromoData(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
enum class R5RPromoData : SQInteger
{
PromoLargeTitle,
PromoLargeDesc,
PromoLeftTitle,
PromoLeftDesc,
PromoRightTitle,
PromoRightDesc
};
2022-05-27 14:39:43 +02:00
R5RPromoData ePromoIndex = static_cast<R5RPromoData>(sq_getinteger(v, 1));
2022-05-27 14:39:43 +02:00
string svPromo;
switch (ePromoIndex)
{
case R5RPromoData::PromoLargeTitle:
{
svPromo = "#PROMO_LARGE_TITLE";
break;
}
case R5RPromoData::PromoLargeDesc:
{
svPromo = "#PROMO_LARGE_DESCRIPTION";
break;
}
case R5RPromoData::PromoLeftTitle:
{
svPromo = "#PROMO_LEFT_TITLE";
break;
}
case R5RPromoData::PromoLeftDesc:
{
svPromo = "#PROMO_LEFT_DESCRIPTION";
break;
}
case R5RPromoData::PromoRightTitle:
{
svPromo = "#PROMO_RIGHT_TITLE";
break;
}
case R5RPromoData::PromoRightDesc:
{
svPromo = "#PROMO_RIGHT_DESCRIPTION";
break;
}
default:
{
svPromo = "#PROMO_SDK_ERROR";
break;
}
}
2022-03-29 22:27:56 +02:00
sq_pushstring(v, svPromo.c_str(), -1);
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: set netchannel encryption key and connect to server
//-----------------------------------------------------------------------------
SQRESULT SetEncKeyAndConnect(HSQUIRRELVM v)
{
2022-05-27 16:28:03 +02:00
SQInteger iServerIndex = sq_getinteger(v, 1);
// !TODO: Create glue class instead.
g_pBrowser->ConnectToServer(g_pBrowser->m_vServerList[iServerIndex].m_svIpAddress,
g_pBrowser->m_vServerList[iServerIndex].m_svGamePort,
g_pBrowser->m_vServerList[iServerIndex].m_svEncryptionKey);
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: create server via native serverbrowser entries
//-----------------------------------------------------------------------------
SQRESULT CreateServerFromMenu(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
string svServerName = sq_getstring(v, 1);
string svServerMapName = sq_getstring(v, 2);
string svServerPlaylist = sq_getstring(v, 3);
EServerVisibility eServerVisibility = static_cast<EServerVisibility>(sq_getinteger(v, 4));
if (svServerName.empty() || svServerMapName.empty() || svServerPlaylist.empty())
return SQ_OK;
// Adjust browser settings.
g_pBrowser->m_Server.m_svPlaylist = svServerPlaylist;
g_pBrowser->m_Server.m_svMapName = svServerMapName;
g_pBrowser->m_Server.m_svHostName = svServerName;
g_pBrowser->eServerVisibility = eServerVisibility;
// Launch server.
g_pBrowser->LaunchServer();
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: request token from pylon and join server with result.
//-----------------------------------------------------------------------------
SQRESULT JoinPrivateServerFromMenu(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
string svHiddenServerRequestMessage;
string svToken = sq_getstring(v, 1);
NetGameServer_t svListing;
2022-07-01 10:29:27 +02:00
bool result = g_pMasterServer->GetServerByToken(svListing, svHiddenServerRequestMessage, svToken); // Send szToken connect request.
if (result)
{
g_pBrowser->ConnectToServer(svListing.m_svIpAddress, svListing.m_svGamePort, svListing.m_svEncryptionKey);
}
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: get response from private server request
//-----------------------------------------------------------------------------
SQRESULT GetPrivateServerMessage(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
string svHiddenServerRequestMessage;
string svToken = sq_getstring(v, 1);
NetGameServer_t serverListing;
2022-07-01 10:29:27 +02:00
bool result = g_pMasterServer->GetServerByToken(serverListing, svHiddenServerRequestMessage, svToken); // Send token connect request.
if (!serverListing.m_svHostName.empty())
{
svHiddenServerRequestMessage = "Found Server: " + serverListing.m_svHostName;
2022-03-29 22:27:56 +02:00
sq_pushstring(v, svHiddenServerRequestMessage.c_str(), -1);
}
else
{
svHiddenServerRequestMessage = "Error: Server Not Found";
2022-03-29 22:27:56 +02:00
sq_pushstring(v, svHiddenServerRequestMessage.c_str(), -1);
}
DevMsg(eDLL_T::UI, "GetPrivateServeMessage response: %s\n", svHiddenServerRequestMessage.c_str());
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: connect to server from native server browser entries
//-----------------------------------------------------------------------------
SQRESULT ConnectToIPFromMenu(HSQUIRRELVM v)
{
2022-05-27 14:39:43 +02:00
string svIpAddr = sq_getstring(v, 1);
string svEncKey = sq_getstring(v, 2);
if (svIpAddr.empty() || svEncKey.empty())
return SQ_OK;
DevMsg(eDLL_T::UI, "Connecting to server with ip-address '%s' and encryption key '%s'\n", svIpAddr.c_str(), svEncKey.c_str());
g_pBrowser->ConnectToServer(svIpAddr, svEncKey);
return SQ_OK;
}
}
#endif // !DEDICATED
}