mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-23 06:23:08 +00:00
refactor(component): the script component picks its own script
The ScriptComponent row (server and client script) loads in LoadDataFromTemplate and custom_script_server / custom_script_client in LoadConfigData, out of Entity::Initialize, which only decides whether there is one (a registry entry, a character or a custom script), as the client's allocator does. Same scripts and bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -493,56 +493,13 @@ void Entity::Initialize() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case eReplicaComponentType::SCRIPT: {
|
case eReplicaComponentType::SCRIPT: {
|
||||||
/**
|
// LWOScriptComponent's allocator: a script component for a registry entry, a character, or a custom script in
|
||||||
* This is a bit of a mess
|
// the level config; the component picks its script
|
||||||
* yep i aint touching this
|
|
||||||
*/
|
|
||||||
|
|
||||||
CDScriptComponentTable* const scriptCompTable = CDClientManager::GetTable<CDScriptComponentTable>();
|
CDScriptComponentTable* const scriptCompTable = CDClientManager::GetTable<CDScriptComponentTable>();
|
||||||
const int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1);
|
const int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1);
|
||||||
|
const bool hasCustomScript = !GetVarAsString(u"custom_script_server").empty() || !GetVarAsString(u"custom_script_client").empty();
|
||||||
std::string scriptName = "";
|
if (scriptComponentID >= 0 || m_Character || hasCustomScript) {
|
||||||
bool client = false;
|
AddComponent<ScriptComponent>(scriptComponentID);
|
||||||
if (scriptComponentID > 0 || m_Character) {
|
|
||||||
std::string clientScriptName;
|
|
||||||
if (!m_Character) {
|
|
||||||
CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID);
|
|
||||||
scriptName = scriptCompData.script_name;
|
|
||||||
clientScriptName = scriptCompData.client_script_name;
|
|
||||||
} else {
|
|
||||||
scriptName = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scriptName != "" || (scriptName == "" && m_Character)) {
|
|
||||||
|
|
||||||
} else if (clientScriptName != "") {
|
|
||||||
client = true;
|
|
||||||
} else if (!m_Character) {
|
|
||||||
client = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string customScriptServer;
|
|
||||||
bool hasCustomServerScript = false;
|
|
||||||
|
|
||||||
const auto customScriptServerName = GetVarAsString(u"custom_script_server");
|
|
||||||
const auto customScriptClientName = GetVarAsString(u"custom_script_client");
|
|
||||||
|
|
||||||
if (!customScriptServerName.empty()) {
|
|
||||||
customScriptServer = customScriptServerName;
|
|
||||||
hasCustomServerScript = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!customScriptClientName.empty()) {
|
|
||||||
client = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasCustomServerScript && scriptName.empty()) {
|
|
||||||
scriptName = customScriptServer;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!scriptName.empty() || client || m_Character || scriptComponentID >= 0) {
|
|
||||||
AddComponent<ScriptComponent>(scriptComponentID, scriptName, true, client && scriptName.empty());
|
|
||||||
addProximityAndPathfinding();
|
addProximityAndPathfinding();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "ScriptComponent.h"
|
#include "ScriptComponent.h"
|
||||||
|
#include "CDClientManager.h"
|
||||||
|
#include "CDScriptComponentTable.h"
|
||||||
#include "GameMessages.h"
|
#include "GameMessages.h"
|
||||||
#include "Amf3.h"
|
#include "Amf3.h"
|
||||||
|
|
||||||
@@ -19,6 +21,38 @@ ScriptComponent::ScriptComponent(Entity* parent, const int32_t componentID, cons
|
|||||||
Component::RegisterMsg(&ScriptComponent::OnGetObjectReportInfo);
|
Component::RegisterMsg(&ScriptComponent::OnGetObjectReportInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ScriptComponent::ScriptComponent(Entity* parent, const int32_t componentID) : Component(parent, componentID) {
|
||||||
|
m_Serialized = true;
|
||||||
|
m_Client = false;
|
||||||
|
m_FromObject = true;
|
||||||
|
m_Script = nullptr;
|
||||||
|
Component::RegisterMsg(&ScriptComponent::OnGetObjectReportInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The ScriptComponent row's server and client scripts (a character has neither)
|
||||||
|
void ScriptComponent::LoadDataFromTemplate() {
|
||||||
|
if (!m_FromObject || m_ComponentID <= 0 || m_Parent->GetCharacter()) return;
|
||||||
|
const auto row = CDClientManager::GetTable<CDScriptComponentTable>()->GetByID(m_ComponentID);
|
||||||
|
m_ScriptName = row.script_name;
|
||||||
|
m_ClientScriptName = row.client_script_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// custom_script_server replaces a missing server script; a client script (the row's or custom_script_client, or
|
||||||
|
// any object's without a server script) makes it a client script component
|
||||||
|
void ScriptComponent::LoadConfigData() {
|
||||||
|
if (!m_FromObject) return;
|
||||||
|
const bool isCharacter = m_Parent->GetCharacter() != nullptr;
|
||||||
|
bool client = false;
|
||||||
|
if ((m_ComponentID > 0 || isCharacter) && m_ScriptName.empty() && !isCharacter) client = true;
|
||||||
|
|
||||||
|
const auto customScriptServer = m_Parent->GetVarAsString(u"custom_script_server");
|
||||||
|
if (!m_Parent->GetVarAsString(u"custom_script_client").empty()) client = true;
|
||||||
|
if (!customScriptServer.empty() && m_ScriptName.empty()) m_ScriptName = customScriptServer;
|
||||||
|
|
||||||
|
m_Client = client && m_ScriptName.empty();
|
||||||
|
SetScript(m_ScriptName);
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
void ScriptComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
||||||
// A server-only script (the zone control object's) sends nothing: the client made no script component for it
|
// A server-only script (the zone control object's) sends nothing: the client made no script component for it
|
||||||
if (!m_Serialized) return;
|
if (!m_Serialized) return;
|
||||||
|
|||||||
@@ -26,6 +26,13 @@ public:
|
|||||||
|
|
||||||
ScriptComponent(Entity* parent, const int32_t componentID, const std::string& scriptName, bool serialized, bool client = false);
|
ScriptComponent(Entity* parent, const int32_t componentID, const std::string& scriptName, bool serialized, bool client = false);
|
||||||
|
|
||||||
|
// One the object's registry or level config gives (LWOScriptComponent's allocator): its script comes from the
|
||||||
|
// ScriptComponent row and custom_script_server / custom_script_client
|
||||||
|
ScriptComponent(Entity* parent, const int32_t componentID);
|
||||||
|
|
||||||
|
void LoadDataFromTemplate() override;
|
||||||
|
void LoadConfigData() override;
|
||||||
|
|
||||||
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
|
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,6 +86,11 @@ private:
|
|||||||
bool m_Client;
|
bool m_Client;
|
||||||
|
|
||||||
std::string m_ScriptName;
|
std::string m_ScriptName;
|
||||||
|
|
||||||
|
// Made from the registry / level config: LoadDataFromTemplate and LoadConfigData pick the script
|
||||||
|
bool m_FromObject = false;
|
||||||
|
// The row's client script name (the client runs one when there is no server script)
|
||||||
|
std::string m_ClientScriptName;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SCRIPTCOMPONENT_H
|
#endif // SCRIPTCOMPONENT_H
|
||||||
|
|||||||
Reference in New Issue
Block a user