DarkflameServer/dGame/dPropertyBehaviors/ControlBehaviors.h
jadebenn 53877a0bc3
refactor: Rewrite AMF and property behavior logic to use smart pointers, references, and string_views over raw pointers and std::string& (#1452)
* Rewrite AMF and behavior logic to use smart pointers, references, and string_views over raw pointers and std::string&

* fix m_BehaviorID initialization

* Fix BlockDefinition member naming

* remove redundant reset()s

* Replace UB forward template declarations with header include

* remove unneeded comment

* remove non-const ref getters

* simplify default behavior id initialization

* Fix invalidated use of Getter to set a value

* Update AddStripMessage.cpp - change push_back to emplace_back

* fix pointer to ref conversion mistake (should not have directly grabbed from the other branch commit)

* deref

* VERY experimental testing of forward declaration of templates - probably will revert

* Revert changes (as expected)

* Update BlockDefinition.h - remove extraneous semicolons

* Update BlockDefinition.h - remove linebreak

* Update Amf3.h member naming scheme

* fix duplicated code

* const iterators

* const pointers

* reviving this branch

* update read switch cases
2024-11-18 20:45:24 -06:00

69 lines
2.1 KiB
C++

#pragma once
#ifndef __CONTROLBEHAVIORS__H__
#define __CONTROLBEHAVIORS__H__
#include <map>
#include <optional>
#include <string>
#include "BlockDefinition.h"
#include "Singleton.h"
class AMFArrayValue;
class Entity;
class ModelComponent;
class SystemAddress;
// Type definition to clarify what is used where
typedef std::string BlockName; //! A block name
struct ControlBehaviorContext {
ControlBehaviorContext(const AMFArrayValue& args, ModelComponent* modelComponent, Entity* modelOwner) noexcept
: arguments{ args }, modelComponent{ modelComponent }, modelOwner{ modelOwner } {
};
operator bool() const {
return modelComponent != nullptr && modelOwner != nullptr;
}
std::reference_wrapper<const AMFArrayValue> arguments;
ModelComponent* modelComponent;
Entity* modelOwner;
};
class ControlBehaviors: public Singleton<ControlBehaviors> {
public:
ControlBehaviors();
/**
* @brief Main driver for processing Property Behavior commands
*
* @param modelEntity The model that sent this command
* @param arguments The arguments formatted as an AMFArrayValue
* @param command The command to perform
* @param modelOwner The owner of the model which sent this command
*/
void ProcessCommand(Entity* const modelEntity, const AMFArrayValue& arguments, const std::string_view command, Entity* const modelOwner);
/**
* @brief Gets a blocks parameter values by the name
* No exception will be thrown in this function.
*
* @param blockName The block name to get the parameters of
*
* @return A pair of the block parameter name to its typing
*/
[[nodiscard]] std::optional<BlockDefinition> GetBlockInfo(const std::string_view blockName);
private:
void RequestUpdatedID(ControlBehaviorContext& context);
void SendBehaviorListToClient(const ControlBehaviorContext& context);
void SendBehaviorBlocksToClient(ControlBehaviorContext& context);
void UpdateAction(const AMFArrayValue& arguments);
std::map<BlockName, BlockDefinition, std::less<>> blockTypes{};
// If false, property behaviors will not be able to be edited.
bool isInitialized = false;
};
#endif //!__CONTROLBEHAVIORS__H__