Skip to content

Commit

Permalink
fix!: Address ModMessageSystem memory issue; No messages should be he…
Browse files Browse the repository at this point in the history
…ld during game-time (#445)

No messages should be held during game-time
  • Loading branch information
LeeTwentyThree committed Jul 31, 2023
1 parent 0ef6fb1 commit 77c5bd9
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Nautilus/Utility/ModMessages/ModMessageSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static ModMessageSystem()
SaveUtils.RegisterOnStartLoadingEvent(OnStartLoading);
}

private static bool _allowedToHoldGlobalMessages = true;
private static bool _allowedToHoldMessages = true;

// address - inbox
private static Dictionary<string, ModInbox> _inboxes = new Dictionary<string, ModInbox>();
Expand Down Expand Up @@ -47,14 +47,15 @@ public static void SendGlobal(string subject, params object[] contents)
{
globalMessage.TrySendMessageToInbox(inbox);
}
if (_allowedToHoldGlobalMessages)
if (_allowedToHoldMessages)
{
_globalMessages.Add(globalMessage);
}
}

/// <summary>
/// Sends a single message to a <see cref="ModInbox"/>. If the message is not read immediately, it will be held until read.
/// Sends a single message to a <see cref="ModInbox"/>. If the message is not read immediately, it will be held until read. Messages can not be held
/// during game-time.
/// </summary>
/// <param name="messageInstance">The message to send.</param>
public static void Send(ModMessage messageInstance)
Expand All @@ -67,6 +68,9 @@ public static void Send(ModMessage messageInstance)

// add to held messages instead:

if (!_allowedToHoldMessages)
return;

if (!_heldMessages.TryGetValue(messageInstance.Recipient, out var heldMessageList))
_heldMessages.Add(messageInstance.Recipient, new List<ModMessage>());

Expand Down Expand Up @@ -129,6 +133,7 @@ internal static void SendHeldMessagesToInbox(ModInbox inbox)
private static void OnStartLoading()
{
_globalMessages.Clear();
_allowedToHoldGlobalMessages = false;
_heldMessages.Clear();
_allowedToHoldMessages = false;
}
}

0 comments on commit 77c5bd9

Please sign in to comment.