r5sdk/r5dev/squirrel/sqinit.cpp

303 lines
11 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"
#include "engine/sys_utils.h"
2022-03-29 22:27:56 +02:00
#include "squirrel/sqtype.h"
#include "squirrel/sqapi.h"
#include "squirrel/sqinit.h"
2022-03-29 22:27:56 +02:00
#include "networksystem/r5net.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
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT SDKNativeTest(HSQUIRRELVM* v)
{
// Function code goes here.
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: expose SDK version to the VScript API
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetSDKVersion(HSQUIRRELVM* v)
{
2022-03-29 22:27:56 +02:00
sq_pushstring(v, g_pR5net->GetSDKVersion().c_str(), -1);
return SQ_OK;
}
}
2022-03-28 12:02:11 +02:00
#ifndef CLIENT_DLL
namespace SERVER
{
}
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
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetServerName(HSQUIRRELVM* v)
{
2022-03-29 22:27:56 +02:00
int iServerIndex = sq_getinteger(v, 1);
std::string svServerName = g_pIBrowser->m_vServerList[iServerIndex].svServerName;
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
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetServerPlaylist(HSQUIRRELVM* v)
{
2022-03-29 22:27:56 +02:00
int iServerIndex = sq_getinteger(v, 1);
std::string svServerPlaylist = g_pIBrowser->m_vServerList[iServerIndex].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
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetServerMap(HSQUIRRELVM* v)
{
2022-03-29 22:27:56 +02:00
int iServerIndex = sq_getinteger(v, 1);
std::string svServerMapName = g_pIBrowser->m_vServerList[iServerIndex].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
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetServerCount(HSQUIRRELVM* v)
{
g_pIBrowser->GetServerList(); // Refresh svListing list.
2022-03-29 22:27:56 +02:00
sq_pushinteger(v, g_pIBrowser->m_vServerList.size());
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: get promo data for serverbrowser panels
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetPromoData(HSQUIRRELVM* v)
{
enum class R5RPromoData : int
{
PromoLargeTitle,
PromoLargeDesc,
PromoLeftTitle,
PromoLeftDesc,
PromoRightTitle,
PromoRightDesc
};
2022-03-29 22:27:56 +02:00
R5RPromoData ePromoIndex = (R5RPromoData)sq_getinteger(v, 1);
std::string svPromo = std::string();
switch (ePromoIndex)
{
case R5RPromoData::PromoLargeTitle:
{
svPromo = "Welcome To R5Reloaded!";
break;
}
case R5RPromoData::PromoLargeDesc:
{
svPromo = "Make sure to join the discord! discord.gg/r5reloaded";
break;
}
case R5RPromoData::PromoLeftTitle:
{
svPromo = "Yes";
break;
}
case R5RPromoData::PromoLeftDesc:
{
svPromo = "Your ad could be here";
break;
}
case R5RPromoData::PromoRightTitle:
{
svPromo = "Yes2";
break;
}
case R5RPromoData::PromoRightDesc:
{
svPromo = "Yes3";
break;
}
default:
{
svPromo = "You should not see this.";
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
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT SetEncKeyAndConnect(HSQUIRRELVM* v)
{
2022-03-29 22:27:56 +02:00
int iServerIndex = sq_getinteger(v, 1);
// !TODO: Create glue class instead.
g_pIBrowser->ConnectToServer(g_pIBrowser->m_vServerList[iServerIndex].svIpAddress, g_pIBrowser->m_vServerList[iServerIndex].svPort, g_pIBrowser->m_vServerList[iServerIndex].svEncryptionKey);
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: create server via native serverbrowser entries
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT CreateServerFromMenu(HSQUIRRELVM* v)
{
2022-03-29 22:27:56 +02:00
std::string svServerName = sq_getstring(v, 1);
std::string svServerMapName = sq_getstring(v, 2);
std::string svServerPlaylist = sq_getstring(v, 3);
EServerVisibility eServerVisibility = (EServerVisibility)sq_getinteger(v, 4);
if (svServerName.empty() || svServerMapName.empty() || svServerPlaylist.empty())
return SQ_OK;
// Adjust browser settings.
g_pIBrowser->m_Server.svPlaylist = svServerPlaylist;
g_pIBrowser->m_Server.svMapName = svServerMapName;
g_pIBrowser->m_Server.svServerName = svServerName;
g_pIBrowser->eServerVisibility = eServerVisibility;
// Launch server.
g_pIBrowser->LaunchServer();
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: request token from pylon and join server with result.
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT JoinPrivateServerFromMenu(HSQUIRRELVM* v)
{
std::string svHiddenServerRequestMessage = std::string();
2022-03-29 22:27:56 +02:00
std::string svToken = sq_getstring(v, 1);
ServerListing svListing;
bool result = g_pR5net->GetServerByToken(svListing, svHiddenServerRequestMessage, svToken); // Send szToken connect request.
if (result)
{
g_pIBrowser->ConnectToServer(svListing.svIpAddress, svListing.svPort, svListing.svEncryptionKey);
}
return SQ_OK;
}
//-----------------------------------------------------------------------------
// Purpose: get response from private server request
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetPrivateServerMessage(HSQUIRRELVM* v)
{
std::string svHiddenServerRequestMessage = std::string();
2022-03-29 22:27:56 +02:00
std::string svToken = sq_getstring(v, 1);
ServerListing serverListing;
bool result = g_pR5net->GetServerByToken(serverListing, svHiddenServerRequestMessage, svToken); // Send szToken connect request.
if (!serverListing.svServerName.empty())
{
svHiddenServerRequestMessage = "Found Server: " + serverListing.svServerName;
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
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT ConnectToIPFromMenu(HSQUIRRELVM* v)
{
2022-03-29 22:27:56 +02:00
std::string svIpAddr = sq_getstring(v, 1);
std::string svEncKey = sq_getstring(v, 2);
if (svIpAddr.empty() || svEncKey.empty())
return SQ_OK;
2022-01-16 12:40:25 +01:00
DevMsg(eDLL_T::UI, "Connecting to server with connection string '%s' and encryption key '%s'\n", svIpAddr.c_str(), svEncKey.c_str());
g_pIBrowser->ConnectToServer(svIpAddr, svEncKey);
return SQ_OK;
}
//-----------------------------------------------------------------------------
2022-01-16 12:40:25 +01:00
// Purpose: return all available maps
//-----------------------------------------------------------------------------
2022-03-29 22:27:56 +02:00
SQRESULT GetAvailableMaps(HSQUIRRELVM* v)
{
std::vector<std::string> vsvMapList = g_pIBrowser->m_vszMapFileNameList;
if (vsvMapList.empty())
{
DevMsg(eDLL_T::UI, "Available maps is empty!!!\n");
return SQ_OK;
}
DevMsg(eDLL_T::UI, "Requesting an array of '%i' available maps from script\n", vsvMapList.size());
2022-03-29 22:27:56 +02:00
sq_newarray(v, 0);
for (auto& it : vsvMapList)
{
2022-03-29 22:27:56 +02:00
sq_pushstring(v, it.c_str(), -1);
sq_arrayappend(v, -2);
}
return SQ_OK;
}
}
#endif // !DEDICATED
}