diff --git a/code/fgame/scriptdelegate.cpp b/code/fgame/scriptdelegate.cpp new file mode 100644 index 00000000..383edd1b --- /dev/null +++ b/code/fgame/scriptdelegate.cpp @@ -0,0 +1,162 @@ +/* +=========================================================================== +Copyright (C) 2025 the OpenMoHAA team + +This file is part of OpenMoHAA source code. + +OpenMoHAA source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +OpenMoHAA source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with OpenMoHAA source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ + +#include "scriptdelegate.h" +#include "../script/scriptexception.h" + +ScriptDelegate *ScriptDelegate::root = NULL; + +ScriptRegisteredDelegate_Script::ScriptRegisteredDelegate_Script(const ScriptThreadLabel& inLabel) + : label(inLabel) +{} + +void ScriptRegisteredDelegate_Script::Execute(const Event& ev) +{ + Event newev = ev; + + label.Execute(NULL, newev); +} + +bool ScriptRegisteredDelegate_Script::operator==(const ScriptRegisteredDelegate_Script& registeredDelegate) const +{ + return label == registeredDelegate.label; +} + +ScriptRegisteredDelegate_CodeMember::ScriptRegisteredDelegate_CodeMember( + Class *inObject, DelegateClassResponse inResponse +) + : object(inObject) + , response(inResponse) +{} + +void ScriptRegisteredDelegate_CodeMember::Execute(const Event& ev) +{ + if (!object) { + return; + } + + (object->*response)(ev); +} + +bool ScriptRegisteredDelegate_CodeMember::operator==(const ScriptRegisteredDelegate_CodeMember& registeredDelegate +) const +{ + return object == registeredDelegate.object && response == registeredDelegate.response; +} + +ScriptRegisteredDelegate_Code::ScriptRegisteredDelegate_Code(DelegateResponse inResponse) + : response(inResponse) +{} + +void ScriptRegisteredDelegate_Code::Execute(const Event& ev) +{ + (*response)(ev); +} + +bool ScriptRegisteredDelegate_Code::operator==(const ScriptRegisteredDelegate_Code& registeredDelegate) const +{ + return response == registeredDelegate.response; +} + +ScriptDelegate::ScriptDelegate(const char *inName, const char *inDescription) + : name(inName) + , description(inDescription) +{ + LL_SafeAdd(root, this, next, prev); +} + +ScriptDelegate::~ScriptDelegate() +{ + LL_SafeRemoveRoot(root, this, next, prev); +} + +const ScriptDelegate *ScriptDelegate::GetRoot() +{ + return root; +} + +const ScriptDelegate *ScriptDelegate::GetNext() const +{ + return next; +} + +void ScriptDelegate::Register(const ScriptThreadLabel& label) +{ + if (!label.IsSet()) { + ScriptError("Invalid label specified for the script delegate"); + } + + list_script.AddUniqueObject(label); +} + +void ScriptDelegate::Unregister(const ScriptThreadLabel& label) +{ + list_script.RemoveObject(label); +} + +void ScriptDelegate::Register(ScriptRegisteredDelegate_Code::DelegateResponse response) +{ + list_code.AddUniqueObject(ScriptRegisteredDelegate_Code(response)); +} + +void ScriptDelegate::Unregister(ScriptRegisteredDelegate_Code::DelegateResponse response) +{ + list_code.RemoveObject(response); +} + +void ScriptDelegate::Register(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response) +{ + list_codeMember.AddUniqueObject(ScriptRegisteredDelegate_CodeMember(object, response)); +} + +void ScriptDelegate::Unregister(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response) +{ + list_codeMember.RemoveObject(ScriptRegisteredDelegate_CodeMember(object, response)); +} + +void ScriptDelegate::Trigger(const Event& ev) const +{ + size_t i; + + for (i = 1; i <= list_script.NumObjects(); i++) { + list_script.ObjectAt(i).Execute(ev); + } + + for (i = 1; i <= list_code.NumObjects(); i++) { + list_code.ObjectAt(i).Execute(ev); + } + + for (i = 1; i <= list_codeMember.NumObjects(); i++) { + list_codeMember.ObjectAt(i).Execute(ev); + } +} + +ScriptDelegate *ScriptDelegate::GetScriptDelegate(const char *name) +{ + for (ScriptDelegate *delegate = root; delegate; delegate = delegate->next) { + if (!Q_stricmp(delegate->name, name)) { + return delegate; + } + } + + return NULL; +} diff --git a/code/fgame/scriptdelegate.h b/code/fgame/scriptdelegate.h new file mode 100644 index 00000000..1472d7d8 --- /dev/null +++ b/code/fgame/scriptdelegate.h @@ -0,0 +1,183 @@ +/* +=========================================================================== +Copyright (C) 2025 the OpenMoHAA team + +This file is part of OpenMoHAA source code. + +OpenMoHAA source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +OpenMoHAA source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with OpenMoHAA source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ + +// scriptdelegate -- manages function delegate + +#include "../qcommon/listener.h" +#include "../qcommon/delegate.h" +#include "gamescript.h" + +class ScriptRegisteredDelegate +{ +public: + void Execute(const Event& ev); +}; + +/** + * Registered delegate, for scripts. + * It contains a ScriptThreadLabel with the game script and the label to execute. + */ +class ScriptRegisteredDelegate_Script : public ScriptRegisteredDelegate +{ +public: + ScriptRegisteredDelegate_Script(const ScriptThreadLabel& inLabel); + + void Execute(const Event& ev); + + bool operator==(const ScriptRegisteredDelegate_Script& registeredDelegate) const; + +private: + ScriptThreadLabel label; +}; + +/** + * Registered delegate, for code use. + * It contains the function to execute. + */ +class ScriptRegisteredDelegate_Code : public ScriptRegisteredDelegate +{ +public: + using DelegateResponse = void (*)(const Event& ev); + +public: + ScriptRegisteredDelegate_Code(DelegateResponse inResponse); + + void Execute(const Event& ev); + + bool operator==(const ScriptRegisteredDelegate_Code& registeredDelegate) const; + +private: + DelegateResponse response; +}; + +/** + * Registered delegate, for code use. + * It contains the object along the member function to execute. + * The function will not be executed if the object is NULL. + */ +class ScriptRegisteredDelegate_CodeMember : public ScriptRegisteredDelegate +{ +public: + using DelegateClassResponse = void (Class::*)(const Event& ev); + +public: + ScriptRegisteredDelegate_CodeMember(Class *inObject, DelegateClassResponse inResponse); + + void Execute(const Event& ev); + + bool operator==(const ScriptRegisteredDelegate_CodeMember& registeredDelegate) const; + +private: + SafePtr object; + DelegateClassResponse response; +}; + +/** + * A script delegate provides a way for code to subscribe for events. + * Scripts and code can register for a delegate and have their function executed + * when the delegate gets triggered. + */ +class ScriptDelegate +{ +public: + ScriptDelegate(const char *name, const char *description); + ~ScriptDelegate(); + + static const ScriptDelegate *GetRoot(); + const ScriptDelegate *GetNext() const; + + /** + * Register a script label. + * + * @param label The label to be executed + */ + void Register(const ScriptThreadLabel& label); + + /** + * Unregistered the label. + * + * @param label The label to unregister + */ + void Unregister(const ScriptThreadLabel& label); + + /** + * Register a function. + * + * @param response The function to be executed + */ + void Register(ScriptRegisteredDelegate_Code::DelegateResponse response); + + /** + * Unregistered the function. + * + * @param response the function to unregister + */ + void Unregister(ScriptRegisteredDelegate_Code::DelegateResponse response); + + /** + * Register with an object and a member function. + * + * @param object The object to notify + * @param response The member function of the object to be executed + */ + void Register(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response); + + /** + * Unregistered the member function. + * + * @param object The object where the member function is + * @param response The member function to unregister + */ + void Unregister(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response); + + /** + * Executes all registered delegates with the specified event. + * + * @param ev Parameter list + */ + void Trigger(const Event& ev) const; + + /** + * Search and return the specified script delegate by name. + * + * @param name The name to search for + */ + static ScriptDelegate *GetScriptDelegate(const char *name); + + // non-movable and non-copyable + ScriptDelegate(ScriptDelegate&& other) = delete; + ScriptDelegate& operator=(ScriptDelegate&& other) = delete; + ScriptDelegate(const ScriptDelegate& other) = delete; + ScriptDelegate& operator=(const ScriptDelegate& other) = delete; + +private: + // Linked-list + ScriptDelegate *next; + ScriptDelegate *prev; + static ScriptDelegate *root; + const char *name; + const char *description; + + Container list_script; + Container list_code; + Container list_codeMember; +}; diff --git a/code/qcommon/CMakeLists.txt b/code/qcommon/CMakeLists.txt index 88281dc5..bde54547 100644 --- a/code/qcommon/CMakeLists.txt +++ b/code/qcommon/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES_SHARED_UBER "${CMAKE_SOURCE_DIR}/code/qcommon/class.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/con_set.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/con_timer.cpp" + "${CMAKE_SOURCE_DIR}/code/qcommon/delegate.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/lightclass.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/listener.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/lz77.cpp" diff --git a/code/qcommon/delegate.cpp b/code/qcommon/delegate.cpp new file mode 100644 index 00000000..48899977 --- /dev/null +++ b/code/qcommon/delegate.cpp @@ -0,0 +1,52 @@ +/* +=========================================================================== +Copyright (C) 2025 the OpenMoHAA team + +This file is part of OpenMoHAA source code. + +OpenMoHAA source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +OpenMoHAA source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with OpenMoHAA source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ + +#include "delegate.h" + +uint64_t DelegateHandle::currentHandle = 0; + +DelegateHandle::DelegateHandle() + : handle(GenerateDelegateID()) +{} + +bool DelegateHandle::operator==(const DelegateHandle& other) const +{ + return handle == other.handle; +} + +bool DelegateHandle::operator!=(const DelegateHandle& other) const +{ + return handle != other.handle; +} + +uint64_t DelegateHandle::GenerateDelegateID() +{ + uint64_t handle; + + handle = ++currentHandle; + + if (handle == 0) { + handle = ++currentHandle; + } + + return handle; +} diff --git a/code/qcommon/delegate.h b/code/qcommon/delegate.h new file mode 100644 index 00000000..394e84c7 --- /dev/null +++ b/code/qcommon/delegate.h @@ -0,0 +1,125 @@ +/* +=========================================================================== +Copyright (C) 2025 the OpenMoHAA team + +This file is part of OpenMoHAA source code. + +OpenMoHAA source code is free software; you can redistribute it +and/or modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the License, +or (at your option) any later version. + +OpenMoHAA source code is distributed in the hope that it will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with OpenMoHAA source code; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +=========================================================================== +*/ + +#include +#include "container.h" + +template +using Delegate = std::function; + +struct DelegateHandle { +public: + DelegateHandle(); + + bool operator==(const DelegateHandle& other) const; + bool operator!=(const DelegateHandle& other) const; + +private: + static uint64_t GenerateDelegateID(); + static uint64_t currentHandle; + + uint64_t handle; +}; + +template +class DelegateMultiElement +{ +public: + DelegateMultiElement(Delegate&& inFunction); + + template + void Execute(Args&&...args) const; + + DelegateHandle GetHandle() const; + +private: + DelegateHandle handle; + Delegate func; +}; + +template +DelegateMultiElement::DelegateMultiElement(Delegate&& inFunction) + : func(inFunction) +{} + +template +template +void DelegateMultiElement::Execute(Args&&...args) const +{ + func(std::move(args)...); +} + +template +DelegateHandle DelegateMultiElement::GetHandle() const +{ + return handle; +} + +template +class MulticastDelegate +{ +public: + DelegateHandle Add(Delegate&& function); + void Remove(DelegateHandle handle); + + template + void Execute(Args&&...args); + +private: + Container> delegates; +}; + +template +DelegateHandle MulticastDelegate::Add(Delegate&& function) +{ + int index = delegates.AddObject(DelegateMultiElement(std::move(function))); + + return delegates.ObjectAt(index).GetHandle(); +} + +template +void MulticastDelegate::Remove(DelegateHandle handle) +{ + size_t i; + + for (i = delegates.NumObjects(); i > 0; i--) { + const DelegateMultiElement& elem = delegates.ObjectAt(i); + + if (elem.GetHandle() == handle) { + delegates.RemoveObjectAt(i); + break; + } + } +} + +template +template +void MulticastDelegate::Execute(Args&&...args) +{ + size_t i; + + for (i = 1; i <= delegates.NumObjects(); i++) { + const DelegateMultiElement& element = delegates.ObjectAt(i); + + element.Execute(std::move(args)...); + } +}