2026-03-26 20:14:58 +08:00
|
|
|
#include "Scripting/ScriptComponent.h"
|
|
|
|
|
|
2026-03-26 20:45:41 +08:00
|
|
|
#include "Scripting/ScriptEngine.h"
|
|
|
|
|
|
2026-03-26 20:14:58 +08:00
|
|
|
#include <random>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Scripting {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
uint64_t GenerateScriptComponentUUID() {
|
|
|
|
|
static std::random_device rd;
|
|
|
|
|
static std::mt19937_64 gen(rd());
|
|
|
|
|
static std::uniform_int_distribution<uint64_t> dis(1, UINT64_MAX);
|
|
|
|
|
return dis(gen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
ScriptComponent::ScriptComponent()
|
|
|
|
|
: m_scriptComponentUUID(GenerateScriptComponentUUID()) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptComponent::SetScriptClass(const std::string& namespaceName, const std::string& className) {
|
2026-03-27 16:30:16 +08:00
|
|
|
const bool hadScriptClass = HasScriptClass();
|
2026-03-26 20:14:58 +08:00
|
|
|
m_namespaceName = namespaceName;
|
|
|
|
|
m_className = className;
|
2026-03-27 16:30:16 +08:00
|
|
|
if (!hadScriptClass && HasScriptClass()) {
|
|
|
|
|
ScriptEngine::Get().OnScriptComponentEnabled(this);
|
|
|
|
|
}
|
2026-03-26 20:14:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptComponent::SetScriptClass(const std::string& assemblyName, const std::string& namespaceName, const std::string& className) {
|
2026-03-27 16:30:16 +08:00
|
|
|
const bool hadScriptClass = HasScriptClass();
|
2026-03-26 20:14:58 +08:00
|
|
|
m_assemblyName = assemblyName;
|
|
|
|
|
m_namespaceName = namespaceName;
|
|
|
|
|
m_className = className;
|
2026-03-27 16:30:16 +08:00
|
|
|
if (!hadScriptClass && HasScriptClass()) {
|
|
|
|
|
ScriptEngine::Get().OnScriptComponentEnabled(this);
|
|
|
|
|
}
|
2026-03-26 20:14:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ScriptComponent::GetFullClassName() const {
|
|
|
|
|
if (m_className.empty()) {
|
|
|
|
|
return std::string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_namespaceName.empty()) {
|
|
|
|
|
return m_className;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_namespaceName + "." + m_className;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 20:45:41 +08:00
|
|
|
void ScriptComponent::OnEnable() {
|
|
|
|
|
ScriptEngine::Get().OnScriptComponentEnabled(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptComponent::OnDisable() {
|
|
|
|
|
ScriptEngine::Get().OnScriptComponentDisabled(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptComponent::OnDestroy() {
|
|
|
|
|
ScriptEngine::Get().OnScriptComponentDestroyed(this);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 20:14:58 +08:00
|
|
|
void ScriptComponent::Serialize(std::ostream& os) const {
|
|
|
|
|
os << "scriptComponentUUID=" << m_scriptComponentUUID << ";";
|
|
|
|
|
os << "assembly=" << EscapeScriptString(m_assemblyName) << ";";
|
|
|
|
|
os << "namespace=" << EscapeScriptString(m_namespaceName) << ";";
|
|
|
|
|
os << "class=" << EscapeScriptString(m_className) << ";";
|
|
|
|
|
os << "fields=" << EscapeScriptString(m_fieldStorage.SerializeToString()) << ";";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptComponent::Deserialize(std::istream& is) {
|
|
|
|
|
std::ostringstream buffer;
|
|
|
|
|
buffer << is.rdbuf();
|
|
|
|
|
|
|
|
|
|
std::istringstream input(buffer.str());
|
|
|
|
|
std::string token;
|
|
|
|
|
while (std::getline(input, token, ';')) {
|
|
|
|
|
if (token.empty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t eqPos = token.find('=');
|
|
|
|
|
if (eqPos == std::string::npos) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string key = token.substr(0, eqPos);
|
|
|
|
|
const std::string value = token.substr(eqPos + 1);
|
|
|
|
|
|
|
|
|
|
if (key == "scriptComponentUUID") {
|
|
|
|
|
m_scriptComponentUUID = static_cast<uint64_t>(std::stoull(value));
|
|
|
|
|
} else if (key == "assembly") {
|
|
|
|
|
m_assemblyName = UnescapeScriptString(value);
|
|
|
|
|
} else if (key == "namespace") {
|
|
|
|
|
m_namespaceName = UnescapeScriptString(value);
|
|
|
|
|
} else if (key == "class") {
|
|
|
|
|
m_className = UnescapeScriptString(value);
|
|
|
|
|
} else if (key == "fields") {
|
|
|
|
|
m_fieldStorage.DeserializeFromString(UnescapeScriptString(value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Scripting
|
|
|
|
|
} // namespace XCEngine
|