Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
691f02f
Inital commit
Pirulax Apr 1, 2021
c7c950c
Add readme.md to help understand how the new system works
Pirulax Apr 1, 2021
5d6e173
Finish up EventHandler operator()
Pirulax Apr 1, 2021
1c803a8
add missing function definitions to EventHandlerCallDispatcher.cpp
Pirulax Apr 1, 2021
afa5b2e
Add all neccessary stuff, and connect it with CElement Initial commit…
Pirulax Apr 2, 2021
d8ff733
Add temporary test code
Pirulax Apr 2, 2021
3dd78e7
Refactor code to use BultInEvent/CustomEvent instead of strings to ca…
Pirulax Apr 2, 2021
179b66c
[SERVER SIDE] Refactor Lua API functions, add some missing stuff to E…
Pirulax Apr 3, 2021
eeb47ee
Refactor DebugHookManager to use const std::string& instead of const …
Pirulax Apr 3, 2021
47fbb69
Refactor CPerfStatLuaTiming to use const std::string& for event name
Pirulax Apr 3, 2021
b175599
Modify EventHandler to use CLuaArg to store globals on call
Pirulax Apr 3, 2021
5134aa8
RIP CMapEventManager and CMapEvent
Pirulax Apr 3, 2021
9c46c81
Refactor DebugHookManager to use EventHandler instead of CMapEvent
Pirulax Apr 3, 2021
7aa8832
Re-add debug hook stuff to event dispatcher and handler
Pirulax Apr 3, 2021
b6e428b
Implement custom Lua function to calls event handler functions, as it…
Pirulax Apr 3, 2021
7ddc44d
Fix GCC compilation error
Pirulax Apr 3, 2021
893b829
Cleanp EventHandler.cpp
Pirulax Apr 3, 2021
136d123
Add temp hack for GCC not compiling
Pirulax Apr 3, 2021
c502278
Revert "Add temp hack for GCC not compiling"
Pirulax Apr 3, 2021
26e109b
apparently from_chars float isn't supported yet in libstdc++. Use stof
Pirulax Apr 3, 2021
77b67dd
Add IterChildren, IterAncestors, IterAncestorsThenChildren
Pirulax Apr 4, 2021
d26a3e4
Handle CustomEvent removal on VM close
Pirulax Apr 4, 2021
910a839
Refactor EventDispatcher to use the new iter functions of CElement
Pirulax Apr 4, 2021
f3316c4
Edit some note for EventHandlerCallDispatcher::Remove
Pirulax Apr 4, 2021
9e420e0
77b67dd120a addendum
Pirulax Apr 4, 2021
7e51890
d26a3e466b addendum
Pirulax Apr 4, 2021
924da51
77b67dd120a addendum
Pirulax Apr 4, 2021
66c4b71
Make s_CustomEvents to have external linkage (so its the same object …
Pirulax Apr 5, 2021
96b7212
Fix message from stack non popped in case of error in EventHandler::o…
Pirulax Apr 5, 2021
16ed2e6
Fix getEventHandlers not returning a table
Pirulax Apr 5, 2021
ccff237
Fix priority parsing
Pirulax Apr 5, 2021
44f1b61
Fix event handler not called by priority order
Pirulax Apr 5, 2021
32490cb
[CLIENT] Add template crap + CClientEntity::Iter stuff for client sid…
Pirulax Apr 5, 2021
c255854
Client crap continues - Redirect callEvent to dispatch.call, add timi…
Pirulax Apr 5, 2021
05e8c0b
Even more macros and timing crap in EventHandler and EventHandlerColl…
Pirulax Apr 5, 2021
02fc10f
Delete CMapEvent(and Manager)
Pirulax Apr 5, 2021
513becc
Refactor CLuaFunctionDefs.Event to use new parser and event system
Pirulax Apr 5, 2021
17ee56e
Refactor CPacketHandler::Packet_LuaEven
Pirulax Apr 5, 2021
df61c41
Refactor CallEvent("eventName", ...) to CallEvent(BuiltInEvents::even…
Pirulax Apr 5, 2021
e1a8dfd
Reverse engineer our CGI implementation and fix event stuff in it
Pirulax Apr 5, 2021
bb88634
Fix event stuff in CClientPlayerVoice
Pirulax Apr 5, 2021
4825ccb
Fix CClientWebBrowser event stuff
Pirulax Apr 5, 2021
5c64c69
Fix GetEventHandlers
Pirulax Apr 5, 2021
9a91c51
CallEvent set bCallOnChildren to true by default
Pirulax Apr 5, 2021
964e24c
Fix client/server inconsistency related errors
Pirulax Apr 5, 2021
dfb3ef5
Fix LuaManager missing CLuaFunctionParser.h
Pirulax Apr 5, 2021
a69e7ee
Fix missing rename to BuiltInEvents:: in ScriptDebugging
Pirulax Apr 5, 2021
07cd846
Make GetGame static in EventDispatcher.cpp
Pirulax Apr 5, 2021
d5e0098
Fix custom event not cleared on VM stop
Pirulax Apr 5, 2021
fda687d
Cleanup custom events on ~CClientGame
Pirulax Apr 5, 2021
e48e63e
Fix dassert in `Event::Get`
Pirulax Apr 5, 2021
d1a0af6
Fix server not compiling
Pirulax Apr 5, 2021
7a21a4b
Remove CEvents from client and server
Pirulax Apr 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/CVoiceRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void CVoiceRecorder::SetPTTState(bool bState)
{
m_Mutex.unlock();
CLuaArguments Arguments;
bool bEventTriggered = g_pClientGame->GetLocalPlayer()->CallEvent("onClientPlayerVoiceStart", Arguments, true);
bool bEventTriggered = g_pClientGame->GetLocalPlayer()->CallEvent(BuiltInEvents::onClientPlayerVoiceStart, Arguments, true);

if (!bEventTriggered)
return;
Expand All @@ -229,7 +229,7 @@ void CVoiceRecorder::SetPTTState(bool bState)
{
m_Mutex.unlock();
CLuaArguments Arguments;
g_pClientGame->GetLocalPlayer()->CallEvent("onClientPlayerVoiceStop", Arguments, true);
g_pClientGame->GetLocalPlayer()->CallEvent(BuiltInEvents::onClientPlayerVoiceStop, Arguments, true);
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/ClientCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool COMMAND_Executed(const char* szCommand, const char* szArguments, bool bHand
Arguments.PushString(strClumpedCommand);
}

pLocalPlayer->CallEvent("onClientConsole", Arguments, true);
pLocalPlayer->CallEvent(BuiltInEvents::onClientConsole, Arguments, true);
}

// Write the chatlength and the content
Expand Down
2 changes: 0 additions & 2 deletions Client/mods/deathmatch/StdInc.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
#include <CCustomData.h>
#include <CElementArray.h>
#include <CLogger.h>
#include <CMapEventManager.h>
#include <CClientModelManager.h>
#include <CModelNames.h>
#include <CIFPEngine.h>
Expand Down Expand Up @@ -154,7 +153,6 @@
// Deathmatch includes
#include "ClientCommands.h"
#include "CClient.h"
#include "CEvents.h"
#include "HeapTrace.h"
#include "logic/CClientGame.h"
#include "logic/CClientModelCacheManager.h"
Expand Down
8 changes: 4 additions & 4 deletions Client/mods/deathmatch/logic/CClientColManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ void CClientColManager::HandleHitDetectionResult(bool bHit, CClientColShape* pSh
CLuaArguments Arguments;
Arguments.PushElement(pEntity);
Arguments.PushBoolean((pShape->GetDimension() == pEntity->GetDimension()));
pShape->CallEvent("onClientColShapeHit", Arguments, true);
pShape->CallEvent(BuiltInEvents::onClientColShapeHit, Arguments, true);

CLuaArguments Arguments2;
Arguments2.PushElement(pShape);
Arguments2.PushBoolean((pShape->GetDimension() == pEntity->GetDimension()));
pEntity->CallEvent("onClientElementColShapeHit", Arguments2, true);
pEntity->CallEvent(BuiltInEvents::onClientElementColShapeHit, Arguments2, true);
}

// Run whatever callback the collision item might have attached
Expand All @@ -177,12 +177,12 @@ void CClientColManager::HandleHitDetectionResult(bool bHit, CClientColShape* pSh
CLuaArguments Arguments;
Arguments.PushElement(pEntity);
Arguments.PushBoolean((pShape->GetDimension() == pEntity->GetDimension()));
pShape->CallEvent("onClientColShapeLeave", Arguments, true);
pShape->CallEvent(BuiltInEvents::onClientColShapeLeave, Arguments, true);

CLuaArguments Arguments2;
Arguments2.PushElement(pShape);
Arguments2.PushBoolean((pShape->GetDimension() == pEntity->GetDimension()));
pEntity->CallEvent("onClientElementColShapeLeave", Arguments2, true);
pEntity->CallEvent(BuiltInEvents::onClientElementColShapeLeave, Arguments2, true);

pShape->CallLeaveCallback(*pEntity);
}
Expand Down
113 changes: 9 additions & 104 deletions Client/mods/deathmatch/logic/CClientEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*****************************************************************************/

#include "StdInc.h"
#include <event/EventDispatcher.h>

using std::list;

Expand Down Expand Up @@ -41,7 +42,6 @@ CClientEntity::CClientEntity(ElementID ID) : ClassInit(this)
CElementIDs::SetElement(ID, this);

m_pCustomData = new CCustomData;
m_pEventManager = new CMapEventManager;

m_pAttachedToEntity = NULL;

Expand Down Expand Up @@ -101,12 +101,6 @@ CClientEntity::~CClientEntity()

RemoveAllCollisions();

if (m_pEventManager)
{
delete m_pEventManager;
m_pEventManager = NULL;
}

if (m_pElementGroup)
{
m_pElementGroup->Remove(this);
Expand Down Expand Up @@ -484,7 +478,7 @@ void CClientEntity::SetCustomData(const char* szName, const CLuaArgument& Variab
Arguments.PushString(szName);
Arguments.PushArgument(oldVariable);
Arguments.PushArgument(Variable);
CallEvent("onClientElementDataChange", Arguments, true);
CallEvent(BuiltInEvents::onClientElementDataChange, Arguments, true);
}

void CClientEntity::DeleteCustomData(const char* szName)
Expand All @@ -504,7 +498,7 @@ void CClientEntity::DeleteCustomData(const char* szName)
Arguments.PushString(szName);
Arguments.PushArgument(oldVariable);
Arguments.PushArgument(CLuaArgument()); // Use nil as the new value to indicate the data has been removed
CallEvent("onClientElementDataChange", Arguments, true);
CallEvent(BuiltInEvents::onClientElementDataChange, Arguments, true);
}
}

Expand Down Expand Up @@ -604,7 +598,7 @@ void CClientEntity::SetDimension(unsigned short usDimension)
CLuaArguments Arguments;
Arguments.PushNumber(usOldDimension);
Arguments.PushNumber(usDimension);
CallEvent("onClientElementDimensionChange", Arguments, true);
CallEvent(BuiltInEvents::onClientElementDimensionChange, Arguments, true);
}

bool CClientEntity::IsOutOfBounds()
Expand Down Expand Up @@ -724,104 +718,15 @@ void CClientEntity::SetAttachedOffsets(CVector& vecPosition, CVector& vecRotatio
m_vecAttachedRotation = vecRotation;
}

bool CClientEntity::AddEvent(CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction, bool bPropagated, EEventPriorityType eventPriority,
float fPriorityMod)
{
return m_pEventManager->Add(pLuaMain, szName, iLuaFunction, bPropagated, eventPriority, fPriorityMod);
}

bool CClientEntity::CallEvent(const char* szName, const CLuaArguments& Arguments, bool bCallOnChildren)
{
if (!g_pClientGame->GetDebugHookManager()->OnPreEvent(szName, Arguments, this, NULL))
return false;

TIMEUS startTime = GetTimeUs();

CEvents* pEvents = g_pClientGame->GetEvents();

// Make sure our event-manager knows we're about to call an event
pEvents->PreEventPulse();

// Call the event on our parents/us first
CallParentEvent(szName, Arguments, this);

if (bCallOnChildren)
{
// Call it on all our children
CallEventNoParent(szName, Arguments, this);
}

// Tell the event manager that we're done calling the event
pEvents->PostEventPulse();

if (IS_TIMING_CHECKPOINTS())
{
TIMEUS deltaTimeUs = GetTimeUs() - startTime;
if (deltaTimeUs > 10000)
TIMING_DETAIL(SString("Event: %s [%d ms]", szName, deltaTimeUs / 1000));
}

g_pClientGame->GetDebugHookManager()->OnPostEvent(szName, Arguments, this, NULL);

// Return whether it got cancelled or not
return (!pEvents->WasEventCancelled());
}

void CClientEntity::CallEventNoParent(const char* szName, const CLuaArguments& Arguments, CClientEntity* pSource)
{
// Call it on us if this isn't the same class it was raised on
// TODO not sure why the null check is necessary (eAi)
if (pSource != this && m_pEventManager != NULL && m_pEventManager->HasEvents())
{
m_pEventManager->Call(szName, Arguments, pSource, this);
}

// Call it on all our children
if (!m_Children.empty())
{
CElementListSnapshot* pList = GetChildrenListSnapshot();
pList->AddRef(); // Keep list alive during use
for (CElementListSnapshot::const_iterator iter = pList->begin(); iter != pList->end(); iter++)
{
CClientEntity* pEntity = *iter;
if (!pEntity->IsBeingDeleted())
{
if (!pEntity->m_pEventManager || pEntity->m_pEventManager->HasEvents() || !pEntity->m_Children.empty())
{
pEntity->CallEventNoParent(szName, Arguments, pSource);
if (m_bBeingDeleted)
break;
}
}
}
pList->Release();
}
}

void CClientEntity::CallParentEvent(const char* szName, const CLuaArguments& Arguments, CClientEntity* pSource)
{
// Call the event on us
if (m_pEventManager && m_pEventManager->HasEvents())
{
m_pEventManager->Call(szName, Arguments, pSource, this);
}

// Call parent's handler
if (m_pParent)
{
m_pParent->CallParentEvent(szName, Arguments, pSource);
}
}

bool CClientEntity::DeleteEvent(CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction)
bool CClientEntity::CallEvent(const Event& event, const CLuaArguments& Arguments, bool bCallOnChildren)
{
return m_pEventManager->Delete(pLuaMain, szName, iLuaFunction);
return s_EventDispatcher.Call(event, Arguments, bCallOnChildren, this);
}

void CClientEntity::DeleteEvents(CLuaMain* pLuaMain, bool bRecursive)
{
// Delete it from our events
m_pEventManager->Delete(pLuaMain);
GetEventHandlerCallDispatcher().Remove(pLuaMain);

// Delete it from all our children's events
if (bRecursive)
Expand All @@ -836,7 +741,7 @@ void CClientEntity::DeleteEvents(CLuaMain* pLuaMain, bool bRecursive)

void CClientEntity::DeleteAllEvents()
{
m_pEventManager->DeleteAll();
GetEventHandlerCallDispatcher().Clear();
}

void CClientEntity::CleanUpForVM(CLuaMain* pLuaMain, bool bRecursive)
Expand Down Expand Up @@ -1310,7 +1215,7 @@ void CClientEntity::SetInterior(unsigned char ucInterior)
CLuaArguments Arguments;
Arguments.PushNumber(ucOldInterior);
Arguments.PushNumber(ucInterior);
CallEvent("onClientElementInteriorChange", Arguments, true);
CallEvent(BuiltInEvents::onClientElementInteriorChange, Arguments, true);
}

bool CClientEntity::IsOnScreen()
Expand Down
65 changes: 55 additions & 10 deletions Client/mods/deathmatch/logic/CClientEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CClientEntity;
#include "CClientCommon.h"
#include <core/CClientEntityBase.h>
#include "logic/CClientEntityRefManager.h"
#include <event/EventHandlerCallDispatcher.h>
class CLuaFunctionRef;

// Used to check fast version of getElementsByType
Expand Down Expand Up @@ -87,7 +88,6 @@ class CElementGroup;
class CLuaArgument;
class CLuaArguments;
class CLuaMain;
class CMapEventManager;
typedef CFastList<CClientEntity*> CChildListType;

// List of elements which is auto deleted when the last user calls Release()
Expand Down Expand Up @@ -242,12 +242,7 @@ class CClientEntity : public CClientEntityBase
virtual bool IsAttachToable();
virtual void DoAttaching();

bool AddEvent(CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction, bool bPropagated, EEventPriorityType eventPriority,
float fPriorityMod);
bool CallEvent(const char* szName, const CLuaArguments& Arguments, bool bCallOnChildren);
void CallEventNoParent(const char* szName, const CLuaArguments& Arguments, CClientEntity* pSource);
void CallParentEvent(const char* szName, const CLuaArguments& Arguments, CClientEntity* pSource);
bool DeleteEvent(CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction);
bool CallEvent(const Event& event, const CLuaArguments& Arguments, bool bCallOnChildren = true);
void DeleteEvents(CLuaMain* pLuaMain, bool bRecursive);
void DeleteAllEvents();

Expand Down Expand Up @@ -281,8 +276,6 @@ class CClientEntity : public CClientEntityBase

static unsigned int GetTypeID(const char* szTypeName);

CMapEventManager* GetEventManager() { return m_pEventManager; };

void DeleteClientChildren();

// Returns true if this class is inherited by CClientStreamElement
Expand Down Expand Up @@ -327,6 +320,57 @@ class CClientEntity : public CClientEntityBase
bool CanBeDestroyedByScript() { return m_canBeDestroyedByScript; }
void SetCanBeDestroyedByScript(bool canBeDestroyedByScript) { m_canBeDestroyedByScript = canBeDestroyedByScript; }

EventHandlerCallDispatcher& GetEventHandlerCallDispatcher() { return m_eventHandlerCallDispatcher; }

// Iter a snapshot list of our children
// The snapshot list is inmutable, thus even if it gets modified by
// scripts it wont crash.
// If the provided `Fn` function returns bool `false`
// the given child and its sub-tree will be skipped
//
// TODO: This snapshot list stuff seems absolute bs
// Its inconsistent compared to IterAncestors
// since that function iterates the `live` version
// while this one a `snapshot`
// eg.: Parent gets changed in the middle of iteration:
// it will still iterate the old children, as the snapshot wont be updated
template<typename Fn>
void IterChildren(const Fn& fn)
{
auto childrenSnapshot = GetChildrenListSnapshot();
childrenSnapshot->AddRef();
for (auto* child : *childrenSnapshot)
{
if (child->IsBeingDeleted())
continue;
if (IsBeingDeleted())
break;
// Check if the function returns a bool
if constexpr (std::is_same_v<bool, std::invoke_result_t<Fn, decltype(child)>>)
{
if (!fn(child))
continue; // Skip child and it's sub-tree
}
else
fn(child);
child->IterChildren(fn); // Recurse into child
}
childrenSnapshot->Release();
}

template<typename Fn>
void IterAncestors(const Fn& fn)
{
for (CClientEntity* it = m_pParent; it; it = it->m_pParent)
fn(it);
}

template<typename Fn>
void IterAncestorsThenChildren(const Fn& fn)
{
IterAncestors(fn);
IterChildren(fn);
}
protected:
CClientManager* m_pManager;
CClientEntity* m_pParent;
Expand All @@ -336,6 +380,8 @@ class CClientEntity : public CClientEntityBase

CCustomData* m_pCustomData;

EventHandlerCallDispatcher m_eventHandlerCallDispatcher;

ElementID m_ID;
CVector m_vecRelativePosition;

Expand All @@ -360,7 +406,6 @@ class CClientEntity : public CClientEntityBase

bool m_bBeingDeleted;
bool m_bSystemEntity;
CMapEventManager* m_pEventManager;
CModelInfo* m_pModelInfo;
CFastList<class CClientColShape*> m_Collisions;
CElementGroup* m_pElementGroup;
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CClientExplosionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ bool CClientExplosionManager::Hook_ExplosionCreation(CEntity* pGameExplodingEnti
Arguments.PushNumber(vecPosition.fY);
Arguments.PushNumber(vecPosition.fZ);
Arguments.PushNumber(explosionWeaponType);
const bool bAllowExplosion = pResponsible->CallEvent("onClientExplosion", Arguments, true);
const bool bAllowExplosion = pResponsible->CallEvent(BuiltInEvents::onClientExplosion, Arguments, true);
return bAllowExplosion;
}

Expand Down
Loading