Skip to content

Commit a4197a3

Browse files
committed
🐛 fix rapid variable updates causing null pointer exceptions
semver: patch
1 parent bc2e4a4 commit a4197a3

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

EliteVA/Variables/VoiceAttackVariables.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,31 @@ public class VoiceAttackVariables(dynamic vaProxy)
99
{
1010
public record Variable(string Name, dynamic Value, TypeCode Type);
1111

12+
private readonly object _lock = new();
1213
private List<Variable> _setVariables = [];
1314

14-
public IReadOnlyList<Variable> SetVariables => _setVariables.ToList();
15+
public IReadOnlyList<Variable> SetVariables
16+
{
17+
get
18+
{
19+
lock (_lock)
20+
return _setVariables.ToList();
21+
}
22+
}
1523

1624
public event EventHandler? OnVariablesSet;
1725

1826
public void ClearStartingWith(string name)
1927
{
20-
var variablesToClear = _setVariables.Where(x => x.Name.StartsWith(name)).ToList();
28+
List<Variable> variablesToClear;
29+
lock (_lock)
30+
{
31+
variablesToClear = _setVariables.Where(x => x.Name.StartsWith(name)).ToList();
32+
_setVariables = _setVariables.Where(x => !x.Name.StartsWith(name)).ToList();
33+
}
2134

2235
foreach (var variable in variablesToClear)
2336
Clear(variable.Name, variable.Type);
24-
25-
_setVariables = _setVariables.Where(x => !x.Name.StartsWith(name)).ToList();
2637
}
2738

2839
/// <summary>
@@ -263,19 +274,24 @@ private void ClearDate(string name)
263274

264275
private void SetVariable(string name, dynamic? value, TypeCode type)
265276
{
266-
var index = _setVariables.FindIndex(x => x.Name == name);
277+
lock (_lock)
278+
{
279+
var index = _setVariables.FindIndex(x => x.Name == name);
267280

268-
if (index >= 0)
269-
_setVariables[index] = new(name, value, type);
270-
else
271-
_setVariables.Insert(0, new(name, value, type));
281+
if (index >= 0)
282+
_setVariables[index] = new(name, value, type);
283+
else
284+
_setVariables.Insert(0, new(name, value, type));
285+
}
272286

273287
OnVariablesSet?.Invoke(this, EventArgs.Empty);
274288
}
275289

276290
private void ClearVariable(string name)
277291
{
278-
_setVariables.RemoveAll(x => x.Name == name);
292+
lock (_lock)
293+
_setVariables.RemoveAll(x => x.Name == name);
294+
279295
OnVariablesSet?.Invoke(this, EventArgs.Empty);
280296
}
281297
}

0 commit comments

Comments
 (0)