From 5e6218ad6d9925817924fba28a2b072ea9b2d44f Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sat, 6 Jun 2020 13:58:51 +0200 Subject: [PATCH] [General] Modernize handling of client script local variables in packets Disable placeholder handling of client script member variables. --- apps/openmw-mp/Script/ScriptFunctions.hpp | 1 + .../object/ProcessorClientScriptLocal.hpp | 7 ++ apps/openmw/mwmp/ObjectList.cpp | 102 ++++++++++-------- apps/openmw/mwmp/ObjectList.hpp | 4 +- components/openmw-mp/Base/BaseObject.hpp | 7 +- components/openmw-mp/Base/BaseStructs.hpp | 2 +- .../Object/PacketClientScriptLocal.cpp | 29 +++-- .../Object/PacketScriptMemberShort.cpp | 6 +- 8 files changed, 97 insertions(+), 61 deletions(-) diff --git a/apps/openmw-mp/Script/ScriptFunctions.hpp b/apps/openmw-mp/Script/ScriptFunctions.hpp index 8bd689886..50de39ebc 100644 --- a/apps/openmw-mp/Script/ScriptFunctions.hpp +++ b/apps/openmw-mp/Script/ScriptFunctions.hpp @@ -213,6 +213,7 @@ public: {"OnWorldKillCount", Callback()}, {"OnWorldMap", Callback()}, {"OnWorldWeather", Callback()}, + {"OnClientScriptLocal", Callback()}, {"OnClientScriptGlobal", Callback()}, {"OnMpNumIncrement", Callback()}, {"OnRequestDataFileList", Callback<>()}, diff --git a/apps/openmw-mp/processors/object/ProcessorClientScriptLocal.hpp b/apps/openmw-mp/processors/object/ProcessorClientScriptLocal.hpp index b95efd8c0..8e20fe43f 100644 --- a/apps/openmw-mp/processors/object/ProcessorClientScriptLocal.hpp +++ b/apps/openmw-mp/processors/object/ProcessorClientScriptLocal.hpp @@ -12,6 +12,13 @@ namespace mwmp { BPP_INIT(ID_CLIENT_SCRIPT_LOCAL) } + + void Do(ObjectPacket& packet, Player& player, BaseObjectList& objectList) override + { + LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Received %s from %s", strPacketID.c_str(), player.npc.mName.c_str()); + + Script::Call(player.getId(), objectList.cell.getDescription().c_str()); + } }; } diff --git a/apps/openmw/mwmp/ObjectList.cpp b/apps/openmw/mwmp/ObjectList.cpp index 18640be3e..a4120d870 100644 --- a/apps/openmw/mwmp/ObjectList.cpp +++ b/apps/openmw/mwmp/ObjectList.cpp @@ -959,22 +959,7 @@ void ObjectList::setClientLocals(MWWorld::CellStore* cellStore) { for (const auto &baseObject : baseObjects) { - std::string valueAsString; - std::string variableTypeAsString; - - if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::SHORT || baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::LONG) - { - variableTypeAsString = baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::SHORT ? "short" : "long"; - valueAsString = std::to_string(baseObject.clientVariable.intValue); - } - else if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::FLOAT) - { - variableTypeAsString = "float"; - valueAsString = std::to_string(baseObject.clientVariable.floatValue); - } - - LOG_APPEND(TimedLog::LOG_VERBOSE, "- cellRef: %s %i-%i, index: %i, type %s, value: %s", baseObject.refId.c_str(), - baseObject.refNum, baseObject.mpNum, baseObject.index, variableTypeAsString.c_str(), valueAsString.c_str()); + LOG_APPEND(TimedLog::LOG_VERBOSE, "- cellRef: %s %i-%i", baseObject.refId.c_str(), baseObject.refNum, baseObject.mpNum); MWWorld::Ptr ptrFound = cellStore->searchExact(baseObject.refNum, baseObject.mpNum); @@ -983,18 +968,36 @@ void ObjectList::setClientLocals(MWWorld::CellStore* cellStore) LOG_APPEND(TimedLog::LOG_VERBOSE, "-- Found %s %i-%i", ptrFound.getCellRef().getRefId().c_str(), ptrFound.getCellRef().getRefNum(), ptrFound.getCellRef().getMpNum()); - if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::SHORT) - ptrFound.getRefData().getLocals().mShorts.at(baseObject.index) = baseObject.clientVariable.intValue; - else if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::LONG) - ptrFound.getRefData().getLocals().mLongs.at(baseObject.index) = baseObject.clientVariable.intValue; - else if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::FLOAT) - ptrFound.getRefData().getLocals().mFloats.at(baseObject.index) = baseObject.clientVariable.floatValue; + for (const auto& clientLocal : baseObject.clientLocals) + { + std::string valueAsString; + std::string variableTypeAsString; + + if (clientLocal.variableType == mwmp::VARIABLE_TYPE::SHORT || clientLocal.variableType == mwmp::VARIABLE_TYPE::LONG) + { + variableTypeAsString = clientLocal.variableType == mwmp::VARIABLE_TYPE::SHORT ? "short" : "long"; + valueAsString = std::to_string(clientLocal.intValue); + } + else if (clientLocal.variableType == mwmp::VARIABLE_TYPE::FLOAT) + { + variableTypeAsString = "float"; + valueAsString = std::to_string(clientLocal.floatValue); + } + + if (clientLocal.variableType == mwmp::VARIABLE_TYPE::SHORT) + ptrFound.getRefData().getLocals().mShorts.at(clientLocal.internalIndex) = clientLocal.intValue; + else if (clientLocal.variableType == mwmp::VARIABLE_TYPE::LONG) + ptrFound.getRefData().getLocals().mLongs.at(clientLocal.internalIndex) = clientLocal.intValue; + else if (clientLocal.variableType == mwmp::VARIABLE_TYPE::FLOAT) + ptrFound.getRefData().getLocals().mFloats.at(clientLocal.internalIndex) = clientLocal.floatValue; + } } } } void ObjectList::setMemberShorts() { + /* for (const auto &baseObject : baseObjects) { LOG_APPEND(TimedLog::LOG_VERBOSE, "- cellRef: %s, index: %i, shortVal: %i", baseObject.refId.c_str(), @@ -1016,6 +1019,7 @@ void ObjectList::setMemberShorts() ptrFound.getRefData().getLocals().mShorts.at(baseObject.index) = baseObject.shortVal;; } } + */ } void ObjectList::playMusic() @@ -1283,35 +1287,41 @@ void ObjectList::addVideoPlay(std::string filename, bool allowSkipping) addBaseObject(baseObject); } -void ObjectList::addClientScriptLocal(const MWWorld::Ptr& ptr, int index, int value, mwmp::VARIABLE_TYPE variableType) +void ObjectList::addClientScriptLocal(const MWWorld::Ptr& ptr, int internalIndex, int value, mwmp::VARIABLE_TYPE variableType) { cell = *ptr.getCell()->getCell(); mwmp::BaseObject baseObject = getBaseObjectFromPtr(ptr); - baseObject.clientVariable.index = index; - baseObject.clientVariable.variableType = variableType; - baseObject.clientVariable.intValue = value; + ClientVariable clientLocal; + clientLocal.internalIndex = internalIndex; + clientLocal.variableType = variableType; + clientLocal.intValue = value; + baseObject.clientLocals.push_back(clientLocal); addBaseObject(baseObject); } -void ObjectList::addClientScriptLocal(const MWWorld::Ptr& ptr, int index, float value) +void ObjectList::addClientScriptLocal(const MWWorld::Ptr& ptr, int internalIndex, float value) { cell = *ptr.getCell()->getCell(); mwmp::BaseObject baseObject = getBaseObjectFromPtr(ptr); - baseObject.clientVariable.index = index; - baseObject.clientVariable.variableType = mwmp::VARIABLE_TYPE::FLOAT; - baseObject.clientVariable.floatValue = value; + ClientVariable clientLocal; + clientLocal.internalIndex = internalIndex; + clientLocal.variableType = mwmp::VARIABLE_TYPE::FLOAT; + clientLocal.floatValue = value; + baseObject.clientLocals.push_back(clientLocal); addBaseObject(baseObject); } void ObjectList::addScriptMemberShort(std::string refId, int index, int shortVal) { + /* mwmp::BaseObject baseObject; baseObject.refId = refId; baseObject.index = index; baseObject.shortVal = shortVal; addBaseObject(baseObject); + */ } void ObjectList::sendObjectActivate() @@ -1438,22 +1448,26 @@ void ObjectList::sendClientScriptLocal() for (const auto &baseObject : baseObjects) { - std::string valueAsString; - std::string variableTypeAsString; + LOG_APPEND(TimedLog::LOG_VERBOSE, "- cellRef: %s %i-%i", baseObject.refId.c_str(), baseObject.refNum, baseObject.mpNum); - if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::SHORT || baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::LONG) + for (const auto& clientLocal : baseObject.clientLocals) { - variableTypeAsString = baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::SHORT ? "short" : "long"; - valueAsString = std::to_string(baseObject.clientVariable.intValue); - } - else if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::FLOAT) - { - variableTypeAsString = "float"; - valueAsString = std::to_string(baseObject.clientVariable.floatValue); - } + std::string valueAsString; + std::string variableTypeAsString; - LOG_APPEND(TimedLog::LOG_VERBOSE, "- cellRef: %s %i-%i, index: %i, type %s, value: %s", baseObject.refId.c_str(), - baseObject.refNum, baseObject.mpNum, baseObject.clientVariable.index, variableTypeAsString.c_str(), valueAsString.c_str()); + if (clientLocal.variableType == mwmp::VARIABLE_TYPE::SHORT || clientLocal.variableType == mwmp::VARIABLE_TYPE::LONG) + { + variableTypeAsString = clientLocal.variableType == mwmp::VARIABLE_TYPE::SHORT ? "short" : "long"; + valueAsString = std::to_string(clientLocal.intValue); + } + else if (clientLocal.variableType == mwmp::VARIABLE_TYPE::FLOAT) + { + variableTypeAsString = "float"; + valueAsString = std::to_string(clientLocal.floatValue); + } + + LOG_APPEND(TimedLog::LOG_VERBOSE, "- type %s, value: %s", variableTypeAsString.c_str(), valueAsString.c_str()); + } } mwmp::Main::get().getNetworking()->getObjectPacket(ID_CLIENT_SCRIPT_LOCAL)->setObjectList(this); @@ -1462,6 +1476,7 @@ void ObjectList::sendClientScriptLocal() void ObjectList::sendScriptMemberShort() { + /* LOG_MESSAGE_SIMPLE(TimedLog::LOG_VERBOSE, "Sending ID_SCRIPT_MEMBER_SHORT"); for (const auto &baseObject : baseObjects) @@ -1470,6 +1485,7 @@ void ObjectList::sendScriptMemberShort() mwmp::Main::get().getNetworking()->getObjectPacket(ID_SCRIPT_MEMBER_SHORT)->setObjectList(this); mwmp::Main::get().getNetworking()->getObjectPacket(ID_SCRIPT_MEMBER_SHORT)->Send(); + */ } void ObjectList::sendContainer() diff --git a/apps/openmw/mwmp/ObjectList.hpp b/apps/openmw/mwmp/ObjectList.hpp index cf6ba9100..5b3d69b90 100644 --- a/apps/openmw/mwmp/ObjectList.hpp +++ b/apps/openmw/mwmp/ObjectList.hpp @@ -70,8 +70,8 @@ namespace mwmp void addDoorState(const MWWorld::Ptr& ptr, MWWorld::DoorState state); void addMusicPlay(std::string filename); void addVideoPlay(std::string filename, bool allowSkipping); - void addClientScriptLocal(const MWWorld::Ptr& ptr, int index, int value, mwmp::VARIABLE_TYPE variableType); - void addClientScriptLocal(const MWWorld::Ptr& ptr, int index, float value); + void addClientScriptLocal(const MWWorld::Ptr& ptr, int internalIndex, int value, mwmp::VARIABLE_TYPE variableType); + void addClientScriptLocal(const MWWorld::Ptr& ptr, int internalIndex, float value); void addScriptMemberShort(std::string refId, int index, int shortVal); void sendObjectActivate(); diff --git a/components/openmw-mp/Base/BaseObject.hpp b/components/openmw-mp/Base/BaseObject.hpp index eaa4d92b2..584eeeb2d 100644 --- a/components/openmw-mp/Base/BaseObject.hpp +++ b/components/openmw-mp/Base/BaseObject.hpp @@ -63,12 +63,6 @@ namespace mwmp std::string animGroup; int animMode; - ClientVariable clientVariable; - - int index; - int shortVal; - float floatVal; - bool isDisarmed; bool droppedByPlayer; @@ -84,6 +78,7 @@ namespace mwmp bool hasContainer; + std::vector clientLocals; std::vector containerItems; unsigned int containerItemCount; diff --git a/components/openmw-mp/Base/BaseStructs.hpp b/components/openmw-mp/Base/BaseStructs.hpp index bef694d7e..32a82ae19 100644 --- a/components/openmw-mp/Base/BaseStructs.hpp +++ b/components/openmw-mp/Base/BaseStructs.hpp @@ -30,7 +30,7 @@ namespace mwmp struct ClientVariable { std::string id; - int index; + int internalIndex; char variableType; diff --git a/components/openmw-mp/Packets/Object/PacketClientScriptLocal.cpp b/components/openmw-mp/Packets/Object/PacketClientScriptLocal.cpp index b1c0abcd1..902cb01b8 100644 --- a/components/openmw-mp/Packets/Object/PacketClientScriptLocal.cpp +++ b/components/openmw-mp/Packets/Object/PacketClientScriptLocal.cpp @@ -12,11 +12,28 @@ PacketClientScriptLocal::PacketClientScriptLocal(RakNet::RakPeerInterface *peer) void PacketClientScriptLocal::Object(BaseObject &baseObject, bool send) { ObjectPacket::Object(baseObject, send); - RW(baseObject.clientVariable.index, send); - RW(baseObject.clientVariable.variableType, send); - if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::SHORT || baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::LONG) - RW(baseObject.clientVariable.intValue, send); - else if (baseObject.clientVariable.variableType == mwmp::VARIABLE_TYPE::FLOAT) - RW(baseObject.clientVariable.floatValue, send); + uint32_t clientLocalsCount; + + if (send) + clientLocalsCount = static_cast(baseObject.clientLocals.size()); + + RW(clientLocalsCount, send); + + if (!send) + { + baseObject.clientLocals.clear(); + baseObject.clientLocals.resize(clientLocalsCount); + } + + for (auto&& clientLocal : baseObject.clientLocals) + { + RW(clientLocal.internalIndex, send); + RW(clientLocal.variableType, send); + + if (clientLocal.variableType == mwmp::VARIABLE_TYPE::SHORT || clientLocal.variableType == mwmp::VARIABLE_TYPE::LONG) + RW(clientLocal.intValue, send); + else if (clientLocal.variableType == mwmp::VARIABLE_TYPE::FLOAT) + RW(clientLocal.floatValue, send); + } } diff --git a/components/openmw-mp/Packets/Object/PacketScriptMemberShort.cpp b/components/openmw-mp/Packets/Object/PacketScriptMemberShort.cpp index 8cae9ab07..3203492a0 100644 --- a/components/openmw-mp/Packets/Object/PacketScriptMemberShort.cpp +++ b/components/openmw-mp/Packets/Object/PacketScriptMemberShort.cpp @@ -10,7 +10,7 @@ PacketScriptMemberShort::PacketScriptMemberShort(RakNet::RakPeerInterface *peer) void PacketScriptMemberShort::Object(BaseObject &baseObject, bool send) { - RW(baseObject.refId, send); - RW(baseObject.index, send); - RW(baseObject.shortVal, send); + //RW(baseObject.refId, send); + //RW(baseObject.index, send); + //RW(baseObject.shortVal, send); }