From c0e065cc76bd17b23f5772aa14b5e2904bae5b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sun, 18 Jul 2021 20:33:50 +0200 Subject: [PATCH] Fix certain Lua APIs mangling integers Also fix a few warnings. --- src/gui/game/GameModel.cpp | 2 +- src/gui/options/OptionsView.cpp | 2 +- src/lua/LuaScriptInterface.cpp | 27 +++++++++++++++------------ src/simulation/StructProperty.h | 3 +-- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/gui/game/GameModel.cpp b/src/gui/game/GameModel.cpp index 7431a179c..9e6fa9f06 100644 --- a/src/gui/game/GameModel.cpp +++ b/src/gui/game/GameModel.cpp @@ -94,7 +94,7 @@ GameModel::GameModel(): //Load config into simulation edgeMode = Client::Ref().GetPrefInteger("Simulation.EdgeMode", 0); sim->SetEdgeMode(edgeMode); - ambientAirTemp = R_TEMP + 273.15; + ambientAirTemp = float(R_TEMP) + 273.15f; { auto temp = Client::Ref().GetPrefNumber("Simulation.AmbientAirTemp", ambientAirTemp); if (MIN_TEMP <= temp && MAX_TEMP >= temp) diff --git a/src/gui/options/OptionsView.cpp b/src/gui/options/OptionsView.cpp index aa1631af5..92e904cda 100644 --- a/src/gui/options/OptionsView.cpp +++ b/src/gui/options/OptionsView.cpp @@ -387,7 +387,7 @@ void OptionsView::UpdateAirTemp(String temp, bool isDefocus) if (temp.empty()) { isValid = true; - airTemp = R_TEMP + 273.15; + airTemp = float(R_TEMP) + 273.15f; } else if (!isValid) return; diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 8d77221a3..a79d0b51f 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -2630,9 +2630,6 @@ void LuaScriptInterface::LuaGetProperty(lua_State* l, StructProperty property, i case StructProperty::Float: lua_pushnumber(l, *((float*)propertyAddress)); break; - case StructProperty::Char: - lua_pushnumber(l, *((char*)propertyAddress)); - break; case StructProperty::UChar: lua_pushnumber(l, *((unsigned char*)propertyAddress)); break; @@ -2660,6 +2657,15 @@ void LuaScriptInterface::LuaGetProperty(lua_State* l, StructProperty property, i } } +static int32_t int32_truncate(double n) +{ + if (n >= 0x1p31) + { + n -= 0x1p32; + } + return int32_t(n); +} + void LuaScriptInterface::LuaSetProperty(lua_State* l, StructProperty property, intptr_t propertyAddress, int stackPos) { switch (property.Type) @@ -2667,19 +2673,16 @@ void LuaScriptInterface::LuaSetProperty(lua_State* l, StructProperty property, i case StructProperty::TransitionType: case StructProperty::ParticleType: case StructProperty::Integer: - *((int*)propertyAddress) = luaL_checkinteger(l, stackPos); + *((int*)propertyAddress) = int32_truncate(luaL_checknumber(l, stackPos)); break; case StructProperty::UInteger: - *((unsigned int*)propertyAddress) = luaL_checkinteger(l, stackPos); + *((unsigned int*)propertyAddress) = int32_truncate(luaL_checknumber(l, stackPos)); break; case StructProperty::Float: *((float*)propertyAddress) = luaL_checknumber(l, stackPos); break; - case StructProperty::Char: - *((char*)propertyAddress) = luaL_checkinteger(l, stackPos); - break; case StructProperty::UChar: - *((unsigned char*)propertyAddress) = luaL_checkinteger(l, stackPos); + *((unsigned char*)propertyAddress) = int32_truncate(luaL_checknumber(l, stackPos)); break; case StructProperty::BString: *((ByteString*)propertyAddress) = ByteString(luaL_checkstring(l, stackPos)); @@ -2689,9 +2692,9 @@ void LuaScriptInterface::LuaSetProperty(lua_State* l, StructProperty property, i break; case StructProperty::Colour: #if PIXELSIZE == 4 - *((unsigned int*)propertyAddress) = luaL_checkinteger(l, stackPos); + *((unsigned int*)propertyAddress) = int32_truncate(luaL_checknumber(l, stackPos)); #else - *((unsigned short*)propertyAddress) = luaL_checkinteger(l, stackPos); + *((unsigned short*)propertyAddress) = int32_truncate(luaL_checknumber(l, stackPos)); #endif break; case StructProperty::Removed: @@ -3458,7 +3461,7 @@ int LuaScriptInterface::graphics_fillCircle(lua_State * l) int LuaScriptInterface::graphics_getColors(lua_State * l) { - unsigned int color = lua_tointeger(l, 1); + unsigned int color = int32_truncate(lua_tonumber(l, 1)); int a = color >> 24; int r = (color >> 16)&0xFF; diff --git a/src/simulation/StructProperty.h b/src/simulation/StructProperty.h index 76ba9519c..4659ca782 100644 --- a/src/simulation/StructProperty.h +++ b/src/simulation/StructProperty.h @@ -16,7 +16,6 @@ struct StructProperty Float, BString, String, - Char, UChar, Removed }; @@ -34,7 +33,7 @@ struct StructProperty StructProperty(): Name(""), - Type(Char), + Type(Integer), Offset(0) {