Skip to content

Commit

Permalink
fix(Profile::Validation): Be sure we're not creating/updating a profi…
Browse files Browse the repository at this point in the history
…le with the exact same trigger

Fixes #753
Fixes SOUNDSWITCH-AE
  • Loading branch information
Belphemur committed Sep 11, 2021
1 parent 8ee4ef6 commit d1312c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
24 changes: 23 additions & 1 deletion SoundSwitch/Framework/Profile/ProfileManager.cs
Expand Up @@ -98,7 +98,6 @@ private bool RegisterTriggers(Profile profile, bool onInit = false)
return true;
},
() => true);

}

return success;
Expand Down Expand Up @@ -404,6 +403,29 @@ public IEnumerable<ITriggerDefinition> AvailableTriggers()
return string.Format(SettingsStrings.profile_error_name, profile.Name);
}

//Only hotkey doesn't need validation since you can have multiple profile with the same hotkey
foreach (var groups in profile.Triggers.Where(trigger => trigger.Type != TriggerFactory.Enum.HotKey).GroupBy(trigger => trigger.Type).Where(triggers => triggers.Count() > 1))
{
//has different trigger of the same type, not a problem
if (groups.Distinct().Count() > 1)
{
continue;
}
var trigger = groups.First();

var error = groups.Key.Match(() => null,
() => string.Format(SettingsStrings.profile_error_window, trigger.WindowName),
() => string.Format(SettingsStrings.profile_error_application, trigger.ApplicationPath),
() => SettingsStrings.profile_error_steam,
() => null,
() => string.Format(SettingsStrings.profile_error_window, trigger.WindowName),
() => null);
if (error != null)
{
return error;
}
}

foreach (var trigger in profile.Triggers)
{
var error = trigger.Type.Match(() =>
Expand Down
12 changes: 6 additions & 6 deletions SoundSwitch/Framework/Profile/Trigger/Trigger.cs
Expand Up @@ -10,10 +10,10 @@ public Trigger(TriggerFactory.Enum type)
Type = type;
}

public TriggerFactory.Enum Type { get; }
public string WindowName { get; set; }
public string ApplicationPath { get; set; }
public HotKey HotKey { get; set; }
public TriggerFactory.Enum Type { get; }
public string WindowName { get; set; }
public string ApplicationPath { get; set; }
public HotKey HotKey { get; set; }

/// <summary>
/// Should this trigger restore the devices after the app is closed
Expand All @@ -37,14 +37,14 @@ public override bool Equals(object obj)
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Trigger) obj);
return Equals((Trigger)obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = (int) Type;
var hashCode = (int)Type;
hashCode = (hashCode * 397) ^ (WindowName != null ? WindowName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (ApplicationPath != null ? ApplicationPath.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (HotKey != null ? HotKey.GetHashCode() : 0);
Expand Down

0 comments on commit d1312c3

Please sign in to comment.