Add support for fade transitions for music segments#10
Add support for fade transitions for music segments#10
Conversation
| auto slot = Music::instance()->GetSlot(index); | ||
| if (!slot) return luaL_error(L, "request_transition: index out of bounds"); | ||
|
|
||
| auto [music_index, preset_index] = Get_MusicPreset(static_cast<uint32_t>(Lua_MusicPreset::Index(L, 2))); |
Check notice
Code scanning / CodeQL
Unused local variable Note
| auto slot = Music::instance()->GetSlot(music_index); | ||
| if (!slot) return luaL_error(L, "add_segment: index out of bounds"); | ||
|
|
||
| auto segment_index = slot->AddSegmentToPreset(preset_index, lua_tonumber(L, 2)); |
Check warning
Code scanning / CodeQL
Lossy function result cast Warning
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 6 months ago
The best way to fix this problem is to explicitly handle conversion from lua_tonumber to integer, clarifying the intended behaviour. The code should explicitly round, floor, ceil, or truncate as appropriate, and check that the value is within the allowed range for uint32_t (i.e., >= 0 and <= UINT32_MAX), giving an error if the value is invalid.
The recommended fix is to:
- Retrieve the value from
lua_tonumber(L, 2)into alua_Numbervariable. - Check that the value is integral (e.g., that it matches its
uint32_trepresentation, or that it equalsfloor(val)). - Check that the value is within the range for
uint32_t(0 ≤ val ≤ UINT32_MAX). - If checks pass, cast to
uint32_t(usingstatic_cast<uint32_t>(val)). - If invalid, return a Lua error.
You may need to #include <cmath> for floor or similar, and possibly #include <limits> for UINT32_MAX.
All changes should be made only in the code snippets shown, specifically in the implementation of Lua_MusicPreset_Add_Segment around line 325.
| @@ -1,5 +1,7 @@ | ||
| #include "lua_music.h" | ||
| #include "Music.h" | ||
| #include <cmath> | ||
| #include <limits> | ||
|
|
||
| static std::vector<std::pair<uint32_t, uint32_t>> music_presets_indexes; | ||
| static std::vector<std::tuple<uint32_t, uint32_t, uint32_t>> music_segment_indexes; | ||
| @@ -322,7 +324,11 @@ | ||
| auto slot = Music::instance()->GetSlot(music_index); | ||
| if (!slot) return luaL_error(L, "add_segment: index out of bounds"); | ||
|
|
||
| auto segment_index = slot->AddSegmentToPreset(preset_index, lua_tonumber(L, 2)); | ||
| lua_Number segment_num = lua_tonumber(L, 2); | ||
| if (segment_num < 0 || segment_num > static_cast<lua_Number>(std::numeric_limits<uint32_t>::max()) || segment_num != floor(segment_num)) { | ||
| return luaL_error(L, "add_segment: segment index must be an integer between 0 and %u", std::numeric_limits<uint32_t>::max()); | ||
| } | ||
| auto segment_index = slot->AddSegmentToPreset(preset_index, static_cast<uint32_t>(segment_num)); | ||
| if (!segment_index.has_value()) return luaL_error(L, "add_segment: index out of bounds"); | ||
|
|
||
| Lua_MusicSegment::Push(L, Add_MusicSegment(music_index, preset_index, segment_index.value())); |
| static int Lua_MusicSegment_Get_Preset(lua_State* L) | ||
| { | ||
| auto index = static_cast<uint32_t>(Lua_MusicSegment::Index(L, 1)); | ||
| auto [music_index, preset_index, segment_index] = Get_MusicSegment(index); |
Check notice
Code scanning / CodeQL
Unused local variable Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 6 months ago
The best way to fix this problem is to remove the unused variable from the unpacking/destructuring assignment on line 368 in Source_Files/Lua/lua_music.cpp. Specifically, change the line
auto [music_index, preset_index, segment_index] = Get_MusicSegment(index);to
auto [music_index, preset_index, /*segment_index*/] = Get_MusicSegment(index);or, preferably, omit it entirely via C++17's structured binding and the ignore-diskards:
In C++, if you are using structured bindings and don't need one of the unpacked values, you can simply ignore it, like so:
auto [music_index, preset_index, std::ignore] = Get_MusicSegment(index);But C++17 does not actually support std::ignore for structured bindings; it's usually used only for std::tuple::tie(). Therefore, the standard way is just to declare only the variables you need. Since the current code returns a std::tuple<uint32_t, uint32_t, uint32_t>, and structured bindings force you to bind all three values, a workaround is to retrieve the tuple into a named variable, and then unpack only the necessary elements, or to keep the unused variable but name it _ to indicate that it's intentionally unused.
But to keep things clean, it's best just to bind the variables you actually use, or assign the unused value to a variable named _ as per common convention.
Modification required:
- On line 368, change
auto [music_index, preset_index, segment_index] = ...toauto [music_index, preset_index, _] = ...or, if your team does not use_for unused variables, simply revert to using a std::tuple and access only the first two indices.
But as per convention and for clarity, replacing segment_index with _ will suffice.
No other changes or new imports/methods are required.
| @@ -365,7 +365,7 @@ | ||
| static int Lua_MusicSegment_Get_Preset(lua_State* L) | ||
| { | ||
| auto index = static_cast<uint32_t>(Lua_MusicSegment::Index(L, 1)); | ||
| auto [music_index, preset_index, segment_index] = Get_MusicSegment(index); | ||
| auto [music_index, preset_index, _] = Get_MusicSegment(index); | ||
| auto lua_preset_index = Get_LuaMusicPreset(music_index, preset_index); | ||
| Lua_MusicPreset::Push(L, lua_preset_index); | ||
| return 1; |
8818b46 to
df6aa14
Compare
No description provided.