Skip to content

Commit

Permalink
Make filter Lua bindings unable to crash game
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebandit21 committed Aug 12, 2021
1 parent 8ce9f39 commit c8c8025
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Etterna/Models/Songs/Song.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1750,8 +1750,8 @@ Song::ChartMatchesFilter(Steps* chart, float rate) const
/* Iterate over all skillsets, as well as
* two placeholders for song length and best clear %
*/
const auto lb = FILTERMAN->SSFilterLowerBounds[ss];
const auto ub = FILTERMAN->SSFilterUpperBounds[ss];
const auto lb = FILTERMAN->GetSSFilter(static_cast<Skillset>(ss), 0);
const auto ub = FILTERMAN->GetSSFilter(static_cast<Skillset>(ss), 1);
if (lb > 0.F || ub > 0.F) { // If either bound is active, continue
if (!FILTERMAN->ExclusiveFilter) {
/* Non-Exclusive filter
Expand Down
47 changes: 33 additions & 14 deletions src/Etterna/Singletons/FilterManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ FilterManager* FILTERMAN = nullptr;
FilterManager::FilterManager()
{
// filter stuff - mina
ZERO(SSFilterLowerBounds);
ZERO(SSFilterUpperBounds);
SSFilterUpperBounds.fill(0);
SSFilterLowerBounds.fill(0);
m_pPlayerState = new PlayerState;
m_pPlayerState->SetPlayerNumber(PLAYER_1);

Expand All @@ -33,29 +33,32 @@ FilterManager::~FilterManager()
float
FilterManager::GetSSFilter(Skillset ss, int bound)
{
// Bound checking is done within the Lua binding
if (bound == 0)
return SSFilterLowerBounds[ss];
return SSFilterLowerBounds.at(ss);

return SSFilterUpperBounds[ss];
return SSFilterUpperBounds.at(ss);
}

void
FilterManager::SetSSFilter(float v, Skillset ss, int bound)
{
// Bound checking is done within the Lua binding
if (bound == 0)
SSFilterLowerBounds[ss] = v;
SSFilterLowerBounds.at(ss) = v;
else
SSFilterUpperBounds[ss] = v;
SSFilterUpperBounds.at(ss) = v;
}

// reset button for filters
void
FilterManager::ResetSSFilters()
{
for (int ss = 0; ss < NUM_Skillset + 2; ss++) {
// Skillsets + 2 other values (time, clear %)
SSFilterLowerBounds[ss] = 0;
SSFilterUpperBounds[ss] = 0;
for (auto& val : SSFilterLowerBounds) {
val = 0;
}
for (auto& val : SSFilterUpperBounds) {
val = 0;
}
}

Expand All @@ -74,11 +77,12 @@ FilterManager::ResetAllFilters()
bool
FilterManager::AnyActiveFilter()
{
for (int ss = 0; ss < NUM_Skillset + 2; ss++) {
// Skillsets + 2 other values (time, clear %)
if (SSFilterLowerBounds[ss] > 0)
for (const auto& val : SSFilterLowerBounds) {
if (val > 0)
return true;
if (SSFilterUpperBounds[ss] > 0)
}
for (const auto& val : SSFilterUpperBounds) {
if (val > 0)
return true;
}
return false;
Expand Down Expand Up @@ -109,11 +113,26 @@ class LunaFilterManager : public Luna<FilterManager>
DEFINE_METHOD(AnyActiveFilter, AnyActiveFilter())
static int SetSSFilter(T* p, lua_State* L)
{
// float v, Skillset ss, int bound
int ss = IArg(2) - 1;
if (ss < 0 || ss >= NUM_Skillset + 2) {
luaL_error(
L, "Invalid skillset value %d in call to SetSSFilter", ss);
return 0;
}
p->SetSSFilter(FArg(1), static_cast<Skillset>(IArg(2) - 1), IArg(3));
return 0;
}
static int GetSSFilter(T* p, lua_State* L)
{
// Skillset ss, int bound
int ss = IArg(1) - 1;
if (ss < 0 || ss >= NUM_Skillset + 2) {
luaL_error(
L, "Invalid skillset value %d in call to GetSSFilter", ss);
lua_pushnumber(L, 0);
return 1;
}
float f = p->GetSSFilter(static_cast<Skillset>(IArg(1) - 1), IArg(2));
lua_pushnumber(L, f);
return 1;
Expand Down
5 changes: 3 additions & 2 deletions src/Etterna/Singletons/FilterManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Etterna/Models/Misc/GameConstantsAndTypes.h"
#include "Etterna/Models/Misc/PlayerNumber.h"
#include <unordered_map>
#include <array>

class PlayerState;
class FilterManager
Expand All @@ -13,8 +14,8 @@ class FilterManager

PlayerState* m_pPlayerState;

float SSFilterLowerBounds[NUM_Skillset + 2] = { 0 }; // Zero-initialize
float SSFilterUpperBounds[NUM_Skillset + 2] = { 0 }; // Zero-initialize
std::array<float, NUM_Skillset + 2> SSFilterLowerBounds;
std::array<float, NUM_Skillset + 2> SSFilterUpperBounds;
/* Skill_Overall,
* Skill_Stream,
* Skill_Jumpstream,
Expand Down

0 comments on commit c8c8025

Please sign in to comment.