Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event "OnResourceStateChange" #3325

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,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