Fix certain Lua APIs mangling integers

Also fix a few warnings.
This commit is contained in:
Tamás Bálint Misius 2021-07-18 20:33:50 +02:00
parent 133c8db0c7
commit c0e065cc76
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
4 changed files with 18 additions and 16 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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)
{