Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions S1API/Internal/Abstraction/EventHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using UnityEngine.Events;

namespace S1API.Internal.Abstraction
{
/// <summary>
/// INTERNAL: This static class provides us an easy wrapper for subscribing and unsubscribing unity actions.
/// </summary>
internal static class EventHelper
{
/// <summary>
/// INTERNAL: Tracking for subscribed actions.
/// </summary>
internal static readonly Dictionary<Action, UnityAction> SubscribedActions = new Dictionary<Action, UnityAction>();

/// <summary>
/// INTERNAL: Adds a listener to the event, as well as the subscription list.
/// </summary>
/// <param name="listener">The action / method you want to subscribe.</param>
/// <param name="unityEvent">The event you want to subscribe to.</param>
internal static void AddListener(Action listener, UnityEvent unityEvent)
{
if (SubscribedActions.ContainsKey(listener))
return;

UnityAction wrappedListener = (UnityAction)listener.Invoke;
unityEvent.AddListener(wrappedListener);
SubscribedActions.Add(listener, wrappedListener);
}

/// <summary>
/// INTERNAL: Removes a listener to the event, as well as the subscription list.
/// </summary>
/// <param name="listener">The action / method you want to unsubscribe.</param>
/// <param name="unityEvent">The event you want to unsubscribe from.</param>
internal static void RemoveListener(Action listener, UnityEvent unityEvent)
{
SubscribedActions.TryGetValue(listener, out UnityAction wrappedAction);
SubscribedActions.Remove(listener);
unityEvent.RemoveListener(wrappedAction);
}
}
}
7 changes: 4 additions & 3 deletions S1API/Quests/QuestEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#elif (MONO)
using S1Quests = ScheduleOne.Quests;
#endif

using System;
using S1API.Internal.Abstraction;
using UnityEngine;
using UnityEngine.Events;

namespace S1API.Quests
{
Expand All @@ -31,8 +32,8 @@ internal QuestEntry(S1Quests.QuestEntry questEntry) =>
/// </summary>
public event Action OnComplete
{
add => S1QuestEntry.onComplete.AddListener((UnityAction)value.Invoke);
remove => S1QuestEntry.onComplete.RemoveListener((UnityAction)value.Invoke);
add => EventHelper.AddListener(value, S1QuestEntry.onComplete);
remove => EventHelper.RemoveListener(value, S1QuestEntry.onComplete);
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions S1API/Storages/StorageInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

using System;
using System.Linq;
using S1API.Internal.Abstraction;
using S1API.Items;
using UnityEngine.Events;

namespace S1API.Storages
{
Expand Down Expand Up @@ -48,23 +48,23 @@ public bool CanItemFit(ItemInstance itemInstance, int quantity = 1) =>
/// <param name="itemInstance">The item instance you want to store.</param>
public void AddItem(ItemInstance itemInstance) =>
S1Storage.InsertItem(itemInstance.S1ItemInstance);

/// <summary>
/// An action fired when the storage container is opened by the player.
/// </summary>
public event Action OnOpened
{
add => S1Storage.onOpened.AddListener((UnityAction)value.Invoke);
remove => S1Storage.onOpened.RemoveListener((UnityAction)value.Invoke);
add => EventHelper.AddListener(value, S1Storage.onOpened);
remove => EventHelper.RemoveListener(value, S1Storage.onOpened);
}

/// <summary>
/// An action fired when the storage container is closed by the player.
/// </summary>
public event Action OnClosed
{
add => S1Storage.onClosed.AddListener((UnityAction)value.Invoke);
remove => S1Storage.onClosed.RemoveListener((UnityAction)value.Invoke);
add => EventHelper.AddListener(value, S1Storage.onClosed);
remove => EventHelper.RemoveListener(value, S1Storage.onClosed);
}
}
}
Loading