Skip to content

Commit

Permalink
Add event "OnResourceStateChange" (#3325)
Browse files Browse the repository at this point in the history
* Added `onResourceStateChange` event

Syntax: `onResourceStateChange ( resource changedResource, string oldState, string newState )`

* Moved condition check to top

* Update

Refactoring is a separate issue that will be discussed in another PR
  • Loading branch information
TracerDS committed May 23, 2024
1 parent 7c939ad commit cfe9cd9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,7 @@ void CGame::AddBuiltInEvents()
m_Events.AddEvent("onResourcePreStart", "resource", NULL, false);
m_Events.AddEvent("onResourceStart", "resource", NULL, false);
m_Events.AddEvent("onResourceStop", "resource, deleted", NULL, false);
m_Events.AddEvent("onResourceStateChange", "resource, oldState, newState", nullptr, false);
m_Events.AddEvent("onResourceLoadStateChange", "resource, oldState, newState", NULL, false);

// Blip events
Expand Down
41 changes: 41 additions & 0 deletions Server/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,13 @@ bool CResource::Unload()
m_pNodeSettings = nullptr;
}

OnResourceStateChange("unloaded");

m_strResourceZip = "";
m_strResourceCachePath = "";
m_strResourceDirectoryPath = "";
m_eState = EResourceState::None;

return true;
}

Expand Down Expand Up @@ -741,6 +744,8 @@ bool CResource::Start(std::list<CResource*>* pDependents, bool bManualStart, con
if (m_bDestroyed)
return false;

OnResourceStateChange("starting");

m_eState = EResourceState::Starting;

CLuaArguments PreStartArguments;
Expand Down Expand Up @@ -974,6 +979,8 @@ bool CResource::Start(std::list<CResource*>* pDependents, bool bManualStart, con
AddDependent(pDependent);
}

OnResourceStateChange("running");

m_eState = EResourceState::Running;

// Call the onResourceStart event. If it returns false, cancel this script again
Expand Down Expand Up @@ -1016,6 +1023,37 @@ bool CResource::Start(std::list<CResource*>* pDependents, bool bManualStart, con
return true;
}

void CResource::OnResourceStateChange(const char* state) noexcept
{
if (!m_pResourceElement)
return;

CLuaArguments stateArgs;
stateArgs.PushResource(this);
switch (m_eState)
{
case EResourceState::Loaded: // When resource is stopped
stateArgs.PushString("loaded");
break;
case EResourceState::Running: // When resource is running
stateArgs.PushString("running");
break;
case EResourceState::Starting: // When resource is starting
stateArgs.PushString("starting");
break;
case EResourceState::Stopping: // When resource is stopping
stateArgs.PushString("stopping");
break;
case EResourceState::None: // When resource is not loaded
default:
stateArgs.PushString("unloaded");
break;
}
stateArgs.PushString(state);

m_pResourceElement->CallEvent("onResourceStateChange", stateArgs);
}

bool CResource::Stop(bool bManualStop)
{
if (m_eState == EResourceState::Loaded)
Expand All @@ -1027,6 +1065,8 @@ bool CResource::Stop(bool bManualStop)
if (m_bStartedManually && !bManualStop)
return false;

OnResourceStateChange("stopping");

m_eState = EResourceState::Stopping;
m_pResourceManager->RemoveMinClientRequirement(this);
m_pResourceManager->RemoveSyncMapElementDataOption(this);
Expand Down Expand Up @@ -1113,6 +1153,7 @@ bool CResource::Stop(bool bManualStop)
// Broadcast the packet to joined players
g_pGame->GetPlayerManager()->BroadcastOnlyJoined(removePacket);

OnResourceStateChange("loaded");
m_eState = EResourceState::Loaded;
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/CResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ class CResource : public EHS
void OnPlayerJoin(CPlayer& Player);
void SendNoClientCacheScripts(CPlayer* pPlayer = nullptr);

void OnResourceStateChange(const char* state) noexcept;

CDummy* GetResourceRootElement() { return m_pResourceElement; }
const CDummy* GetResourceRootElement() const noexcept { return m_pResourceElement; }

Expand Down

0 comments on commit cfe9cd9

Please sign in to comment.