Skip to content

Commit

Permalink
Improve ThreadLocal usage in IgnoreHelper, remove unnecessary lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kaenganxt committed Sep 5, 2023
1 parent 4ee8bc8 commit d59d6e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
47 changes: 18 additions & 29 deletions src/api/Helpers/IgnoreHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ namespace CSM.API.Helpers
/// </summary>
public class IgnoreHelper
{
public static IgnoreHelper Instance = new IgnoreHelper();
public static IgnoreHelper Instance
{
get => _instance.Value;
set => _instance.Value = value;
}

private readonly ThreadLocal<int> _ignoreAll = new ThreadLocal<int>();
private readonly ThreadLocal<HashSet<string>> _exceptions = new ThreadLocal<HashSet<string>>();
private static readonly ThreadLocal<IgnoreHelper> _instance = new ThreadLocal<IgnoreHelper>(() => new IgnoreHelper());

private int _ignoreAll = 0;
private readonly HashSet<string> _exceptions = new HashSet<string>();

/// <summary>
/// Starts the ignore mode where the injection handlers
/// ignore all method calls.
/// </summary>
public void StartIgnore()
{
_ignoreAll.Value++;
_ignoreAll++;
}

/// <summary>
Expand All @@ -33,11 +39,7 @@ public void StartIgnore()
public void StartIgnore(string except)
{
StartIgnore();
if (_exceptions.Value == null)
{
_exceptions.Value = new HashSet<string>();
}
_exceptions.Value.Add(except);
_exceptions.Add(except);
}

/// <summary>
Expand All @@ -46,7 +48,7 @@ public void StartIgnore(string except)
/// </summary>
public void EndIgnore()
{
_ignoreAll.Value = Math.Max(_ignoreAll.Value - 1, 0);
_ignoreAll = Math.Max(_ignoreAll - 1, 0);
}

/// <summary>
Expand All @@ -57,11 +59,7 @@ public void EndIgnore()
public void EndIgnore(string except)
{
EndIgnore();
if (_exceptions.Value == null)
{
_exceptions.Value = new HashSet<string>();
}
_exceptions.Value.Remove(except);
_exceptions.Remove(except);
}

/// <summary>
Expand All @@ -70,7 +68,7 @@ public void EndIgnore(string except)
/// <returns>If the calls should be ignored.</returns>
public bool IsIgnored()
{
return _ignoreAll.Value > 0;
return _ignoreAll > 0;
}

/// <summary>
Expand All @@ -80,26 +78,17 @@ public bool IsIgnored()
/// <returns>If the calls should be ignored.</returns>
public bool IsIgnored(string action)
{
if (_exceptions.Value == null)
{
_exceptions.Value = new HashSet<string>();
}
return IsIgnored() && !_exceptions.Value.Contains(action);
return IsIgnored() && !_exceptions.Contains(action);
}

/// <summary>
/// Reset ignore state.
/// </summary>
public void ResetIgnore()
{
if (_exceptions.Value == null)
{
_exceptions.Value = new HashSet<string>();
}

Log.Debug($"Resetting {_ignoreAll.Value} levels of ignoring: {string.Join(", ", _exceptions.Value.ToArray())}");
_ignoreAll.Value = 0;
_exceptions.Value.Clear();
Log.Debug($"Resetting {_ignoreAll} levels of ignoring: {string.Join(", ", _exceptions.ToArray())}");
_ignoreAll = 0;
_exceptions.Clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ protected override void Handle(SetVarsityColorCommand command)
// We need to set the color beforehand, as the call to SetVarsityColor will copy the park struct and not reflect the results on the object
districtPark.m_varsityColor = command.Color;
ReflectionHelper.Call(districtPark, "SetVarsityColor", command.Campus, command.Color);
Color newColor = Singleton<DistrictManager>.instance.m_parks.m_buffer[command.Campus].m_varsityColor;

// Refresh panel if is campus
if (InfoPanelHelper.IsPark(typeof(CampusWorldInfoPanel), command.Campus, out WorldInfoPanel panel))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ protected override void Handle(DistrictChangeStyleCommand command)

if (InfoPanelHelper.IsDistrict(typeof(DistrictWorldInfoPanel), command.DistrictId, out WorldInfoPanel panel))
{
DistrictWorldInfoPanel districtPanel = (DistrictWorldInfoPanel)panel;
SimulationManager.instance.m_ThreadingWrapper.QueueMainThread(() =>
{
ReflectionHelper.GetAttr<UIDropDown>(districtPanel, "m_Style").selectedIndex = command.Style;
ReflectionHelper.GetAttr<UIDropDown>((DistrictWorldInfoPanel)panel, "m_Style").selectedIndex = command.Style;
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/csm/Mods/ModCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static void BuildModInfo(UIPanel panel)
panel.AddScrollbar(modInfoPanel);

panel.width = 720;
modInfoPanel.CreateLabel("Mod Support", new Vector2(0, 0), 340, 20);
modInfoPanel.CreateLabel("Mod/DLC Support", new Vector2(0, 0), 340, 20);

Log.Debug($"Mod support: {string.Join(", ", modSupport.Select(m => $"{m.TypeName} ({m.Type})").ToArray())}");
int y = -50;
Expand Down

0 comments on commit d59d6e6

Please sign in to comment.