From 38e79e97cfab9554acf2c986c570042f0d25046e Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 15:12:49 +0200 Subject: [PATCH 1/7] Add ResolveModelID to CStaticFDs --- Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h index 4776be6ca1e..86731bdd096 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h @@ -634,6 +634,12 @@ class CStaticFunctionDefinitions static bool ResetAllSurfaceInfo(); static bool ResetSurfaceInfo(short sSurfaceID); + static inline unsigned short ResolveModelID(const std::variant& variant) + { + if (std::holds_alternative(variant)) + return std::get(variant); + return CModelNames::ResolveModelID(std::get(variant)); + } // Input functions static bool BindKey(const char* szKey, const char* szHitState, CLuaMain* pLuaMain, const CLuaFunctionRef& iLuaFunction, CLuaArguments& Arguments); static bool BindKey(const char* szKey, const char* szHitState, const char* szCommandName, const char* szArguments, const char* szResource); From 824800c30e8e5082e6c351d2e552e01d01bd6e01 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 15:22:19 +0200 Subject: [PATCH 2/7] Add and includes ti CLuaEngineDefs.h, used for refactoring the new engine funcs --- Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h index 9214535f0c7..5c598c3cd04 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h @@ -11,6 +11,8 @@ #pragma once #include "CLuaDefs.h" +#include +#include class CLuaEngineDefs : public CLuaDefs { From d233658c9091e170487136e83e474b471900e50e Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Jun 2020 01:36:00 +0200 Subject: [PATCH 3/7] # This is a combination of 2 commits. # This is the 1st commit message: Refactor EngineSetModeVisibleTime to use new parser # The commit message #2 will be skipped: # Fix build --- .../logic/luadefs/CLuaEngineDefs.cpp | 32 +++++++++++-------- .../deathmatch/logic/luadefs/CLuaEngineDefs.h | 3 +- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp index f35c2094bc3..7d9f8e3184b 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp @@ -10,6 +10,7 @@ *****************************************************************************/ #include "StdInc.h" +#include void CLuaEngineDefs::LoadFunctions() { @@ -37,8 +38,7 @@ void CLuaEngineDefs::LoadFunctions() {"engineGetModelIDFromName", EngineGetModelIDFromName}, {"engineGetModelTextureNames", EngineGetModelTextureNames}, {"engineGetVisibleTextureNames", EngineGetVisibleTextureNames}, - {"engineSetModelVisibleTime", EngineSetModelVisibleTime}, - {"engineGetModelVisibleTime", EngineGetModelVisibleTime}, + {"engineSetModelVisibleTime", ArgumentParser}, {"engineGetModelTextures", EngineGetModelTextures}, {"engineGetSurfaceProperties", EngineGetSurfaceProperties}, {"engineSetSurfaceProperties", EngineSetSurfaceProperties}, @@ -1104,16 +1104,14 @@ int CLuaEngineDefs::EngineGetVisibleTextureNames(lua_State* luaVM) return 1; } -int CLuaEngineDefs::EngineSetModelVisibleTime(lua_State* luaVM) +bool CLuaEngineDefs::EngineSetModelVisibleTime(const std::variant variantModelID, char hourOn, char hourOff) { - // bool engineSetModelVisibleTime ( int/string modelID, int hourOn, int hourOff ) - SString strModelId; - char cHourOn,cHourOff; - CScriptArgReader argStream(luaVM); - argStream.ReadString(strModelId); - argStream.ReadNumber(cHourOn); - argStream.ReadNumber(cHourOff); + const unsigned short modelID = CStaticFunctionDefinitions::ResolveModelID(variantModelID); + CModelInfo* const modelInfo = g_pGame->GetModelInfo(modelID); + if (!modelInfo) + throw std::invalid_argument(SString("Invalid model id %u", modelID)); +<<<<<<< HEAD if (!argStream.HasErrors()) { ushort usModelID = CModelNames::ResolveModelID(strModelId); @@ -1129,10 +1127,18 @@ int CLuaEngineDefs::EngineSetModelVisibleTime(lua_State* luaVM) } else luaL_error(luaVM, argStream.GetFullErrorMessage()); +======= + if (hourOn > 24 || hourOn < 0) + throw std::invalid_argument("hourOn must be between 0 and 24"); - // Failed - lua_pushboolean(luaVM, false); - return 1; + if (hourOff > 24 || hourOff < 0) + throw std::invalid_argument("hourOff must be between 0 and 24"); +>>>>>>> 2e0e48872... Refactor EngineSetModelVisibleTime to use the new parser + + if (hourOn > hourOff) + std::swap(hourOn, hourOff); + + return modelInfo->SetTime(hourOn, hourOff); } int CLuaEngineDefs::EngineGetModelVisibleTime(lua_State* luaVM) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h index 5c598c3cd04..cbbc2d5c7ff 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h @@ -49,8 +49,7 @@ class CLuaEngineDefs : public CLuaDefs LUA_DECLARE(EngineGetModelIDFromName); LUA_DECLARE(EngineGetModelTextureNames); LUA_DECLARE(EngineGetVisibleTextureNames); - LUA_DECLARE(EngineSetModelVisibleTime); - LUA_DECLARE(EngineGetModelVisibleTime); + static bool EngineSetModelVisibleTime(const std::variant variantModelID, char hourOn, char hourOff); LUA_DECLARE(EngineGetModelTextures); LUA_DECLARE(EngineSetSurfaceProperties); LUA_DECLARE(EngineGetSurfaceProperties); From dddfe67072797e39d30b32cd7eff17401143dcfe Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Jun 2020 01:39:02 +0200 Subject: [PATCH 4/7] Refactor EngineSetModeVisibleTime to use new parser --- Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp | 8 +++----- Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp index 7d9f8e3184b..aca270dadd4 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp @@ -1104,14 +1104,13 @@ int CLuaEngineDefs::EngineGetVisibleTextureNames(lua_State* luaVM) return 1; } -bool CLuaEngineDefs::EngineSetModelVisibleTime(const std::variant variantModelID, char hourOn, char hourOff) +bool CLuaEngineDefs::EngineSetModelVisibleTime(const std::variant variantModelID, short hourOn, short hourOff) { const unsigned short modelID = CStaticFunctionDefinitions::ResolveModelID(variantModelID); CModelInfo* const modelInfo = g_pGame->GetModelInfo(modelID); if (!modelInfo) throw std::invalid_argument(SString("Invalid model id %u", modelID)); -<<<<<<< HEAD if (!argStream.HasErrors()) { ushort usModelID = CModelNames::ResolveModelID(strModelId); @@ -1127,18 +1126,17 @@ bool CLuaEngineDefs::EngineSetModelVisibleTime(const std::variant 24 || hourOn < 0) throw std::invalid_argument("hourOn must be between 0 and 24"); if (hourOff > 24 || hourOff < 0) throw std::invalid_argument("hourOff must be between 0 and 24"); ->>>>>>> 2e0e48872... Refactor EngineSetModelVisibleTime to use the new parser if (hourOn > hourOff) std::swap(hourOn, hourOff); - return modelInfo->SetTime(hourOn, hourOff); + return modelInfo->SetTime((char)hourOn, (char)hourOff); } int CLuaEngineDefs::EngineGetModelVisibleTime(lua_State* luaVM) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h index cbbc2d5c7ff..f96696a2859 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h @@ -49,7 +49,8 @@ class CLuaEngineDefs : public CLuaDefs LUA_DECLARE(EngineGetModelIDFromName); LUA_DECLARE(EngineGetModelTextureNames); LUA_DECLARE(EngineGetVisibleTextureNames); - static bool EngineSetModelVisibleTime(const std::variant variantModelID, char hourOn, char hourOff); + static bool EngineSetModelVisibleTime(const std::variant variantModelID, short hourOn, short hourOff); + static std::tuple EngineGetModelVisibleTime(const std::variant variantModelID); LUA_DECLARE(EngineGetModelTextures); LUA_DECLARE(EngineSetSurfaceProperties); LUA_DECLARE(EngineGetSurfaceProperties); From d09fdecac2a2f7428402eb54c2edd897942de51b Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Jun 2020 01:40:56 +0200 Subject: [PATCH 5/7] Refactor EngineGetModeVisibleTime to use new parser --- .../logic/luadefs/CLuaEngineDefs.cpp | 42 ++++--------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp index aca270dadd4..78c97c5053d 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp @@ -39,6 +39,7 @@ void CLuaEngineDefs::LoadFunctions() {"engineGetModelTextureNames", EngineGetModelTextureNames}, {"engineGetVisibleTextureNames", EngineGetVisibleTextureNames}, {"engineSetModelVisibleTime", ArgumentParser}, + {"engineGetModelVisibleTime", ArgumentParser}, {"engineGetModelTextures", EngineGetModelTextures}, {"engineGetSurfaceProperties", EngineGetSurfaceProperties}, {"engineSetSurfaceProperties", EngineSetSurfaceProperties}, @@ -1139,41 +1140,16 @@ bool CLuaEngineDefs::EngineSetModelVisibleTime(const std::variantSetTime((char)hourOn, (char)hourOff); } -int CLuaEngineDefs::EngineGetModelVisibleTime(lua_State* luaVM) +std::tuple CLuaEngineDefs::EngineGetModelVisibleTime(const std::variant variantModelID) { - // int, int engineGetModelVisibleTime ( int/string modelID ) - SString strModelId; - - CScriptArgReader argStream(luaVM); - argStream.ReadString(strModelId); - - if (!argStream.HasErrors()) - { - ushort usModelID = CModelNames::ResolveModelID(strModelId); - CModelInfo* pModelInfo = g_pGame->GetModelInfo(usModelID); - if (pModelInfo) - { - char cHourOn, cHourOff; - if (pModelInfo->GetTime(cHourOn, cHourOff)) - { - lua_pushnumber(luaVM, cHourOn); - lua_pushnumber(luaVM, cHourOff); - return 2; - } - else // Model is incompatible, don't let confuse user. - { - lua_pushnumber(luaVM, 0); - lua_pushnumber(luaVM, 24); - return 2; - } - } - } - else - luaL_error(luaVM, argStream.GetFullErrorMessage()); + const unsigned short modelID = CStaticFunctionDefinitions::ResolveModelID(variantModelID); + CModelInfo* const modelInfo = g_pGame->GetModelInfo(modelID); + if (!modelInfo) + throw std::invalid_argument(SString("Invalid model id %u", modelID)); - // Failed - lua_pushboolean(luaVM, false); - return 1; + char hourOn = 0, hourOff = 24; // If model is incompatible it returns these numbers(in order: 0, 24) so we dont confuse the user + modelInfo->GetTime(hourOn, hourOff); + return { hourOn, hourOff }; } int CLuaEngineDefs::EngineGetModelTextures(lua_State* luaVM) From 23e3bccbac864539dee184361c429eb8948611b6 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Jun 2020 01:42:06 +0200 Subject: [PATCH 6/7] Refactor EngineGetModeVisibleTime to use new parser --- .../logic/luadefs/CLuaEngineDefs.cpp | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp index 78c97c5053d..35e3a7021de 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp @@ -1112,22 +1112,6 @@ bool CLuaEngineDefs::EngineSetModelVisibleTime(const std::variantGetModelInfo(usModelID); - if (pModelInfo) - { - if (cHourOn >= 0 && cHourOn <= 24 && cHourOff >= 0 && cHourOff <= 24) - { - lua_pushboolean(luaVM, pModelInfo->SetTime(cHourOn, cHourOff)); - return 1; - } - } - } - else - luaL_error(luaVM, argStream.GetFullErrorMessage()); - if (hourOn > 24 || hourOn < 0) throw std::invalid_argument("hourOn must be between 0 and 24"); @@ -1137,7 +1121,7 @@ bool CLuaEngineDefs::EngineSetModelVisibleTime(const std::variant hourOff) std::swap(hourOn, hourOff); - return modelInfo->SetTime((char)hourOn, (char)hourOff); + return modelInfo->SetTime(hourOn, hourOff); } std::tuple CLuaEngineDefs::EngineGetModelVisibleTime(const std::variant variantModelID) From 452e8119263f99ba7fbe782cf5f4bc399cca51d4 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 31 May 2020 15:21:09 +0200 Subject: [PATCH 7/7] Refactor EngineResetModelLODDistance to use the new parser --- .../logic/luadefs/CLuaEngineDefs.cpp | 35 +++++++------------ .../deathmatch/logic/luadefs/CLuaEngineDefs.h | 2 +- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp index 35e3a7021de..0dedb67f55d 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp @@ -30,7 +30,7 @@ void CLuaEngineDefs::LoadFunctions() {"engineRequestModel", EngineRequestModel}, {"engineGetModelLODDistance", EngineGetModelLODDistance}, {"engineSetModelLODDistance", EngineSetModelLODDistance}, - {"engineResetModelLODDistance", EngineResetModelLODDistance}, + {"engineResetModelLODDistance", ArgumentParser}, {"engineSetAsynchronousLoading", EngineSetAsynchronousLoading}, {"engineApplyShaderToWorldTexture", EngineApplyShaderToWorldTexture}, {"engineRemoveShaderFromWorldTexture", EngineRemoveShaderFromWorldTexture}, @@ -773,31 +773,20 @@ int CLuaEngineDefs::EngineSetModelLODDistance(lua_State* luaVM) return 1; } -int CLuaEngineDefs::EngineResetModelLODDistance(lua_State* luaVM) +bool CLuaEngineDefs::EngineResetModelLODDistance(const std::variant variantModelID) { - SString strModel = ""; - CScriptArgReader argStream(luaVM); - argStream.ReadString(strModel); + const unsigned short modelID = CStaticFunctionDefinitions::ResolveModelID(variantModelID); + CModelInfo* const modelInfo = g_pGame->GetModelInfo(modelID); + if (!modelInfo) + throw std::invalid_argument(SString("Invalid model id %u. Valid IDs are in range 0 - 19999", modelID)); - if (argStream.HasErrors()) - return luaL_error(luaVM, argStream.GetFullErrorMessage()); - - unsigned short usModelID = CModelNames::ResolveModelID(strModel); - CModelInfo* pModelInfo = g_pGame->GetModelInfo(usModelID); - if (pModelInfo) - { - float fCurrentDistance = pModelInfo->GetLODDistance(); - float fOriginalDistance = pModelInfo->GetOriginalLODDistance(); - //Make sure we're dealing with a valid LOD distance, and not setting the same LOD distance - if (fOriginalDistance > 0.0f && fOriginalDistance != fCurrentDistance) { - pModelInfo->SetLODDistance(fOriginalDistance, true); - lua_pushboolean(luaVM, true); - return 1; - } - } + //Make sure we're dealing with a valid LOD distance, and not setting the same LOD distance + const float originalDistance = modelInfo->GetOriginalLODDistance(); + if (originalDistance == 0.0f || originalDistance == modelInfo->GetLODDistance()) + return false; - lua_pushboolean(luaVM, false); - return 1; + modelInfo->SetLODDistance(originalDistance, true); + return true; } int CLuaEngineDefs::EngineSetAsynchronousLoading(lua_State* luaVM) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h index f96696a2859..f7a3b920aba 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h @@ -41,7 +41,7 @@ class CLuaEngineDefs : public CLuaDefs LUA_DECLARE(EngineReplaceVehiclePart); LUA_DECLARE(EngineGetModelLODDistance); LUA_DECLARE(EngineSetModelLODDistance); - LUA_DECLARE(EngineResetModelLODDistance); + static bool EngineResetModelLODDistance(const std::variant variantModelID); LUA_DECLARE(EngineSetAsynchronousLoading); LUA_DECLARE(EngineApplyShaderToWorldTexture); LUA_DECLARE(EngineRemoveShaderFromWorldTexture);