From 6b3b5cdab48d1e80d54fcf350d069b26c9698cb9 Mon Sep 17 00:00:00 2001 From: Koncord Date: Thu, 3 Jan 2019 07:36:07 +0800 Subject: [PATCH] [General] Fix warnings and errors found by PVS Studio in Server --- apps/openmw-mp/CMakeLists.txt | 2 +- apps/openmw-mp/Cell.cpp | 4 +- apps/openmw-mp/CellController.cpp | 4 +- apps/openmw-mp/CellController.hpp | 4 +- apps/openmw-mp/MasterClient.cpp | 15 +++--- apps/openmw-mp/MasterClient.hpp | 10 ++-- apps/openmw-mp/Networking.cpp | 40 +++++++-------- apps/openmw-mp/Networking.hpp | 14 +++--- apps/openmw-mp/Player.cpp | 14 +++--- apps/openmw-mp/Player.hpp | 16 +++--- apps/openmw-mp/Script/API/PublicFnAPI.cpp | 2 +- apps/openmw-mp/Script/API/TimerAPI.cpp | 14 +++--- apps/openmw-mp/Script/API/TimerAPI.hpp | 6 +-- apps/openmw-mp/Script/Functions/Cells.cpp | 2 +- apps/openmw-mp/Script/Functions/CharClass.cpp | 2 +- apps/openmw-mp/Script/Functions/Mechanics.cpp | 2 +- apps/openmw-mp/Script/Functions/Server.cpp | 2 +- apps/openmw-mp/Script/Functions/Stats.cpp | 4 +- apps/openmw-mp/Script/LangLua/LangLua.cpp | 4 +- apps/openmw-mp/Script/LangLua/LuaFunc.cpp | 12 +++-- apps/openmw-mp/Script/LangMono/LangMono.cpp | 50 +++++++------------ apps/openmw-mp/Script/LangMono/LangMono.hpp | 23 +++++++-- .../Script/LangNative/LangNative.cpp | 2 +- apps/openmw-mp/Script/ScriptFunction.cpp | 25 ++++------ apps/openmw-mp/Script/ScriptFunctions.cpp | 2 +- apps/openmw-mp/Utils.cpp | 3 +- apps/openmw-mp/Utils.hpp | 2 +- apps/openmw-mp/main.cpp | 12 +++-- apps/openmw-mp/processors/ActorProcessor.cpp | 2 +- apps/openmw-mp/processors/ObjectProcessor.cpp | 2 +- .../processors/WorldstateProcessor.cpp | 2 +- components/openmw-mp/Base/BaseActor.hpp | 2 +- components/openmw-mp/Base/BaseObject.hpp | 5 +- components/openmw-mp/Master/MasterData.hpp | 6 +-- components/openmw-mp/Packets/BasePacket.hpp | 2 +- 35 files changed, 158 insertions(+), 155 deletions(-) diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index aa6a9150a..9b68ce353 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -223,4 +223,4 @@ endif (MSVC) pvs_studio_add_target(TARGET tes3mp-server.analyze ALL OUTPUT FORMAT errorfile ANALYZE tes3mp-server - LOG target.err) + LOG target_tes3mp-server.err) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 2da4f3ea3..f6138251d 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -55,7 +55,7 @@ void Cell::addPlayer(Player *player) void Cell::removePlayer(Player *player, bool cleanPlayer) { - for (Iterator it = begin(); it != end(); it++) + for (auto it = begin(); it != end(); ++it) { if (*it == player) { @@ -161,7 +161,7 @@ void Cell::removeActors(const mwmp::BaseActorList *newActorList) } if (!foundActor) - it++; + ++it; } cellActorList.count = cellActorList.baseActors.size(); diff --git a/apps/openmw-mp/CellController.cpp b/apps/openmw-mp/CellController.cpp index 493ebd5c5..bdb3c4a87 100644 --- a/apps/openmw-mp/CellController.cpp +++ b/apps/openmw-mp/CellController.cpp @@ -64,7 +64,7 @@ Cell *CellController::getCellByXY(int x, int y) return *it; } -Cell *CellController::getCellByName(std::string cellName) +Cell *CellController::getCellByName(const std::string &cellName) { auto it = find_if(cells.begin(), cells.end(), [cellName](const Cell *c) { @@ -80,7 +80,7 @@ Cell *CellController::getCellByName(std::string cellName) return *it; } -Cell *CellController::addCell(ESM::Cell cellData) +Cell *CellController::addCell(const ESM::Cell &cellData) { LOG_APPEND(Log::LOG_INFO, "- Loaded cells: %d", cells.size()); auto it = find_if(cells.begin(), cells.end(), [cellData](const Cell *c) { diff --git a/apps/openmw-mp/CellController.hpp b/apps/openmw-mp/CellController.hpp index 7c3d5a686..477fdd902 100644 --- a/apps/openmw-mp/CellController.hpp +++ b/apps/openmw-mp/CellController.hpp @@ -27,14 +27,14 @@ public: typedef std::deque TContainer; typedef TContainer::iterator TIter; - Cell * addCell(ESM::Cell cell); + Cell * addCell(const ESM::Cell &cell); void removeCell(Cell *); void deletePlayer(Player *player); Cell *getCell(ESM::Cell *esmCell); Cell *getCellByXY(int x, int y); - Cell *getCellByName(std::string cellName); + Cell *getCellByName(const std::string &cellName); void update(Player *player); diff --git a/apps/openmw-mp/MasterClient.cpp b/apps/openmw-mp/MasterClient.cpp index a13718d42..92d96dab3 100644 --- a/apps/openmw-mp/MasterClient.cpp +++ b/apps/openmw-mp/MasterClient.cpp @@ -51,7 +51,7 @@ void MasterClient::SetMaxPlayers(unsigned pl) mutexData.unlock(); } -void MasterClient::SetHostname(std::string hostname) +void MasterClient::SetHostname(const std::string &hostname) { mutexData.lock(); string substr = hostname.substr(0, 200); @@ -63,7 +63,7 @@ void MasterClient::SetHostname(std::string hostname) mutexData.unlock(); } -void MasterClient::SetModname(std::string modname) +void MasterClient::SetModname(const std::string &modname) { mutexData.lock(); string substr = modname.substr(0, 200); @@ -75,7 +75,7 @@ void MasterClient::SetModname(std::string modname) mutexData.unlock(); } -void MasterClient::SetRuleString(std::string key, std::string value) +void MasterClient::SetRuleString(const std::string &key, std::string value) { mutexData.lock(); if (queryData.rules.find(key) == queryData.rules.end() || queryData.rules[key].type != 's' @@ -90,7 +90,7 @@ void MasterClient::SetRuleString(std::string key, std::string value) mutexData.unlock(); } -void MasterClient::SetRuleValue(std::string key, double value) +void MasterClient::SetRuleValue(const std::string &key, double value) { mutexData.lock(); if (queryData.rules.find(key) == queryData.rules.end() || queryData.rules[key].type != 'v' @@ -105,7 +105,7 @@ void MasterClient::SetRuleValue(std::string key, double value) mutexData.unlock(); } -void MasterClient::PushPlugin(Plugin plugin) +void MasterClient::PushPlugin(const Plugin &plugin) { mutexData.lock(); queryData.plugins.push_back(plugin); @@ -208,7 +208,7 @@ void MasterClient::Thread() } else { - for (int i = 0; pIt != players->end(); i++, pIt++) + for (int i = 0; pIt != players->end(); i++, ++pIt) { if (queryData.players[i] != pIt->second->npc.mName) { @@ -258,5 +258,6 @@ void MasterClient::SetUpdateRate(unsigned int rate) timeout = min_rate; else if (timeout > max_rate) timeout = max_rate; - timeout = rate; + else + timeout = rate; } diff --git a/apps/openmw-mp/MasterClient.hpp b/apps/openmw-mp/MasterClient.hpp index 858946297..b8b80be33 100644 --- a/apps/openmw-mp/MasterClient.hpp +++ b/apps/openmw-mp/MasterClient.hpp @@ -22,11 +22,11 @@ public: MasterClient(RakNet::RakPeerInterface *peer, std::string queryAddr, unsigned short queryPort); void SetPlayers(unsigned pl); void SetMaxPlayers(unsigned pl); - void SetHostname(std::string hostname); - void SetModname(std::string hostname); - void SetRuleString(std::string key, std::string value); - void SetRuleValue(std::string key, double value); - void PushPlugin(Plugin plugin); + void SetHostname(const std::string &hostname); + void SetModname(const std::string &hostname); + void SetRuleString(const std::string &key, std::string value); + void SetRuleValue(const std::string &key, double value); + void PushPlugin(const Plugin &plugin); bool Process(RakNet::Packet *packet); void Start(); diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index a3bab8141..0b7c45ecb 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -75,7 +75,7 @@ Networking::~Networking() delete worldstatePacketController; } -void Networking::setServerPassword(std::string passw) noexcept +void Networking::setServerPassword(const std::string &passw) noexcept { serverPassword = passw.empty() ? TES3MP_DEFAULT_PASSW : passw; } @@ -239,7 +239,7 @@ bool Networking::preInit(RakNet::Packet *packet, RakNet::BitStream &bsIn) auto plugin = plugins.begin(); if (samples.size() == plugins.size()) { - for (int i = 0; plugin != plugins.end(); plugin++, i++) + for (int i = 0; plugin != plugins.end(); ++plugin, i++) { LOG_APPEND(Log::LOG_VERBOSE, "- %X\t%s", plugin->second[0], plugin->first.c_str()); // Check if the filenames match, ignoring case @@ -311,7 +311,7 @@ void Networking::update(RakNet::Packet *packet, RakNet::BitStream &bsIn) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled RakNet packet with identifier %i has arrived", packet->data[0]); } -void Networking::newPlayer(RakNet::RakNetGUID guid) +void Networking::newPlayer(const RakNet::RakNetGUID &guid) { playerPacketController->GetPacket(ID_PLAYER_BASEINFO)->RequestData(guid); playerPacketController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->RequestData(guid); @@ -321,27 +321,27 @@ void Networking::newPlayer(RakNet::RakNetGUID guid) LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Sending info about other players to %lu", guid.g); - for (TPlayers::iterator pl = players->begin(); pl != players->end(); pl++) //sending other players to new player + for (auto &player : *players) //sending other players to new player { // If we are iterating over the new player, don't send the packets below - if (pl->first == guid) continue; + if (player.first == guid) continue; // If an invalid key makes it into the Players map, ignore it - else if (pl->first == RakNet::UNASSIGNED_CRABNET_GUID) continue; + else if (player.first == RakNet::UNASSIGNED_CRABNET_GUID) continue; // if player not fully connected - else if (pl->second == nullptr) continue; + else if (player.second == nullptr) continue; // If we are iterating over a player who has inputted their name, proceed - else if (pl->second->getLoadState() == Player::POSTLOADED) + else if (player.second->getLoadState() == Player::POSTLOADED) { - playerPacketController->GetPacket(ID_PLAYER_BASEINFO)->setPlayer(pl->second); - playerPacketController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->setPlayer(pl->second); - playerPacketController->GetPacket(ID_PLAYER_ATTRIBUTE)->setPlayer(pl->second); - playerPacketController->GetPacket(ID_PLAYER_SKILL)->setPlayer(pl->second); - playerPacketController->GetPacket(ID_PLAYER_POSITION)->setPlayer(pl->second); - playerPacketController->GetPacket(ID_PLAYER_CELL_CHANGE)->setPlayer(pl->second); - playerPacketController->GetPacket(ID_PLAYER_EQUIPMENT)->setPlayer(pl->second); + playerPacketController->GetPacket(ID_PLAYER_BASEINFO)->setPlayer(player.second); + playerPacketController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->setPlayer(player.second); + playerPacketController->GetPacket(ID_PLAYER_ATTRIBUTE)->setPlayer(player.second); + playerPacketController->GetPacket(ID_PLAYER_SKILL)->setPlayer(player.second); + playerPacketController->GetPacket(ID_PLAYER_POSITION)->setPlayer(player.second); + playerPacketController->GetPacket(ID_PLAYER_CELL_CHANGE)->setPlayer(player.second); + playerPacketController->GetPacket(ID_PLAYER_EQUIPMENT)->setPlayer(player.second); playerPacketController->GetPacket(ID_PLAYER_BASEINFO)->Send(guid); playerPacketController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->Send(guid); @@ -357,7 +357,7 @@ void Networking::newPlayer(RakNet::RakNetGUID guid) } -void Networking::disconnectPlayer(RakNet::RakNetGUID guid) +void Networking::disconnectPlayer(const RakNet::RakNetGUID &guid) { Player *player = Players::getPlayer(guid); if (!player) @@ -452,7 +452,7 @@ Networking *Networking::getPtr() return sThis; } -RakNet::SystemAddress Networking::getSystemAddress(RakNet::RakNetGUID guid) +RakNet::SystemAddress Networking::getSystemAddress(const RakNet::RakNetGUID &guid) { return peer->GetSystemAddressFromGuid(guid); } @@ -532,7 +532,7 @@ int Networking::mainLoop() return exitCode; } -void Networking::kickPlayer(RakNet::RakNetGUID guid, bool sendNotification) +void Networking::kickPlayer(const RakNet::RakNetGUID &guid, bool sendNotification) { peer->CloseConnection(guid, sendNotification); } @@ -557,7 +557,7 @@ unsigned int Networking::maxConnections() const return peer->GetMaximumIncomingConnections(); } -int Networking::getAvgPing(RakNet::AddressOrGUID addr) const +int Networking::getAvgPing(const RakNet::AddressOrGUID &addr) const { return peer->GetAveragePing(addr); } @@ -572,7 +572,7 @@ MasterClient *Networking::getMasterClient() return mclient; } -void Networking::InitQuery(std::string queryAddr, unsigned short queryPort) +void Networking::InitQuery(const std::string &queryAddr, unsigned short queryPort) { mclient = new MasterClient(peer, queryAddr, queryPort); } diff --git a/apps/openmw-mp/Networking.hpp b/apps/openmw-mp/Networking.hpp index 211bc8c1c..d1e397ffd 100644 --- a/apps/openmw-mp/Networking.hpp +++ b/apps/openmw-mp/Networking.hpp @@ -17,13 +17,13 @@ namespace mwmp Networking(RakNet::RakPeerInterface *peer); ~Networking(); - void newPlayer(RakNet::RakNetGUID guid); - void disconnectPlayer(RakNet::RakNetGUID guid); - void kickPlayer(RakNet::RakNetGUID guid, bool sendNotification = true); + void newPlayer(const RakNet::RakNetGUID &guid); + void disconnectPlayer(const RakNet::RakNetGUID &guid); + void kickPlayer(const RakNet::RakNetGUID &guid, bool sendNotification = true); void banAddress(const char *ipAddress); void unbanAddress(const char *ipAddress); - RakNet::SystemAddress getSystemAddress(RakNet::RakNetGUID guid); + RakNet::SystemAddress getSystemAddress(const RakNet::RakNetGUID &guid); void processPlayerPacket(RakNet::Packet *packet); void processActorPacket(RakNet::Packet *packet); @@ -33,7 +33,7 @@ namespace mwmp unsigned short numberOfConnections() const; unsigned int maxConnections() const; - int getAvgPing(RakNet::AddressOrGUID) const; + int getAvgPing(const RakNet::AddressOrGUID &) const; unsigned short getPort() const; int mainLoop(); @@ -60,8 +60,8 @@ namespace mwmp void setScriptErrorIgnoringState(bool state); MasterClient *getMasterClient(); - void InitQuery(std::string queryAddr, unsigned short queryPort); - void setServerPassword(std::string passw) noexcept; + void InitQuery(const std::string &queryAddr, unsigned short queryPort); + void setServerPassword(const std::string &passw) noexcept; bool isPassworded() const; static const Networking &get(); diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 296496702..d0171acd3 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -10,7 +10,7 @@ TSlots Players::slots; using namespace std; -void Players::deletePlayer(RakNet::RakNetGUID guid) +void Players::deletePlayer(const RakNet::RakNetGUID &guid) { LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Deleting player with guid %lu", guid.g); @@ -26,7 +26,7 @@ void Players::deletePlayer(RakNet::RakNetGUID guid) } } -void Players::newPlayer(RakNet::RakNetGUID guid) +void Players::newPlayer(const RakNet::RakNetGUID &guid) { LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Creating new player with guid %lu", guid.g); @@ -41,7 +41,7 @@ void Players::newPlayer(RakNet::RakNetGUID guid) for (unsigned int i = 0; i < mwmp::Networking::get().maxConnections(); i++) { - if (slots[i] == 0) + if (slots[i] == nullptr) { LOG_APPEND(Log::LOG_INFO, "- Storing in slot %i", i); @@ -52,7 +52,7 @@ void Players::newPlayer(RakNet::RakNetGUID guid) } } -Player *Players::getPlayer(RakNet::RakNetGUID guid) +Player *Players::getPlayer(const RakNet::RakNetGUID &guid) { auto it = players.find(guid); if (it == players.end()) @@ -70,7 +70,7 @@ unsigned short Players::getLastPlayerId() return slots.rbegin()->first; } -Player::Player(RakNet::RakNetGUID guid) : BasePlayer(guid) +Player::Player(const RakNet::RakNetGUID &guid) : BasePlayer(guid), id(InvalidID) { handshakeCounter = 0; loadState = NOTLOADED; @@ -81,7 +81,7 @@ Player::~Player() } -unsigned short Player::getId() +unsigned int Player::getId() { return id; } @@ -177,7 +177,7 @@ void Player::forEachLoaded(std::function func) } } -bool Players::doesPlayerExist(RakNet::RakNetGUID guid) +bool Players::doesPlayerExist(const RakNet::RakNetGUID &guid) { return players.find(guid) != players.end(); } diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index a59319f3b..1aa83985f 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -28,13 +28,13 @@ typedef std::map TSlots; class Players { public: - static void newPlayer(RakNet::RakNetGUID guid); - static void deletePlayer(RakNet::RakNetGUID guid); - static Player *getPlayer(RakNet::RakNetGUID guid); + static void newPlayer(const RakNet::RakNetGUID &guid); + static void deletePlayer(const RakNet::RakNetGUID &guid); + static Player *getPlayer(const RakNet::RakNetGUID &guid); static Player *getPlayer(unsigned short id); static TPlayers *getPlayers(); static unsigned short getLastPlayerId(); - static bool doesPlayerExist(RakNet::RakNetGUID guid); + static bool doesPlayerExist(const RakNet::RakNetGUID &guid); private: static TPlayers players; @@ -44,9 +44,8 @@ private: class Player : public mwmp::BasePlayer { friend class Cell; - unsigned short id; public: - + const unsigned int InvalidID = (unsigned int) -1; enum { NOTLOADED=0, @@ -54,9 +53,9 @@ public: POSTLOADED, KICKED }; - Player(RakNet::RakNetGUID guid); + Player(const RakNet::RakNetGUID &guid); - unsigned short getId(); + unsigned int getId(); void setId(unsigned short id); bool isHandshaked(); @@ -78,6 +77,7 @@ private: CellController::TContainer cells; int loadState; int handshakeCounter; + unsigned int id; }; diff --git a/apps/openmw-mp/Script/API/PublicFnAPI.cpp b/apps/openmw-mp/Script/API/PublicFnAPI.cpp index b0470b95e..30b995f57 100644 --- a/apps/openmw-mp/Script/API/PublicFnAPI.cpp +++ b/apps/openmw-mp/Script/API/PublicFnAPI.cpp @@ -80,7 +80,7 @@ bool Public::IsLua(const std::string &name) void Public::DeleteAll() { - for (auto it = publics.begin(); it != publics.end(); it++) + for (auto it = publics.begin(); it != publics.end(); ++it) { Public *_public = it->second; delete _public; diff --git a/apps/openmw-mp/Script/API/TimerAPI.cpp b/apps/openmw-mp/Script/API/TimerAPI.cpp index 6aac348b5..fd4c5c50e 100644 --- a/apps/openmw-mp/Script/API/TimerAPI.cpp +++ b/apps/openmw-mp/Script/API/TimerAPI.cpp @@ -10,27 +10,27 @@ using namespace mwmp; using namespace std; -Timer::Timer(ScriptFunc callback, long msec, const std::string& def, std::vector args) : ScriptFunction(callback, 'v', def) +Timer::Timer(ScriptFunc callback, long msec, const std::string& def, std::vector args) : ScriptFunction(callback, 'v', def), args(args) { + startTime = 0; targetMsec = msec; - this->args = args; end = true; } #if defined(ENABLE_LUA) -Timer::Timer(lua_State *lua, ScriptFuncLua callback, long msec, const std::string& def, std::vector args): ScriptFunction(callback, lua, 'v', def) +Timer::Timer(lua_State *lua, ScriptFuncLua callback, long msec, const std::string& def, std::vector args): ScriptFunction(callback, lua, 'v', def), args(args) { + startTime = 0; targetMsec = msec; - this->args = args; end = true; } #endif #ifdef ENABLE_MONO -Timer::Timer(MonoObject *callback, long msec, const std::string &def, std::vector args) : ScriptFunction(callback, 'v', def) +Timer::Timer(MonoObject *callback, long msec, const std::string &def, std::vector args) : ScriptFunction(callback, 'v', def), args(args) { + startTime = 0; targetMsec = msec; - this->args = args; end = true; } #endif @@ -125,7 +125,7 @@ int TimerAPI::CreateTimerMono(MonoObject *callback, long msec, const std::string } #endif -int TimerAPI::CreateTimer(ScriptFunc callback, long msec, const std::string &def, std::vector args) +int TimerAPI::CreateTimer(ScriptFunc callback, long msec, const std::string &def, const std::vector &args) { int id = -1; diff --git a/apps/openmw-mp/Script/API/TimerAPI.hpp b/apps/openmw-mp/Script/API/TimerAPI.hpp index ae324329d..7c1c4a20d 100644 --- a/apps/openmw-mp/Script/API/TimerAPI.hpp +++ b/apps/openmw-mp/Script/API/TimerAPI.hpp @@ -40,9 +40,9 @@ namespace mwmp void Restart(int msec); private: double startTime, targetMsec; - std::string publ, arg_types; + //std::string publ, arg_types; std::vector args; - Script *scr; + //Script *scr; bool end; }; @@ -55,7 +55,7 @@ namespace mwmp #if defined(ENABLE_MONO) static int CreateTimerMono(MonoObject *callback, long msec, const std::string& def, std::vector args); #endif - static int CreateTimer(ScriptFunc callback, long msec, const std::string& def, std::vector args); + static int CreateTimer(ScriptFunc callback, long msec, const std::string& def, const std::vector &args); static void FreeTimer(int timerid); static void ResetTimer(int timerid, long msec); static void StartTimer(int timerid); diff --git a/apps/openmw-mp/Script/Functions/Cells.cpp b/apps/openmw-mp/Script/Functions/Cells.cpp index c78198fba..b497dbd2f 100644 --- a/apps/openmw-mp/Script/Functions/Cells.cpp +++ b/apps/openmw-mp/Script/Functions/Cells.cpp @@ -45,7 +45,7 @@ extern "C" const char *CellFunctions::GetCell(unsigned short pid) noexcept Player *player; GET_PLAYER(pid, player, 0); - tempCellDescription = player->cell.getDescription().c_str(); + tempCellDescription = player->cell.getDescription(); return tempCellDescription.c_str(); } diff --git a/apps/openmw-mp/Script/Functions/CharClass.cpp b/apps/openmw-mp/Script/Functions/CharClass.cpp index f5243b94b..64bdb78ec 100644 --- a/apps/openmw-mp/Script/Functions/CharClass.cpp +++ b/apps/openmw-mp/Script/Functions/CharClass.cpp @@ -99,7 +99,7 @@ extern "C" void CharClassFunctions::SetClassName(unsigned short pid, const char GET_PLAYER(pid, player,); player->charClass.mName = name; - player->charClass.mId = ""; + player->charClass.mId.clear(); } extern "C" void CharClassFunctions::SetClassDesc(unsigned short pid, const char *desc) noexcept diff --git a/apps/openmw-mp/Script/Functions/Mechanics.cpp b/apps/openmw-mp/Script/Functions/Mechanics.cpp index f10ed0dd7..7d04159f8 100644 --- a/apps/openmw-mp/Script/Functions/Mechanics.cpp +++ b/apps/openmw-mp/Script/Functions/Mechanics.cpp @@ -24,7 +24,7 @@ extern "C" const char *MechanicsFunctions::GetMarkCell(unsigned short pid) noexc Player *player; GET_PLAYER(pid, player, 0); - tempCellDescription = player->cell.getDescription().c_str(); + tempCellDescription = player->cell.getDescription(); return tempCellDescription.c_str(); } diff --git a/apps/openmw-mp/Script/Functions/Server.cpp b/apps/openmw-mp/Script/Functions/Server.cpp index 5bef7765e..c83bc4450 100644 --- a/apps/openmw-mp/Script/Functions/Server.cpp +++ b/apps/openmw-mp/Script/Functions/Server.cpp @@ -151,7 +151,7 @@ extern "C" void ServerFunctions::AddPluginHash(const char *pluginName, const cha unsigned hash = 0; - if (strlen(hashStr) != 0) + if (hashStr[0] != '\0') { hash = (unsigned) std::stoul(hashStr); hashList.push_back(hash); diff --git a/apps/openmw-mp/Script/Functions/Stats.cpp b/apps/openmw-mp/Script/Functions/Stats.cpp index 8ff3a04a0..ef19b207e 100644 --- a/apps/openmw-mp/Script/Functions/Stats.cpp +++ b/apps/openmw-mp/Script/Functions/Stats.cpp @@ -239,7 +239,7 @@ extern "C" int StatsFunctions::GetSkillIncrease(unsigned short pid, unsigned int Player *player; GET_PLAYER(pid, player, 0); - if (attributeId > Attribute::Length) + if (attributeId >= Attribute::Length) return 0; return player->npcStats.mSkillIncrease[attributeId]; @@ -484,7 +484,7 @@ extern "C" void StatsFunctions::SetSkillIncrease(unsigned short pid, unsigned in Player *player; GET_PLAYER(pid, player,); - if (attributeId > Attribute::Length) + if (attributeId >= Attribute::Length) return; player->npcStats.mSkillIncrease[attributeId] = value; diff --git a/apps/openmw-mp/Script/LangLua/LangLua.cpp b/apps/openmw-mp/Script/LangLua/LangLua.cpp index 2b264a06f..469bcdc1c 100644 --- a/apps/openmw-mp/Script/LangLua/LangLua.cpp +++ b/apps/openmw-mp/Script/LangLua/LangLua.cpp @@ -223,7 +223,7 @@ boost::any LangLua::Call(const char *name, const char *argl, int buf, ...) break; default: - throw runtime_error("C++ call: Unknown argument identifier " + argl[index]); + throw runtime_error(std::string("C++ call: Unknown argument identifier ") + argl[index]); } } @@ -275,7 +275,7 @@ boost::any LangLua::Call(const char *name, const char *argl, const std::vector::push(lua, boost::any_cast(args.at(index))); break; default: - throw runtime_error("Lua call: Unknown argument identifier " + argl[index]); + throw runtime_error(std::string("Lua call: Unknown argument identifier ") + argl[index]); } } diff --git a/apps/openmw-mp/Script/LangLua/LuaFunc.cpp b/apps/openmw-mp/Script/LangLua/LuaFunc.cpp index 64fca01d7..e7b90a48b 100644 --- a/apps/openmw-mp/Script/LangLua/LuaFunc.cpp +++ b/apps/openmw-mp/Script/LangLua/LuaFunc.cpp @@ -9,7 +9,7 @@ using namespace std; -inline vector DefToVec(lua_State *lua, string types, int args_begin, int args_n) +inline vector DefToVec(lua_State *lua, const string &types, int args_begin, int args_n) { vector args; @@ -93,13 +93,15 @@ int LangLua::CallPublic(lua_State *lua) if (result.empty()) return 0; - if (result.type().hash_code() == typeid(signed int).hash_code()) + auto retTypeHash = result.type().hash_code(); + + if (retTypeHash == typeid(signed int).hash_code()) luabridge::Stack::push(lua, boost::any_cast(result)); - else if (result.type().hash_code() == typeid(unsigned int).hash_code()) + else if (retTypeHash == typeid(unsigned int).hash_code()) luabridge::Stack::push(lua, boost::any_cast(result)); - else if (result.type().hash_code() == typeid(double).hash_code()) + else if (retTypeHash == typeid(double).hash_code()) luabridge::Stack::push(lua, boost::any_cast(result)); - else if (result.type().hash_code() == typeid(const char*).hash_code()) + else if (retTypeHash == typeid(const char*).hash_code()) luabridge::Stack::push(lua, boost::any_cast(result)); return 1; } diff --git a/apps/openmw-mp/Script/LangMono/LangMono.cpp b/apps/openmw-mp/Script/LangMono/LangMono.cpp index 5ed6ff8b7..7ca62ad2b 100644 --- a/apps/openmw-mp/Script/LangMono/LangMono.cpp +++ b/apps/openmw-mp/Script/LangMono/LangMono.cpp @@ -191,7 +191,7 @@ MonoObject *LangMono::AnyToObject(boost::any any, char ret_type) } default: - throw std::runtime_error("Mono call: Unknown argument identifier " + ret_type); + throw std::runtime_error(std::string("Mono call: Unknown argument identifier ") + ret_type); } return object; } @@ -366,6 +366,7 @@ boost::any LangMono::Call(const char *name, const char *argl, int buf, ...) int n_args = (int) (strlen(argl)); std::vector vec(n_args); + TMonoArgsStore argsStore(n_args); for (int index = 0; index < n_args; index++) { @@ -373,38 +374,32 @@ boost::any LangMono::Call(const char *name, const char *argl, int buf, ...) { case 'i': { - auto val = va_arg(vargs, unsigned int); - vec[index] = (void *) &val; + vec[index] = MonoStoreAndGetPtr(argsStore, vargs, index); break; } case 'q': { - auto val = va_arg(vargs, signed int); - vec[index] = (void *) &val; + vec[index] = MonoStoreAndGetPtr(argsStore, vargs, index); break; } case 'l': { - auto val = va_arg(vargs, unsigned long long); - vec[index] = (void *) &val; + vec[index] = MonoStoreAndGetPtr(argsStore, vargs, index); break; } case 'w': { - auto val = va_arg(vargs, signed long long); - vec[index] = (void *) &val; + vec[index] = MonoStoreAndGetPtr(argsStore, vargs, index); break; } case 'f': { - auto val = va_arg(vargs, double); - vec[index] = (void *) &val; + vec[index] = MonoStoreAndGetPtr(argsStore, vargs, index); break; } case 'p': { - auto val = va_arg(vargs, void*); - vec[index] = (void *) &val; + vec[index] = MonoStoreAndGetPtr(argsStore, vargs, index); break; } case 's': @@ -414,13 +409,12 @@ boost::any LangMono::Call(const char *name, const char *argl, int buf, ...) } case 'b': { - auto val = va_arg(vargs, int); - vec[index] = (void *) &val; + vec[index] = MonoStoreAndGetPtr(argsStore, vargs, index); break; } default: - throw std::runtime_error("Mono call: Unknown argument identifier " + argl[index]); + throw std::runtime_error(std::string("Mono call: Unknown argument identifier ") + argl[index]); } } @@ -452,6 +446,7 @@ boost::any LangMono::Call(const char *name, const char *argl, const std::vector< int n_args = args.size(); std::vector vec(n_args); + TMonoArgsStore argsStore(n_args); for (int index = 0; index < n_args; index++) { @@ -459,38 +454,32 @@ boost::any LangMono::Call(const char *name, const char *argl, const std::vector< { case 'i': { - auto val = boost::any_cast(args.at(index)); - vec[index] = ((void *) &val); + vec[index] = MonoStoreAndGetPtr(argsStore, args, index); break; } case 'q': { - auto val = boost::any_cast(args.at(index)); - vec[index] = ((void *) &val); + vec[index] = MonoStoreAndGetPtr(argsStore, args, index); break; } case 'l': { - auto val = boost::any_cast(args.at(index)); - vec[index] = ((void *) &val); + vec[index] = MonoStoreAndGetPtr(argsStore, args, index); break; } case 'w': { - auto val = boost::any_cast(args.at(index)); - vec[index] = ((void *) &val); + vec[index] = MonoStoreAndGetPtr(argsStore, args, index); break; } case 'f': { - auto val = boost::any_cast(args.at(index)); - vec[index] = ((void *) &val); + vec[index] = MonoStoreAndGetPtr(argsStore, args, index); break; } case 'p': { - auto val = boost::any_cast(args.at(index)); - vec[index] = ((void *) &val); + vec[index] = MonoStoreAndGetPtr(argsStore, args, index); break; } case 's': @@ -501,13 +490,12 @@ boost::any LangMono::Call(const char *name, const char *argl, const std::vector< } case 'b': { - auto val = boost::any_cast(args.at(index)); - vec[index] = ((void *) &val); + vec[index] = MonoStoreAndGetPtr(argsStore, args, index); break; } default: - throw std::runtime_error("Mono call: Unknown argument identifier " + argl[index]); + throw std::runtime_error(std::string("Mono call: Unknown argument identifier ") + argl[index]); } } diff --git a/apps/openmw-mp/Script/LangMono/LangMono.hpp b/apps/openmw-mp/Script/LangMono/LangMono.hpp index 944caaf0e..39348e1c6 100644 --- a/apps/openmw-mp/Script/LangMono/LangMono.hpp +++ b/apps/openmw-mp/Script/LangMono/LangMono.hpp @@ -8,6 +8,25 @@ #include