Skip to content

Commit

Permalink
Merge branch 'master' into refactor/CPedSA-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FileEX authored Dec 28, 2024
2 parents 305ceb7 + 9239010 commit 6b65aa2
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 20 deletions.
6 changes: 6 additions & 0 deletions Client/core/DXHook/CProxyDirect3D9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*****************************************************************************/

#include "StdInc.h"
#include <dwmapi.h>

HRESULT HandleCreateDeviceResult(HRESULT hResult, IDirect3D9* pDirect3D, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags,
D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice9** ppReturnedDeviceInterface);
std::vector<IDirect3D9*> ms_CreatedDirect3D9List;
Expand Down Expand Up @@ -166,6 +168,10 @@ HRESULT CProxyDirect3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND
SetWindowTextW(hFocusWindow, MbUTF8ToUTF16("MTA: San Andreas").c_str());
#endif

// Set dark titlebar if needed
BOOL darkTitleBar = GetSystemRegistryValue((uint)HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", "AppsUseLightTheme") == "\x0";
DwmSetWindowAttribute(hFocusWindow, DWMWA_USE_IMMERSIVE_DARK_MODE, &darkTitleBar, sizeof(darkTitleBar));

// Detect if second call to CreateDevice
if (CreateDeviceSecondCallCheck(hResult, m_pDevice, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface))
{
Expand Down
2 changes: 1 addition & 1 deletion Client/core/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ project "Client Core"

links {
"ws2_32", "d3dx9", "Userenv", "DbgHelp", "xinput", "Imagehlp", "dxguid", "dinput8",
"strmiids", "odbc32", "odbccp32", "shlwapi", "winmm", "gdi32", "Imm32", "Psapi",
"strmiids", "odbc32", "odbccp32", "shlwapi", "winmm", "gdi32", "Imm32", "Psapi", "dwmapi",
"pthread", "libpng", "jpeg", "zlib", "tinygettext", "discord-rpc",
}

Expand Down
2 changes: 1 addition & 1 deletion Client/game_sa/CGameSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ CGameSA::CGameSA()
error += ": ";
error += e.what();

MessageBoxUTF8(nullptr, error.c_str(), _("Error"), MB_ICONERROR | MB_OK);
MessageBoxUTF8(nullptr, error, _("Error"), MB_ICONERROR | MB_OK);
ExitProcess(EXIT_FAILURE);
}
catch (const std::exception& e)
Expand Down
4 changes: 2 additions & 2 deletions Client/game_sa/CPhysicalSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class CPhysicalSAInterface : public CEntitySAInterface
CVector m_vecUnk; // 280
uint32 m_pad4; // 292
CPtrNodeDoubleLink<void>* m_pControlCodeNodeLink; // 296
float m_fLighting; // 300
float m_fLighting2; // 304
float m_fLighting; // 300 surface brightness
float m_fLighting2; // 304 dynamic lighting (unused, always set to 0 in the GTA code)
class CShadowDataSA* m_pShadowData; // 308

CRect* GetBoundRect_(CRect* pRect);
Expand Down
13 changes: 11 additions & 2 deletions Client/game_sa/CRendererSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "CMatrix.h"
#include "gamesa_renderware.h"

#define SetLightColoursForPedsCarsAndObjects(fMult) ((RpLight*(__cdecl*)(float))0x735D90)(fMult)
#define SetAmbientColours() ((RpLight*(__cdecl*)())0x735D30)()

CRendererSA::CRendererSA()
{
}
Expand All @@ -23,7 +26,7 @@ CRendererSA::~CRendererSA()
{
}

void CRendererSA::RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix)
void CRendererSA::RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix, float lighting)
{
CBaseModelInfoSAInterface* pModelInfoSAInterface = pModelInfo->GetInterface();
if (!pModelInfoSAInterface)
Expand All @@ -40,7 +43,10 @@ void CRendererSA::RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix)
rwMatrix.up = (RwV3d&)matrix.vFront;
rwMatrix.at = (RwV3d&)matrix.vUp;
rwMatrix.pos = (RwV3d&)matrix.vPos;
RwFrameTransform(pFrame, &rwMatrix, rwCOMBINEREPLACE);
RwFrameTransform(pFrame, &rwMatrix, rwCOMBINEREPLACE);

// Setup ambient light multiplier
SetLightColoursForPedsCarsAndObjects(lighting);

if (pRwObject->type == RP_TYPE_ATOMIC)
{
Expand All @@ -52,4 +58,7 @@ void CRendererSA::RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix)
RpClump* pClump = reinterpret_cast<RpClump*>(pRwObject);
RpClumpRender(pClump);
}

// Restore ambient light
SetAmbientColours();
}
2 changes: 1 addition & 1 deletion Client/game_sa/CRendererSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ class CRendererSA : public CRenderer
CRendererSA();
~CRendererSA();

void RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix) override;
void RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix, float lighting) override;
};
8 changes: 4 additions & 4 deletions Client/mods/deathmatch/logic/CModelRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
#include "game\CRenderer.h"
#include "game\CVisibilityPlugins.h"

bool CModelRenderer::EnqueueModel(CModelInfo* pModelInfo, const CMatrix& matrix)
bool CModelRenderer::EnqueueModel(CModelInfo* pModelInfo, const CMatrix& matrix, float lighting)
{
if (g_pCore->IsWindowMinimized())
return false;

if (pModelInfo && pModelInfo->IsLoaded())
{
m_Queue.emplace_back(pModelInfo, matrix);
m_Queue.emplace_back(pModelInfo, matrix, lighting);
return true;
}

Expand Down Expand Up @@ -54,7 +54,7 @@ void CModelRenderer::Render()
for (auto& modelDesc : m_Queue)
{
if (modelDesc.pModelInfo->IsLoaded() && !modelDesc.pModelInfo->GetIdeFlag(eModelIdeFlag::DRAW_LAST))
pRenderer->RenderModel(modelDesc.pModelInfo, modelDesc.matrix);
pRenderer->RenderModel(modelDesc.pModelInfo, modelDesc.matrix, modelDesc.lighting);
}

m_Queue.clear();
Expand All @@ -68,5 +68,5 @@ void CModelRenderer::RenderEntity(SModelToRender* modelDesc, float distance)
CRenderer* pRenderer = g_pGame->GetRenderer();
assert(pRenderer);

pRenderer->RenderModel(modelDesc->pModelInfo, modelDesc->matrix);
pRenderer->RenderModel(modelDesc->pModelInfo, modelDesc->matrix, modelDesc->lighting);
}
8 changes: 5 additions & 3 deletions Client/mods/deathmatch/logic/CModelRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ class CModelRenderer final
{
CModelInfo* pModelInfo;
CMatrix matrix;
float lighting;

SModelToRender(CModelInfo* pModelInfo, const CMatrix& matrix) :
SModelToRender(CModelInfo* pModelInfo, const CMatrix& matrix, float lighting = 0.0f) :
pModelInfo(pModelInfo),
matrix(matrix)
matrix(matrix),
lighting(lighting)
{
}
};

bool EnqueueModel(CModelInfo* pModelInfo, const CMatrix& matrix);
bool EnqueueModel(CModelInfo* pModelInfo, const CMatrix& matrix, float lighting);

void Update();

Expand Down
36 changes: 36 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,42 @@ void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMat
}
}

//
// Check 4x4 lua table
//
bool IsValidMatrixLuaTable(lua_State* luaVM, std::uint32_t argIndex) noexcept
{
std::uint32_t cell = 0;

if (lua_type(luaVM, argIndex) == LUA_TTABLE)
{
lua_pushnil(luaVM);
for (std::uint32_t row = 0; lua_next(luaVM, argIndex) != 0; lua_pop(luaVM, 1), ++row)
{
if (lua_type(luaVM, -1) != LUA_TTABLE)
return false;

std::uint32_t col = 0;

lua_pushnil(luaVM);
for (; lua_next(luaVM, -2) != 0; lua_pop(luaVM, 1), ++col, ++cell)
{
int argumentType = lua_type(luaVM, -1);
if (argumentType != LUA_TNUMBER && argumentType != LUA_TSTRING)
return false;
}

if (col != 4)
return false;
}
}

if (cell != 16)
return false;

return true;
}

//
// 4x4 matrix into CMatrix
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ class CScriptArgReader;
void MixedReadDxFontString(CScriptArgReader& argStream, eFontType& outFontType, eFontType defaultFontType, CClientDxFont*& poutDxFontElement);
void MixedReadGuiFontString(CScriptArgReader& argStream, SString& strFontName, const char* szDefaultFontName, CClientGuiFont*& poutGuiFontElement);
void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMaterialElement);
bool IsValidMatrixLuaTable(lua_State* luaVM, std::uint32_t argIndex) noexcept;
bool ReadMatrix(lua_State* luaVM, uint uiArgIndex, CMatrix& outMatrix);
void MinClientReqCheck(lua_State* luaVM, const char* szVersionReq, const char* szReason);
bool MinClientReqCheck(CScriptArgReader& argStream, const char* szVersionReq, const char* szReason = nullptr);
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,7 @@ bool CLuaDrawingDefs::DxDrawWiredSphere(lua_State* const luaVM, const CVector po
return true;
}

bool CLuaDrawingDefs::DxDrawModel3D(std::uint32_t modelID, CVector position, CVector rotation, const std::optional<CVector> scale)
bool CLuaDrawingDefs::DxDrawModel3D(std::uint32_t modelID, CVector position, CVector rotation, const std::optional<CVector> scale, const std::optional<float> lighting)
{
CModelInfo* pModelInfo = g_pGame->GetModelInfo(modelID);
if (!pModelInfo)
Expand All @@ -2138,5 +2138,5 @@ bool CLuaDrawingDefs::DxDrawModel3D(std::uint32_t modelID, CVector position, CVe
ConvertDegreesToRadians(rotation);

return g_pClientGame->GetModelRenderer()->EnqueueModel(pModelInfo,
CMatrix{position, rotation, scale.value_or(CVector{1.0f, 1.0f, 1.0f})});
CMatrix{position, rotation, scale.value_or(CVector{1.0f, 1.0f, 1.0f})}, lighting.value_or(0.0f));
}
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CLuaDrawingDefs : public CLuaDefs
static bool DxDrawWiredSphere(lua_State* const luaVM, const CVector position, const float radius, const std::optional<SColor> color,
const std::optional<float> lineWidth, const std::optional<unsigned int> iterations);

static bool DxDrawModel3D(std::uint32_t modelID, CVector position, CVector rotation, const std::optional<CVector> scale);
static bool DxDrawModel3D(std::uint32_t modelID, CVector position, CVector rotation, const std::optional<CVector> scale, const std::optional<float> lighting);

private:
static void AddDxMaterialClass(lua_State* luaVM);
Expand Down
42 changes: 42 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void CLuaElementDefs::LoadFunctions()
{"setElementFrozen", SetElementFrozen},
{"setLowLODElement", ArgumentParser<SetLowLodElement>},
{"setElementCallPropagationEnabled", SetElementCallPropagationEnabled},
{"setElementLighting", ArgumentParser<SetElementLighting>},
};

// Add functions
Expand Down Expand Up @@ -191,6 +192,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "setLowLOD", "setLowLODElement");
lua_classfunction(luaVM, "setCallPropagationEnabled", "setElementCallPropagationEnabled");
lua_classfunction(luaVM, "setStreamable", "setElementStreamable");
lua_classfunction(luaVM, "setLighting", "setElementLighting");

lua_classvariable(luaVM, "callPropagationEnabled", "setElementCallPropagationEnabled", "isElementCallPropagationEnabled");
lua_classvariable(luaVM, "waitingForGroundToLoad", NULL, "isElementWaitingForGroundToLoad");
Expand Down Expand Up @@ -225,6 +227,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classvariable(luaVM, "velocity", SetElementVelocity, OOP_GetElementVelocity);
lua_classvariable(luaVM, "angularVelocity", SetElementAngularVelocity, OOP_GetElementTurnVelocity);
lua_classvariable(luaVM, "isElement", NULL, "isElement");
lua_classvariable(luaVM, "lighting", "setElementLighting", "getElementLighting");
// TODO: Support element data: player.data["age"] = 1337; <=> setElementData(player, "age", 1337)

lua_registerclass(luaVM, "Element");
Expand Down Expand Up @@ -1340,6 +1343,7 @@ std::variant<bool, float> CLuaElementDefs::GetElementLighting(CClientEntity* ent
break;
}
case CCLIENTOBJECT:
case CCLIENTWEAPON:
{
CObject* object = static_cast<CClientObject*>(entity)->GetGameObject();
if (object)
Expand Down Expand Up @@ -2604,3 +2608,41 @@ int CLuaElementDefs::IsElementWaitingForGroundToLoad(lua_State* luaVM)
lua_pushboolean(luaVM, false);
return 1;
}

bool CLuaElementDefs::SetElementLighting(CClientEntity* entity, float lighting)
{
switch (entity->GetType())
{
case CCLIENTPLAYER:
case CCLIENTPED:
{
auto* ped = static_cast<CClientPed*>(entity)->GetGamePlayer();
if (!ped)
return false;

ped->SetLighting(lighting);
return true;
}
case CCLIENTVEHICLE:
{
auto* vehicle = static_cast<CClientVehicle*>(entity)->GetGameVehicle();
if (!vehicle)
return false;

vehicle->SetLighting(lighting);
return true;
}
case CCLIENTOBJECT:
case CCLIENTWEAPON:
{
auto* object = static_cast<CClientObject*>(entity)->GetGameObject();
if (!object)
return false;

object->SetLighting(lighting);
return true;
}
}

return false;
}
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaElementDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ class CLuaElementDefs : public CLuaDefs
LUA_DECLARE(SetElementFrozen);
static bool SetLowLodElement(lua_State* luaVM, CClientEntity* pEntity, std::optional<CClientEntity*> pLowLodEntity);
LUA_DECLARE(SetElementCallPropagationEnabled);
static bool SetElementLighting(CClientEntity* entity, float lighting);
};
1 change: 0 additions & 1 deletion Client/multiplayer_sa/multiplayer_keysync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ VOID InitKeysyncHooks()
HookInstallMethod(VTBL_CBoat__ProcessControl, (DWORD)HOOK_CBoat__ProcessControl);
HookInstallMethod(VTBL_CBike__ProcessControl, (DWORD)HOOK_CBike__ProcessControl);
HookInstallMethod(VTBL_CHeli__ProcessControl, (DWORD)HOOK_CHeli__ProcessControl);
HookInstallMethod(VTBL_CHeli__ProcessControl, (DWORD)HOOK_CHeli__ProcessControl);

// not strictly for keysync, to make CPlayerPed::GetPlayerInfoForThisPlayerPed always return the local playerinfo
// 00609FF2 EB 1F JMP SHORT gta_sa_u.0060A013
Expand Down
2 changes: 1 addition & 1 deletion Client/sdk/game/CRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ class CRenderer
public:
virtual ~CRenderer() {}

virtual void RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix) = 0;
virtual void RenderModel(CModelInfo* pModelInfo, const CMatrix& matrix, float lighting) = 0;
};
36 changes: 36 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,42 @@ void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions)
}
}

//
// Check 4x4 lua table
//
bool IsValidMatrixLuaTable(lua_State* luaVM, std::uint32_t argIndex) noexcept
{
std::uint32_t cell = 0;

if (lua_type(luaVM, argIndex) == LUA_TTABLE)
{
lua_pushnil(luaVM);
for (std::uint32_t row = 0; lua_next(luaVM, argIndex) != 0; lua_pop(luaVM, 1), ++row)
{
if (lua_type(luaVM, -1) != LUA_TTABLE)
return false;

std::uint32_t col = 0;

lua_pushnil(luaVM);
for (; lua_next(luaVM, -2) != 0; lua_pop(luaVM, 1), ++col, ++cell)
{
int argumentType = lua_type(luaVM, -1);
if (argumentType != LUA_TNUMBER && argumentType != LUA_TSTRING)
return false;
}

if (col != 4)
return false;
}
}

if (cell != 16)
return false;

return true;
}

//
// 4x4 matrix into CMatrix
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ void MixedReadResourceString(CScriptArgReader& argStream, CResource*& pOutRes
bool StringToBool(const SString& strText);
void MinServerReqCheck(CScriptArgReader& argStream, const char* szVersionReq, const char* szReason);
void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions);
bool IsValidMatrixLuaTable(lua_State* luaVM, std::uint32_t argIndex) noexcept;
bool ReadMatrix(lua_State* luaVM, uint uiArgIndex, CMatrix& outMatrix);

//
Expand Down
Loading

0 comments on commit 6b65aa2

Please sign in to comment.