Ejemplo n.º 1
0
        /// <summary>
        /// Add a mail to the player's mailbox.
        /// </summary>
        /// <param name="mail">The mail to add.</param>
        /// <param name="arrivalDay">
        /// The in-game day when the mail will arrive in the player's mailbox. Pass in the current date to instantly add a mail to the mailbox.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="arrivalDay"/> is in the past.</exception>
        /// <exception cref="ArgumentNullException">The specified <paramref name="mail"/> is be <c>null</c>.</exception>
        public void AddMail(Mail mail, SDate arrivalDay)
        {
            if (arrivalDay == null)
            {
                throw new ArgumentNullException(nameof(arrivalDay));
            }

            AddMail(mail, SDateHelper.GetCurrentDayOffsetFromDate(arrivalDay));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called when a game menu is opened. This function is responsible for creating the UI for
        /// the user registered mails.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void OnMenuChanged(object sender, MenuChangedEventArgs e)
        {
            if (!(e.OldMenu is LetterViewerMenu) && e.NewMenu is LetterViewerMenu letterMenu)
            {
                var mailId = reflectionHelper.GetField <string>(letterMenu, "mailTitle").GetValue();

                // If the opened LetterViewerMenu instance does not represent a mail -> do nothing
                if (mailId == null)
                {
                    return;
                }

                // If the opened mail is not a mail registered via the framework, we still parse the mail content
                // for the framework's text coloring API and provide the item selection fix (only showing the last
                // item).
                if (!this.registeredMailsMetaData.TryGetValue(mailId, out MailMetaData mailMetaData))
                {
                    string mailContent = GetMailContentForGameMail(mailId);

                    // Create and show the menu for this mail.
                    var gLetterMenu = new LetterViewerMenuEx(mailId, mailContent);
                    Game1.activeClickableMenu = gLetterMenu;

                    // Since this is a mail which wasn't added to the game using this framework we are done.
                    return;
                }

                // If a mail with the given ID has been registered in the framework, but no mail sender has been found,
                // we remove the mail ID from the framework and don't proceed further.
                if (!this.mailSenders.TryGetValue(mailMetaData.ModId, out IMailSender mailSender))
                {
                    // A mail with this mailId was added to the framework at some point, but there is no sender
                    // owning this mail any longer. This can be due to the removal of a mod consuming the mail API of FeTK
                    // by the user. We can thus savely remove this mail from the framework on saving, as even if the consuming
                    // mod will be added back, for this save, the mail won't be displayed any longer (because it was already shown).
                    RemoveMail(mailId, mailMetaData.ArrivalDay);

                    monitor.Log($"The mail \"{mailMetaData.UserId}\" was added by the mod {mailMetaData.ModId} which seems to be no longer present.");
                    return;
                }

                // Request the actual mail data from the mail service which was used to add this mail to the game.
                var arrivalDate = SDateHelper.GetDateFromDay(mailMetaData.ArrivalDay);
                var mail        = mailSender.GetMailFromId(mailMetaData.UserId, arrivalDate);
                if (mail == null)
                {
                    monitor.Log($"An unexpected error occured. The mail \"{mailId}\" could not be retrieved from the mail service it was registered with.");
                    return;
                }

                // Get the editable content for this mail.
                MailContent content = mail.GetMailContent();

                // Raise the mail-opening event for this mail.
                mailSender.OnMailOpening(new MailOpeningEventArgs(mail.Id, arrivalDate, content));

                // Update the mail's content based on consumer-requested changes.
                mail.UpdateMailContent(content);

                // Create the menu for this mail.
                var nLetterMenu = new LetterViewerMenuWrapper(mail);

                // Setup the mail-closed event for this mail.
                nLetterMenu.MenuClosed += (s, e2) =>
                {
                    // Remove the closed mail from the mail manager.
                    RemoveMail(mailId, mailMetaData.ArrivalDay);

                    // Notify its sender that the mail has been closed.
                    mailSender.OnMailClosed(new MailClosedCoreEventArgs(mailMetaData.UserId, arrivalDate, e2.InteractionRecord));
                };

                monitor.Log($"Opening custom mail \"{mailMetaData.UserId}\".");

                // Show the menu for this mail.
                nLetterMenu.Show();
            }
        }